This repository was archived by the owner on Jun 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_widgets.py
216 lines (185 loc) · 7.82 KB
/
custom_widgets.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
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class PushButton(QPushButton):
BASE_COLOR = '#202020'
HOVER_COLOR = '#505050'
PRESS_COLOR = '#707070'
def __init__(self, basecolor=BASE_COLOR, hovercolor=HOVER_COLOR, presscolor=PRESS_COLOR):
super(PushButton, self).__init__()
self._animation1 = QVariantAnimation(
startValue=QColor(hovercolor),
endValue=QColor(basecolor),
valueChanged=self._on_value_changed,
duration=400,
)
self._animation2 = QVariantAnimation(
startValue=QColor(presscolor),
endValue=QColor(hovercolor),
valueChanged=self._on_value_changed,
duration=150,
)
self._update_stylesheet(QColor(basecolor)) # passing in default background color
# self.setCursor(Qt.PointingHandCursor)
def _on_value_changed(self, color):
self._update_stylesheet(color)
def _update_stylesheet(self, background):
self.setStyleSheet(
f"""
font-weight: normal;
min-width:64px;
min-height: 70px;
margin: -5px;
background-color: {background.name()};
"""
)
def enterEvent(self, event, *args, **kwargs):
self._animation1.setDirection(QAbstractAnimation.Backward)
self._animation1.start()
super().enterEvent(event)
def leaveEvent(self, event, *args, **kwargs):
self._animation1.setDirection(QAbstractAnimation.Forward)
self._animation1.start()
super().leaveEvent(event)
def mousePressEvent(self, event, *args, **kwargs):
self._animation2.setDirection(QAbstractAnimation.Backward)
self._animation2.start()
super(PushButton, self).mousePressEvent(event)
def mouseReleaseEvent(self, event, *args, **kwargs):
self._animation2.setDirection(QAbstractAnimation.Forward)
self._animation2.start()
super(PushButton, self).mouseReleaseEvent(event)
class NoCursorLineEdit(QLineEdit):
def __init__(self):
super(NoCursorLineEdit, self).__init__()
self.setReadOnly(True)
def paintEvent(self, event): # setting blinking cursor color to same as background color to hide it...lel big brain🧠
super(NoCursorLineEdit, self).paintEvent(event)
rect = self.cursorRect()
painter = QPainter(self)
new_rect = QRect(rect.x() + (rect.width() / 2), rect.y(), rect.width() - (rect.width() * 0.8),
rect.height())
painter.fillRect(new_rect, QColor('#353535'))
class BlurEffect(QGraphicsBlurEffect):
effectRect = None
def setEffectRect(self, rect):
self.effectRect = rect
self.update()
def draw(self, qp):
if self.effectRect is None or self.effectRect.isNull():
# no valid effect rect to be used, use the default implementation
super().draw(qp)
print('bao')
else:
qp.save()
# clip the drawing so that it's restricted to the effectRect
qp.setClipRect(self.effectRect)
# call the default implementation, which will draw the effect
super().draw(qp)
# get the full region that should be painted
fullRegion = QRegion(qp.viewport())
# and subtract the effect rectangle
fullRegion -= QRegion(self.effectRect)
qp.setClipRegion(fullRegion)
# draw the *source*, which has no effect applied
self.drawSource(qp)
qp.restore()
class HSeparationLine(QFrame):
"""
Custom Class to create a horizontal separation line.
"""
def __init__(self):
super().__init__()
self.setMinimumWidth(1)
self.setFixedHeight(1)
self.setFrameShape(QFrame.HLine)
self.setFrameShadow(QFrame.Sunken)
self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Minimum)
return
# WILL NEED WAYYY EXTRA CHECKS AND LOGIC TO SYNC IT WITH BUTTON FUNCTIONS - maybe do later 💀
# class NumberInputValidator(QValidator):
# """
# Custom validator class to validate number input in Calc screen.
# """
#
# def validate(self, v_string: str, index: int):
# # invalid_chars =
# # `~!@#$%^&*()-_=+{[}]|\\'",<>/?
# state = None
# if len(v_string) < 1 or v_string[:1] != '0':
# # ic('Less than 2 or not 0 - invalid')
# state = QValidator.Invalid
#
# if len(v_string) >= 1:
# # ic('Greater than 1 - VALID')
# state = QValidator.Intermediate
#
# if (v_string[:].isdigit()) is True:
# # ic('All digits - VALID')
# state = QValidator.Acceptable
#
# for x in v_string[:]:
# if ((x.isdigit() is False) and '.' not in v_string[:]) or x.isalpha():
# # ic('Alpha or Anything else - invalid')
# state = QValidator.Invalid
#
# nums = v_string[:].split('.')
# if '.' in v_string[:]:
# if (nums[0].isdigit() and nums[1].isdigit()) is True:
# # ic('Has period and both numbers are legit - VALID')
# state = QValidator.Acceptable
# if '.' in v_string[:] and len(v_string.split('.')[1]) >= 1:
# if (nums[0].isdigit() is True) and (nums[1].isdigit() is False):
# # ic('Has period, but other chars too - invalid')
# state = QValidator.Invalid
# if len(nums[1]) > 2:
# # ic('More than 2 digits after period - invalid')
# state = QValidator.Invalid
#
# if v_string.count('.') > 1:
# # ic('More than one period - invalid')
# state = QValidator.Invalid
#
# return state, v_string, index
# THIS BREAKS MY APPS UI - It's supposed to realign button icons with text.
# class ProxyStyle(QProxyStyle):
# def drawControl(self, element, option, painter, widget=None):
# if element == QStyle.CE_PushButtonLabel:
# icon = QIcon(option.icon)
# option.icon = QIcon()
# super(ProxyStyle, self).drawControl(element, option, painter, widget)
# if element == QStyle.CE_PushButtonLabel:
# if not icon.isNull():
# iconSpacing = 4
# mode = (
# QIcon.Normal
# if option.state & QStyle.State_Enabled
# else QIcon.Disabled
# )
# if (
# mode == QIcon.Normal
# and option.state & QStyle.State_HasFocus
# ):
# mode = QIcon.Active
# state = QIcon.Off
# if option.state & QStyle.State_On:
# state = QIcon.On
# window = widget.window().windowHandle() if widget is not None else None
# pixmap = icon.pixmap(window, option.iconSize, mode, state)
# pixmapWidth = pixmap.width() / pixmap.devicePixelRatio()
# pixmapHeight = pixmap.height() / pixmap.devicePixelRatio()
# iconRect = QRect(
# QPoint(), QSize(pixmapWidth, pixmapHeight)
# )
# iconRect.moveCenter(option.rect.center())
# iconRect.moveLeft(option.rect.left() + iconSpacing)
# iconRect = self.visualRect(option.direction, option.rect, iconRect)
# iconRect.translate(
# self.proxy().pixelMetric(
# QStyle.PM_ButtonShiftHorizontal, option, widget
# ),
# self.proxy().pixelMetric(
# QStyle.PM_ButtonShiftVertical, option, widget
# ),
# )
# painter.drawPixmap(iconRect, pixmap)