-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathqt_subclasses.py
More file actions
262 lines (218 loc) · 9.61 KB
/
qt_subclasses.py
File metadata and controls
262 lines (218 loc) · 9.61 KB
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
"""
=========================================================================
Extra functionality for PyQT classes.
=========================================================================
"""
# author : AC Chamberlain <[email protected]>
# copyright: AC Chamberlain (c) 2023-2026
# SPDX-License-Identifier: Licence.txt:
from PyQt5.QtWidgets import (
QApplication,
QToolButton,
QWidget,
QVBoxLayout,
QHBoxLayout,
QLabel,
QFrame,
QStatusBar,
QDoubleSpinBox)
from PyQt5.QtCore import QTimer, Qt, pyqtSignal, QEvent
from PyQt5.QtGui import QPalette
from linaqa_types import faint_red, faint_green, faint_yellow
class ColorStatusBar(QStatusBar):
"""Status bar to display and retain colour coded error, warn and good messages"""
def status_clear(self):
# Clear the status bar
qsb_color = self.palette().color(QPalette.Base).getRgb()
mystylesheet = f"background-color: {qsb_color}; border-top: 1px outset grey;"
self.setStyleSheet(mystylesheet)
self.showMessage("")
def status_message(self, status_message):
# Clear the status bar
qsb_color = self.palette().color(QPalette.Base).getRgb()
mystylesheet = f"background-color: {qsb_color}; border-top: 1px outset grey;"
self.setStyleSheet(mystylesheet)
# self.setToolTip(self.toolTip() + "\n" + status_message)
self.showMessage(status_message)
def status_warn(self, status_message):
mystylesheet = f"background-color: {faint_yellow}; border-top: 1px outset grey;"
self.setStyleSheet(mystylesheet)
self.setToolTip(self.toolTip() + "\n" + status_message)
self.showMessage(status_message)
def status_good(self, status_message):
mystylesheet = f"background-color: {faint_green}; border-top: 1px outset grey;"
self.setStyleSheet(mystylesheet)
self.setToolTip(self.toolTip() + "\n" + status_message)
self.showMessage(status_message)
def status_error(self, status_message):
mystylesheet = f"background-color: {faint_red}; border-top: 1px outset grey;"
self.setStyleSheet(mystylesheet)
self.setToolTip(self.toolTip() + "\n" + status_message)
self.showMessage(status_message)
class MyDoubleSpinBox(QDoubleSpinBox):
def validate(self, text: str, pos: int) -> object:
text = text.replace(".", ",")
return QDoubleSpinBox.validate(self, text, pos)
def valueFromText(self, text: str) -> float:
text = text.replace(",", ".")
return float(text)
class PopupToolbar(QFrame):
"""Custom popup widget that acts as a toolbar with controls"""
closed = pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent, Qt.Tool | Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.setFrameStyle(QFrame.StyledPanel | QFrame.Raised)
self.setLineWidth(2)
self.setAttribute(Qt.WA_DeleteOnClose, False)
# Main layout
self.main_layout = QVBoxLayout(self)
self.main_layout.setContentsMargins(8, 8, 8, 8)
self.main_layout.setSpacing(6)
# Track if we're currently showing
self._is_showing = False
def add_hcontrol(self, label, widget: QWidget=None):
"""Add a labeled control to the popup toolbar"""
row = QHBoxLayout()
if label != "":
row.addWidget(QLabel(label))
if widget is not None:
row.addWidget(widget)
self.main_layout.addLayout(row)
def add_vcontrol(self, label, widget: QWidget=None):
"""Add a labeled control to the popup toolbar"""
col = QVBoxLayout()
if label != "":
col.addWidget(QLabel(label))
if widget is not None:
col.addWidget(widget)
self.main_layout.addLayout(col)
def add_widget(self, widget):
"""Add a widget directly to the popup toolbar"""
self.main_layout.addWidget(widget)
def add_toolbar(self, toolbar):
"""Add a QToolBar to the popup"""
self.main_layout.addWidget(toolbar)
def show_below(self, reference_widget):
"""Show the popup below the reference widget"""
pos = reference_widget.mapToGlobal(reference_widget.rect().bottomLeft())
self.move(pos)
self.show()
self.activateWindow()
self.raise_()
# Install event filter when showing
if not self._is_showing:
QApplication.instance().installEventFilter(self)
self._is_showing = True
def show_next_to(self, reference_widget):
"""Show the popup below the reference widget"""
pos = reference_widget.mapToGlobal(reference_widget.rect().topRight())
self.move(pos)
self.show()
self.activateWindow()
self.raise_()
# Install event filter when showing
if not self._is_showing:
QApplication.instance().installEventFilter(self)
self._is_showing = True
def eventFilter(self, obj, event):
"""Filter events to detect clicks outside the popup"""
if event.type() == QEvent.MouseButtonPress and self._is_showing:
# Check if click is outside this widget and its children
if self.isVisible():
click_pos = event.globalPos()
popup_rect = self.rect()
popup_rect.moveTopLeft(self.mapToGlobal(self.rect().topLeft()))
if not popup_rect.contains(click_pos):
# Check if the click is on a child widget (like a combobox dropdown)
clicked_widget = QApplication.widgetAt(click_pos)
# Check if clicked widget is related to our popup
if clicked_widget is not None:
# Check if it's a child of this popup
if self.isAncestorOf(clicked_widget):
return False
# Check if it's a popup window related to our widgets (like combobox dropdown)
for child in self.findChildren(QWidget):
# Check for combobox dropdowns and other popups
if hasattr(child, "view") and callable(child.view):
# This is likely a combobox
try:
view = child.view()
if view and (clicked_widget == view or view.isAncestorOf(clicked_widget)):
return False
except:
pass
# Click was outside the popup and not on a related widget
self.close()
return False # Allow the click to propagate
return False
def closeEvent(self, event):
"""Clean up event filter when closing"""
if self._is_showing:
QApplication.instance().removeEventFilter(self)
self._is_showing = False
self.closed.emit()
super().closeEvent(event)
def hideEvent(self, event):
"""Clean up when hidden"""
if self._is_showing:
QApplication.instance().removeEventFilter(self)
self._is_showing = False
super().hideEvent(event)
class LongPressToolButton(QToolButton):
"""Custom QToolButton that detects long press and shows popup toolbar"""
longPressed = pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent)
self.long_press_timer = QTimer()
self.long_press_timer.setSingleShot(True)
self.long_press_timer.timeout.connect(self.on_long_press)
self.long_press_duration = 500 # milliseconds
self.is_long_press = False
self.popup_widget = None
self.popup_initializer = None # Callback to initialize popup before showing
def set_popup_widget(self, widget):
"""Set the popup widget that will appear on long press"""
self.popup_widget = widget
def set_popup_initializer(self, callback):
"""
Set a callback function that will be called before showing the popup.
The callback receives the popup widget as parameter.
Example:
def init_popup(popup):
# Update values in popup before showing
popup.some_widget.setText("new value")
button.set_popup_initializer(init_popup)
"""
self.popup_initializer = callback
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.is_long_press = False
self.long_press_timer.start(self.long_press_duration)
event.accept()
elif event.button() == Qt.RightButton:
if self.popup_widget:
# if self.popup_initializer:
# self.popup_initializer()
self.popup_widget.show_next_to(self)
event.accept()
else:
super().mousePressEvent(event)
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.long_press_timer.stop()
if not self.is_long_press:
# Short click - trigger default action
if self.defaultAction():
self.defaultAction().trigger()
event.accept()
else:
super().mouseReleaseEvent(event)
def on_long_press(self):
"""Called when long press is detected"""
self.is_long_press = True
self.longPressed.emit()
if self.popup_widget:
# Call initializer callback if set
if self.popup_initializer:
self.popup_initializer()
self.popup_widget.show_next_to(self)