This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tabs.py
275 lines (200 loc) · 8.35 KB
/
tabs.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
from .core.overlay_widget import *
from .buttons import *
class QMaterialTabsInkBar(QMaterialOverlayWidget):
def __init__(self, parent):
QMaterialOverlayWidget.__init__(self, parent)
self.m_tabs = parent
self.m_animation = QPropertyAnimation()
self.m_geometry = QRect()
self.m_previousGeometry = QRect()
self.m_tween = 0
self.m_animation.setPropertyName(b"_tweenValue")
self.m_animation.setEasingCurve(QEasingCurve.OutCirc)
self.m_animation.setTargetObject(self)
self.m_animation.setDuration(700)
self.m_tabs.installEventFilter(self)
self.setAttribute(Qt.WA_TransparentForMouseEvents)
self.setAttribute(Qt.WA_NoSystemBackground)
def setTweenValue(self, value: float) -> None:
self.m_tween = value
self.refreshGeometry()
def tweenValue(self) -> float:
return self.m_tween
def refreshGeometry(self) -> None:
item: QLayoutItem = self.m_tabs.layout().itemAt(self.m_tabs.currentIndex())
if item:
r = QRect(item.geometry())
s: float = 1 - self.m_tween
if QAbstractAnimation.Running != self.m_animation.state():
self.m_geometry = QRect(r.left(), r.bottom() - 1, r.width(), 2)
else:
left: float = (
self.m_previousGeometry.left() * s + r.left() * self.m_tween
)
width: float = (
self.m_previousGeometry.width() * s + r.width() * self.m_tween
)
self.m_geometry = QRect(left, r.bottom() - 1, width, 2)
self.m_tabs.update()
def animate(self) -> None:
self.raise_()
self.m_previousGeometry = self.m_geometry
self.m_animation.stop()
self.m_animation.setStartValue(0)
self.m_animation.setEndValue(1)
self.m_animation.start()
def eventFilter(self, obj: QObject, event: QEvent) -> bool:
if event.type() in [QEvent.Move, QEvent.Resize]:
self.refreshGeometry()
return QMaterialOverlayWidget.eventFilter(self, obj, event)
def paintEvent(self, event: QPaintEvent) -> None:
painter = QPainter(self)
painter.setOpacity(1)
painter.fillRect(self.m_geometry, self.m_tabs.inkColor())
_tweenValue = Property(float, fset=setTweenValue, fget=tweenValue)
class QMaterialTab(QMaterialFlatButton):
def __init__(self, parent):
QMaterialFlatButton.__init__(self, parent)
self.m_tabs = parent
self.m_active = bool(False)
self.setMinimumHeight(50)
f = QFont(self.font())
f.setStyleName("Normal")
self.setFont(f)
self.setCornerRadius(0)
self.setRole(Material.Primary)
self.setBackgroundMode(Qt.OpaqueMode)
self.setBaseOpacity(0.25)
self.clicked.connect(self.activateTab)
def setActive(self, state: bool) -> None:
self.m_active = state
self.update()
def isActive(self) -> bool:
return self.m_active
def sizeHint(self) -> QSize:
if self.icon().isNull():
return QMaterialFlatButton.sizeHint(self)
else:
return QSize(40, self.iconSize().height() + 46)
def activateTab(self) -> None:
self.m_tabs.setCurrentTab(self)
def paintForeground(self, painter: QPainter) -> None:
painter.setPen(self.foregroundColor())
if not self.icon().isNull():
painter.translate(0, 12)
textSize = QSize(self.fontMetrics().size(Qt.TextSingleLine, self.text()))
base = QSize(self.size() - textSize)
textGeometry = QRect(QPoint(base.width(), base.height()) / 2, textSize)
painter.drawText(textGeometry, Qt.AlignCenter, self.text())
if not self.icon().isNull():
size: QSize = self.iconSize()
iconRect = QRect(QPoint((self.width() - size.width()) / 2, 0), size)
pixmap: QPixmap = self.icon().pixmap(self.iconSize())
icon = QPainter(pixmap)
icon.setCompositionMode(QPainter.CompositionMode_SourceIn)
icon.fillRect(pixmap.rect(), painter.pen().color())
painter.drawPixmap(iconRect, pixmap)
if not self.m_active:
if not icon().isNull():
painter.translate(0, -12)
overlay = QBrush()
overlay.setStyle(Qt.SolidPattern)
overlay.setColor(self.backgroundColor())
painter.setOpacity(0.36)
painter.fillRect(self.rect(), overlay)
class QMaterialTabs(QWidget):
currentChanged = Signal(int)
def __init__(self, parent: QWidget = None):
QWidget.__nit__(self, parent)
self.m_inkBar = QMaterialTabsInkBar(self.q)
self.m_tabLayout = QHBoxLayout
self.m_rippleStyle = Material.CenteredRipple
self.m_tab = -1
self.m_showHalo = True
self.m_useThemeColors = True
self.setLayout(self.m_tabLayout)
self.setStyle(QMaterialStyle.instance())
self.m_tabLayout.setSpacing(0)
self.m_tabLayout.setMargin(0)
def setUseThemeColors(self, value: bool) -> None:
self.m_useThemeColors = value
def useThemeColors(self) -> bool:
return self.m_useThemeColors
def setHaloVisible(self, value: bool) -> None:
self.m_showHalo = value
self.updateTabs()
def isHaloVisible(self) -> bool:
return self.m_showHalo
def setRippleStyle(self, style: Material.RippleStyle) -> None:
self.m_rippleStyle = style
self.updateTabs()
def rippleStyle(self) -> Material.RippleStyle:
return self.m_rippleStyle
def setInkColor(self, color: QColor) -> None:
self.m_inkColor = color
MATERIAL_DISABLE_THEME_COLORS(self)
self.m_inkBar.update()
self.update()
def inkColor(self) -> QColor:
if self.m_useThemeColors or not self.m_inkColor.isValid():
return QMaterialStyle.instance().themeColor("accent1")
else:
return self.m_inkColor
def setBackgroundColor(self, color: QColor) -> None:
self.m_backgroundColor = color
MATERIAL_DISABLE_THEME_COLORS(self)
self.updateTabs()
self.update()
def backgroundColor(self) -> QColor:
if self.m_useThemeColors or not self.m_backgroundColor.isValid():
return QMaterialStyle.instance().themeColor("primary1")
else:
return self.m_backgroundColor
def setTextColor(self, color: QColor) -> None:
self.m_textColor = color
MATERIAL_DISABLE_THEME_COLORS(self)
self.updateTabs()
self.update()
def textColor(self) -> QColor:
if self.m_useThemeColors or not self.m_textColor.isValid():
return QMaterialStyle.instance().themeColor("canvas")
else:
return self.m_textColor
def addTab(self, text: str, icon: QIcon = QIcon()) -> None:
tab: QMaterialTab = QMaterialTab(self)
tab.setText(text)
tab.setHaloVisible(self.isHaloVisible())
tab.setRippleStyle(self.rippleStyle())
if not icon.isNull():
tab.setIcon(icon)
tab.setIconSize(QSize(22, 22))
self.m_tabLayout.addWidget(tab)
if -1 == self.m_tab:
self.m_tab = 0
self.m_inkBar.refreshGeometry()
self.m_inkBar.raise_()
tab.setActive(True)
def setCurrentTab(self, tab: QMaterialTab = None, index: int = 0) -> None:
if tab:
index = self.m_tabLayout.indexOf(tab)
self.setTabActive(self.m_tab, False)
self.m_tab = index
self.setTabActive(index, True)
self.m_inkBar.animate()
self.emit(self.currentChanged(index))
def currentIndex(self) -> int:
return self.m_tab
def setTabActive(self, index: int, active: bool = True) -> None:
if index > -1:
tab: QMaterialTab = self.m_tabLayout.itemAt(index).widget()
if tab:
tab.setActive(active)
def updateTabs(self) -> None:
for i in range(self.m_tabLayout.count()):
item: QLayoutItem = self.m_tabLayout.itemAt(i)
tab: QMaterialTab = item.widget()
if tab:
tab.setRippleStyle(self.m_rippleStyle)
tab.setHaloVisible(self.m_showHalo)
tab.setBackgroundColor(self.backgroundColor())
tab.setForegroundColor(self.textColor())