-
Notifications
You must be signed in to change notification settings - Fork 5
/
settingsunit.py
634 lines (532 loc) · 23.1 KB
/
settingsunit.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# Copyright (C) 2013 Riverbank Computing Limited.
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
"""PySide6 port of the widgets/tools/settingseditor example from Qt v5.x
Back ported to PyQt5 and adapted by AC Chamberlain"""
import sys
from PyQt5.QtCore import (QByteArray, QDate, QDateTime, QDir, QEvent, QPoint,
QRect, QRegularExpression, QSettings, QSize, QTime, QTimer, Qt, pyqtSlot as Slot)
from PyQt5.QtGui import (QColor, QIcon, QIntValidator, QDoubleValidator, QRegularExpressionValidator, QValidator)
from PyQt5.QtWidgets import (QAbstractItemView, QCheckBox, QDialog, QDialogButtonBox, QGridLayout,
QHeaderView, QItemDelegate, QLineEdit, QStyle, QSpinBox, QStyleOptionViewItem,
QTableWidgetItem, QTreeWidget, QTreeWidgetItem)
def set_default_settings(settings):
settings.beginGroup('General')
if not settings.contains('Logo'):
settings.setValue('Logo', '')
if not settings.contains('Metadata'):
settings.setValue('Metadata', {})
settings.endGroup()
settings.beginGroup('3D Phantom')
if not settings.contains('Type'):
settings.setValue('Type', 'CatPhan604')
if not settings.contains('HU Tolerance'):
settings.setValue('HU Tolerance', '40')
if not settings.contains('Thickness Tolerance'):
settings.setValue('Thickness Tolerance', '0.2')
if not settings.contains('Scaling Tolerance'):
settings.setValue('Scaling Tolerance', '1')
settings.endGroup()
settings.beginGroup('Picket Fence')
if not settings.contains('MLC Type'):
settings.setValue('MLC Type', 'HD Millennium')
if not settings.contains('Leaf Tolerance'):
settings.setValue('Leaf Tolerance', '0.5')
if not settings.contains('Leaf Action'):
settings.setValue('Leaf Action', '0.25')
if not settings.contains('Number of pickets'):
settings.setValue('Number of pickets', '10')
if not settings.contains('Apply median filter'):
settings.setValue('Apply median filter', 'False')
settings.endGroup()
settings.beginGroup('Star shot')
if not settings.contains('DPI'):
settings.setValue('DPI', '76')
if not settings.contains('SID'):
settings.setValue('SID', '1000')
if not settings.contains('Normalised analysis radius'):
settings.setValue('Normalised analysis radius', '0.85')
if not settings.contains('Tolerance'):
settings.setValue('Tolerance', '1')
if not settings.contains('Recursive analysis'):
settings.setValue('Recursive analysis', 'False')
settings.endGroup()
settings.beginGroup('VMAT')
if not settings.contains('Test type'):
settings.setValue('Test type', 'DRGS')
if not settings.contains('Tolerance'):
settings.setValue('Tolerance', '1.5')
settings.endGroup()
settings.beginGroup('Winston-Lutz')
if not settings.contains('BB Size'):
settings.setValue('BB Size', '5')
if not settings.contains('Open field'):
settings.setValue('Open field', 'False')
if not settings.contains('Low density BB'):
settings.setValue('Low density BB', 'False')
settings.endGroup()
settings.beginGroup('2D Phantom')
if not settings.contains('Type'):
settings.setValue('Type', 'Leeds TOR')
if not settings.contains('Low contrast threshold'):
settings.setValue('Low contrast threshold', '0.1')
if not settings.contains('High contrast threshold'):
settings.setValue('High contrast threshold', '0.5')
if not settings.contains('Force image inversion'):
settings.setValue('Force image inversion', 'False')
if not settings.contains('SSD'):
settings.setValue('SSD', '1000')
settings.endGroup()
settings.beginGroup('Gamma Analysis')
if not settings.contains('Dose to agreement'):
settings.setValue('Dose to agreement', '2')
if not settings.contains('Distance to agreement'):
settings.setValue('Distance to agreement', '2')
if not settings.contains('Gamma cap'):
settings.setValue('Gamma cap', '2')
if not settings.contains('Global dose'):
settings.setValue('Global dose', 'True')
if not settings.contains('Dose threshold'):
settings.setValue('Dose threshold', '5')
settings.endGroup()
settings.beginGroup('Window')
if not settings.contains('Position'):
settings.setValue('Position', QPoint(100, 200))
if not settings.contains('Size'):
settings.setValue('Size', QSize(678, 682))
settings.endGroup()
class TypeChecker:
def __init__(self, parent=None):
self.bool_exp = QRegularExpression('^(true)|(false)$')
assert self.bool_exp.isValid()
self.bool_exp.setPatternOptions(QRegularExpression.CaseInsensitiveOption)
self.byteArray_exp = QRegularExpression(r'^[\x00-\xff]*$')
assert self.byteArray_exp.isValid()
self.char_exp = QRegularExpression('^.$')
assert self.char_exp.isValid()
pattern = r'^[+-]?\d+$'
self.int_exp = QRegularExpression(pattern)
assert self.int_exp.isValid()
pattern = r'^\(([0-9]*),([0-9]*),([0-9]*),([0-9]*)\)$'
self.color_exp = QRegularExpression(pattern)
assert self.color_exp.isValid()
pattern = r'^\((-?[0-9]*),(-?[0-9]*)\)$'
self.point_exp = QRegularExpression(pattern)
assert self.point_exp.isValid()
pattern = r'^\((-?[0-9]*),(-?[0-9]*),(-?[0-9]*),(-?[0-9]*)\)$'
self.rect_exp = QRegularExpression(pattern)
assert self.rect_exp.isValid()
self.size_exp = QRegularExpression(self.point_exp)
date_pattern = '([0-9]{,4})-([0-9]{,2})-([0-9]{,2})'
self.date_exp = QRegularExpression(f'^{date_pattern}$')
assert self.date_exp.isValid()
time_pattern = '([0-9]{,2}):([0-9]{,2}):([0-9]{,2})'
self.time_exp = QRegularExpression(f'^{time_pattern}$')
assert self.time_exp.isValid()
pattern = f'^{date_pattern}T{time_pattern}$'
self.dateTime_exp = QRegularExpression(pattern)
assert self.dateTime_exp.isValid()
def type_from_text(self, text):
if self.bool_exp.match(text).hasMatch():
return bool
if self.int_exp.match(text).hasMatch():
return int
return None
def create_validator(self, value, parent):
if isinstance(value, bool):
return QRegularExpressionValidator(self.bool_exp, parent)
if isinstance(value, float):
return QDoubleValidator(parent)
if isinstance(value, int):
return QIntValidator(parent)
if isinstance(value, QByteArray):
return QRegularExpressionValidator(self.byteArray_exp, parent)
if isinstance(value, QColor):
return QRegularExpressionValidator(self.color_exp, parent)
if isinstance(value, QDate):
return QRegularExpressionValidator(self.date_exp, parent)
if isinstance(value, QDateTime):
return QRegularExpressionValidator(self.dateTime_exp, parent)
if isinstance(value, QTime):
return QRegularExpressionValidator(self.time_exp, parent)
if isinstance(value, QPoint):
return QRegularExpressionValidator(self.point_exp, parent)
if isinstance(value, QRect):
return QRegularExpressionValidator(self.rect_exp, parent)
if isinstance(value, QSize):
return QRegularExpressionValidator(self.size_exp, parent)
return None
def from_string(self, text, original_value):
if isinstance(original_value, QColor):
match = self.color_exp.match(text)
return QColor(min(int(match.captured(1)), 255),
min(int(match.captured(2)), 255),
min(int(match.captured(3)), 255),
min(int(match.captured(4)), 255))
if isinstance(original_value, QDate):
value = QDate.fromString(text, Qt.ISODate)
return value if value.isValid() else None
if isinstance(original_value, QDateTime):
value = QDateTime.fromString(text, Qt.ISODate)
return value if value.isValid() else None
if isinstance(original_value, QTime):
value = QTime.fromString(text, Qt.ISODate)
return value if value.isValid() else None
if isinstance(original_value, QPoint):
match = self.point_exp.match(text)
return QPoint(int(match.captured(1)),
int(match.captured(2)))
if isinstance(original_value, QRect):
match = self.rect_exp.match(text)
return QRect(int(match.captured(1)),
int(match.captured(2)),
int(match.captured(3)),
int(match.captured(4)))
if isinstance(original_value, QSize):
match = self.size_exp.match(text)
return QSize(int(match.captured(1)),
int(match.captured(2)))
if isinstance(original_value, list):
return text.split(',')
if isinstance(original_value, dict):
text = text.strip('{}').replace('\"', '').replace('\'', '')
pairs = text.split(',')
return {key.strip(' '): value.strip(' ') for key, value in (pair.split(':') for pair in pairs)}
return type(original_value)(text)
class Settings(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.gridLayout = QGridLayout()
self.setLayout(self.gridLayout)
self.gridLayout.setObjectName("gridLayout")
self.settings_tree = SettingsTree()
self.gridLayout.addWidget(self.settings_tree, 0, 0, 1, 1)
self.setWindowTitle("Settings Editor")
self.resize(500, 600)
settings = QSettings()
self.set_settings_object(settings)
def set_settings_object(self, settings):
settings.setFallbacksEnabled(True)
self.settings_tree.set_settings_object(settings)
nice_name = QDir.fromNativeSeparators(settings.fileName())
nice_name = nice_name.split('/')[-1]
if not settings.isWritable():
nice_name += " (read only)"
self.setWindowTitle(f"{nice_name} - Settings Editor")
def format(self):
if self.format_combo.currentIndex() == 0:
return QSettings.NativeFormat
else:
return QSettings.IniFormat
def scope(self):
if self.scope_cCombo.currentIndex() == 0:
return QSettings.UserScope
else:
return QSettings.SystemScope
def organization(self):
return self.organization_combo.currentText()
def application(self):
if self.application_combo.currentText() == "Any":
return ''
return self.application_combo.currentText()
def update_locations(self):
self.locations_table.setUpdatesEnabled(False)
self.locations_table.setRowCount(0)
for i in range(2):
if i == 0:
if self.scope() == QSettings.SystemScope:
continue
actual_scope = QSettings.UserScope
else:
actual_scope = QSettings.SystemScope
for j in range(2):
if j == 0:
if not self.application():
continue
actual_application = self.application()
else:
actual_application = ''
settings = QSettings(self.format(), actual_scope,
self.organization(), actual_application)
row = self.locations_table.rowCount()
self.locations_table.setRowCount(row + 1)
item0 = QTableWidgetItem()
item0.setText(settings.fileName())
item1 = QTableWidgetItem()
disable = not (settings.childKeys() or settings.childGroups())
if row == 0:
if settings.isWritable():
item1.setText("Read-write")
disable = False
else:
item1.setText("Read-only")
self.button_box.button(QDialogButtonBox.Ok).setDisabled(disable)
else:
item1.setText("Read-only fallback")
if disable:
item0.setFlags(item0.flags() & ~Qt.ItemIsEnabled)
item1.setFlags(item1.flags() & ~Qt.ItemIsEnabled)
self.locations_table.setItem(row, 0, item0)
self.locations_table.setItem(row, 1, item1)
self.locations_table.setUpdatesEnabled(True)
class SettingsTree(QTreeWidget):
def __init__(self, parent=None):
super().__init__(parent)
self._type_checker = TypeChecker()
self.setItemDelegate(VariantDelegate(self._type_checker, self))
self.setHeaderLabels(("Setting", "Type", "Value"))
self.header().setSectionResizeMode(0, QHeaderView.Stretch)
self.header().setSectionResizeMode(2, QHeaderView.Stretch)
self.settings = None
self.refresh_timer = QTimer()
self.refresh_timer.setInterval(2000)
self.auto_refresh = False
self.group_icon = QIcon()
style = self.style()
self.group_icon.addPixmap(style.standardPixmap(QStyle.SP_DirClosedIcon),
QIcon.Normal, QIcon.Off)
self.group_icon.addPixmap(style.standardPixmap(QStyle.SP_DirOpenIcon),
QIcon.Normal, QIcon.On)
self.key_icon = QIcon()
self.key_icon.addPixmap(style.standardPixmap(QStyle.SP_FileIcon))
self.refresh_timer.timeout.connect(self.maybe_refresh)
def set_settings_object(self, settings):
self.settings = settings
self.clear()
if self.settings is not None:
self.settings.setParent(self)
self.refresh()
if self.auto_refresh:
self.refresh_timer.start()
else:
self.refresh_timer.stop()
def sizeHint(self):
return QSize(800, 600)
@Slot(bool)
def set_auto_refresh(self, autoRefresh):
self.auto_refresh = autoRefresh
if self.settings is not None:
if self.auto_refresh:
self.maybe_refresh()
self.refresh_timer.start()
else:
self.refresh_timer.stop()
@Slot(bool)
def set_fallbacks_enabled(self, enabled):
if self.settings is not None:
self.settings.setFallbacksEnabled(enabled)
self.refresh()
@Slot()
def maybe_refresh(self):
if self.state() != QAbstractItemView.EditingState:
self.refresh()
@Slot()
def refresh(self):
if self.settings is None:
return
# The signal might not be connected.
try:
self.itemChanged.disconnect(self.update_setting)
except:
pass
self.settings.sync()
self.update_child_items(None)
self.itemChanged.connect(self.update_setting)
def event(self, event):
if event.type() == QEvent.WindowActivate:
if self.isActiveWindow() and self.auto_refresh:
self.maybe_refresh()
return super(SettingsTree, self).event(event)
def update_setting(self, item):
key = item.text(0)
ancestor = item.parent()
while ancestor:
key = ancestor.text(0) + '/' + key
ancestor = ancestor.parent()
d = item.data(2, Qt.UserRole)
self.settings.setValue(key, item.data(2, Qt.UserRole))
if self.auto_refresh:
self.refresh()
def update_child_items(self, parent):
divider_index = 0
for group in self.settings.childGroups():
child_index = self.find_child(parent, group, divider_index)
if child_index != -1:
child = self.child_at(parent, child_index)
child.setText(1, '')
child.setText(2, '')
child.setData(2, Qt.UserRole, None)
self.move_item_forward(parent, child_index, divider_index)
else:
child = self.create_item(group, parent, divider_index)
child.setIcon(0, self.group_icon)
divider_index += 1
self.settings.beginGroup(group)
self.update_child_items(child)
self.settings.endGroup()
for key in self.settings.childKeys():
child_index = self.find_child(parent, key, 0)
if child_index == -1 or child_index >= divider_index:
if child_index != -1:
child = self.child_at(parent, child_index)
for i in range(child.childCount()):
self.delete_item(child, i)
self.move_item_forward(parent, child_index, divider_index)
else:
child = self.create_item(key, parent, divider_index)
child.setIcon(0, self.key_icon)
divider_index += 1
else:
child = self.child_at(parent, child_index)
value = self.settings.value(key)
if value is None:
child.setText(1, 'Invalid')
else:
# Try to convert to type unless a QByteArray is received
if isinstance(value, str):
value_type = self._type_checker.type_from_text(value)
if value_type:
value = self.settings.value(key, type=value_type)
child.setText(1, value.__class__.__name__)
child.setText(2, VariantDelegate.display_text(value))
child.setData(2, Qt.UserRole, value)
while divider_index < self.child_count(parent):
self.delete_item(parent, divider_index)
def create_item(self, text, parent, index):
after = None
if index != 0:
after = self.child_at(parent, index - 1)
if parent is not None:
item = QTreeWidgetItem(parent, after)
else:
item = QTreeWidgetItem(self, after)
item.setText(0, text)
item.setFlags(item.flags() | Qt.ItemIsEditable)
return item
def delete_item(self, parent, index):
if parent is not None:
item = parent.takeChild(index)
else:
item = self.takeTopLevelItem(index)
del item
def child_at(self, parent, index):
if parent is not None:
return parent.child(index)
else:
return self.topLevelItem(index)
def child_count(self, parent):
if parent is not None:
return parent.childCount()
else:
return self.topLevelItemCount()
def find_child(self, parent, text, startIndex):
for i in range(self.child_count(parent)):
if self.child_at(parent, i).text(0) == text:
return i
return -1
def move_item_forward(self, parent, oldIndex, newIndex):
for int in range(oldIndex - newIndex):
self.delete_item(parent, newIndex)
class VariantDelegate(QItemDelegate):
def __init__(self, type_checker, parent=None):
super().__init__(parent)
self._type_checker = type_checker
def paint(self, painter, option, index):
if index.column() == 2:
value = index.model().data(index, Qt.UserRole)
if not self.is_supported_type(value):
my_option = QStyleOptionViewItem(option)
my_option.state &= ~QStyle.State_Enabled
super(VariantDelegate, self).paint(painter, my_option, index)
return
super(VariantDelegate, self).paint(painter, option, index)
def createEditor(self, parent, option, index):
if index.column() != 2:
return None
original_value = index.model().data(index, Qt.UserRole)
if not self.is_supported_type(original_value):
return None
editor = None
if isinstance(original_value, bool):
editor = QCheckBox(parent)
elif isinstance(original_value, int):
editor = QSpinBox(parent)
editor.setRange(-32767, 32767)
else:
editor = QLineEdit(parent)
editor.setFrame(False)
validator = self._type_checker.create_validator(original_value, editor)
if validator:
editor.setValidator(validator)
return editor
def setEditorData(self, editor, index):
if not editor:
return
value = index.model().data(index, Qt.UserRole)
if isinstance(editor, QCheckBox):
editor.setCheckState(Qt.Checked if value else Qt.Unchecked)
elif isinstance(editor, QSpinBox):
editor.setValue(value)
else:
editor.setText(self.display_text(value))
def value_from_lineedit(self, lineedit, model, index):
if not lineedit.isModified():
return None
text = lineedit.text()
validator = lineedit.validator()
if validator is not None:
state, text, _ = validator.validate(text, 0)
if state != QValidator.Acceptable:
return None
original_value = index.model().data(index, Qt.UserRole)
return self._type_checker.from_string(text, original_value)
def setModelData(self, editor, model, index):
value = None
if isinstance(editor, QCheckBox):
value = editor.checkState() == Qt.Checked
elif isinstance(editor, QSpinBox):
value = editor.value()
else:
value = self.value_from_lineedit(editor, model, index)
if value is not None:
model.setData(index, value, Qt.UserRole)
model.setData(index, self.display_text(value), Qt.DisplayRole)
@staticmethod
def is_supported_type(value):
return isinstance(value, (bool, float, int, QByteArray, str, QColor,
QDate, QDateTime, QTime, QPoint, QRect,
QSize, list, dict))
@staticmethod
def display_text(value):
if isinstance(value, str):
return value
if isinstance(value, bool):
return '✓' if value else '☐'
if isinstance(value, (int, float, QByteArray)):
return str(value)
if isinstance(value, QColor):
(r, g, b, a) = (value.red(), value.green(), value.blue(), value.alpha())
return f'({r},{g},{b},{a})'
if isinstance(value, (QDate, QDateTime, QTime)):
return value.toString(Qt.ISODate)
if isinstance(value, QPoint):
x = value.x()
y = value.y()
return f'({x},{y})'
if isinstance(value, QRect):
x = value.x()
y = value.y()
w = value.width()
h = value.height()
return f'({x},{y},{w},{h})'
if isinstance(value, QSize):
w = value.width()
h = value.height()
return f'({w},{h})'
if isinstance(value, list):
return ','.join(map(repr, value))
if isinstance(value, dict):
return str(value)
if value is None:
return '<Invalid>'
return f'<{value}>'