-
Notifications
You must be signed in to change notification settings - Fork 0
/
QShortcutEditor.py
330 lines (260 loc) · 13.5 KB
/
QShortcutEditor.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Copyright (C) 2019-2020 Jean-Didier Garaud
# This file's most recent version can be retrieved at:
# https://github.com/Demi-Lune/snippets/blob/master/QShortcutEditor.py
"""
PySide2 widgets to display and configure an application's shortcuts.
Provides QShortcutViewer and QShortcutEditor.
For the impatient: type `python3 -m QShortcutEditor` to see them in action.
Typical usage, call it from your QMainWindow::
class MyGUI(QMainWindow):
# ...
def customizeShortcuts(self):
from QShortcutEditor import QShortcutEditor
ed = QShortcutEditor(gui=self)
ed.show()
# keep a reference, otherwise it may be garbage-collected too soon:
self._shortcut_editor = ed
Note: it also works with PyQt5, just change the from...import statement.
"""
__version__ = '1.0.0'
# See also: https://stackoverflow.com/questions/57328901/list-all-shortcuts-of-a-qmainwindow
#
# TODO:
# - improve the 2-parts modify_shortcut, it seems weird to have to do this
# - decide whether more than 2 shortcuts is necessary, if yes allow to change the 3rd or 4th; currently 3+ shortcuts are allowed, but can only change the first two
# - click on column order to sort
# - allow to remove a shortcut (right click? or little cross? del or backspace?); in qt-5.12, qkeysequenceedit.cpp:77, a TODO announces a "clear button"
# - add a save/load mechanism
# - warn if shortcuts are conflicting, when the window is closed
# - add a statusbar for this warning messages or statusTip
# - would QTableView be a better design?
# - keeping a ref on the _gui is not very OO; allowing to only pass the list of actions would be better?
# - add buttons apply/close/cancel
from PySide2 import QtCore, QtGui, QtWidgets
#from PyQt5 import QtCore, QtGui, QtWidgets
class QShortcutViewer(QtWidgets.QWidget):
def __init__(self, gui=None, parent=None):
super().__init__(parent)
self.resize(400,600)
self.columns = ['Shortcut', 'Alternate', 'Description']
self.verticalLayout = QtWidgets.QVBoxLayout(self)
self.table = QtWidgets.QTableWidget(5, 3, self)
self.verticalLayout.addWidget(self.table)
self.table.horizontalHeader().setStretchLastSection(True)
self.table.horizontalHeader().setVisible(True)
self.table.setHorizontalHeaderLabels(self.columns)
self.table.verticalHeader().setVisible(False)
self.setWindowTitle("Shortcuts")
act = QtWidgets.QAction("Close", self)
act.setShortcuts([QtGui.QKeySequence(QtCore.Qt.Key_Escape)])
act.triggered.connect(self.close)
self.addAction(act)
self._gui = gui
def _all_actions(self):
"Prepare the list of interesting actions."
self._actions = [ action for action in self._gui.findChildren(QtWidgets.QAction)
if (len(action.shortcuts()) > 0) ]
def _uniq_sort(self):
"Sort and remove duplicates in the given list of actions."
# now let's remove duplicates (e.g. when an action is in a menu and a context menu)
# unfortunately using a set doesn't work: actions = list(set(actions))
# even though their repr/str is equal (so I guess the C++ QAction is indeed the same)
# let's use the repr of the actions to do the comparison:
# uniqactions = { repr(a): a for a in actions }
# actions2 = list(uniqactions.values())
# well, it turns out that the duplicates were my mistake. Still, looking for incoherent or duplicates
# would be interesting. I keep the dict-by-comprehension trick, might turn out useful sometimes later.
# The _hash below could also be useful.
actions2 = list(set(self._actions))
# Sorting is a little tricky when we click the display_all_actions box:
# actions.sort(key=lambda act: act.shortcuts()[0].toString())
# actions.sort(key=lambda act: "" if not act.shortcuts() else act.shortcuts()[0].toString())
actions2.sort(key=lambda act: [ x.toString() for x in act.shortcuts()])
self._actions = actions2
def _prepare(self):
"List all shortcuts from the given widget, to be ready for a show()."
try: # just a little cosmetics:
self.setWindowIcon(gui.windowIcon())
except:
pass
self._all_actions()
self._uniq_sort()
#print(actions)
tab = self.table
tab.clearContents()
tab.setRowCount(len(self._actions))
for (irow, action) in enumerate(self._actions):
tip = action.toolTip()
#short = action.shortcuts()[0].toString()
## again, if display_all, we need this precaution:
short = "" if not action.shortcuts() else action.shortcuts()[0].toString()
alt = [ a.toString() for a in action.shortcuts()[1:] ]
x = QtWidgets.QTableWidgetItem()
x.setText(short)
x.setFlags(x.flags() ^ QtCore.Qt.ItemIsEditable)
tab.setItem(irow, self.columns.index('Shortcut'), x)
x = QtWidgets.QTableWidgetItem()
x.setText(' '.join(alt)) # this will display the 3rd shortcut of an action; but with current code, you'll never manage to change it
x.setFlags(x.flags() ^ QtCore.Qt.ItemIsEditable)
tab.setItem(irow, self.columns.index('Alternate'), x)
x = QtWidgets.QTableWidgetItem()
x.setText(tip)
x.setFlags(x.flags() ^ QtCore.Qt.ItemIsEditable)
x.setToolTip(action.statusTip())
tab.setItem(irow, self.columns.index('Description'), x)
def show(self):
self._prepare()
super().show()
class QShortcutEditor(QShortcutViewer):
def __init__(self, gui=None, parent=None):
super().__init__(gui, parent)
self.setWindowTitle("Customize shortcuts")
# could use QTableWidget.sortItems to allow sorting?
self.display_all_actions = QtWidgets.QCheckBox('All')
self.display_all_actions.setToolTip('Hide/display actions that do not (yet) have a shortcut')
self.verticalLayout.addWidget(self.display_all_actions)
self.display_all_actions.toggled.connect(self._prepare)
#self.table.itemDoubleClicked[QtWidgets.QTableWidgetItem].connect(self.modify_shortcut)
self.table.cellDoubleClicked[int,int].connect(self.modify_shortcut)
self.currently_modifying = None
self.button_print = QtWidgets.QPushButton('Print shortcuts')
self.button_print.setToolTip('Print shortcuts to the console (only the modified ones)') # copy-to-cliboard would be a lot smarter!
self.verticalLayout.addWidget(self.button_print)
self.button_print.clicked.connect(lambda: print(self.pickle()))
#self.button_print.clicked.connect(lambda: self.save('my_saved_shortcuts.py'))
# store the initial shortcuts, to be able to tell which were customized
actions = [ action for action in self._gui.findChildren(QtWidgets.QAction)
if (len(action.toolTip()) > 0) ]
self._initial_shortcuts = {
self._hash(a): a.shortcuts()
for a in actions }
def _all_actions(self):
"""Prepare the list of configurable actions.
Uses the checkbox whether to ignore or display actions that don't have a shortcut.
If an action neither has tooltip nor shortcut, it's probably not interesting, so we filter them out.
"""
if self.display_all_actions.isChecked():
actions = [ action for action in self._gui.findChildren(QtWidgets.QAction)
if (len(action.toolTip()) > 0) ]
else:
actions = [ action for action in self._gui.findChildren(QtWidgets.QAction)
if (len(action.shortcuts()) > 0) ]
self._actions = actions
def modify_shortcut(self, row, column):
if column == self.columns.index('Description'): return # don't want to modify the tooltip
if self.currently_modifying is not None: return # we're currently modifying another one
# this could be nicer by detecting a focusOut event in the QKeySequenceEdit, with e.g. QApplication::focusChanged
action = self._actions[row]
# Note: maybe want to play with QAbstractItemView::EditTriggers (https://doc.qt.io/qt-5/qabstractitemview.html), to allow only double-click?
# not exactly correct if we have 3 shortcuts, but it's a good start and ok for 1 or 2 shortcuts:
shorts = action.shortcuts()
# note that QKeySequenceEdit allow emacs-style shortcuts :-D
x = QtWidgets.QKeySequenceEdit("", self)
self.currently_modifying = (action, x, column - self.columns.index('Shortcut'))
self.table.setCellWidget(row, column, x)
x.editingFinished.connect(self.modify_shortcut_end)
x.show()
x.setFocus()
def modify_shortcut_end(self):
# fixme: don't really like to split the function in 2... could do better?
# currently not nice if the user double clicks elsewhere before changing the shortcut
action, x, column = self.currently_modifying
shorts = action.shortcuts()
new_short = x.keySequence()
try:
shorts[column] = new_short
except IndexError:
shorts.append(new_short)
action.setShortcuts(shorts)
x.close()
self._prepare() # to update the table
self.currently_modifying = None
def _hash(self, action):
"Return a hash of the given QAction."
# The hash should be anything reproducible between runs
# Pitfall: if someone fixes a typo in the statusTip, the hash changes...
return (action.toolTip(), action.statusTip())
def pickle(self):
"Return a pickable representation of the *modified* shortcuts."
db = {}
for a in self._actions:
h = self._hash(a)
if a.shortcuts() != self._initial_shortcuts[h]:
db[h] = [ s.toString() for s in a.shortcuts() ]
return db
def unpickle(self, actions, db):
"Modify shortcuts given the db (a dictionary, typically saved previously by `pickle`)."
for a in actions:
h = self._hash(a)
try:
a.setShortcuts(db[h])
except KeyError as e:
pass
def _prepare(self):
super()._prepare()
def save(self, filename):
fic = open(filename, 'w')
db = self.pickle()
# if db:
# print(db)
# else:
# print('Shortcuts have not been modified')
fic.write('shortcuts = %s\n'%repr(db))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
# let's build a quick widget:
gui = QtWidgets.QMainWindow()
gui.setWindowTitle('Simple test')
menuFile = gui.menuBar().addMenu("&File")
action = QtWidgets.QAction("Quit", gui)
action.setShortcuts([ QtGui.QKeySequence.Quit,
QtGui.QKeySequence(QtCore.Qt.Key_Escape),
'Ctrl+B' # as in byebye, to see how triple shortcuts work
])
action.triggered.connect(gui.close)
menuFile.addAction(action)
menu_edit = gui.menuBar().addMenu("&Tools")
action_view = QtWidgets.QAction("View shortcuts...", gui)
menu_edit.addAction(action_view)
action_view.setShortcuts(['F1'])
action_view.setStatusTip('View shortcuts')
# To work properly, the QShortcutEditor should be constructed after the gui has attached all its actions.
# ... hmmm, there's a bit of chicken and egg issue here: need ed to triggered.connect,
# but need the action to be already in the menu so I can assign it a shortcut ????
viewer = QShortcutViewer(gui)
action_view.triggered.connect(viewer.show)
action_edit = QtWidgets.QAction("Edit shortcuts...", gui)
menu_edit.addAction(action_edit)
action_edit.setShortcuts(['F2'])
action_edit.setStatusTip('View and edit shortcuts')
editor = QShortcutEditor(gui)
action_edit.triggered.connect(editor.show)
toolbar = gui.addToolBar("Main toolbar") # notice that menus and toolbar have non-shortcut-actions
toolbar.addAction(action_view)
toolbar.addAction(action_edit)
gui.setCentralWidget(QtWidgets.QLabel(' Hit F1 to display shortcuts. \n Hit F2 to view and edit them. \n Hit Esc to quit.'))
# # # this is the dockable version:
# it = QtWidgets.QDockWidget("View/Edit shortcuts", gui)
# it.setObjectName("dockShortcutEditor")
# it.setAllowedAreas(QtCore.Qt.LeftDockWidgetArea|QtCore.Qt.RightDockWidgetArea)
# ed2 = QShortcutEditor(parent=it, gui=gui)
# it.setWidget(ed2)
# gui.addDockWidget(QtCore.Qt.RightDockWidgetArea, it)
gui.show()
actions = [ action for action in gui.findChildren(QtWidgets.QAction)
if (len(action.toolTip()) > 0) ]
editor.unpickle(actions,
{('View shortcuts', 'View shortcuts'): ['F1', 'F3', 'F4']} # to see how multiple shortcuts work
)
sys.exit(app.exec_())