Move selection_changed to Completer

This commit is contained in:
Florian Bruhin 2014-06-03 13:37:11 +02:00
parent 7d440426eb
commit 968bb3d430
3 changed files with 45 additions and 35 deletions

View File

@ -349,6 +349,7 @@ class QuteBrowser(QApplication):
completion = self.mainwindow.completion completion = self.mainwindow.completion
tabs = self.mainwindow.tabs tabs = self.mainwindow.tabs
cmd = self.mainwindow.status.cmd cmd = self.mainwindow.status.cmd
completer = self.mainwindow.completion.completer
# misc # misc
self.lastWindowClosed.connect(self.shutdown) self.lastWindowClosed.connect(self.shutdown)
@ -410,9 +411,8 @@ class QuteBrowser(QApplication):
cmd.clear_completion_selection.connect( cmd.clear_completion_selection.connect(
completion.on_clear_completion_selection) completion.on_clear_completion_selection)
cmd.hide_completion.connect(completion.hide) cmd.hide_completion.connect(completion.hide)
cmd.update_completion.connect( cmd.update_completion.connect(completer.on_update_completion)
completion.completer.on_update_completion) completer.change_completed_part.connect(cmd.on_change_completed_part)
completion.change_completed_part.connect(cmd.on_change_completed_part)
def _recover_pages(self): def _recover_pages(self):
"""Try to recover all open pages. """Try to recover all open pages.

View File

@ -17,7 +17,7 @@
"""Completer attached to a CompletionView.""" """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.config as config
import qutebrowser.config.configdata as configdata import qutebrowser.config.configdata as configdata
@ -41,8 +41,18 @@ class Completer(QObject):
ignore_change: Whether to ignore the next completion update. ignore_change: Whether to ignore the next completion update.
_models: dict of available completion models. _models: dict of available completion models.
_lastmodel: The model set in the last iteration. _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): def __init__(self, view):
super().__init__(view) super().__init__(view)
self.view = view self.view = view
@ -129,6 +139,35 @@ class Completer(QObject):
model = self._models.get(completion_name) model = self._models.get(completion_name)
return model 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) @pyqtSlot(str, list, int)
def on_update_completion(self, prefix, parts, cursor_part): def on_update_completion(self, prefix, parts, cursor_part):
"""Check if completions are available and activate them. """Check if completions are available and activate them.

View File

@ -47,19 +47,12 @@ class CompletionView(QTreeView):
Attributes: Attributes:
completer: The Completer instance to use. completer: The Completer instance to use.
_lastmodel: The model set in the last iteration.
_enabled: Whether showing the CompletionView is enabled. _enabled: Whether showing the CompletionView is enabled.
_height: The height to use for the CompletionView. _height: The height to use for the CompletionView.
_height_perc: Either None or a percentage if height should be relative. _height_perc: Either None or a percentage if height should be relative.
_delegate: The item delegate used. _delegate: The item delegate used.
Signals: 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. resize_completion: Emitted when the completion should be resized.
""" """
@ -95,14 +88,12 @@ class CompletionView(QTreeView):
# FIXME style scrollbar # FIXME style scrollbar
change_completed_part = pyqtSignal(str, bool)
resize_completion = pyqtSignal() resize_completion = pyqtSignal()
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent) super().__init__(parent)
self.completer = Completer(self) self.completer = Completer(self)
self._enabled = config.get('completion', 'show') self._enabled = config.get('completion', 'show')
self._lastmodel = None
self._delegate = CompletionItemDelegate(self) self._delegate = CompletionItemDelegate(self)
self.setItemDelegate(self._delegate) self.setItemDelegate(self._delegate)
@ -222,29 +213,9 @@ class CompletionView(QTreeView):
self._next_prev_item(prev=False) self._next_prev_item(prev=False)
def selectionChanged(self, selected, deselected): def selectionChanged(self, selected, deselected):
"""Extend selectionChanged to emit change_completed_part if necessary. """Extend selectionChanged to call completers selection_changed."""
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
super().selectionChanged(selected, deselected) super().selectionChanged(selected, deselected)
self.completer.selection_changed(selected, deselected)
def resizeEvent(self, e): def resizeEvent(self, e):
"""Extend resizeEvent to adjust column size.""" """Extend resizeEvent to adjust column size."""