From 968bb3d430665f51ba7c66e0cc5c41fee1fbaea0 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 3 Jun 2014 13:37:11 +0200 Subject: [PATCH] Move selection_changed to Completer --- qutebrowser/app.py | 6 ++--- qutebrowser/utils/completer.py | 41 +++++++++++++++++++++++++++++- qutebrowser/widgets/_completion.py | 33 ++---------------------- 3 files changed, 45 insertions(+), 35 deletions(-) diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 2baa997cb..4be7e963c 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -349,6 +349,7 @@ class QuteBrowser(QApplication): completion = self.mainwindow.completion tabs = self.mainwindow.tabs cmd = self.mainwindow.status.cmd + completer = self.mainwindow.completion.completer # misc self.lastWindowClosed.connect(self.shutdown) @@ -410,9 +411,8 @@ class QuteBrowser(QApplication): cmd.clear_completion_selection.connect( completion.on_clear_completion_selection) cmd.hide_completion.connect(completion.hide) - cmd.update_completion.connect( - completion.completer.on_update_completion) - completion.change_completed_part.connect(cmd.on_change_completed_part) + cmd.update_completion.connect(completer.on_update_completion) + completer.change_completed_part.connect(cmd.on_change_completed_part) def _recover_pages(self): """Try to recover all open pages. diff --git a/qutebrowser/utils/completer.py b/qutebrowser/utils/completer.py index 1135b7cbc..2a45985e2 100644 --- a/qutebrowser/utils/completer.py +++ b/qutebrowser/utils/completer.py @@ -17,7 +17,7 @@ """Completer attached to a CompletionView.""" -from PyQt5.QtCore import QObject, pyqtSlot +from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject import qutebrowser.config.config as config import qutebrowser.config.configdata as configdata @@ -41,8 +41,18 @@ class Completer(QObject): ignore_change: Whether to ignore the next completion update. _models: dict of available completion models. _lastmodel: The model set in the last iteration. + + Signals: + change_completed_part: Text which should be substituted for the word + we're currently completing. + arg 0: The text to change to. + arg 1: True if the text should be set + immediately, without continuing + completing the current field. """ + change_completed_part = pyqtSignal(str, bool) + def __init__(self, view): super().__init__(view) self.view = view @@ -129,6 +139,35 @@ class Completer(QObject): model = self._models.get(completion_name) return model + def selection_changed(self, selected, deselected): + """Emit change_completed_part if a new item was selected. + + Called from the views selectionChanged method. + + Args: + selected: New selection. + delected: Previous selection. + + Emit: + change_completed_part: Emitted when there's data for the new item. + """ + indexes = selected.indexes() + if not indexes: + return + model = self.view.model() + data = model.data(indexes[0]) + if data is None: + return + if model.item_count == 1 and config.get('completion', + 'quick-complete'): + # If we only have one item, we want to apply it immediately + # and go on to the next part. + self.change_completed_part.emit(data, True) + else: + self.ignore_change = True + self.change_completed_part.emit(data, False) + self.ignore_change = False + @pyqtSlot(str, list, int) def on_update_completion(self, prefix, parts, cursor_part): """Check if completions are available and activate them. diff --git a/qutebrowser/widgets/_completion.py b/qutebrowser/widgets/_completion.py index ef87c2aca..85d67c3de 100644 --- a/qutebrowser/widgets/_completion.py +++ b/qutebrowser/widgets/_completion.py @@ -47,19 +47,12 @@ class CompletionView(QTreeView): Attributes: completer: The Completer instance to use. - _lastmodel: The model set in the last iteration. _enabled: Whether showing the CompletionView is enabled. _height: The height to use for the CompletionView. _height_perc: Either None or a percentage if height should be relative. _delegate: The item delegate used. Signals: - change_completed_part: Text which should be substituted for the word - we're currently completing. - arg 0: The text to change to. - arg 1: True if the text should be set - immediately, without continuing - completing the current field. resize_completion: Emitted when the completion should be resized. """ @@ -95,14 +88,12 @@ class CompletionView(QTreeView): # FIXME style scrollbar - change_completed_part = pyqtSignal(str, bool) resize_completion = pyqtSignal() def __init__(self, parent=None): super().__init__(parent) self.completer = Completer(self) self._enabled = config.get('completion', 'show') - self._lastmodel = None self._delegate = CompletionItemDelegate(self) self.setItemDelegate(self._delegate) @@ -222,29 +213,9 @@ class CompletionView(QTreeView): self._next_prev_item(prev=False) def selectionChanged(self, selected, deselected): - """Extend selectionChanged to emit change_completed_part if necessary. - - Args: - selected: New selection. - delected: Previous selection. - - Emit: - change_completed_part: Emitted when there's data for the new item. - """ - indexes = selected.indexes() - if indexes: - data = self.model().data(indexes[0]) - if data is not None: - if self.model().item_count == 1 and config.get( - 'completion', 'quick-complete'): - # If we only have one item, we want to apply it immediately - # and go on to the next part. - self.change_completed_part.emit(data, True) - else: - self.completer.ignore_change = True - self.change_completed_part.emit(data, False) - self.completer.ignore_change = False + """Extend selectionChanged to call completers selection_changed.""" super().selectionChanged(selected, deselected) + self.completer.selection_changed(selected, deselected) def resizeEvent(self, e): """Extend resizeEvent to adjust column size."""