-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Don't use a QCompleter for CommandPalette
- Loading branch information
1 parent
7439110
commit 609d9b7
Showing
4 changed files
with
108 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,93 @@ | ||
from __future__ import absolute_import | ||
|
||
import six | ||
from Qt.QtCore import QPoint, Qt | ||
from Qt.QtWidgets import QFrame, QHBoxLayout, QLineEdit, QShortcut | ||
from functools import partial | ||
|
||
from .workbox_completer import WorkboxCompleter | ||
from Qt.QtCore import QModelIndex, QPoint, Qt, Signal | ||
from Qt.QtWidgets import QFrame, QLineEdit, QListView, QShortcut, QVBoxLayout | ||
|
||
from .workbox_item_model import WorkboxFuzzyFilterProxyModel | ||
|
||
|
||
class CommandPalette(QFrame): | ||
canceled = Signal("QModelIndex") | ||
"""Passes the original QModelIndex for the tab that was selected when the | ||
widget was first shown. This lets you reset back to the orignal state.""" | ||
highlighted = Signal("QModelIndex") | ||
"""Emitted when the user navitages to the given index, but hasn't selected.""" | ||
selected = Signal("QModelIndex") | ||
"""Emitted when the user selects a item.""" | ||
|
||
def __init__(self, model, parent=None, **kwargs): | ||
super(CommandPalette, self).__init__(parent=parent, **kwargs) | ||
self.y_offset = 100 | ||
lyt = QHBoxLayout(self) | ||
self.uiLineEDIT = QLineEdit(parent=self) | ||
lyt.addWidget(self.uiLineEDIT) | ||
self.setMinimumSize(400, self.sizeHint().height()) | ||
self.setMinimumSize(400, 200) | ||
self.uiCloseSCT = QShortcut( | ||
Qt.Key_Escape, self, context=Qt.WidgetWithChildrenShortcut | ||
) | ||
self.uiCloseSCT.activated.connect(self.hide) | ||
self.uiLineCOMPL = WorkboxCompleter() | ||
self.uiLineCOMPL.split_char = None | ||
self.uiLineCOMPL.setCaseSensitivity(False) | ||
self.uiLineCOMPL.setModel(model) | ||
self.uiLineEDIT.setCompleter(self.uiLineCOMPL) | ||
self.uiLineCOMPL.activated.connect(self.completed) | ||
self.uiLineCOMPL.highlighted.connect(self.completer_selected) | ||
# self.uiLineCOMPL.popup().clicked.connect(self.completed) | ||
self.uiCloseSCT.activated.connect(self._canceled) | ||
|
||
self.uiUpSCT = QShortcut(Qt.Key_Up, self, context=Qt.WidgetWithChildrenShortcut) | ||
self.uiUpSCT.activated.connect(partial(self.increment_selection, -1)) | ||
self.uiDownSCT = QShortcut( | ||
Qt.Key_Down, self, context=Qt.WidgetWithChildrenShortcut | ||
) | ||
self.uiDownSCT.activated.connect(partial(self.increment_selection, 1)) | ||
|
||
lyt = QVBoxLayout(self) | ||
self.uiLineEDIT = QLineEdit(parent=self) | ||
self.uiLineEDIT.textChanged.connect(self.update_completer) | ||
self.current_name = parent.name_for_workbox(parent.current_workbox()) | ||
self.uiLineEDIT.returnPressed.connect(self.activated) | ||
lyt.addWidget(self.uiLineEDIT) | ||
self.uiResultsLIST = QListView(self) | ||
self.uiResultsLIST.activated.connect(self.activated) | ||
self.proxy_model = WorkboxFuzzyFilterProxyModel(self) | ||
self.proxy_model.setSourceModel(model) | ||
self.uiResultsLIST.setModel(self.proxy_model) | ||
lyt.addWidget(self.uiResultsLIST) | ||
|
||
def update_completer(self, wildcard): | ||
self.uiLineCOMPL.updatePattern(wildcard) | ||
self.original_model_index = model.original_model_index | ||
|
||
def completed(self, name): | ||
if isinstance(name, six.string_types): | ||
self.current_name = name | ||
else: | ||
self.current_name = self.uiLineCOMPL.pathFromIndex(name) | ||
def activated(self): | ||
current = self.uiResultsLIST.currentIndex() | ||
self.selected.emit(current) | ||
self.hide() | ||
|
||
def completer_selected(self, name): | ||
self.parent().workbox_for_name(name.rstrip("/"), visible=True) | ||
def increment_selection(self, direction): | ||
current = self.uiResultsLIST.currentIndex() | ||
col = 0 | ||
row = 0 | ||
if current.isValid(): | ||
col = current.column() | ||
row = current.row() + direction | ||
new = self.uiResultsLIST.model().index(row, col) | ||
self.uiResultsLIST.setCurrentIndex(new) | ||
self.highlighted.emit(new) | ||
|
||
def update_completer(self, wildcard): | ||
if wildcard: | ||
if not self.uiResultsLIST.currentIndex().isValid(): | ||
new = self.uiResultsLIST.model().index(0, 0) | ||
self.uiResultsLIST.setCurrentIndex(new) | ||
else: | ||
self.uiResultsLIST.clearSelection() | ||
self.uiResultsLIST.setCurrentIndex(QModelIndex()) | ||
self.proxy_model.setFuzzySearch(wildcard) | ||
self.highlighted.emit(self.uiResultsLIST.currentIndex()) | ||
|
||
def hide(self): | ||
# Close the popup if its open | ||
self.uiLineCOMPL.popup().hide() | ||
def _canceled(self): | ||
# Restore the original tab as the user didn't choose the new tab | ||
self.completer_selected(self.current_name) | ||
super(CommandPalette, self).hide() | ||
self.canceled.emit(self.original_model_index) | ||
self.hide() | ||
|
||
def reposition(self): | ||
pgeo = self.parent().geometry() | ||
geo = self.geometry() | ||
center = QPoint(pgeo.width() // 2, self.y_offset) | ||
center = QPoint(pgeo.width() // 2, 0) | ||
geo.moveCenter(center) | ||
geo.setY(self.y_offset) | ||
self.setGeometry(geo) | ||
|
||
def popup(self): | ||
self.show() | ||
self.reposition() | ||
self.uiLineEDIT.setFocus(Qt.PopupFocusReason) | ||
self.show() | ||
self.uiLineCOMPL.complete() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters