当前位置: 首页>>代码示例>>Python>>正文


Python QListWidget.currentItem方法代码示例

本文整理汇总了Python中PyQt5.Qt.QListWidget.currentItem方法的典型用法代码示例。如果您正苦于以下问题:Python QListWidget.currentItem方法的具体用法?Python QListWidget.currentItem怎么用?Python QListWidget.currentItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PyQt5.Qt.QListWidget的用法示例。


在下文中一共展示了QListWidget.currentItem方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: change_builtin

# 需要导入模块: from PyQt5.Qt import QListWidget [as 别名]
# 或者: from PyQt5.Qt.QListWidget import currentItem [as 别名]
 def change_builtin(self):
     d = QDialog(self)
     lw = QListWidget(d)
     for (trigger, syntaxes), snip in iteritems(builtin_snippets):
         snip = copy.deepcopy(snip)
         snip['trigger'], snip['syntaxes'] = trigger, syntaxes
         i = QListWidgetItem(self.snip_to_text(snip), lw)
         i.setData(Qt.UserRole, snip)
     d.l = l = QVBoxLayout(d)
     l.addWidget(QLabel(_('Choose the built-in snippet to modify:')))
     l.addWidget(lw)
     lw.itemDoubleClicked.connect(d.accept)
     d.bb = bb = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
     l.addWidget(bb)
     bb.accepted.connect(d.accept), bb.rejected.connect(d.reject)
     if d.exec_() == d.Accepted and lw.currentItem() is not None:
         self.stack.setCurrentIndex(1)
         self.edit_snip.apply_snip(lw.currentItem().data(Qt.UserRole), creating_snippet=True)
开发者ID:JimmXinu,项目名称:calibre,代码行数:20,代码来源:snippets.py

示例2: PrefsViewerDialog

