forked from Matthijs-utf8/TimeTrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyQTCustomItems.py
285 lines (230 loc) · 8.25 KB
/
PyQTCustomItems.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
from PyQt5.QtCore import Qt, QSize, pyqtSignal
from PyQt5.QtGui import QFont, QIcon, QColor, QPainter, QPen, QPixmap
from PyQt5.QtWidgets import (
QSpinBox,
QLabel,
QPushButton,
QDoubleSpinBox,
QWidget,
QHBoxLayout,
QLineEdit,
)
class CustomCheckBoxLineEdit(QWidget):
"""
A custom widget consisting of a checkbox and a line edit.
Args:
label_text (str): The label text for the checkbox.
text (str): The initial text for the line edit.
"""
def __init__(self, label_text="", text=""):
super().__init__()
# Initialize variables
self.label = label_text
self.text = text
# Main layout
layout = QHBoxLayout()
# Create a checkbox
self.checkbox = CustomCheckBox(label_text, initial_checked=True)
self.checkbox.setMinimumWidth(300)
layout.addWidget(self.checkbox)
# Line edit
self.line_edit = QLineEdit()
self.line_edit.setText(str(text))
layout.addWidget(self.line_edit)
# Set layout
self.setLayout(layout)
def setText(self, text):
self.line_edit.setText(str(text))
self.text = text
class CustomCheckBox(QWidget):
"""
A custom checkbox widget.
Args:
text (str): The label text for the checkbox.
initial_checked (bool): The initial checked state.
"""
stateChanged = pyqtSignal(bool)
def __init__(self, text='', initial_checked=False):
super().__init__()
self.checked = initial_checked
# Create layout for checkbox and text
layout = QHBoxLayout()
layout.setAlignment(Qt.AlignLeft)
# Create label for the checkmark icon
self.checkmark_label = QLabel()
self.checkmark_label.setAlignment(Qt.AlignLeft)
# Create label for the text
self.text_label = QLabel(text)
self.text_label.setAlignment(Qt.AlignLeft)
# Set initial icon
self.update_icon()
# Add labels to layout
layout.addWidget(self.checkmark_label)
layout.addWidget(self.text_label)
# Set layout for the widget
self.setLayout(layout)
def update_icon(self):
if self.checked:
# Blue checkmark icon
pixmap = QPixmap(20, 20)
pixmap.fill(QColor("transparent"))
painter = QPainter(pixmap)
painter.setPen(QPen(QColor("#64aaf0"), 2, Qt.SolidLine))
painter.drawRect(2, 2, 16, 16)
painter.drawLine(5, 10, 8, 15)
painter.drawLine(8, 15, 15, 5)
painter.end()
self.checkmark_label.setPixmap(pixmap)
else:
# Red outline icon
pixmap = QPixmap(20, 20)
pixmap.fill(QColor("transparent"))
painter = QPainter(pixmap)
painter.setPen(QPen(QColor("#f07878"), 2, Qt.SolidLine))
painter.drawRect(2, 2, 16, 16)
painter.end()
self.checkmark_label.setPixmap(pixmap)
def isChecked(self):
return self.checked
def mousePressEvent(self, event):
self.checked = not self.checked
self.update_icon()
self.stateChanged.emit(self.checked)
def setText(self, text):
self.text_label.setText(text)
class CustomSpinBox(QSpinBox):
"""
A custom integer spin box widget.
"""
def __init__(self, parent=None):
super().__init__(parent)
# Set the initial properties
self.setStyleSheet("""
* {
background-color: transparent;
color: #fff4c9; /* Change the text color */
font-family: "Courier", sans-serif;
font-weight: bold; /* Make the font bold */
font-size: 20px; /* Change the font size to 10 */
}
QSpinBox {
background-color: transparent;
border: 1px solid black;
color: #fff4c9;
}
QSpinBox::down-button, QSpinBox::up-button {
background-color: transparent;
border-radius: 0;
color: fff4c9;
}
QSpinBox::up-arrow {
width: 12px;
height: 12px;
image: url(../Data/icons/up-arrow.png);
image-position: center center;
}
QSpinBox::down-arrow {
width: 12px;
height: 12px;
image: url(../Data/icons/down-arrow.png);
image-position: center center;
}
""")
self.setButtonSymbols(QSpinBox.UpDownArrows)
self.setCorrectionMode(QSpinBox.CorrectToNearestValue)
self.setCursor(Qt.PointingHandCursor)
self.setMinimumSize(0, 35)
self.setMaximumSize(4000, 35)
self.setFont(QFont("Courier", 10))
self.setInputMethodHints(Qt.ImhDigitsOnly)
self.setWrapping(False)
self.setFrame(False)
class CustomDoubleSpinBox(QDoubleSpinBox):
"""
A custom double spin box widget.
"""
def __init__(self, parent=None):
super().__init__(parent)
# Set the initial properties
self.setStyleSheet("""
* {
background-color: transparent;
color: #fff4c9; /* Change the text color */
font-family: "Courier", sans-serif;
font-weight: bold; /* Make the font bold */
font-size: 20px; /* Change the font size to 10 */
}
QDoubleSpinBox {
background-color: transparent;
border: 1px solid black;
color: #fff4c9;
}
QDoubleSpinBox::down-button, QDoubleSpinBox::up-button {
background-color: transparent;
border-radius: 0;
color: fff4c9;
}
QDoubleSpinBox::up-arrow {
width: 12px;
height: 12px;
image: url(../Data/icons/up-arrow.png);
image-position: center center;
}
QDoubleSpinBox::down-arrow {
width: 12px;
height: 12px;
image: url(../Data/icons/down-arrow.png);
image-position: center center;
}
""")
self.setButtonSymbols(QSpinBox.UpDownArrows)
self.setCorrectionMode(QSpinBox.CorrectToNearestValue)
self.setCursor(Qt.PointingHandCursor)
self.setMinimumSize(0, 35)
self.setMaximumSize(4000, 35)
self.setFont(QFont("Courier", 10))
self.setInputMethodHints(Qt.ImhDigitsOnly)
self.setWrapping(False)
self.setFrame(False)
class CustomLabel(QLabel):
"""
A custom label widget.
Args:
text (str): The text to display in the label.
parent: The parent widget (optional).
"""
def __init__(self, text, parent=None):
super().__init__(str(text), parent)
self._prefix = ""
self._suffix = ""
self.setContentsMargins(0, 0, 0, 0)
def setText(self, text):
super().setText(f"{self._prefix}{str(text)}{self._suffix}")
def setPrefix(self, prefix):
self._prefix = prefix
self.setText(f"{self._prefix}{str(self.text())}{self._suffix}")
def setSuffix(self, suffix):
self._suffix = suffix
self.setText(f"{self._prefix}{str(self.text())}{self._suffix}")
class CustomPushButton(QPushButton):
"""
A custom push button widget.
Args:
icon (str): The path to the button's icon.
function: The function to call when the button is clicked.
tooltip (str): The tooltip text for the button.
size (int): The size of the button (both width and height).
"""
def __init__(self, icon, function, tooltip, size=50):
super().__init__()
self.setIcon(QIcon(icon))
self.setFixedSize(size, size)
self.setIconSize(QSize(int(round(size * 0.8)), int(round(size * 0.8))))
self.clicked.connect(function)
self.setToolTip(tooltip)
def enterEvent(self, event):
# Change cursor to pointing hand when mouse enters the button
self.setCursor(Qt.PointingHandCursor)
def leaveEvent(self, event):
# Reset cursor to default when mouse leaves the button
self.setCursor(Qt.ArrowCursor)