diff --git a/qutebrowser/models/completionfilter.py b/qutebrowser/models/completionfilter.py index 0ba6709be..ba2e1e057 100644 --- a/qutebrowser/models/completionfilter.py +++ b/qutebrowser/models/completionfilter.py @@ -32,12 +32,15 @@ class CompletionFilterModel(QSortFilterProxyModel): """Subclass of QSortFilterProxyModel with custom sorting/filtering. Attributes: + srcmodel: The source model of this QSFPM. _pattern: The pattern to filter with, used in pattern property. """ - def __init__(self, parent=None): + def __init__(self, source, parent=None): super().__init__(parent) + super().setSourceModel(source) + self.srcmodel = source self._pattern = '' @property @@ -61,12 +64,11 @@ class CompletionFilterModel(QSortFilterProxyModel): self._pattern = val self.invalidateFilter() sortcol = 0 - if self.sourceModel() is not None: - try: - self.sourceModel().sort(sortcol) - except NotImplementedError: - self.sort(sortcol) - self.invalidate() + try: + self.srcmodel.sort(sortcol) + except NotImplementedError: + self.sort(sortcol) + self.invalidate() def first_item(self): """Return the first item in the model.""" @@ -82,6 +84,7 @@ class CompletionFilterModel(QSortFilterProxyModel): """Override QSortFilterProxyModel's setSourceModel to clear pattern.""" logging.debug("Setting source model: {}".format(model)) self.pattern = '' + self.srcmodel = model super().setSourceModel(model) def filterAcceptsRow(self, row, parent): @@ -100,8 +103,8 @@ class CompletionFilterModel(QSortFilterProxyModel): """ if parent == QModelIndex(): return True - idx = self.sourceModel().index(row, 0, parent) - data = self.sourceModel().data(idx).value() + idx = self.srcmodel.index(row, 0, parent) + data = self.srcmodel.data(idx).value() # TODO more sophisticated filtering if not self.pattern: return True @@ -121,8 +124,8 @@ class CompletionFilterModel(QSortFilterProxyModel): True if left < right, else False """ - left = self.sourceModel().data(lindex).value() - right = self.sourceModel().data(rindex).value() + left = self.srcmodel.data(lindex).value() + right = self.srcmodel.data(rindex).value() leftstart = left.startswith(self.pattern) rightstart = right.startswith(self.pattern) diff --git a/qutebrowser/widgets/completion.py b/qutebrowser/widgets/completion.py index 9c74eaaac..3e9d1190d 100644 --- a/qutebrowser/widgets/completion.py +++ b/qutebrowser/widgets/completion.py @@ -49,6 +49,7 @@ class CompletionView(QTreeView): Highlights completions based on marks in the UserRole. Attributes: + model: The currently active filter model. _STYLESHEET: The stylesheet template for the CompletionView. _completion_models: dict of available completion models. _ignore_next: Whether to ignore the next cmd_text_changed signal. @@ -99,16 +100,13 @@ class CompletionView(QTreeView): def __init__(self, parent=None): super().__init__(parent) self._enabled = config.config.get('general', 'show_completion') - self._completion_models = {} - self._completion_models[''] = None - self._completion_models['command'] = CompletionFilterModel( - CommandCompletionModel()) - self._completion_models['setting'] = CompletionFilterModel( - SettingCompletionModel()) + self._completion_models = { + 'command': CompletionFilterModel(CommandCompletionModel(self)), + 'setting': CompletionFilterModel(SettingCompletionModel(self)), + } self._ignore_next = False self._completing = False - self.set_model('command') self._delegate = _CompletionItemDelegate(self) self.setItemDelegate(self._delegate) self.setStyleSheet(get_stylesheet(self._STYLESHEET)) @@ -122,6 +120,7 @@ class CompletionView(QTreeView): # Some background: http://bugs.quassel-irc.org/issues/663 # The proposed fix there was later reverted because it didn't help. self.setUniformRowHeights(True) + self.set_model('command') self.hide() # FIXME set elidemode @@ -161,7 +160,9 @@ class CompletionView(QTreeView): model: An index into self._completion_models. """ - self.setModel(self._completion_models[model]) + m = self._completion_models[model] + self.setModel(m) + self.model = m self.expandAll() self.resizeColumnToContents(0) @@ -205,7 +206,7 @@ class CompletionView(QTreeView): text = text.split()[-1] logging.debug("pattern: {}".format(text)) self.model.pattern = text - self.model.sourceModel().mark_all_items(text) + self.model.srcmodel.mark_all_items(text) if self._enabled: self.show()