# 需要导入模块: from PyQt5.Qt import QListWidget [as 别名]
# 或者: from PyQt5.Qt.QListWidget import currentItem [as 别名]
class PrefsViewerDialog(SizePersistedDialog):

    def __init__(self, gui, namespace):
        SizePersistedDialog.__init__(self, gui, _('Prefs Viewer dialog'))
        self.setWindowTitle(_('Preferences for: ')+namespace)

        self.gui = gui
        self.db = gui.current_db
        self.namespace = namespace
        self._init_controls()
        self.resize_dialog()

        self._populate_settings()

        if self.keys_list.count():
            self.keys_list.setCurrentRow(0)

    def _init_controls(self):
        layout = QVBoxLayout(self)
        self.setLayout(layout)

        ml = QHBoxLayout()
        layout.addLayout(ml, 1)

        self.keys_list = QListWidget(self)
        self.keys_list.setSelectionMode(QAbstractItemView.SingleSelection)
        self.keys_list.setFixedWidth(150)
        self.keys_list.setAlternatingRowColors(True)
        ml.addWidget(self.keys_list)
        self.value_text = QTextEdit(self)
        self.value_text.setTabStopWidth(24)
        self.value_text.setReadOnly(True)
        ml.addWidget(self.value_text, 1)

        button_box = QDialogButtonBox(QDialogButtonBox.Ok)
        button_box.accepted.connect(self.accept)
        self.clear_button = button_box.addButton(_('Clear'), QDialogButtonBox.ResetRole)
        self.clear_button.setIcon(get_icon('trash.png'))
        self.clear_button.setToolTip(_('Clear all settings for this plugin'))
        self.clear_button.clicked.connect(self._clear_settings)

        if DEBUG:
            self.edit_button = button_box.addButton(_('Edit'), QDialogButtonBox.ResetRole)
            self.edit_button.setIcon(get_icon('edit_input.png'))
            self.edit_button.setToolTip(_('Edit settings.'))
            self.edit_button.clicked.connect(self._edit_settings)

            self.save_button = button_box.addButton(_('Save'), QDialogButtonBox.ResetRole)
            self.save_button.setIcon(get_icon('save.png'))
            self.save_button.setToolTip(_('Save setting for this plugin'))
            self.save_button.clicked.connect(self._save_settings)
            self.save_button.setEnabled(False)
        layout.addWidget(button_box)

    def _populate_settings(self):
        self.keys_list.clear()
        ns_prefix = self._get_ns_prefix()
        keys = sorted([k[len(ns_prefix):] for k in self.db.prefs.iterkeys()
                       if k.startswith(ns_prefix)])
        for key in keys:
            self.keys_list.addItem(key)
        self.keys_list.setMinimumWidth(self.keys_list.sizeHintForColumn(0))
        self.keys_list.currentRowChanged[int].connect(self._current_row_changed)

    def _current_row_changed(self, new_row):
        if new_row < 0:
            self.value_text.clear()
            return
        key = unicode(self.keys_list.currentItem().text())
        val = self.db.prefs.get_namespaced(self.namespace, key, '')
        self.value_text.setPlainText(self.db.prefs.to_raw(val))

    def _get_ns_prefix(self):
        return 'namespaced:%s:'% self.namespace

    def _edit_settings(self):
        from calibre.gui2.dialogs.confirm_delete import confirm
        message = '<p>' + _('Are you sure you want to edit settings in this library for this plugin?') + '</p>' \
                  + '<p>' + _('The FanFicFare team does not support hand edited configurations.') + '</p>'
        if confirm(message, self.namespace+'_edit_settings', self):
            self.save_button.setEnabled(True)
            self.edit_button.setEnabled(False)
            self.value_text.setReadOnly(False)

    def _save_settings(self):
        from calibre.gui2.dialogs.confirm_delete import confirm
        message = '<p>' + _('Are you sure you want to save this setting in this library for this plugin?') + '</p>' \
                  + '<p>' + _('Any settings in other libraries or stored in a JSON file in your calibre plugins folder will not be touched.') + '</p>' \
                  + '<p>' + _('You must restart calibre afterwards.') + '</p>'
        if not confirm(message, self.namespace+'_save_settings', self):
            return
        ns_prefix = self._get_ns_prefix()
        key = unicode(self.keys_list.currentItem().text())
        self.db.prefs.set_namespaced(self.namespace, key,
                                     self.db.prefs.raw_to_object(self.value_text.toPlainText()))
        d = info_dialog(self, 'Settings saved',
                        '<p>' + _('All settings for this plugin in this library have been saved.') + '</p>' \
                        + '<p>' + _('Please restart calibre now.') + '</p>',
                        show_copy_button=False)
        b = d.bb.addButton(_('Restart calibre now'), d.bb.AcceptRole)
#.........这里部分代码省略.........
开发者ID:JimmXinu,项目名称:FanFicFare,代码行数:103,代码来源:common_utils.py

示例3: ManageKeysDialog

# 需要导入模块: from PyQt5.Qt import QListWidget [as 别名]
# 或者: from PyQt5.Qt.QListWidget import currentItem [as 别名]

