From 6d43691e46b03266035e684a92664c3327ef355c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 27 Aug 2011 12:14:26 -0600 Subject: [PATCH] Keyboard preferences: Add highligh_group() method to ease embedding of keyboard prefs in plugin config dialogs --- src/calibre/gui2/actions/__init__.py | 6 +++--- src/calibre/gui2/keyboard.py | 21 +++++++++++++++++++++ src/calibre/gui2/preferences/keyboard.py | 3 +++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/calibre/gui2/actions/__init__.py b/src/calibre/gui2/actions/__init__.py index ba8eafb5ca..9498d8ad9d 100644 --- a/src/calibre/gui2/actions/__init__.py +++ b/src/calibre/gui2/actions/__init__.py @@ -149,11 +149,11 @@ class InterfaceAction(QObject): if shortcut_name is None and spec[0]: shortcut_name = unicode(spec[0]) - if shortcut_name and not (attr=='qaction' and self.popup_type == - QToolButton.InstantPopup): + if shortcut_name and self.action_spec[0] and not ( + attr == 'qaction' and self.popup_type == QToolButton.InstantPopup): try: self.gui.keyboard.register_shortcut(self.unique_name + ' - ' + attr, - unicode(spec[0]), default_keys=keys, + shortcut_name, default_keys=keys, action=shortcut_action, description=desc, group=self.action_spec[0]) except NameConflict as e: diff --git a/src/calibre/gui2/keyboard.py b/src/calibre/gui2/keyboard.py index 354a3e786a..889e472d06 100644 --- a/src/calibre/gui2/keyboard.py +++ b/src/calibre/gui2/keyboard.py @@ -307,6 +307,18 @@ class ConfigModel(QAbstractItemModel, SearchQueryParser): return (self.index(ans[0], 0) if ans[1] < 0 else self.index(ans[1], 0, self.index(ans[0], 0))) + def index_for_group(self, name): + for i in range(self.rowCount()): + node = self.data[i] + if node.data == name: + return self.index(i, 0) + + @property + def group_names(self): + for i in range(self.rowCount()): + node = self.data[i] + yield node.data + # }}} class Editor(QFrame): # {{{ @@ -632,6 +644,15 @@ class ShortcutConfig(QWidget): # {{{ unicode(self.search.currentText()), backwards=True) self.highlight_index(idx) + def highlight_group(self, group_name): + idx = self.view.model().index_for_group(group_name) + if idx is not None: + self.view.expand(idx) + self.view.scrollTo(idx, self.view.PositionAtTop) + self.view.selectionModel().select(idx, + self.view.selectionModel().ClearAndSelect) + self.view.setCurrentIndex(idx) + self.view.setFocus(Qt.OtherFocusReason) # }}} diff --git a/src/calibre/gui2/preferences/keyboard.py b/src/calibre/gui2/preferences/keyboard.py index ccc3a390d3..961a928cb3 100644 --- a/src/calibre/gui2/preferences/keyboard.py +++ b/src/calibre/gui2/preferences/keyboard.py @@ -37,6 +37,9 @@ class ConfigWidget(ConfigWidgetBase): def refresh_gui(self, gui): gui.keyboard.finalize() + def highligh_group(self, group_name): + self.conf_widget.highlight_group(group_name) + if __name__ == '__main__': from PyQt4.Qt import QApplication app = QApplication([])