This repository has been archived by the owner on Sep 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
FileMenu.py
448 lines (370 loc) · 13.7 KB
/
FileMenu.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/usr/bin/python
# -*- coding: latin-1 -*-
# PyQtRibbon: a ribbon library for PyQt
# Copyright (C) 2014 RoadrunnerWMC
# This file is part of PyQtRibbon.
# PyQtRibbon 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.
# PyQtRibbon 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 PyQtRibbon. If not, see <http://www.gnu.org/licenses/>.
# FileMenu.py
# Contains classes related to the file menu
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
Qt = QtCore.Qt
from PyQtRibbon.common import createButton, createHorzLine
from PyQtRibbon.RecentFilesManager import QRecentFilesManager
class QFileMenu(QtWidgets.QMenu):
"""
A menu that acts like a standard ribbon File menu
"""
_arrowBtns = []
_panels = []
_recentFilesMgr = None
_recentFilesText = 'Recent files'
_shortcutAdded = QtCore.pyqtSignal()
_shortcuts = []
recentFileClicked = QtCore.pyqtSignal(str)
def __init__(self):
"""
Initialize the QFileMenu
"""
QtWidgets.QMenu.__init__(self)
minHeight = 384
# Create a button widget
btnWidget = QtWidgets.QFrame()
sp = btnWidget.sizePolicy()
sp.setVerticalPolicy(sp.Expanding)
btnWidget.setSizePolicy(sp)
btnWidget.setMinimumHeight(minHeight)
self._btnLayout = QtWidgets.QVBoxLayout()
self._btnLayout.setSpacing(0)
self._btnLayout.setContentsMargins(0, 0, 0, 0)
masterLayout = QtWidgets.QVBoxLayout()
masterLayout.setSpacing(0)
masterLayout.setContentsMargins(0, 0, 0, 0)
masterLayout.addLayout(self._btnLayout)
masterLayout.addStretch(1)
btnWidget.setLayout(masterLayout)
# Create a dynamic content widget
self._dynContentStack = QtWidgets.QStackedWidget()
self._dynContentStack.setMinimumWidth(256)
sp = self._dynContentStack.sizePolicy()
sp.setVerticalPolicy(sp.Expanding)
self._dynContentStack.setSizePolicy(sp)
self._dynContentStack.setMinimumHeight(minHeight)
# Add a recent files menu to the dyn content widget
self._populateRecentFilesPanel()
# Create a spacer widget for the bottom
btmSpacer = QtWidgets.QFrame()
btmSpacer.setMinimumHeight(12)
# Main layout
mL = QtWidgets.QGridLayout()
mL.setSpacing(0)
mL.setContentsMargins(0, 0, 0, 0)
mL.addWidget(btnWidget, 0, 0)
mL.addWidget(self._dynContentStack, 0, 1)
mL.addWidget(btmSpacer, 1, 0, 1, 2)
# Add all of that as a widget action
w = QtWidgets.QWidget()
w.setLayout(mL)
wa = QtWidgets.QWidgetAction(None)
wa.setDefaultWidget(w)
self.clear()
self.addAction(wa)
self._widgetAction = wa
# Change the button widget bg color
btnWidget.setAutoFillBackground(True)
p = btnWidget.palette()
p.setColor(p.Window, p.color(p.Base))
btnWidget.setPalette(p)
# Set the frame styles
for w in (btnWidget, self._dynContentStack):
w.setFrameStyle(QtWidgets.QFrame.Sunken | QtWidgets.QFrame.Box)
w.setLineWidth(1)
# Final setup stuff
self._setDynContentRecentFiles()
self.aboutToShow.connect(self._handleShow)
def _addBtn(self, w):
"""
Add a button to the bottom of the buttons layout
"""
self._btnLayout.addWidget(w)
def _handleArrowBtnClicked(self, checked, idx):
"""
Handle the user clicking an arrow button
"""
if not checked:
self._dynContentStack.setCurrentIndex(0)
else:
# Deselect all other buttons
for btnidx in range(len(self._arrowBtns)):
if btnidx == (idx-1): continue
self._arrowBtns[btnidx].setChecked(False)
# Update the dyn content stack
self._dynContentStack.setCurrentIndex(idx)
def _handleRecentFileAdded(self):
"""
Handle a new entry being added to self._recentFilesMgr
"""
self._populateRecentFilesPanel()
self._dynContentStack.setCurrentIndex(0)
def _handleShortcut(self, button):
"""
Handle the user typing a keyboard shortcut for a button
"""
button.click()
def _handleShortcutAddedToPanel(self):
"""
Handle a shortcut being added to a panel
"""
for p in self._panels:
for s in p._shortcuts: self._shortcuts.append(s)
self._shortcutAdded.emit()
def _handleShow(self):
"""
Handle the menu being opened
"""
self._setDynContentRecentFiles()
for btn in self._arrowBtns: btn.setChecked(False)
def _handleRecentFileClick(self, path):
"""
Handle a user clicking a recent files entry
"""
self.recentFileClicked.emit(path)
def _populateRecentFilesPanel(self):
"""
Populate the Recent Files panel from self._recentFilesMgr
"""
# Reset the panel
self._resetRecentFilesWidget()
# Iterate through each path
if self._recentFilesMgr == None: pths = []
else: pths = self._recentFilesMgr.paths()
idx = 1
for p in pths:
# Pick a title string to use
title = p.split('/')[-1]
# Create a toolbutton
btn = QtWidgets.QToolButton()
btn.setText(title)
btn.setToolTip(p)
btn.setAutoRaise(True)
btn.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
if idx <= 9: btn.setShortcut(str(idx))
sp = btn.sizePolicy()
sp.setHorizontalPolicy(sp.Expanding)
btn.setSizePolicy(sp)
btn.clicked.connect(lambda checked, patharg=p: self._handleRecentFileClick(patharg))
# Create an icon
pix = QtGui.QPixmap(16, 16)
pix.fill(QtGui.QColor.fromRgb(0,0,0,0))
ptr = QtGui.QPainter(pix)
f = btn.font()
f.setUnderline(True)
ptr.setFont(f)
if idx <= 9: ptr.drawText(0, 12, '%d'%idx)
del ptr
btn.setIcon(QtGui.QIcon(pix))
# Add the toolbutton
self._recentFilesLayout.addWidget(btn)
idx += 1
def _resetRecentFilesWidget(self):
"""
Create a new Recent Files widget
"""
# Get the old recent files widget
oldWidget = self._dynContentStack.widget(0)
# Create a new layout to add recent file toolbuttons to
RFL = QtWidgets.QVBoxLayout()
RFL.setSpacing(0)
RFL.setContentsMargins(0, 0, 0, 0)
RFL.setAlignment(Qt.AlignLeft | Qt.AlignTop)
# Set that to a widget
RFLw = QtWidgets.QWidget()
RFLw.setLayout(RFL)
sp = RFLw.sizePolicy()
sp.setVerticalPolicy(sp.Expanding)
RFLw.setSizePolicy(sp)
# Make a label
label = QtWidgets.QLabel(self._recentFilesText)
f = label.font()
f.setWeight(f.Bold)
label.setFont(f)
label.setEnabled(False)
self._recentFilesLabel = label
# Make an overall layout
L = QtWidgets.QVBoxLayout()
L.setSpacing(4)
L.setContentsMargins(4, 4, 4, 4)
L.addWidget(label)
L.addWidget(createHorzLine())
L.addWidget(RFLw)
L.setAlignment(Qt.AlignTop)
# Make a widget for that
w = QtWidgets.QWidget()
w.setLayout(L)
# Remove the old widget
if oldWidget != 0: self._dynContentStack.removeWidget(oldWidget)
# Set the new one in its place
self._dynContentStack.insertWidget(0, w)
# Assign it to self._recentFilesLayout
self._recentFilesLayout = RFL
def _setDynContentRecentFiles(self):
"""
Set the dynamic content stack to Recent Files mode
"""
self._dynContentStack.setCurrentIndex(0)
def addButton(self, icon=None, title='', handler=None, shortcut=None, statusTip=None):
"""
Add a button to the bottom
"""
btn = createButton(icon, title, handler, shortcut, statusTip)
# shortcut stuff
if shortcut != None:
sh = QtWidgets.QShortcut(shortcut, self)
sh.activated.connect(lambda arg=btn: self._handleShortcut(arg))
self._shortcuts.append(sh)
self._shortcutAdded.emit()
self._addBtn(btn)
return btn
def addArrowButton(self, panel, icon=None, title='', handler=None, shortcut=None, statusTip=None):
"""
Add a button with an arrow to the bottom
"""
btn = createButton(icon, title, handler, shortcut, statusTip)
self._panels.append(panel)
# Add an arrowbutton
arrowBtn = QtWidgets.QToolButton()
arrowBtn.setAutoRaise(True)
sp = arrowBtn.sizePolicy()
sp.setVerticalPolicy(sp.Expanding)
arrowBtn.setSizePolicy(sp)
arrowBtn.setArrowType(Qt.RightArrow)
arrowBtn.setCheckable(True)
self._arrowBtns.append(arrowBtn)
# Add it to self.arrowBtns, and set up a handler
idx = self._dynContentStack.addWidget(panel)
arrowBtn.clicked.connect(lambda checked, idxarg=idx: self._handleArrowBtnClicked(checked, idxarg))
# Final setup
L = QtWidgets.QHBoxLayout()
L.setSpacing(0)
L.setContentsMargins(0, 0, 0, 0)
L.addWidget(btn)
L.addWidget(arrowBtn)
w = QtWidgets.QWidget()
w.setLayout(L)
self._addBtn(w)
# Do stuff with the shortcut
if shortcut != None:
sh = QtWidgets.QShortcut(shortcut, self)
sh.activated.connect(lambda arg=btn: self._handleShortcut(arg))
self._shortcuts.append(sh)
self._shortcutAdded.emit()
panel._shortcutAdded.connect(self._handleShortcutAddedToPanel)
for s in panel._shortcuts: self._shortcuts.append(s)
self._shortcutAdded.emit()
return w
def addSeparator(self):
"""
Add a separator to the bottom
"""
h = createHorzLine()
h.setMinimumWidth(128)
h.setMaximumWidth(128)
self._addBtn(h)
self._btnLayout.setAlignment(h, Qt.AlignRight)
def RecentFilesManager(self):
"""
Return the QRecentFilesManager currently in use
"""
if self._recentFilesMgr == None:
self._recentFilesMgr = QRecentFilesManager()
return self._recentFilesMgr
def recentFilesText(self):
"""
Return the current 'Recent files' text
"""
return self._recentFilesText
def setRecentFilesManager(self, manager):
"""
Set a recent files manager to be used
"""
if not isinstance(manager, QRecentFilesManager): return False
self._recentFilesMgr = manager
manager.pathAdded.connect(self._handleRecentFileAdded)
self._populateRecentFilesPanel()
self._dynContentStack.setCurrentIndex(0)
def setRecentFilesText(self, text):
"""
Set the text of the 'Recent files' label
"""
text = str(text)
self._recentFilesText = text
self._recentFilesLabel.setText(text)
class QFileMenuPanel(QtWidgets.QWidget):
"""
A widget that acts like a standard right-side button list
panel in a standard ribbon File menu
"""
_shortcutAdded = QtCore.pyqtSignal()
_shortcuts = []
def __init__(self, title=''):
"""
Initialize the QFileMenuPanel
"""
QtWidgets.QWidget.__init__(self)
sp = self.sizePolicy()
sp.setVerticalPolicy(sp.Expanding)
sp.setHorizontalPolicy(sp.Expanding)
self.setSizePolicy(sp)
self._titleLabel = QtWidgets.QLabel(title)
f = self._titleLabel.font()
f.setWeight(f.Bold)
self._titleLabel.setFont(f)
self._titleLabel.setEnabled(False)
self._mainLayout = QtWidgets.QVBoxLayout()
self._mainLayout.setSpacing(0)
self._mainLayout.setContentsMargins(0, 0, 0, 0)
masterLayout = QtWidgets.QVBoxLayout()
masterLayout.setSpacing(4)
masterLayout.setContentsMargins(4, 4, 4, 4)
masterLayout.addWidget(self._titleLabel)
masterLayout.addWidget(createHorzLine())
masterLayout.addLayout(self._mainLayout)
masterLayout.addStretch(1)
self.setLayout(masterLayout)
def _handleShortcut(self, button):
"""
Handle the user typing a keyboard shortcut
"""
button.click()
def addButton(self, icon=None, title='', handler=None, shortcut=None, statusTip=None):
"""
Add a button to the bottom
"""
btn = createButton(icon, title, handler, shortcut, statusTip)
self._mainLayout.addWidget(btn)
if shortcut != None:
sh = QtWidgets.QShortcut(shortcut, self)
sh.activated.connect(lambda arg=btn: self._handleShortcut(arg))
self._shortcuts.append(sh)
self._shortcutAdded.emit()
return btn
def setTitle(self, title):
"""
Set the title
"""
title = str(title)
self._titleLabel.setText(title)
def title(self):
"""
Return the title
"""
return str(self._titleLabel.text())