#.........这里部分代码省略.........
        if type(self.plugin_keys) == dict:
            for key in self.plugin_keys.keys():
                self.listy.addItem(QListWidgetItem(key))
        else:
            for key in self.plugin_keys:
                self.listy.addItem(QListWidgetItem(key))

    def add_key(self):
        d = self.create_key(self)
        d.exec_()

        if d.result() != d.Accepted:
            # New key generation cancelled.
            return
        new_key_value = d.key_value
        if type(self.plugin_keys) == dict:
            if new_key_value in self.plugin_keys.values():
                old_key_name = [name for name, value in self.plugin_keys.iteritems() if value == new_key_value][0]
                info_dialog(None, "{0} {1}: Duplicate {2}".format(PLUGIN_NAME, PLUGIN_VERSION,self.key_type_name),
                                    u"The new {1} is the same as the existing {1} named <strong>{0}</strong> and has not been added.".format(old_key_name,self.key_type_name), show=True)
                return
            self.plugin_keys[d.key_name] = new_key_value
        else:
            if new_key_value in self.plugin_keys:
                info_dialog(None, "{0} {1}: Duplicate {2}".format(PLUGIN_NAME, PLUGIN_VERSION,self.key_type_name),
                                    u"This {0} is already in the list of {0}s has not been added.".format(self.key_type_name), show=True)
                return

            self.plugin_keys.append(d.key_value)
        self.listy.clear()
        self.populate_list()

    def rename_key(self):
        if not self.listy.currentItem():
            errmsg = u"No {0} selected to rename. Highlight a keyfile first.".format(self.key_type_name)
            r = error_dialog(None, "{0} {1}".format(PLUGIN_NAME, PLUGIN_VERSION),
                                    _(errmsg), show=True, show_copy_button=False)
            return

        d = RenameKeyDialog(self)
        d.exec_()

        if d.result() != d.Accepted:
            # rename cancelled or moot.
            return
        keyname = unicode(self.listy.currentItem().text())
        if not question_dialog(self, "{0} {1}: Confirm Rename".format(PLUGIN_NAME, PLUGIN_VERSION), u"Do you really want to rename the {2} named <strong>{0}</strong> to <strong>{1}</strong>?".format(keyname,d.key_name,self.key_type_name), show_copy_button=False, default_yes=False):
            return
        self.plugin_keys[d.key_name] = self.plugin_keys[keyname]
        del self.plugin_keys[keyname]

        self.listy.clear()
        self.populate_list()

    def delete_key(self):
        if not self.listy.currentItem():
            return
        keyname = unicode(self.listy.currentItem().text())
        if not question_dialog(self, "{0} {1}: Confirm Delete".format(PLUGIN_NAME, PLUGIN_VERSION), u"Do you really want to delete the {1} <strong>{0}</strong>?".format(keyname, self.key_type_name), show_copy_button=False, default_yes=False):
            return
        if type(self.plugin_keys) == dict:
            del self.plugin_keys[keyname]
        else:
            self.plugin_keys.remove(keyname)

        self.listy.clear()
开发者ID:Bearhomeng,项目名称:DeDRM_tools,代码行数:70,代码来源:config.py

示例4: ManageKeysDialog

