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
/
Copy pathRibbon.py
607 lines (491 loc) · 17.8 KB
/
Ribbon.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#!/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/>.
# Ribbon.py
# Contains classes related to the ribbon itself
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
Qt = QtCore.Qt
from PyQtRibbon.common import createVertLine
class QRibbon(QtWidgets.QWidget):
"""
A widget that acts like a standard ribbon
"""
_helpMenu = None
_helpIcon = None
_tabs = [None]
_tabBarIdx = 1
_tabChanging = False
_tabHidden = False
def __init__(self):
"""
Initialize the QRibbon
"""
QtWidgets.QWidget.__init__(self)
self.setFocusPolicy(Qt.ClickFocus)
# Create a tab bar
self._tabBar = QtWidgets.QTabBar()
self._tabBar.setExpanding(False)
self._tabBar.currentChanged.connect(self._currentTabChanged)
self._tabBar.mousePressEvent = self._handleTabBarClick
# Create a widget stack
self._stack = QtWidgets.QStackedWidget(self)
# Create a file menu
self._QFileMenu = QtWidgets.QMenu()
# Create a minimize button
self._minBtn = QtWidgets.QToolButton()
self._minBtn.setAutoRaise(True)
self._minBtn.setArrowType(Qt.UpArrow)
self._minBtn.clicked.connect(self._handleMinBtnClick)
# Create a menu for when the QRibbon is mimmimized
self._minMenu = QtWidgets.QMenu()
# Create a help button
self._helpBtn = QtWidgets.QToolButton()
self._helpBtn.setAutoRaise(True)
self._helpBtn.setVisible(False)
self._helpBtn.setPopupMode(self._helpBtn.InstantPopup)
# Set up a tab bar widget with buttons on the right edge
tbLayout = QtWidgets.QGridLayout()
tbLayout.setContentsMargins(0, 0, 0, 0)
tbLayout.setSpacing(0)
tbLayout.addWidget(self._tabBar, 0, 0, 1, 2)
tbLayout.addWidget(self._minBtn, 0, 0)
tbLayout.addWidget(self._helpBtn, 0, 1)
tbLayout.setAlignment(self._minBtn, Qt.AlignRight)
tbLayout.setAlignment(self._helpBtn, Qt.AlignRight)
tbLayout.setColumnStretch(0, 1)
tbWidget = QtWidgets.QWidget()
tbWidget.setLayout(tbLayout)
tbWidget.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
self._tabBar.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
self._tabLayerWidget = tbWidget
# Set up a layout
self._mainLayout = QtWidgets.QGridLayout()
self._mainLayout.addWidget(tbWidget, 0, 0)
self._mainLayout.addWidget(self._stack, 1, 0)
self._mainLayout.setRowStretch(1, 1)
self._mainLayout.setSpacing(0)
self._mainLayout.setContentsMargins(0,0,0,0)
self.setLayout(self._mainLayout)
# Add a File tab
self._tabChanging = True
self._tabBar.addTab('File')
self._tabChanging = False
# Change the tab bar colors
self._tabBar.setTabTextColor(0, Qt.blue)
self._tabBar.setAutoFillBackground(True)
p = self._tabBar.palette()
p.setColor(p.Window, p.color(p.Base)) # background
p.setColor(p.Dark, p.color(p.Mid)) # horz btm line
self._tabBar.setPalette(p)
def _currentTabChanged(self, idx):
"""
Handle the user changing the current tab
"""
if self._tabChanging: return
if idx == 0:
# This doesn't return until the menu is closed
self._QFileMenu.exec_(self._tabBar.mapToGlobal(QtCore.QPoint(0, self._tabBar.height())))
# Now set the tab bar to the previous tab
self._tabChanging = True
self._tabBar.setCurrentIndex(self._tabBarIdx)
self._tabChanging = False
else:
self._stack.setCurrentIndex(idx - 1)
self._tabBarIdx = idx
def _handleMinBtnClick(self):
"""
Handle the user clicking the minimize/pin button
"""
if self._tabHidden:
# The user clicked the button in its "maximize QRibbon" state
self._tabHidden = False
self._minBtn.setArrowType(Qt.UpArrow)
# Change the layout stuff
self._minMenuLayout.removeWidget(self._stack)
self._mainLayout.addWidget(self._stack, 1, 0)
else:
# The user clicked the button in its "minimize QRibbon" state
self._tabHidden = True
self._minBtn.setArrowType(Qt.DownArrow)
self._tabBar.setCurrentIndex(-1)
# Change the layout stuff
self._mainLayout.removeWidget(self._stack)
self._minMenuLayout = QtWidgets.QHBoxLayout()
self._minMenuLayout.setSpacing(0)
self._minMenuLayout.setContentsMargins(0,0,0,0)
self._minMenuLayout.addWidget(self._stack)
w = QtWidgets.QWidget()
w.setLayout(self._minMenuLayout)
wa = QtWidgets.QWidgetAction(None)
wa.setDefaultWidget(w)
self._minMenu.clear()
self._minMenu.addAction(wa)
self._widgetAction = wa # prevents bugs
def _handleTabBarClick(self, event):
"""
Handle the user clicking the tab bar
"""
if self._tabChanging: return
QtWidgets.QTabBar.mousePressEvent(self._tabBar, event)
if self._tabHidden: self._showTabMenu()
def _handleShortcutAdded(self):
"""
Handle a shortcut being added to a QRibbonSection
"""
for tab in self._tabs:
if tab == None: continue
for sh in tab._shortcuts():
sh.setParent(self)
if self._QFileMenu != None:
for sh in self._QFileMenu._shortcuts:
sh.setParent(self)
def _showTabMenu(self):
"""
Display the menu containing the tab content
If the ribbon is not minimized, does nothing
"""
if not self._tabHidden: return
self._minMenu.setMinimumWidth(self._tabBar.width())
self._minMenu.exec_(self._tabBar.mapToGlobal(QtCore.QPoint(0, self._tabBar.height())))
def _tabTitleChanged(self, tab, newTitle):
"""
Handle the title of a tab being changed
"""
try: idx = self.tabs.index(tab)
except: return
self.tabBar.setTabText(idx, newTitle)
def addTab(self, title):
"""
Add a tab to the end of the ribbon
"""
tab = QRibbonTab(title)
self._tabs.append(tab)
self._tabBar.addTab(title)
tab._titleChanged.connect(self._tabTitleChanged)
tab._shortcutAdded.connect(self._handleShortcutAdded)
self._handleShortcutAdded()
scrl = QtWidgets.QScrollArea()
scrl.setFrameStyle(QtWidgets.QFrame.NoFrame)
scrl.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
scrl.setWidget(tab)
scrl.setWidgetResizable(True)
self._stack.addWidget(scrl)
if len(self._tabs) == 2:
self._tabChanging = True
self._tabBar.setCurrentIndex(1)
self._tabChanging = False
self._stack.setCurrentIndex(0)
return tab
def helpIcon(self):
"""
Return the help button icon
"""
return self._helpIcon
def helpMenu(self):
"""
Return the help menu
"""
return self._helpMenu
def fileMenu(self):
"""
Return the file menu
"""
return self._QFileMenu
def fileTitle(self):
"""
Return the title of the File tab
"""
return str(self._tabBar.tabText(0))
def setFileMenu(self, menu):
"""
Sets the file menu
"""
self._QFileMenu = menu
self._QFileMenu._shortcutAdded.connect(self._handleShortcutAdded)
self._handleShortcutAdded()
def setFileTitle(self, title):
"""
Set the title of the File tab
"""
self._tabBar.setTabText(0, title)
def setHelpIcon(self, icon):
"""
Set the help button icon
"""
self._helpIcon = icon
self._helpBtn.setIcon(icon)
def setHelpMenu(self, menu):
"""
Set the help menu
"""
self._helpMenu = menu
self._helpBtn.setVisible(True)
self._helpBtn.setMenu(menu)
# Attempt to remove the down arrow
#option = QtWidgets.QStyleOptionToolButton()
#self._helpBtn.initStyleOption(option)
#option.features |= QtWidgets.QStyleOptionToolButton.MenuButtonPopup
#self._helpBtn.seyStyleOption(option)
def stylizeOnWindows(self, window):
"""
On Windows, extends the window frame behind the tab bar.
On other platforms, does nothing.
Requires the window this ribbon is located in.
"""
return # glitches the window out, currently
if sys.platform != 'win32': return
try:
#return
from PyQt5 import QtWinExtras
QtWin = QtWinExtras.QtWin
window.setAttribute(Qt.WA_TranslucentBackground, True)
QtWin.extendFrameIntoClientArea(window, 0, 24, 0, 0)
self._tabBar.setAutoFillBackground(False)
self._tabLayerWidget.setAutoFillBackground(False)
self.setAutoFillBackground(False)
except: pass # fail silently
class QRibbonTab(QtWidgets.QWidget):
"""
A widget that acts like a standard ribbon tab content area
"""
_orientation = Qt.Horizontal
_sections = []
_shortcutAdded = QtCore.pyqtSignal()
_title = ''
_titleChanged = QtCore.pyqtSignal()
def __init__(self, title):
"""
Initialize the QRibbonTab
"""
QtWidgets.QWidget.__init__(self)
self._title = title
self._mainLayout = QtWidgets.QHBoxLayout()
self._mainLayout.setContentsMargins(4, 4, 4, 4)
self.setLayout(self._mainLayout)
def _handleShortcutAdded(self):
"""
Alerts the QRibbon associated with this QRibbonTab that a
shortcut has been added
"""
self._shortcutAdded.emit()
def _regenerateLayout(self):
"""
Regenerates the layout
"""
pass
def _shortcuts(self):
"""
Return all shortcuts of the current QRibbonTabSections
"""
shortcuts = []
for sect in self._sections:
for sh in sect._shortcuts: shortcuts.append(sh)
return shortcuts
def addSection(self, title):
"""
Add a QRibbonSection to the end
"""
section = QRibbonSection(title)
self._sections.append(section)
section._shortcutAdded.connect(self._handleShortcutAdded)
self._handleShortcutAdded()
vline = createVertLine()
vline.setEnabled(False)
c = self._mainLayout.count()
if c != 0: self._mainLayout.takeAt(c-1)
self._mainLayout.addWidget(section)
self._mainLayout.addWidget(vline)
self._mainLayout.addStretch(1)
return section
def orientation(self):
"""
Return the orientation that will be used for
arranging small buttons
"""
return self._orientation
def setOrientation(self, orientation):
"""
Set the orientation that will be used for
arranging small buttons
"""
self._orientation = orientation
self._regenerateLayout()
def setTitle(self, title):
"""
Set the title
"""
self._title = title
self._titleChanged.emit(self, title)
def title(self):
"""
Return the title
"""
return self._title
class QRibbonSection(QtWidgets.QWidget):
"""
A widget that acts like a ribbon tab shortcut container
"""
_widgetRow = 0
_widgetCol = 0
_shortcutAdded = QtCore.pyqtSignal()
_shortcuts = []
def __init__(self, title):
"""
Initialize the QRibbonSection
"""
QtWidgets.QWidget.__init__(self)
self._titleLabel = QtWidgets.QLabel(title)
self._titleLabel.setEnabled(False) # grays it out
# self._mainLayout is inside masterLayout
# so that changes to self._mainLayout won't
# affect self._titleLabel
self._mainLayout = QtWidgets.QGridLayout()
self._mainLayout.setContentsMargins(0, 0, 0, 0)
self._mainLayout.setSpacing(0)
masterLayout = QtWidgets.QVBoxLayout()
masterLayout.setContentsMargins(0, 0, 0, 0)
masterLayout.setSpacing(0)
masterLayout.addLayout(self._mainLayout)
masterLayout.addWidget(self._titleLabel, 0, Qt.AlignHCenter | Qt.AlignBottom)
self.setLayout(masterLayout)
def _addWidget(self, widget, full=True):
"""
Add a widget to the end
"""
# "full" is a bool specifying if this widget
# should span all 3 layout rows or not.
rowspan = 3 if full else 1
if full and (self._widgetRow in [1, 2]):
self._widgetRow = 0
self._widgetCol += 1
self._mainLayout.addWidget(widget, self._widgetRow, self._widgetCol, rowspan, 1)
if full:
self._widgetRow = 0
self._widgetCol += 1
else:
self._widgetRow += 1
if self._widgetRow == 3:
self._widgetRow = 0
self._widgetCol += 1
def _handleShortcut(self, button):
"""
Handles the user clicking a shortcut
"""
button.click()
def addCustomWidget(self, widget, full=True):
"""
Add a custom widget to the end
"""
self._addWidget(self, widget, full)
def addButton(self, full=False, icon=None, title=None, handler=None, shortcut=None, statusTip=None):
"""
Add a button to the end
"""
btn = QtWidgets.QToolButton()
btn.setAutoRaise(True)
# full
s = 32 if full else 16
btn.setIconSize(QtCore.QSize(s, s))
if full:
sp = btn.sizePolicy()
sp.setVerticalPolicy(sp.Expanding)
btn.setSizePolicy(sp)
# icon
if icon != None:
icon = QtGui.QIcon(icon)
btn.setIcon(icon)
# title
if title != None:
btn.setText(title)
if full: btn.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
else: btn.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
# handler
if handler != None:
btn.clicked.connect(handler)
# 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()
# statusTip
if statusTip != None:
btn.setStatusTip(statusTip)
self._addWidget(btn, full)
return btn
def addSmallButton(self, icon=None, title='', handler=None, shortcut=None, statusTip=None):
"""
Add a small button to the end
"""
return self.addButton(False, icon, title, handler, shortcut, statusTip)
def addFullButton(self, icon=None, title='', handler=None, shortcut=None, statusTip=None):
"""
Add a full-size button to the end
"""
return self.addButton(True, icon, title, handler, shortcut, statusTip)
def addToggleButton(self, full=False, icon=None, title=None, handler=None, shortcut=None, statusTip=None):
"""
Add a togglebutton to the end
"""
btn = QtWidgets.QToolButton()
btn.setAutoRaise(True)
btn.setCheckable(True)
# full
s = 32 if full else 16
btn.setIconSize(QtCore.QSize(s, s))
# icon
if icon != None:
icon = QtGui.QIcon(icon)
btn.setIcon(icon)
# title
if title != None:
btn.setText(title)
if full: btn.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
else: btn.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
# handler
if handler != None:
btn.clicked.connect(handler)
# 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()
# statusTip
if statusTip != None:
btn.setStatusTip(statusTip)
self._addWidget(btn, full)
return btn
def addSmallToggleButton(self, icon=None, title='', handler=None, shortcut=None, statusTip=None):
"""
Add a small togglebutton to the end
"""
return self.addToggleButton(False, icon, title, handler, shortcut, statusTip)
def addFullToggleButton(self, icon=None, title='', handler=None, shortcut=None, statusTip=None):
"""
Add a full-size togglebutton to the end
"""
return self.addToggleButton(True, icon, title, handler, shortcut, statusTip)
def setTitle(self, title):
"""
Set the text of the title label
"""
self._titleLabel.setText(title)
def title(self):
"""
Return the text of the title label
"""
return str(self._titleLabel.text())