def setModel(self, model): QTreeView.setModel(self, model) model.current_toc_nodes_changed.connect( self.current_toc_nodes_changed, type=Qt.ConnectionType.QueuedConnection)
class ShortcutConfig(QWidget): # {{{ changed_signal = pyqtSignal() def __init__(self, parent=None): QWidget.__init__(self, parent) self._layout = l = QVBoxLayout(self) self.header = QLabel( _('Double click on any entry to change the' ' keyboard shortcuts associated with it')) l.addWidget(self.header) self.view = QTreeView(self) self.view.setAlternatingRowColors(True) self.view.setHeaderHidden(True) self.view.setAnimated(True) self.view.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) self.view.customContextMenuRequested.connect(self.show_context_menu) l.addWidget(self.view) self.delegate = Delegate() self.view.setItemDelegate(self.delegate) self.delegate.sizeHintChanged.connect( self.editor_opened, type=Qt.ConnectionType.QueuedConnection) self.delegate.changed_signal.connect(self.changed_signal) self.search = SearchBox2(self) self.search.initialize('shortcuts_search_history', help_text=_('Search for a shortcut by name')) self.search.search.connect(self.find) self._h = h = QHBoxLayout() l.addLayout(h) h.addWidget(self.search) self.nb = QPushButton(QIcon(I('arrow-down.png')), _('&Next'), self) self.pb = QPushButton(QIcon(I('arrow-up.png')), _('&Previous'), self) self.nb.clicked.connect(self.find_next) self.pb.clicked.connect(self.find_previous) h.addWidget(self.nb), h.addWidget(self.pb) h.setStretch(0, 100) def show_context_menu(self, pos): menu = QMenu(self) menu.addAction(_('Expand all'), self.view.expandAll) menu.addAction(_('Collapse all'), self.view.collapseAll) menu.exec_(self.view.mapToGlobal(pos)) def restore_defaults(self): self._model.restore_defaults() self.changed_signal.emit() def commit(self): if self.view.state() == QAbstractItemView.State.EditingState: self.delegate.accept_changes() self._model.commit() def initialize(self, keyboard): self._model = ConfigModel(keyboard, parent=self) self.view.setModel(self._model) def editor_opened(self, index): self.view.scrollTo(index, QAbstractItemView.ScrollHint.EnsureVisible) @property def is_editing(self): return self.view.state() == QAbstractItemView.State.EditingState def find(self, query): if not query: return try: idx = self._model.find(query) except ParseException: self.search.search_done(False) return self.search.search_done(True) if not idx.isValid(): info_dialog(self, _('No matches'), _('Could not find any shortcuts matching %s') % query, show=True, show_copy_button=False) return self.highlight_index(idx) def highlight_index(self, idx): self.view.scrollTo(idx) self.view.selectionModel().select( idx, QItemSelectionModel.SelectionFlag.ClearAndSelect) self.view.setCurrentIndex(idx) self.view.setFocus(Qt.FocusReason.OtherFocusReason) def find_next(self, *args): idx = self.view.currentIndex() if not idx.isValid(): idx = self._model.index(0, 0) idx = self._model.find_next(idx, unicode_type(self.search.currentText())) self.highlight_index(idx) def find_previous(self, *args): idx = self.view.currentIndex() if not idx.isValid(): idx = self._model.index(0, 0) idx = self._model.find_next(idx, unicode_type(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, QAbstractItemView.ScrollHint.PositionAtTop) self.view.selectionModel().select( idx, QItemSelectionModel.SelectionFlag.ClearAndSelect) self.view.setCurrentIndex(idx) self.view.setFocus(Qt.FocusReason.OtherFocusReason)