-
Notifications
You must be signed in to change notification settings - Fork 10
/
titlewidget.py
302 lines (234 loc) · 11.1 KB
/
titlewidget.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
import typing
from qtpy import QtCore, QtGui, QtWidgets
from .menu import RibbonMenu
from .tabbar import RibbonTabBar
from .utils import DataFile
class RibbonApplicationButton(QtWidgets.QToolButton):
"""Application button in the ribbon bar."""
def addFileMenu(self) -> RibbonMenu:
"""Add a new ribbon menu to the application button.
:return: The new ribbon menu.
"""
menu = RibbonMenu(self)
self.setPopupMode(QtWidgets.QToolButton.InstantPopup)
self.setMenu(menu)
return menu
class RibbonTitleLabel(QtWidgets.QLabel):
"""Title label in the ribbon bar."""
pass
class RibbonTitleWidget(QtWidgets.QFrame):
"""The title widget of the ribbon."""
#: Signal, the help button was clicked.
helpButtonClicked = QtCore.Signal(bool)
#: Signal, the collapse button wa clicked.
collapseRibbonButtonClicked = QtCore.Signal(bool)
#: Buttons
_quickAccessButtons = []
_rightToolButtons = []
_quickAccessButtonHeight = 30
_rightButtonHeight = 24
# Mouse move events
_start_point = None
_window_point = None
@typing.overload
def __init__(self, title="PyQtRibbon", parent=None):
pass
@typing.overload
def __init__(self, parent=None):
pass
def __init__(self, *args, **kwargs):
"""Initialize the ribbon title widget.
:param title: The title of the ribbon.
:param parent: The parent widget.
"""
if (args and not isinstance(args[0], QtWidgets.QWidget)) or ("title" in kwargs):
title = args[0] if len(args) > 0 else kwargs.get("title", "PyQtRibbon")
parent = args[1] if len(args) > 1 else kwargs.get("parent", None)
else:
title = "PyQtRibbon"
parent = args[0] if len(args) > 0 else kwargs.get("parent", None)
super().__init__(parent)
# Tab bar layout
self.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred) # type: ignore
self._tabBarLayout = QtWidgets.QHBoxLayout(self)
self._tabBarLayout.setContentsMargins(0, 0, 0, 0)
self._tabBarLayout.setSpacing(0)
# Application
self._applicationButton = RibbonApplicationButton() # type: ignore
self._applicationButton.setIcon(QtGui.QIcon(DataFile("icons/python.png")))
self._applicationButton.setIconSize(QtCore.QSize(self._quickAccessButtonHeight, self._quickAccessButtonHeight))
self._applicationButton.setText("PyQtRibbon")
self._applicationButton.setToolTip("PyQtRibbon")
self._quickAccessToolBar = QtWidgets.QToolBar()
self._quickAccessToolBar.setIconSize(QtCore.QSize(self._quickAccessButtonHeight, self._quickAccessButtonHeight))
self._quickAccessToolBar.setOrientation(QtCore.Qt.Horizontal)
self._quickAccessToolBar.setMovable(False)
self._quickAccessToolBar.addWidget(self._applicationButton)
self._quickAccessToolBarWidget = QtWidgets.QWidget()
self._quickAccessToolBarLayout = QtWidgets.QHBoxLayout(self._quickAccessToolBarWidget)
self._quickAccessToolBarLayout.setContentsMargins(0, 0, 0, 0)
self._quickAccessToolBarLayout.setSpacing(0)
self._quickAccessToolBarLayout.addWidget(self._quickAccessToolBar, 0, QtCore.Qt.AlignBottom)
# right toolbar
self._rightToolBar = QtWidgets.QToolBar()
self._rightToolBar.setOrientation(QtCore.Qt.Horizontal)
self._rightToolBar.setIconSize(QtCore.QSize(self._rightButtonHeight, self._rightButtonHeight))
self._collapseRibbonButton = QtWidgets.QToolButton(self)
self._collapseRibbonButton.setIconSize(QtCore.QSize(self._rightButtonHeight, self._rightButtonHeight))
self._collapseRibbonButton.setIcon(QtGui.QIcon(DataFile("icons/up.png")))
self._collapseRibbonButton.setAutoRaise(True)
self._collapseRibbonButton.setToolTip("Collapse Ribbon")
self._collapseRibbonButton.clicked.connect(self.collapseRibbonButtonClicked) # type: ignore
self._helpButton = QtWidgets.QToolButton(self)
self._helpButton.setIconSize(QtCore.QSize(self._rightButtonHeight, self._rightButtonHeight))
self._helpButton.setIcon(QtGui.QIcon(DataFile("icons/help.png")))
self._helpButton.setAutoRaise(True)
self._helpButton.setToolTip("Help")
self._helpButton.clicked.connect(self.helpButtonClicked) # type: ignore
self.addRightToolButton(self._collapseRibbonButton)
self.addRightToolButton(self._helpButton)
# category tab bar
self._tabBar = RibbonTabBar(self)
self._tabBar.setExpanding(False)
self._tabBar.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred) # type: ignore
font = self._tabBar.font()
font.setPointSize(font.pointSize() + 3)
self._tabBar.setFont(font)
self._tabBar.setShape(QtWidgets.QTabBar.RoundedNorth)
self._tabBar.setDocumentMode(True)
# Title label
self._titleLabel = RibbonTitleLabel(self)
self._titleLabel.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred) # type: ignore
self._titleLabel.setAlignment(QtCore.Qt.AlignCenter | QtCore.Qt.AlignBottom) # type: ignore
self._titleLabel.setText(title)
font = self._titleLabel.font()
font.setPointSize(font.pointSize() + 3)
self._titleLabel.setFont(font)
self._tabBarLayout.addWidget(self._quickAccessToolBarWidget, 0, QtCore.Qt.AlignVCenter)
self._tabBarLayout.addWidget(self._tabBar, 0, QtCore.Qt.AlignVCenter)
self._tabBarLayout.addWidget(self._titleLabel, 1, QtCore.Qt.AlignVCenter)
self._tabBarLayout.addWidget(self._rightToolBar, 0, QtCore.Qt.AlignVCenter)
def applicationButton(self) -> RibbonApplicationButton:
"""Return the application button."""
return self._applicationButton
def setApplicationIcon(self, icon: QtGui.QIcon):
"""Set the application icon.
:param icon: The icon to set.
"""
self._applicationButton.setIcon(icon)
def addTitleWidget(self, widget: QtWidgets.QWidget):
"""Add a widget to the title layout.
:param widget: The widget to add.
"""
self._tabBarLayout.addWidget(widget)
def insertTitleWidget(self, index: int, widget: QtWidgets.QWidget):
"""Insert a widget to the title layout.
:param index: The index to insert the widget.
:param widget: The widget to insert.
"""
self._tabBarLayout.insertWidget(index, widget)
def removeTitleWidget(self, widget: QtWidgets.QWidget):
"""Remove a widget from the title layout.
:param widget: The widget to remove.
"""
self._tabBarLayout.removeWidget(widget)
def tabBar(self) -> RibbonTabBar:
"""Return the tab bar of the ribbon.
:return: The tab bar of the ribbon.
"""
return self._tabBar
def quickAccessToolBar(self) -> QtWidgets.QToolBar:
"""Return the quick access toolbar of the ribbon.
:return: The quick access toolbar of the ribbon.
"""
return self._quickAccessToolBar
def quickAccessButtons(self) -> typing.List[QtWidgets.QToolButton]:
"""Return the quick access buttons of the ribbon.
:return: The quick access buttons of the ribbon.
"""
return self._quickAccessButtons
def addQuickAccessButton(self, button: QtWidgets.QToolButton):
"""Add a widget to the quick access bar.
:param button: The button to add.
"""
button.setIconSize(QtCore.QSize(self._quickAccessButtonHeight, self._quickAccessButtonHeight))
self._quickAccessButtons.append(button)
self._quickAccessToolBar.addWidget(button)
def setQuickAccessButtonHeight(self, height: int = 30):
"""Set the height of the quick access buttons.
:param height: The height to set.
"""
self._quickAccessButtonHeight = height
self._applicationButton.setIconSize(QtCore.QSize(height, height))
for button in self._quickAccessButtons:
button.setIconSize(QtCore.QSize(height, height))
def title(self) -> str:
"""Return the title of the ribbon.
:return: The title of the ribbon.
"""
return self._titleLabel.text()
def setTitle(self, title: str):
"""Set the title of the ribbon.
:param title: The title to set.
"""
self._titleLabel.setText(title)
def rightToolBar(self) -> QtWidgets.QToolBar:
"""Return the right toolbar of the ribbon.
:return: The right toolbar of the ribbon.
"""
return self._rightToolBar
def addRightToolButton(self, button: QtWidgets.QToolButton):
"""Add a widget to the right button bar.
:param button: The button to add.
"""
button.setIconSize(QtCore.QSize(self._rightButtonHeight, self._rightButtonHeight))
self._rightToolButtons.append(button)
self._rightToolBar.addWidget(button)
def setRightToolBarHeight(self, height: int = 24):
"""Set the height of the right buttons.
:param height: The height to set.
"""
self._rightButtonHeight = height
for button in self._rightToolButtons:
button.setIconSize(QtCore.QSize(height, height))
def helpRibbonButton(self) -> QtWidgets.QToolButton:
"""Return the help ribbon button.
:return: The help ribbon button.
"""
return self._helpButton
def setHelpButtonIcon(self, icon: QtGui.QIcon):
"""Set the icon of the help button.
:param icon: The icon to set.
"""
self._helpButton.setIcon(icon)
def removeHelpButton(self):
"""Remove the help button from the ribbon."""
self._helpButton.setVisible(False)
def setCollapseButtonIcon(self, icon: QtGui.QIcon):
"""Set the icon of the min button.
:param icon: The icon to set.
"""
self._collapseRibbonButton.setIcon(icon)
def removeCollapseButton(self):
"""Remove the min button from the ribbon."""
self._collapseRibbonButton.setVisible(False)
def collapseRibbonButton(self) -> QtWidgets.QToolButton:
"""Return the collapse ribbon button.
:return: The collapse ribbon button.
"""
return self._collapseRibbonButton
def topLevelWidget(self) -> QtWidgets.QWidget:
widget = self
while widget.parentWidget():
widget = widget.parentWidget()
return widget
def mousePressEvent(self, e: QtGui.QMouseEvent):
self._start_point = e.pos()
self._window_point = self.topLevelWidget().frameGeometry().topLeft()
def mouseMoveEvent(self, e: QtGui.QMouseEvent):
relpos = e.pos() - self._start_point if self._start_point else None
self.topLevelWidget().move(self._window_point + relpos) if self._window_point and relpos else None
self.topLevelWidget().windowHandle().startSystemMove()
def mouseDoubleClickEvent(self, e: QtGui.QMouseEvent):
mainwindow = self.topLevelWidget()
mainwindow.showNormal() if mainwindow.isMaximized() else mainwindow.showMaximized()