# 需要导入模块: from PyQt5.Qt import QListWidget [as 别名]
# 或者: from PyQt5.Qt.QListWidget import currentItem [as 别名]
class ManageKeysDialog(QDialog):
    def __init__(self, parent, key_type_name, plugin_keys, create_key, keyfile_ext = u""):
        QDialog.__init__(self,parent)
        self.parent = parent
        self.key_type_name = key_type_name
        self.plugin_keys = plugin_keys
        self.create_key = create_key
        self.keyfile_ext = keyfile_ext
        self.json_file = (keyfile_ext == u"k4i")

        self.setWindowTitle("{0} {1}: Manage {2}s".format(PLUGIN_NAME, PLUGIN_VERSION, self.key_type_name))

        # Start Qt Gui dialog layout
        layout = QVBoxLayout(self)
        self.setLayout(layout)

        keys_group_box = QGroupBox(_(u"{0}s".format(self.key_type_name)), self)
        layout.addWidget(keys_group_box)
        keys_group_box_layout = QHBoxLayout()
        keys_group_box.setLayout(keys_group_box_layout)

        self.listy = QListWidget(self)
        self.listy.setToolTip(u"{0}s that will be used to decrypt ebooks".format(self.key_type_name))
        self.listy.setSelectionMode(QAbstractItemView.SingleSelection)
        self.populate_list()
        keys_group_box_layout.addWidget(self.listy)

        button_layout = QVBoxLayout()
        keys_group_box_layout.addLayout(button_layout)
        self._add_key_button = QtGui.QToolButton(self)
        self._add_key_button.setIcon(QIcon(I('plus.png')))
        self._add_key_button.setToolTip(u"Create new {0}".format(self.key_type_name))
        self._add_key_button.clicked.connect(self.add_key)
        button_layout.addWidget(self._add_key_button)

        self._delete_key_button = QtGui.QToolButton(self)
        self._delete_key_button.setToolTip(_(u"Delete highlighted key"))
        self._delete_key_button.setIcon(QIcon(I('list_remove.png')))
        self._delete_key_button.clicked.connect(self.delete_key)
        button_layout.addWidget(self._delete_key_button)

        spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        button_layout.addItem(spacerItem)

        layout.addSpacing(5)
        migrate_layout = QHBoxLayout()
        layout.addLayout(migrate_layout)
        migrate_layout.addStretch()
        self.button_box = QDialogButtonBox(QDialogButtonBox.Close)
        self.button_box.rejected.connect(self.close)
        migrate_layout.addWidget(self.button_box)

        self.resize(self.sizeHint())

    def populate_list(self):
        if type(self.plugin_keys) == dict:
            for key in self.plugin_keys.keys():
                self.listy.addItem(QListWidgetItem(key))
        else:
            for key in self.plugin_keys:
                self.listy.addItem(QListWidgetItem(key))

    def add_key(self):
        d = self.create_key(self)
        d.exec_()

        if d.result() != d.Accepted:
            # New key generation cancelled.
            return
        new_key_value = d.key_value
        if new_key_value in self.plugin_keys:
            info_dialog(None, "{0} {1}: Duplicate {2}".format(PLUGIN_NAME, PLUGIN_VERSION,self.key_type_name),
                        u"This {0} is already in the list of {0}s has not been added.".format(self.key_type_name), show=True)
            return

        self.plugin_keys.append(d.key_value)
        self.listy.clear()
        self.populate_list()

    def rename_key(self):
        if not self.listy.currentItem():
            errmsg = u"No {0} selected to rename. Highlight a keyfile first.".format(self.key_type_name)
            r = error_dialog(None, "{0} {1}".format(PLUGIN_NAME, PLUGIN_VERSION),
                                    _(errmsg), show=True, show_copy_button=False)
            return

        d = RenameKeyDialog(self)
        d.exec_()

        if d.result() != d.Accepted:
            # rename cancelled or moot.
            return
        keyname = unicode(self.listy.currentItem().text())
        if not question_dialog(self, "{0} {1}: Confirm Rename".format(PLUGIN_NAME, PLUGIN_VERSION), u"Do you really want to rename the {2} named <strong>{0}</strong> to <strong>{1}</strong>?".format(keyname,d.key_name,self.key_type_name), show_copy_button=False, default_yes=False):
            return
        self.plugin_keys[d.key_name] = self.plugin_keys[keyname]
        del self.plugin_keys[keyname]

        self.listy.clear()
        self.populate_list()
#.........这里部分代码省略.........
开发者ID:Bearhomeng,项目名称:DeDRM_tools,代码行数:103,代码来源:config.py

示例5: ConfigWidget

# 需要导入模块: from PyQt5.Qt import QListWidget [as 别名]
# 或者: from PyQt5.Qt.QListWidget import currentItem [as 别名]

#.........这里部分代码省略.........
        self.l.addWidget(self.checkbox_include_guide_text, row, 0)
        self.l.addWidget(self.label_include_guide_text, row, 1)
        row += 1
        self.l.addWidget(self.include_guide_text, row, 1)
        row += 1
        
        self.l.addWidget(QLabel(''), row, 1)
        row += 1
        
        self.label_include_toc_text = QLabel('Include in TOC with string:')
        self.checkbox_include_toc_text = QCheckBox('', self)
        self.checkbox_include_toc_text.setChecked(prefs['checkbox_include_toc_text'] == 'True')
        self.include_toc_text = QLineEdit(self)
        self.include_toc_text.setText(prefs['include_toc_text'])
        
        self.l.addWidget(self.checkbox_include_toc_text, row, 0)
        self.l.addWidget(self.label_include_toc_text, row, 1)
        row += 1
        self.l.addWidget(self.include_toc_text, row, 1)
        row += 1
        
        self.l.addWidget(QLabel(''), row, 1)
        row += 1
        
        self.label_ask_replace = QLabel('Ask before replacing book in library')
        self.checkbox_ask_replace = QCheckBox('', self)
        self.checkbox_ask_replace.setChecked(prefs['checkbox_ask_replace'] == 'True')
        self.l.addWidget(self.checkbox_ask_replace, row, 0)
        self.l.addWidget(self.label_ask_replace, row, 1)
        row += 1
        
        self.label_disable_first_last_only = QLabel('When multiple EPUB files are selected, allow insertion points other than "1", "first", and "last"')
        self.checkbox_disable_first_last_only = QCheckBox('', self)
        self.checkbox_disable_first_last_only.setChecked(prefs['checkbox_disable_first_last_only'] == 'True')
        self.l.addWidget(self.checkbox_disable_first_last_only, row, 0)
        self.l.addWidget(self.label_disable_first_last_only, row, 1)
        row += 1
        
        self.l.addWidget(QLabel(''), row, 1)
        row += 1
        
        self.label_replace_strings = QLabel('Replace strings:')
        self.replace_strings = QTextEdit(self)
        self.replace_strings.setText(prefs['replace_strings'])
        self.label_supportedMetadata = QLabel('Supported metadata:')
        self.supportedMetadata = QListWidget(self)
        #QtCore.QObject.connect(self.supportedMetadata, QtCore.SIGNAL("doubleClicked()"), self.add_replace_string)
        self.supportedMetadata.doubleClicked.connect(self.add_replace_string)
        
        producer = exlibris()
        tags = producer.getSupportedMetadataList()
        for tag in tags:
            self.supportedMetadata.addItem(tag.decode('utf-8'))
        
        self.l.addWidget(self.label_replace_strings, row, 1)
        self.l.addWidget(self.label_supportedMetadata, row, 2)
        row += 1
        self.l.addWidget(self.replace_strings, row, 1)
        self.l.addWidget(self.supportedMetadata, row, 2)
        row += 1
                
        self.resize(self.sizeHint())


    def add_replace_string(self):
        currentText = str(self.replace_strings.toPlainText()).strip()
        currentItem = self.supportedMetadata.currentItem().text()
        self.replace_strings.setText("%s\n%s=your_value_goes_here;" % (currentText, currentItem))


    def button_xhtml_filename_handler(self):
        initial_dir = os.path.dirname(str(self.xhtml_filename.text()))
        new_file = QFileDialog.getOpenFileName(self, "Select ex libris XHTML file", initial_dir, "XHTML Files (*.xhtml)")
        if new_file:
            new_file = new_file[0] if isinstance(new_file, tuple) else new_file
            if new_file and os.path.exists(new_file):
                self.xhtml_filename.setText(new_file)

    def button_include_dir_handler(self):
        initial_dir = os.path.dirname(str(self.include_dir.text()))
        dirDialog = QFileDialog()
        dirDialog.setFileMode(QFileDialog.Directory)
        new_dir = dirDialog.getExistingDirectory(self, "Select directory", initial_dir)
        if (len(new_dir) > 0):
            self.include_dir.setText(new_dir)

    def save_settings(self):
        prefs['xhtml_filename'] = unicode(self.xhtml_filename.text())
        prefs['include_dir'] = unicode(self.include_dir.text())
        prefs['include_guide_text'] = unicode(self.include_guide_text.text())
        prefs['include_toc_text'] = unicode(self.include_toc_text.text())
        prefs['replace_strings'] = unicode(self.replace_strings.toPlainText())
        prefs['checkbox_include_dir'] = unicode(self.checkbox_include_dir.isChecked())
        prefs['checkbox_include_guide_text'] = unicode(self.checkbox_include_guide_text.isChecked())
        prefs['checkbox_include_toc_text'] = unicode(self.checkbox_include_toc_text.isChecked())
        prefs['checkbox_ask_replace'] = unicode(self.checkbox_ask_replace.isChecked())
        prefs['checkbox_disable_first_last_only'] = unicode(self.checkbox_disable_first_last_only.isChecked())
        # TODO: save value from main.py (?)
        prefs['include_spine'] = u'1'
        prefs['include_toc'] = u'1'
开发者ID:pettarin,项目名称:exlibris,代码行数:104,代码来源:config.py

示例6: PrefsViewerDialog

