-
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: Add Focus To Name feature and CommandPalette
- Loading branch information
1 parent
486576b
commit 3731cf2
Showing
6 changed files
with
168 additions
and
5 deletions.
There are no files selected for viewing
Empty file.
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import six | ||
from Qt.QtCore import QPoint, Qt | ||
from Qt.QtWidgets import QFrame, QHBoxLayout, QLineEdit, QShortcut | ||
|
||
from .workbox_completer import WorkboxCompleter | ||
|
||
|
||
class CommandPalette(QFrame): | ||
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.uiCloseSCT = QShortcut( | ||
Qt.Key_Escape, self, context=Qt.WidgetWithChildrenShortcut | ||
) | ||
self.uiCloseSCT.activated.connect(self.hide) | ||
self.uiLineCOMPL = WorkboxCompleter() | ||
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.current_name = parent.name_for_workbox(parent.current_workbox()) | ||
|
||
def completed(self, name): | ||
if isinstance(name, six.string_types): | ||
self.current_name = name | ||
else: | ||
self.current_name = self.uiLineCOMPL.pathFromIndex(name) | ||
self.hide() | ||
|
||
def completer_selected(self, name): | ||
self.parent().workbox_for_name(name.rstrip("/"), visible=True) | ||
|
||
def hide(self): | ||
# Close the popup if its open | ||
self.uiLineCOMPL.popup().hide() | ||
# Restore the original tab as the user didn't choose the new tab | ||
self.completer_selected(self.current_name) | ||
super(CommandPalette, self).hide() | ||
|
||
def reposition(self): | ||
pgeo = self.parent().geometry() | ||
geo = self.geometry() | ||
center = QPoint(pgeo.width() // 2, self.y_offset) | ||
geo.moveCenter(center) | ||
self.setGeometry(geo) | ||
|
||
def popup(self): | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from Qt.QtWidgets import QCompleter | ||
|
||
|
||
class WorkboxCompleter(QCompleter): | ||
def splitPath(self, path): | ||
return path.split("/") | ||
|
||
def pathFromIndex(self, index): | ||
return self.model().pathFromIndex(index) |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from Qt.QtCore import Qt | ||
from Qt.QtGui import QStandardItem, QStandardItemModel | ||
|
||
|
||
def manager_items(manager): | ||
for group_index in range(manager.count()): | ||
group_name = manager.tabText(group_index) | ||
|
||
tab_widget = manager.widget(group_index) | ||
for tab_index in range(tab_widget.count()): | ||
tab_name = tab_widget.tabText(tab_index) | ||
yield group_name, tab_name, group_index, tab_index | ||
|
||
|
||
class WorkboxItemModel(QStandardItemModel): | ||
def __init__(self, manager, *args, **kwargs): | ||
super(WorkboxItemModel, self).__init__(*args, **kwargs) | ||
self.manager = manager | ||
|
||
def pathFromIndex(self, index): | ||
parts = [""] | ||
while index.isValid(): | ||
parts.append(self.data(index, Qt.DisplayRole)) | ||
index = index.parent() | ||
if len(parts) == 1: | ||
return "" | ||
return "/".join([x for x in parts[::-1] if x]) | ||
|
||
|
||
class WorkboxTreeItemModel(WorkboxItemModel): | ||
def process(self): | ||
root = self.invisibleRootItem() | ||
prev_group = -1 | ||
for group_name, tab_name, group_index, _ in manager_items(self.manager): | ||
if prev_group != group_index: | ||
group_item = QStandardItem(group_name) | ||
root.appendRow(group_item) | ||
prev_group = group_index | ||
|
||
tab_item = QStandardItem(tab_name) | ||
group_item.appendRow(tab_item) | ||
|
||
# for group in range(self.manager.count()): | ||
# group_name = self.manager.tabText(group) | ||
# group_item = QStandardItem(group_name) | ||
# root.appendRow(group_item) | ||
|
||
# tab_widget = self.manager.widget(group) | ||
# for index in range(tab_widget.count()): | ||
# tab_name = tab_widget.tabText(index) | ||
# tab_item = QStandardItem(tab_name) | ||
# group_item.appendRow(tab_item) | ||
|
||
|
||
class WorkboxListItemModel(WorkboxItemModel): | ||
def process(self): | ||
root = self.invisibleRootItem() | ||
for group_name, tab_name, _, _ in manager_items(self.manager): | ||
group_item = QStandardItem('/'.join((group_name, tab_name))) | ||
root.appendRow(group_item) |
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