-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommandPalette.FCMacro
188 lines (146 loc) · 5.67 KB
/
CommandPalette.FCMacro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# TODO: show disabled actions at the bottom of search
# TODO: better fuzzy search?
# TODO: better visuals:
# see: https://stackoverflow.com/questions/41107202/pyqt-coloring-part-of-text-in-qlistwidget
# - better path: right-aligned + grey
# - show the keyboard shortcut for the item?
# - highlight matched parts of search
# - larger text
# TODO: toggle visibility on keyboard shortcut if already displayed
# Useful methods on QAction menu items
# text(): The text of the menu item with '&' before the shortcut key.
# toolTip(): on hover tooltip as html.
# shortcut(): shortcut keys for that action as a QKeySequence.
# trigger(): invoke that action.
from PySide import QtGui, QtCore
def enumerateActions(menu, path=[]):
actionList = []
for action in menu.actions():
if action.isSeparator():
pass
elif action.menu():
actionList.extend(enumerateActions(action.menu(), path + [action.text()]))
else:
actionList.append((action, path))
return actionList
def getMenuItems():
def convertAction(action, path):
# For an unknown reason, there are a bunch of blank menu items -- filter them out.
if not action.text():
return None
# For now, don't include disabled actions.
if not action.isEnabled():
return None
pathText = "->".join(path)
fullText = f"{action.text()} ({pathText})"
# fixedText = action.text().replace('&', '')
fixedText = fullText.replace('&', '')
item = PaletteListWidgetItem()
item.setText(fixedText)
item.setSearchText(fixedText.casefold())
item.setToolTip(action.toolTip())
item.setIcon(action.icon())
item.setTrigger(action.trigger)
return item
mw = Gui.getMainWindow()
menuBar = mw.menuBar()
return [convertAction(action, path) for action, path in enumerateActions(menuBar) if action is not None]
class PaletteDialog(QtGui.QDialog):
def __init__(self, parent):
super().__init__(parent)
self.installEventFilter(self)
def eventFilter(self, obj, event):
# Hide the palette if it ever loses focus.
if event.type() == QtCore.QEvent.WindowDeactivate:
self.hide()
return True
return False
class PaletteLineEdit(QtGui.QLineEdit):
keyMappings = {}
def keyPressEvent(self, event):
keyFn = self.keyMappings.get(event.key())
if keyFn is not None:
keyFn()
return
super().keyPressEvent(event)
def setMappings(self, keyMappings):
self.keyMappings = keyMappings
class PaletteListWidgetItem(QtGui.QListWidgetItem):
_searchText = None
_trigger = None
def searchText(self):
return self._searchText
def setSearchText(self, text):
self._searchText = text
def matches(self, text):
return False
def runTrigger(self):
self._trigger()
def setTrigger(self, trigger):
self._trigger = trigger
class CommandPalette:
MIN_DIALOG_WIDTH = 600
MIN_DIALOG_HEIGHT = 400
def activate(self):
items = getMenuItems()
dialog = PaletteDialog(Gui.getMainWindow())
dialog.setObjectName("CommandPalette")
dialog.setMinimumWidth(self.MIN_DIALOG_WIDTH)
dialog.setMinimumHeight(self.MIN_DIALOG_HEIGHT)
dialog.setWindowFlags(dialog.windowFlags() | QtCore.Qt.FramelessWindowHint)
vbox = QtGui.QVBoxLayout(dialog)
searchBar = PaletteLineEdit()
searchBar.textChanged.connect(self.textChanged)
searchBar.returnPressed.connect(self.returnPressed)
searchBar.setMappings({
QtCore.Qt.Key.Key_Down: self.downPressed,
QtCore.Qt.Key.Key_Up: self.upPressed,
})
vbox.addWidget(searchBar)
commandList = QtGui.QListWidget()
vbox.addWidget(commandList)
for item in items:
commandList.addItem(item)
commandList.itemClicked.connect(self.itemClicked)
commandList.show()
dialog.show()
self.commandList = commandList
self.dialog = dialog # prevent dialog from being garbage collected
def textChanged(self, newText):
foldedText = newText.casefold()
foldedTerms = foldedText.split()
currentItemSet = False
for i in range(0, self.commandList.count()):
item = self.commandList.item(i)
if all(term in item.searchText() for term in foldedTerms):
item.setHidden(False)
if not currentItemSet:
self.commandList.setCurrentItem(item)
currentItemSet = True
else:
item.setHidden(True)
def returnPressed(self):
# Run the selected action.
currentItem = self.commandList.currentItem()
if currentItem is not None:
self.dialog.hide()
currentItem.runTrigger()
def downPressed(self):
# Select the next unhidden action, if any.
currentRow = self.commandList.currentRow()
for i in range(currentRow + 1, self.commandList.count()):
if not self.commandList.item(i).isHidden():
self.commandList.setCurrentRow(i)
return
def upPressed(self):
# Select the previous unhidden action, if any.
currentRow = self.commandList.currentRow()
for i in range(currentRow - 1, -1, -1):
if not self.commandList.item(i).isHidden():
self.commandList.setCurrentRow(i)
return
def itemClicked(self, item):
self.dialog.hide()
item.runTrigger()
palette = CommandPalette()
palette.activate()