-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_block.py
277 lines (224 loc) · 9.04 KB
/
config_block.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
from dataclasses import dataclass
from typing import Sequence
from PySide6 import QtCore, QtWidgets, QtGui
# This is basically a dictionary wrapper that keeps track of which fields have been used, to help debug.
class ConfigDict(dict):
def __init__(self):
super().__init__()
self._used_fields = set()
def __getitem__(self, key):
self._used_fields.add(key)
return super().__getitem__(key)
def unused_fields(self) -> set[str]:
return set(super().keys()) - self._used_fields
def unused_fields_recursive(self, prefix='') -> list[str]:
ret = []
unused = self.unused_fields()
for key in self.keys():
val = super().__getitem__(key)
if isinstance(val, ConfigDict):
ret.extend(val.unused_fields_recursive(prefix=f'{key}/'))
elif key in unused:
ret.append(f'{prefix}{key}')
return ret
def reset_usage_tracker(self):
self._used_fields = set()
def save_to_settings(self, settings, group=None):
if group is not None:
settings.beginGroup(group)
for k, v in self.items():
if isinstance(v, ConfigDict):
v.save_to_settings(settings, group=k)
else:
settings.setValue(k, v)
if group is not None:
settings.endGroup()
@dataclass
class ConfigBlockElement:
key: str
display_name: str
def __init__(self, key, display_name):
super().__init__()
self.key = key
self.display_name = display_name
def value(self):
raise NotImplementedError()
def set_value(self, new_value):
raise NotImplementedError()
class ConfigBool(ConfigBlockElement, QtWidgets.QCheckBox):
updated = QtCore.Signal()
def __init__(self, key: str, display_name: str, default_value: bool):
super().__init__(key=key, display_name=display_name)
self.setText(display_name)
self.setChecked(default_value)
self.checkStateChanged.connect(self.updated)
def value(self):
return self.isChecked()
def set_value(self, new_value):
self.setChecked(bool(new_value))
class ConfigFloat(ConfigBlockElement, QtWidgets.QVBoxLayout):
updated = QtCore.Signal()
def __init__(self, key: str, display_name: str, default_value: float, min_value: float, max_value: float, resolution: int = 0.1, places: int = 2):
super().__init__(key=key, display_name=display_name)
self._min_value = min_value
self._value = default_value
self._steps = round((max_value - min_value) / resolution)
self._places = places
name_and_val_h_layout = QtWidgets.QHBoxLayout()
name_and_val_h_layout.addWidget(QtWidgets.QLabel(display_name))
name_and_val_h_layout.addStretch(1)
self._value_label = QtWidgets.QLabel('0.0')
name_and_val_h_layout.addWidget(self._value_label)
self.addLayout(name_and_val_h_layout)
self._slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
self._slider.setMinimum(0)
self._slider.setMaximum(self._steps)
self._value_range = max_value - min_value
default_value_steps = round((default_value - min_value) / self._value_range * self._steps)
self._slider.setValue(default_value_steps)
self._slider.valueChanged.connect(self.update_value)
self.update_value()
self.addWidget(self._slider)
@QtCore.Slot()
def update_value(self):
new_value = self._slider.value() / self._steps * self._value_range + self._min_value
self._value_label.setText(f'{new_value:.{self._places}f}')
self._value = new_value
self.updated.emit()
def value(self):
return self._value
def set_value(self, new_value):
self._value_label.setText(f'{new_value:.{self._places}f}')
self._value = new_value
value_steps = round((float(new_value) - self._min_value) / self._value_range * self._steps)
self._slider.setValue(value_steps)
class ConfigInt(ConfigBlockElement, QtWidgets.QVBoxLayout):
updated = QtCore.Signal()
def __init__(self, key: str, display_name: str, default_value: int, min_value: int, max_value: int):
super().__init__(key=key, display_name=display_name)
name_and_val_h_layout = QtWidgets.QHBoxLayout()
name_and_val_h_layout.addWidget(QtWidgets.QLabel(display_name))
name_and_val_h_layout.addStretch(1)
self._value_label = QtWidgets.QLabel('0')
name_and_val_h_layout.addWidget(self._value_label)
self.addLayout(name_and_val_h_layout)
self._slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
self._slider.setMinimum(min_value)
self._slider.setMaximum(max_value)
self._slider.setValue(default_value)
self._slider.valueChanged.connect(self.update_value)
self.update_value()
self.addWidget(self._slider)
@QtCore.Slot()
def update_value(self):
new_value = self._slider.value()
self._value_label.setText(f'{new_value}')
self.updated.emit()
def value(self):
return self._slider.value()
def set_value(self, new_value):
self._value_label.setText(f'{new_value}')
self._slider.setValue(int(new_value))
class ConfigEnum(ConfigBlockElement, QtWidgets.QHBoxLayout):
updated = QtCore.Signal()
def __init__(self, key: str, display_name: str, default_index: int, options: list[tuple[str, str]]):
super().__init__(key=key, display_name=display_name)
assert default_index >= 0 and default_index < len(options)
self.addWidget(QtWidgets.QLabel(display_name))
self._combobox = QtWidgets.QComboBox()
for enum_option_display, enum_option_val in options:
self._combobox.addItem(enum_option_display, enum_option_val)
self._combobox.setCurrentIndex(default_index)
self.addWidget(self._combobox)
self._combobox.currentIndexChanged.connect(self.updated)
def value(self):
return self._combobox.currentData()
def set_value(self, new_value):
find_ret = self._combobox.findData(str(new_value))
if find_ret != -1:
self._combobox.setCurrentIndex(find_ret)
class ConfigPath(ConfigBlockElement, QtWidgets.QVBoxLayout):
updated = QtCore.Signal()
def __init__(self, key: str, display_name: str, default_value: str = [], path_filter: str = ''):
super().__init__(key=key, display_name=display_name)
self._display_name = display_name
self._filter = path_filter
self.addWidget(QtWidgets.QLabel(display_name))
h_layout = QtWidgets.QHBoxLayout()
self._line_edit = QtWidgets.QLineEdit()
open_button = QtWidgets.QPushButton('Open')
open_button.clicked.connect(self.open_file_dialog)
h_layout.addWidget(self._line_edit)
h_layout.addWidget(open_button)
self.addLayout(h_layout)
@QtCore.Slot()
def open_file_dialog(self):
file_name, _ = QtWidgets.QFileDialog.getOpenFileName(None, self._display_name, '', self._filter)
if file_name:
self._line_edit.setText(file_name)
self.updated.emit()
def value(self):
return self._line_edit.text()
def set_value(self, new_value):
self._line_edit.setText(str(new_value))
@dataclass
class ConfigBlockDescription(ConfigBlockElement):
text: str
@dataclass
class ConfigBlockSpec:
block_name: str
display_name: str
checkable: bool
elements: Sequence[ConfigBlockElement]
class ConfigBlock(QtWidgets.QGroupBox):
updated = QtCore.Signal()
def __init__(self, config_block_spec: ConfigBlockSpec, parent: QtWidgets.QWidget = None, settings: QtCore.QSettings | None = None):
super().__init__(config_block_spec.display_name, parent)
self.setCheckable(config_block_spec.checkable)
v_layout = QtWidgets.QVBoxLayout(self)
self._key = config_block_spec.block_name
self._elements = config_block_spec.elements
if settings is not None:
settings.beginGroup(self._key)
if settings.contains('enabled'):
self.setChecked(bool(settings.value('enabled')))
for element in config_block_spec.elements:
if isinstance(element, ConfigBool):
v_layout.addWidget(element)
element.updated.connect(self.updated)
elif isinstance(element, ConfigInt):
v_layout.addLayout(element)
element.updated.connect(self.updated)
elif isinstance(element, ConfigFloat):
v_layout.addLayout(element)
element.updated.connect(self.updated)
elif isinstance(element, ConfigEnum):
v_layout.addLayout(element)
element.updated.connect(self.updated)
elif isinstance(element, ConfigPath):
v_layout.addLayout(element)
element.updated.connect(self.updated)
elif isinstance(element, ConfigBlockDescription):
label = QtWidgets.QLabel(element.text)
label.setWordWrap(True)
v_layout.addWidget(label)
else:
raise ValueError(f'What do we do with a {option}?')
if not isinstance(element, ConfigBlockDescription) and settings is not None:
if settings.contains(element.key):
element.set_value(settings.value(element.key))
if settings is not None:
settings.endGroup()
self.clicked.connect(self.updated)
def to_config_dict(self) -> ConfigDict:
config_dict = ConfigDict()
if self.isCheckable():
config_dict['enabled'] = self.isChecked()
for element in self._elements:
if isinstance(element, ConfigBlockDescription):
continue
else:
config_dict[element.key] = element.value()
return config_dict
def name(self) -> str:
return self._key