From 387c84beffbcdd745bbf9711cc026e257195bfc2 Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Fri, 21 Aug 2015 16:05:33 +0200 Subject: [PATCH 1/5] Completion model filters are now per-window (via completer.py) --- qutebrowser/completion/completer.py | 18 ++++++++++++++++-- qutebrowser/completion/models/base.py | 6 ++++++ qutebrowser/completion/models/instances.py | 11 ++++------- qutebrowser/completion/models/sortfilter.py | 4 +++- qutebrowser/completion/models/urlmodel.py | 1 + 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/qutebrowser/completion/completer.py b/qutebrowser/completion/completer.py index 2df964658..91ebd9f71 100644 --- a/qutebrowser/completion/completer.py +++ b/qutebrowser/completion/completer.py @@ -25,6 +25,7 @@ from qutebrowser.config import config from qutebrowser.commands import cmdexc, cmdutils, runners from qutebrowser.utils import usertypes, log, objreg, utils from qutebrowser.completion.models import instances +from qutebrowser.completion.models.sortfilter import CompletionFilterModel class Completer(QObject): @@ -153,7 +154,19 @@ class Completer(QObject): model = None else: model = instances.get(completion) - return model + return self._get_filter_completion_model(model) + + def _get_filter_completion_model(self, model): + """Wraps the argument model with a CompletionFilterModel. + + Args: + model: the source model. + + Return: + A completion filter model. + """ + filtermodel = CompletionFilterModel(source=model, parent=self.parent()) + return filtermodel def _filter_cmdline_parts(self, parts, cursor_part): """Filter a list of commandline parts to exclude flags. @@ -202,7 +215,8 @@ class Completer(QObject): "{}".format(parts, cursor_part)) if cursor_part == 0: # '|' or 'set|' - return instances.get(usertypes.Completion.command) + model = instances.get(usertypes.Completion.command) + return self._get_filter_completion_model(model) # delegate completion to command try: completions = cmdutils.cmd_dict[parts[0]].completion diff --git a/qutebrowser/completion/models/base.py b/qutebrowser/completion/models/base.py index a35c7f063..df0d7eb77 100644 --- a/qutebrowser/completion/models/base.py +++ b/qutebrowser/completion/models/base.py @@ -40,12 +40,18 @@ class BaseCompletionModel(QStandardItemModel): Used for showing completions later in the CompletionView. Supports setting marks and adding new categories/items easily. + Attributes: + dumb_sort: the sorting that will be used by the wrapping + CompletionFilterModel + Class Attributes: COLUMN_WIDTHS: The width percentages of the columns used in the completion view. + DUMB_SORT: the dumb sorting used by the model """ COLUMN_WIDTHS = (30, 70, 0) + DUMB_SORT = None def __init__(self, parent=None): super().__init__(parent) diff --git a/qutebrowser/completion/models/instances.py b/qutebrowser/completion/models/instances.py index 0fc683488..4e993995b 100644 --- a/qutebrowser/completion/models/instances.py +++ b/qutebrowser/completion/models/instances.py @@ -27,10 +27,9 @@ Module attributes: import functools -from PyQt5.QtCore import pyqtSlot, Qt +from PyQt5.QtCore import pyqtSlot from qutebrowser.completion.models import miscmodels, urlmodel, configmodel -from qutebrowser.completion.models.sortfilter import CompletionFilterModel from qutebrowser.utils import objreg, usertypes, log, debug from qutebrowser.config import configdata @@ -38,7 +37,7 @@ from qutebrowser.config import configdata _instances = {} -def _init_model(klass, *args, dumb_sort=None, **kwargs): +def _init_model(klass, *args, **kwargs): """Helper to initialize a model. Args: @@ -48,8 +47,7 @@ def _init_model(klass, *args, dumb_sort=None, **kwargs): dumb_sort: Passed to CompletionFilterModel. """ app = objreg.get('app') - return CompletionFilterModel(klass(*args, parent=app, **kwargs), - dumb_sort=dumb_sort, parent=app) + return klass(*args, parent=app, **kwargs) def _init_command_completion(): @@ -70,8 +68,7 @@ def _init_url_completion(): """Initialize the URL completion model.""" log.completion.debug("Initializing URL completion.") with debug.log_time(log.completion, 'URL completion init'): - model = _init_model(urlmodel.UrlCompletionModel, - dumb_sort=Qt.DescendingOrder) + model = _init_model(urlmodel.UrlCompletionModel) _instances[usertypes.Completion.url] = model diff --git a/qutebrowser/completion/models/sortfilter.py b/qutebrowser/completion/models/sortfilter.py index 3c247a0a2..3312e3e7a 100644 --- a/qutebrowser/completion/models/sortfilter.py +++ b/qutebrowser/completion/models/sortfilter.py @@ -41,11 +41,13 @@ class CompletionFilterModel(QSortFilterProxyModel): _sort_order: The order to use for sorting if using dumb_sort. """ - def __init__(self, source, parent=None, *, dumb_sort=None): + def __init__(self, source, parent=None): super().__init__(parent) super().setSourceModel(source) self.srcmodel = source self.pattern = '' + + dumb_sort = self.srcmodel.DUMB_SORT if dumb_sort is None: # pylint: disable=invalid-name self.lessThan = self.intelligentLessThan diff --git a/qutebrowser/completion/models/urlmodel.py b/qutebrowser/completion/models/urlmodel.py index ca1ef31fe..9a2ffdfd2 100644 --- a/qutebrowser/completion/models/urlmodel.py +++ b/qutebrowser/completion/models/urlmodel.py @@ -41,6 +41,7 @@ class UrlCompletionModel(base.BaseCompletionModel): TIME_COLUMN = 2 COLUMN_WIDTHS = (40, 50, 10) + DUMB_SORT = Qt.DescendingOrder def __init__(self, parent=None): super().__init__(parent) From 729c10e0a96725d96ed18b560336e8a400b94175 Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Fri, 21 Aug 2015 16:23:28 +0200 Subject: [PATCH 2/5] Fix PEP issue --- qutebrowser/completion/completer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qutebrowser/completion/completer.py b/qutebrowser/completion/completer.py index 91ebd9f71..6fe4c24ad 100644 --- a/qutebrowser/completion/completer.py +++ b/qutebrowser/completion/completer.py @@ -157,7 +157,7 @@ class Completer(QObject): return self._get_filter_completion_model(model) def _get_filter_completion_model(self, model): - """Wraps the argument model with a CompletionFilterModel. + """Wrap the argument model with a CompletionFilterModel. Args: model: the source model. From 7ed4977d6403f1385ed47c43c9632533cc5d0723 Mon Sep 17 00:00:00 2001 From: Alexander Cogneau Date: Fri, 21 Aug 2015 16:56:36 +0200 Subject: [PATCH 3/5] Change parent of filter models --- qutebrowser/completion/completer.py | 2 +- qutebrowser/completion/models/base.py | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/qutebrowser/completion/completer.py b/qutebrowser/completion/completer.py index 6fe4c24ad..6cd11848a 100644 --- a/qutebrowser/completion/completer.py +++ b/qutebrowser/completion/completer.py @@ -165,7 +165,7 @@ class Completer(QObject): Return: A completion filter model. """ - filtermodel = CompletionFilterModel(source=model, parent=self.parent()) + filtermodel = CompletionFilterModel(source=model, parent=self) return filtermodel def _filter_cmdline_parts(self, parts, cursor_part): diff --git a/qutebrowser/completion/models/base.py b/qutebrowser/completion/models/base.py index df0d7eb77..3a0590bcd 100644 --- a/qutebrowser/completion/models/base.py +++ b/qutebrowser/completion/models/base.py @@ -40,10 +40,6 @@ class BaseCompletionModel(QStandardItemModel): Used for showing completions later in the CompletionView. Supports setting marks and adding new categories/items easily. - Attributes: - dumb_sort: the sorting that will be used by the wrapping - CompletionFilterModel - Class Attributes: COLUMN_WIDTHS: The width percentages of the columns used in the completion view. From 17f971344ddcf73bbb37d174ce93c546784c86b1 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sat, 22 Aug 2015 22:53:52 +0200 Subject: [PATCH 4/5] Update authors. --- README.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.asciidoc b/README.asciidoc index bac1cbdfa..6cb8ae069 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -141,9 +141,9 @@ Contributors, sorted by the number of commits in descending order: * Joel Torstensson * Claude * Lamar Pavel +* Alexander Cogneau * Austin Anderson * Artur Shaik -* Alexander Cogneau * ZDarian * Peter Vilim * John ShaggyTwoDope Jenkins From fc6c49f57c3fa990ee94a65bfff18e0bc55dac5a Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sat, 22 Aug 2015 22:59:41 +0200 Subject: [PATCH 5/5] Cleanup --- qutebrowser/completion/completer.py | 19 ++---------- qutebrowser/completion/models/instances.py | 34 +++++++--------------- 2 files changed, 13 insertions(+), 40 deletions(-) diff --git a/qutebrowser/completion/completer.py b/qutebrowser/completion/completer.py index 6cd11848a..befd2c01d 100644 --- a/qutebrowser/completion/completer.py +++ b/qutebrowser/completion/completer.py @@ -24,8 +24,7 @@ from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QTimer from qutebrowser.config import config from qutebrowser.commands import cmdexc, cmdutils, runners from qutebrowser.utils import usertypes, log, objreg, utils -from qutebrowser.completion.models import instances -from qutebrowser.completion.models.sortfilter import CompletionFilterModel +from qutebrowser.completion.models import instances, sortfilter class Completer(QObject): @@ -154,19 +153,7 @@ class Completer(QObject): model = None else: model = instances.get(completion) - return self._get_filter_completion_model(model) - - def _get_filter_completion_model(self, model): - """Wrap the argument model with a CompletionFilterModel. - - Args: - model: the source model. - - Return: - A completion filter model. - """ - filtermodel = CompletionFilterModel(source=model, parent=self) - return filtermodel + return sortfilter.CompletionFilterModel(source=model, parent=self) def _filter_cmdline_parts(self, parts, cursor_part): """Filter a list of commandline parts to exclude flags. @@ -216,7 +203,7 @@ class Completer(QObject): if cursor_part == 0: # '|' or 'set|' model = instances.get(usertypes.Completion.command) - return self._get_filter_completion_model(model) + return sortfilter.CompletionFilterModel(source=model, parent=self) # delegate completion to command try: completions = cmdutils.cmd_dict[parts[0]].completion diff --git a/qutebrowser/completion/models/instances.py b/qutebrowser/completion/models/instances.py index 4e993995b..7db82c8b6 100644 --- a/qutebrowser/completion/models/instances.py +++ b/qutebrowser/completion/models/instances.py @@ -37,30 +37,17 @@ from qutebrowser.config import configdata _instances = {} -def _init_model(klass, *args, **kwargs): - """Helper to initialize a model. - - Args: - klass: The class of the model to initialize. - *args: Arguments to pass to the model. - **kwargs: Keyword arguments to pass to the model. - dumb_sort: Passed to CompletionFilterModel. - """ - app = objreg.get('app') - return klass(*args, parent=app, **kwargs) - - def _init_command_completion(): """Initialize the command completion model.""" log.completion.debug("Initializing command completion.") - model = _init_model(miscmodels.CommandCompletionModel) + model = miscmodels.CommandCompletionModel() _instances[usertypes.Completion.command] = model def _init_helptopic_completion(): """Initialize the helptopic completion model.""" log.completion.debug("Initializing helptopic completion.") - model = _init_model(miscmodels.HelpCompletionModel) + model = miscmodels.HelpCompletionModel() _instances[usertypes.Completion.helptopic] = model @@ -68,24 +55,23 @@ def _init_url_completion(): """Initialize the URL completion model.""" log.completion.debug("Initializing URL completion.") with debug.log_time(log.completion, 'URL completion init'): - model = _init_model(urlmodel.UrlCompletionModel) + model = urlmodel.UrlCompletionModel() _instances[usertypes.Completion.url] = model def _init_setting_completions(): """Initialize setting completion models.""" log.completion.debug("Initializing setting completion.") - _instances[usertypes.Completion.section] = _init_model( - configmodel.SettingSectionCompletionModel) + _instances[usertypes.Completion.section] = ( + configmodel.SettingSectionCompletionModel()) _instances[usertypes.Completion.option] = {} _instances[usertypes.Completion.value] = {} for sectname in configdata.DATA: - model = _init_model(configmodel.SettingOptionCompletionModel, sectname) + model = configmodel.SettingOptionCompletionModel(sectname) _instances[usertypes.Completion.option][sectname] = model _instances[usertypes.Completion.value][sectname] = {} for opt in configdata.DATA[sectname].keys(): - model = _init_model(configmodel.SettingValueCompletionModel, - sectname, opt) + model = configmodel.SettingValueCompletionModel(sectname, opt) _instances[usertypes.Completion.value][sectname][opt] = model @@ -97,7 +83,7 @@ def init_quickmark_completions(): _instances[usertypes.Completion.quickmark_by_name].deleteLater() except KeyError: pass - model = _init_model(miscmodels.QuickmarkCompletionModel) + model = miscmodels.QuickmarkCompletionModel() _instances[usertypes.Completion.quickmark_by_name] = model @@ -109,7 +95,7 @@ def init_bookmark_completions(): _instances[usertypes.Completion.bookmark_by_url].deleteLater() except KeyError: pass - model = _init_model(miscmodels.BookmarkCompletionModel) + model = miscmodels.BookmarkCompletionModel() _instances[usertypes.Completion.bookmark_by_url] = model @@ -121,7 +107,7 @@ def init_session_completion(): _instances[usertypes.Completion.sessions].deleteLater() except KeyError: pass - model = _init_model(miscmodels.SessionCompletionModel) + model = miscmodels.SessionCompletionModel() _instances[usertypes.Completion.sessions] = model