-
Notifications
You must be signed in to change notification settings - Fork 0
/
delegates.py
392 lines (336 loc) · 14.6 KB
/
delegates.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# delegates.py
from PyQt6.QtWidgets import (
QStyledItemDelegate, QComboBox, QCompleter, QDoubleSpinBox, QMessageBox, QLabel, QStyle
)
from PyQt6.QtGui import QKeyEvent, QPalette
from PyQt6.QtCore import Qt, QRectF, QPointF, QLocale
import logging
import re
from html import unescape
import os
def strip_html_tags(text):
"""
Removes HTML tags from the given text and unescapes HTML entities.
Args:
text (str): The text containing HTML tags.
Returns:
str: The cleaned text without HTML tags.
"""
clean = re.compile('<.*?>')
return unescape(re.sub(clean, '', text))
class CustomDoubleSpinBox(QDoubleSpinBox):
"""
A customized QDoubleSpinBox that replaces comma with dot for decimal input.
"""
def keyPressEvent(self, event):
if event.text() == ',':
# Replace comma with dot
new_event = QKeyEvent(
event.type(),
Qt.Key.Key_Period,
event.modifiers(),
'.',
event.isAutoRepeat(),
event.count()
)
super().keyPressEvent(new_event)
else:
super().keyPressEvent(event)
class ComboBoxDelegate(QStyledItemDelegate):
"""
A delegate that provides a QComboBox editor for table cells.
"""
def __init__(self, items, parent=None):
super().__init__(parent)
self.items = items
def createEditor(self, parent, option, index):
comboBox = QComboBox(parent)
comboBox.setEditable(True)
comboBox.addItems(self.items)
comboBox.setInsertPolicy(QComboBox.InsertPolicy.NoInsert)
# Create the completer and set it to be case insensitive
completer = QCompleter(self.items, comboBox)
completer.setFilterMode(Qt.MatchFlag.MatchContains)
completer.setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)
comboBox.setCompleter(completer)
# Apply the dark background style to the completer popup
completer.popup().setStyleSheet("background-color: #2D2D30; color: white;")
comboBox.setStyleSheet("background-color: #2D2D30; color: white;")
# Connect the 'currentIndexChanged' signal to commit data and close editor
comboBox.currentIndexChanged.connect(self.commitAndClose)
return comboBox
def commitAndClose(self):
"""
Commits the data from the editor to the model and closes the editor.
"""
editor = self.sender() # Get the editor that sent the signal
self.commitData.emit(editor)
self.closeEditor.emit(editor, QStyledItemDelegate.EndEditHint.NoHint)
def setEditorData(self, editor, index):
"""
Sets the editor's current value based on the model's data.
"""
value = index.model().data(index, Qt.ItemDataRole.EditRole)
idx = editor.findText(value, Qt.MatchFlag.MatchFixedString)
editor.setCurrentIndex(idx if idx >= 0 else -1)
def setModelData(self, editor, model, index):
"""
Updates the model with the editor's current value.
"""
model.setData(index, editor.currentText(), Qt.ItemDataRole.EditRole)
def updateEditorGeometry(self, editor, option, index):
"""
Sets the editor's geometry.
"""
editor.setGeometry(option.rect)
class RatingDelegate(QStyledItemDelegate):
"""
A delegate that provides a customized QDoubleSpinBox editor for rating cells.
"""
def createEditor(self, parent, option, index):
editor = CustomDoubleSpinBox(parent)
editor.setFrame(False)
editor.setDecimals(2)
# Set locale to English (United States) to use dot as decimal separator
editor.setLocale(QLocale(QLocale.Language.English, QLocale.Country.UnitedStates))
# Correctly locate and set the stylesheet for the editor
style_sheet_path = os.path.join(os.path.dirname(__file__), 'style.qss')
if os.path.exists(style_sheet_path):
try:
with open(style_sheet_path, "r") as file:
stylesheet = file.read()
editor.setStyleSheet(stylesheet)
except Exception as e:
logging.error(f"Failed to apply stylesheet to RatingDelegate editor: {e}")
else:
logging.warning(f"Stylesheet not found at {style_sheet_path}. Skipping stylesheet application.")
return editor
def setEditorData(self, editor, index):
"""
Sets the editor's value based on the model's data.
"""
data = index.model().data(index, Qt.ItemDataRole.EditRole)
try:
value = float(data.replace(",", ".")) if data else 0.0
except ValueError:
value = 0.0
editor.setValue(value)
def setModelData(self, spinBox, model, index):
"""
Updates the model with the editor's current value after validation.
"""
spinBox.interpretText()
text = spinBox.text().replace(",", ".")
try:
value = float(text)
except ValueError:
QMessageBox.warning(None, "Invalid Input", "Invalid number format.")
return
if not 0.00 <= value <= 5.00:
QMessageBox.warning(None, "Invalid Input", "Rating must be between 0.00 and 5.00.")
return
else:
formatted_value = "{:.2f}".format(value)
model.setData(index, formatted_value, Qt.ItemDataRole.EditRole)
def updateEditorGeometry(self, editor, option, index):
"""
Sets the editor's geometry.
"""
editor.setGeometry(option.rect)
class SearchHighlightDelegate(QStyledItemDelegate):
"""
A delegate that highlights search matches within table cells.
"""
def __init__(self, parent=None, highlight_color=Qt.GlobalColor.darkYellow):
super().__init__(parent)
self.search_text = ""
self.highlight_color = highlight_color
def set_search_text(self, text):
"""
Updates the search text and triggers a repaint.
"""
self.search_text = text.lower()
self.parent().viewport().update()
def paint(self, painter, option, index):
try:
painter.save()
parent = self.parent()
if parent is None:
logging.error("Delegate parent is None")
super().paint(painter, option, index)
return
# Draw the background
parent.style().drawPrimitive(
QStyle.PrimitiveElement.PE_PanelItemViewItem, option, painter, parent
)
# Get the cell text
data = index.data(Qt.ItemDataRole.DisplayRole)
if not data:
# Handle QLabel cells (e.g., album names with hyperlinks)
widget = parent.cellWidget(index.row(), index.column())
if isinstance(widget, QLabel):
data = strip_html_tags(widget.text())
if data:
data_lower = data.lower()
if self.search_text and self.search_text in data_lower:
# Prepare to draw the text with highlighted matches
painter.setClipRect(option.rect)
text_rect = option.rect.adjusted(5, 0, -5, 0)
# Set up font metrics
fm = painter.fontMetrics()
text_height = fm.height()
x = text_rect.left()
y = text_rect.top() + (text_rect.height() - text_height) / 2
# Split the text into segments
segments = []
start = 0
while True:
idx = data_lower.find(self.search_text, start)
if idx == -1:
segments.append((data[start:], False))
break
if idx > start:
segments.append((data[start:idx], False))
segments.append((data[idx:idx+len(self.search_text)], True))
start = idx + len(self.search_text)
# Draw each segment
for segment, is_match in segments:
segment_width = fm.horizontalAdvance(segment)
segment_rect = QRectF(x, y, segment_width, text_height)
if is_match:
painter.fillRect(segment_rect, self.highlight_color)
painter.setPen(option.palette.color(QPalette.ColorRole.WindowText))
painter.drawText(QPointF(x, y + fm.ascent()), segment)
x += segment_width
else:
# No matches, draw text normally
super().paint(painter, option, index)
else:
# No data, draw normally
super().paint(painter, option, index)
except Exception as e:
logging.error(f"Error in SearchHighlightDelegate.paint: {e}")
finally:
painter.restore()
class GenreSearchDelegate(QStyledItemDelegate):
"""
A delegate specifically for genre columns that highlights search matches.
"""
def __init__(self, items, parent=None, highlight_color=Qt.GlobalColor.darkYellow):
super().__init__(parent)
self.items = items
self.search_text = ""
self.highlight_color = highlight_color
def set_search_text(self, text):
"""
Updates the search text and triggers a repaint.
"""
self.search_text = text.lower()
self.parent().viewport().update()
def createEditor(self, parent, option, index):
"""
Creates a QComboBox editor with autocomplete for genre selection.
"""
comboBox = QComboBox(parent)
comboBox.setEditable(True)
comboBox.addItems(self.items)
comboBox.setInsertPolicy(QComboBox.InsertPolicy.NoInsert)
# Create the completer and set it to be case insensitive
completer = QCompleter(self.items, comboBox)
completer.setFilterMode(Qt.MatchFlag.MatchContains)
completer.setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)
comboBox.setCompleter(completer)
# Apply the dark background style to the completer popup
completer.popup().setStyleSheet("background-color: #2D2D30; color: white;")
comboBox.setStyleSheet("background-color: #2D2D30; color: white;")
# Connect the 'currentIndexChanged' signal to commit data and close editor
comboBox.currentIndexChanged.connect(self.commitAndClose)
return comboBox
def commitAndClose(self):
"""
Commits the data from the editor to the model and closes the editor.
"""
editor = self.sender() # Get the editor that sent the signal
self.commitData.emit(editor)
self.closeEditor.emit(editor, QStyledItemDelegate.EndEditHint.NoHint)
def setEditorData(self, editor, index):
"""
Sets the editor's current value based on the model's data.
"""
value = index.model().data(index, Qt.ItemDataRole.EditRole)
idx = editor.findText(value, Qt.MatchFlag.MatchFixedString)
editor.setCurrentIndex(idx if idx >= 0 else -1)
def setModelData(self, editor, model, index):
"""
Updates the model with the editor's current value.
"""
model.setData(index, editor.currentText(), Qt.ItemDataRole.EditRole)
def updateEditorGeometry(self, editor, option, index):
"""
Sets the editor's geometry.
"""
editor.setGeometry(option.rect)
def paint(self, painter, option, index):
"""
Overrides the paint method to highlight search matches.
"""
try:
painter.save()
parent = self.parent()
if parent is None:
logging.error("Delegate parent is None")
super().paint(painter, option, index)
return
# Draw the background
parent.style().drawPrimitive(
QStyle.PrimitiveElement.PE_PanelItemViewItem, option, painter, parent
)
# Get the cell text
data = index.data(Qt.ItemDataRole.DisplayRole)
if not data:
# Handle QLabel cells (e.g., album names with hyperlinks)
widget = parent.cellWidget(index.row(), index.column())
if isinstance(widget, QLabel):
data = strip_html_tags(widget.text())
if data:
data_lower = data.lower()
if self.search_text and self.search_text in data_lower:
# Prepare to draw the text with highlighted matches
painter.setClipRect(option.rect)
text_rect = option.rect.adjusted(5, 0, -5, 0)
# Set up font metrics
fm = painter.fontMetrics()
text_height = fm.height()
x = text_rect.left()
y = text_rect.top() + (text_rect.height() - text_height) / 2
# Split the text into segments
segments = []
start = 0
while True:
idx = data_lower.find(self.search_text, start)
if idx == -1:
segments.append((data[start:], False))
break
if idx > start:
segments.append((data[start:idx], False))
segments.append((data[idx:idx+len(self.search_text)], True))
start = idx + len(self.search_text)
# Draw each segment
for segment, is_match in segments:
segment_width = fm.horizontalAdvance(segment)
segment_rect = QRectF(x, y, segment_width, text_height)
if is_match:
painter.fillRect(segment_rect, self.highlight_color)
painter.setPen(option.palette.color(QPalette.ColorRole.WindowText))
painter.drawText(QPointF(x, y + fm.ascent()), segment)
x += segment_width
else:
# No matches, draw text normally
super().paint(painter, option, index)
else:
# No data, draw normally
super().paint(painter, option, index)
except Exception as e:
logging.error(f"Error in GenreSearchDelegate.paint: {e}")
finally:
painter.restore()