# 需要导入模块: from PyQt5.Qt import QListWidget [as 别名]
# 或者: from PyQt5.Qt.QListWidget import currentItem [as 别名]
class PrefsViewerDialog(SizePersistedDialog):

    def __init__(self, gui, namespace):
        SizePersistedDialog.__init__(self, gui, 'Prefs Viewer dialog')
        self.setWindowTitle('Preferences for: '+namespace)

        self.gui = gui
        self.db = gui.current_db
        self.namespace = namespace
        self._init_controls()
        self.resize_dialog()

        self._populate_settings()

        if self.keys_list.count():
            self.keys_list.setCurrentRow(0)

    def _init_controls(self):
        layout = QVBoxLayout(self)
        self.setLayout(layout)

        ml = QHBoxLayout()
        layout.addLayout(ml, 1)

        self.keys_list = QListWidget(self)
        self.keys_list.setSelectionMode(QAbstractItemView.SingleSelection)
        self.keys_list.setFixedWidth(150)
        self.keys_list.setAlternatingRowColors(True)
        ml.addWidget(self.keys_list)
        self.value_text = QTextEdit(self)
        self.value_text.setTabStopWidth(24)
        self.value_text.setReadOnly(False)
        ml.addWidget(self.value_text, 1)

        button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        button_box.accepted.connect(self._apply_changes)
        button_box.rejected.connect(self.reject)
        self.clear_button = button_box.addButton('Clear', QDialogButtonBox.ResetRole)
        self.clear_button.setIcon(get_icon('trash.png'))
        self.clear_button.setToolTip('Clear all settings for this plugin')
        self.clear_button.clicked.connect(self._clear_settings)
        layout.addWidget(button_box)

    def _populate_settings(self):
        self.keys_list.clear()
        ns_prefix = self._get_ns_prefix()
        keys = sorted([k[len(ns_prefix):] for k in self.db.prefs.iterkeys()
                       if k.startswith(ns_prefix)])
        for key in keys:
            self.keys_list.addItem(key)
        self.keys_list.setMinimumWidth(self.keys_list.sizeHintForColumn(0))
        self.keys_list.currentRowChanged[int].connect(self._current_row_changed)

    def _current_row_changed(self, new_row):
        if new_row < 0:
            self.value_text.clear()
            return
        key = unicode(self.keys_list.currentItem().text())
        val = self.db.prefs.get_namespaced(self.namespace, key, '')
        self.value_text.setPlainText(self.db.prefs.to_raw(val))

    def _get_ns_prefix(self):
        return 'namespaced:%s:'% self.namespace

    def _apply_changes(self):
        from calibre.gui2.dialogs.confirm_delete import confirm
        message = '<p>Are you sure you want to change your settings in this library for this plugin?</p>' \
                  '<p>Any settings in other libraries or stored in a JSON file in your calibre plugins ' \
                  'folder will not be touched.</p>' \
                  '<p>You must restart calibre afterwards.</p>'
        if not confirm(message, self.namespace+'_clear_settings', self):
            return

        val = self.db.prefs.raw_to_object(unicode(self.value_text.toPlainText()))
        key = unicode(self.keys_list.currentItem().text())
        self.db.prefs.set_namespaced(self.namespace, key, val)

        restart = prompt_for_restart(self, 'Settings changed',
                           '<p>Settings for this plugin in this library have been changed.</p>'
                           '<p>Please restart calibre now.</p>')
        self.close()
        if restart:
            self.gui.quit(restart=True)

    def _clear_settings(self):
        from calibre.gui2.dialogs.confirm_delete import confirm
        message = '<p>Are you sure you want to clear your settings in this library for this plugin?</p>' \
                  '<p>Any settings in other libraries or stored in a JSON file in your calibre plugins ' \
                  'folder will not be touched.</p>' \
                  '<p>You must restart calibre afterwards.</p>'
        if not confirm(message, self.namespace+'_clear_settings', self):
            return

        ns_prefix = self._get_ns_prefix()
        keys = [k for k in self.db.prefs.iterkeys() if k.startswith(ns_prefix)]
        for k in keys:
            del self.db.prefs[k]
        self._populate_settings()
        restart = prompt_for_restart(self, 'Settings deleted',
                           '<p>All settings for this plugin in this library have been cleared.</p>'
#.........这里部分代码省略.........
开发者ID:hkjinlee,项目名称:calibre-naverbook-plugin,代码行数:103,代码来源:common_utils.py

示例7: SavedSearchEditor

# 需要导入模块: from PyQt5.Qt import QListWidget [as 别名]
# 或者: from PyQt5.Qt.QListWidget import currentItem [as 别名]
class SavedSearchEditor(Dialog):

    def __init__(self, parent, initial_search=None):
        self.initial_search = initial_search
        Dialog.__init__(
            self, _('Manage saved searches'), 'manage-saved-searches', parent)

    def setup_ui(self):
        from calibre.gui2.ui import get_gui
        db = get_gui().current_db
        self.l = l = QVBoxLayout(self)
        b = self.bb.addButton(_('&Add search'), self.bb.ActionRole)
        b.setIcon(QIcon(I('plus.png')))
        b.clicked.connect(self.add_search)

        b = self.bb.addButton(_('&Remove search'), self.bb.ActionRole)
        b.setIcon(QIcon(I('minus.png')))
        b.clicked.connect(self.del_search)

        b = self.bb.addButton(_('&Edit search'), self.bb.ActionRole)
        b.setIcon(QIcon(I('modified.png')))
        b.clicked.connect(self.edit_search)

        self.slist = QListWidget(self)
        self.slist.setStyleSheet('QListView::item { padding: 3px }')
        self.slist.activated.connect(self.edit_search)
        self.slist.setAlternatingRowColors(True)
        self.searches = {name: db.saved_search_lookup(name) for name in db.saved_search_names()}
        self.populate_search_list()
        if self.initial_search is not None and self.initial_search in self.searches:
            self.select_search(self.initial_search)
        elif self.searches:
            self.slist.setCurrentRow(0)
        self.slist.currentItemChanged.connect(self.current_index_changed)
        l.addWidget(self.slist)

        self.desc = la = QLabel('\xa0')
        la.setWordWrap(True)
        l.addWidget(la)

        l.addWidget(self.bb)
        self.current_index_changed(self.slist.currentItem())
        self.setMinimumHeight(500)
        self.setMinimumWidth(600)

    @property
    def current_search_name(self):
        i = self.slist.currentItem()
        if i is not None:
            ans = i.text()
            if ans in self.searches:
                return ans

    def populate_search_list(self):
        self.slist.clear()
        for name in sorted(self.searches.keys(), key=sort_key):
            self.slist.addItem(name)

    def add_search(self):
        d = AddSavedSearch(parent=self, commit_changes=False)
        if d.exec_() != d.Accepted:
            return
        name, expression = d.accepted_data
        nmap = {icu_lower(n):n for n in self.searches}
        if icu_lower(name) in nmap:
            q = nmap[icu_lower(name)]
            del self.searches[q]
            self.select_search(q)
            self.slist.takeItem(self.slist.currentRow())
        self.searches[name] = expression
        self.slist.insertItem(0, name)
        self.slist.setCurrentRow(0)
        self.current_index_changed(self.slist.currentItem())

    def del_search(self):
        n = self.current_search_name
        if n is not None:
            if not confirm(
                '<p>' + _(
                    'The current saved search will be '
                    '<b>permanently deleted</b>. Are you sure?') + '</p>',
                'saved_search_editor_delete', self):
                return
            self.slist.takeItem(self.slist.currentRow())
            del self.searches[n]

    def edit_search(self):
        n = self.current_search_name
        if not n:
            return
        d = AddSavedSearch(parent=self, commit_changes=False, label=_('Edit the name and/or expression below.'), validate=self.validate_edit)
        d.setWindowTitle(_('Edit saved search'))
        d.sname.setText(n)
        d.search.setText(self.searches[n])
        if d.exec_() != d.Accepted:
            return
        name, expression = d.accepted_data
        self.slist.currentItem().setText(name)
        del self.searches[n]
        self.searches[name] = expression
#.........这里部分代码省略.........
开发者ID:JimmXinu,项目名称:calibre,代码行数:103,代码来源:saved_search_editor.py


注:本文中的PyQt5.Qt.QListWidget.currentItem方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。