led-controller-version-3-effect-creation 3.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- led_controller_version_3_effect_creation-3.0.2.dist-info/METADATA +144 -0
- led_controller_version_3_effect_creation-3.0.2.dist-info/RECORD +25 -0
- led_controller_version_3_effect_creation-3.0.2.dist-info/WHEEL +4 -0
- led_controller_version_3_effect_creation-3.0.2.dist-info/entry_points.txt +3 -0
- lefx/effect_creation/__init__.py +54 -0
- lefx/effect_creation/build.py +217 -0
- lefx/effect_creation/cli.py +156 -0
- lefx/effect_creation/imports.py +143 -0
- lefx/effect_creation/scaffold.py +330 -0
- lefx/effect_creation/source.py +258 -0
- lefx/effect_creation/studio/__init__.py +95 -0
- lefx/effect_creation/studio/app.py +169 -0
- lefx/effect_creation/studio/authoring.py +196 -0
- lefx/effect_creation/studio/blueprint.py +759 -0
- lefx/effect_creation/studio/calibrate.py +185 -0
- lefx/effect_creation/studio/calibration_page.py +420 -0
- lefx/effect_creation/studio/catalogue.py +219 -0
- lefx/effect_creation/studio/parameters.py +730 -0
- lefx/effect_creation/studio/preset_dialog.py +188 -0
- lefx/effect_creation/studio/project.py +302 -0
- lefx/effect_creation/studio/ring.py +172 -0
- lefx/effect_creation/studio/session.py +307 -0
- lefx/effect_creation/studio/source_editor.py +902 -0
- lefx/effect_creation/studio/window.py +656 -0
- lefx/effect_creation/validate.py +263 -0
|
@@ -0,0 +1,730 @@
|
|
|
1
|
+
"""Editors built from a definition's declared schema.
|
|
2
|
+
|
|
3
|
+
A ``ParamDefinition`` already says everything an editor needs: the type, the
|
|
4
|
+
bounds, the unit, the allowed values, whether null is a value. So the studio
|
|
5
|
+
does not carry a form per effect — it carries one editor per *type*, and an
|
|
6
|
+
effect written tomorrow gets a full set of controls without this file changing.
|
|
7
|
+
|
|
8
|
+
That is the difference between a tester and a tool. A hand-written form knows
|
|
9
|
+
one catalogue; this knows the schema, which is the thing every catalogue is
|
|
10
|
+
made of.
|
|
11
|
+
|
|
12
|
+
Two rules run through all of it:
|
|
13
|
+
|
|
14
|
+
* **The schema decides, not the widget.** Bounds come from ``minimum`` and
|
|
15
|
+
``maximum``, choices from ``enum_values``, the suffix from ``unit``. Nothing
|
|
16
|
+
here invents a range, because a range invented here would disagree with the
|
|
17
|
+
one the definition is validated against.
|
|
18
|
+
* **Editing is continuous.** Every editor reports on each change rather than on
|
|
19
|
+
commit, because the point is to watch the ring while you drag.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
from typing import Any, Callable, Mapping
|
|
25
|
+
|
|
26
|
+
from PySide6.QtCore import Qt, Signal
|
|
27
|
+
from PySide6.QtGui import QColor
|
|
28
|
+
from PySide6.QtWidgets import (
|
|
29
|
+
QCheckBox,
|
|
30
|
+
QColorDialog,
|
|
31
|
+
QComboBox,
|
|
32
|
+
QDial,
|
|
33
|
+
QDoubleSpinBox,
|
|
34
|
+
QFormLayout,
|
|
35
|
+
QHBoxLayout,
|
|
36
|
+
QLabel,
|
|
37
|
+
QLineEdit,
|
|
38
|
+
QPushButton,
|
|
39
|
+
QSizePolicy,
|
|
40
|
+
QSlider,
|
|
41
|
+
QSpinBox,
|
|
42
|
+
QToolButton,
|
|
43
|
+
QVBoxLayout,
|
|
44
|
+
QWidget,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
from lefx.sdk import ParamDefinition, ParamType, format_color, parse_color
|
|
48
|
+
|
|
49
|
+
SLIDER_STEPS = 1000
|
|
50
|
+
"""How finely a float slider divides its range. Fine enough to look continuous,
|
|
51
|
+
coarse enough that the spin box beside it stays the precise way in."""
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _hex(color: int) -> str:
|
|
55
|
+
return f"#{color & 0xFFFFFF:06X}"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _qcolor(value: Any, fallback: int = 0x000000) -> QColor:
|
|
59
|
+
try:
|
|
60
|
+
return QColor(_hex(parse_color(value)))
|
|
61
|
+
except Exception:
|
|
62
|
+
return QColor(_hex(fallback))
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class ParameterEditor(QWidget):
|
|
66
|
+
"""One control for one declared parameter.
|
|
67
|
+
|
|
68
|
+
Subclasses implement :meth:`read` and :meth:`write` for their type; the
|
|
69
|
+
nullability and the change signal are handled once, here, because they mean
|
|
70
|
+
the same thing whatever the type is.
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
changed = Signal()
|
|
74
|
+
|
|
75
|
+
def __init__(self, parameter: ParamDefinition, parent: QWidget | None = None) -> None:
|
|
76
|
+
super().__init__(parent)
|
|
77
|
+
self.parameter = parameter
|
|
78
|
+
self._null: QCheckBox | None = None
|
|
79
|
+
self._body = QWidget(self)
|
|
80
|
+
|
|
81
|
+
layout = QHBoxLayout(self)
|
|
82
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
83
|
+
layout.setSpacing(6)
|
|
84
|
+
layout.addWidget(self._body, stretch=1)
|
|
85
|
+
|
|
86
|
+
if parameter.nullable:
|
|
87
|
+
# A nullable parameter has two states a person cares about — a value,
|
|
88
|
+
# and deliberately no value — and one control cannot show both. The
|
|
89
|
+
# box is the honest way to reach "null" without typing it.
|
|
90
|
+
self._null = QCheckBox("null")
|
|
91
|
+
self._null.setToolTip("Send no value for this parameter")
|
|
92
|
+
self._null.toggled.connect(self._on_null_toggled)
|
|
93
|
+
layout.addWidget(self._null)
|
|
94
|
+
|
|
95
|
+
self.build(QVBoxLayout(self._body))
|
|
96
|
+
|
|
97
|
+
# -- subclass contract --------------------------------------------------
|
|
98
|
+
|
|
99
|
+
def build(self, layout: QVBoxLayout) -> None: # pragma: no cover - abstract
|
|
100
|
+
raise NotImplementedError
|
|
101
|
+
|
|
102
|
+
def read(self) -> Any: # pragma: no cover - abstract
|
|
103
|
+
raise NotImplementedError
|
|
104
|
+
|
|
105
|
+
def write(self, value: Any) -> None: # pragma: no cover - abstract
|
|
106
|
+
raise NotImplementedError
|
|
107
|
+
|
|
108
|
+
# -- the shared part ----------------------------------------------------
|
|
109
|
+
|
|
110
|
+
def value(self) -> Any:
|
|
111
|
+
if self._null is not None and self._null.isChecked():
|
|
112
|
+
return None
|
|
113
|
+
return self.read()
|
|
114
|
+
|
|
115
|
+
def set_value(self, value: Any) -> None:
|
|
116
|
+
with_signals = self.blockSignals(True)
|
|
117
|
+
try:
|
|
118
|
+
if value is None and self._null is not None:
|
|
119
|
+
self._null.setChecked(True)
|
|
120
|
+
self._body.setEnabled(False)
|
|
121
|
+
return
|
|
122
|
+
if self._null is not None:
|
|
123
|
+
self._null.setChecked(False)
|
|
124
|
+
self._body.setEnabled(True)
|
|
125
|
+
if value is not None:
|
|
126
|
+
self.write(value)
|
|
127
|
+
finally:
|
|
128
|
+
self.blockSignals(with_signals)
|
|
129
|
+
|
|
130
|
+
def _on_null_toggled(self, checked: bool) -> None:
|
|
131
|
+
self._body.setEnabled(not checked)
|
|
132
|
+
self.changed.emit()
|
|
133
|
+
|
|
134
|
+
def _announce(self, *_: Any) -> None:
|
|
135
|
+
self.changed.emit()
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class BoolEditor(ParameterEditor):
|
|
139
|
+
def build(self, layout: QVBoxLayout) -> None:
|
|
140
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
141
|
+
self.box = QCheckBox(self.parameter.description or "")
|
|
142
|
+
self.box.toggled.connect(self._announce)
|
|
143
|
+
layout.addWidget(self.box)
|
|
144
|
+
|
|
145
|
+
def read(self) -> Any:
|
|
146
|
+
return self.box.isChecked()
|
|
147
|
+
|
|
148
|
+
def write(self, value: Any) -> None:
|
|
149
|
+
self.box.setChecked(bool(value))
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
class NumberEditor(ParameterEditor):
|
|
153
|
+
"""A spin box, plus a slider whenever the schema gives both bounds.
|
|
154
|
+
|
|
155
|
+
Without bounds a slider has nothing to span, and inventing a range so that
|
|
156
|
+
one could be drawn would put a limit on the control that the definition does
|
|
157
|
+
not put on the value.
|
|
158
|
+
"""
|
|
159
|
+
|
|
160
|
+
integral = False
|
|
161
|
+
|
|
162
|
+
def build(self, layout: QVBoxLayout) -> None:
|
|
163
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
164
|
+
row = QHBoxLayout()
|
|
165
|
+
row.setContentsMargins(0, 0, 0, 0)
|
|
166
|
+
|
|
167
|
+
parameter = self.parameter
|
|
168
|
+
self.spin: QSpinBox | QDoubleSpinBox
|
|
169
|
+
if self.integral:
|
|
170
|
+
self.spin = QSpinBox()
|
|
171
|
+
self.spin.setRange(
|
|
172
|
+
int(parameter.minimum) if parameter.minimum is not None else -10**9,
|
|
173
|
+
int(parameter.maximum) if parameter.maximum is not None else 10**9,
|
|
174
|
+
)
|
|
175
|
+
else:
|
|
176
|
+
self.spin = QDoubleSpinBox()
|
|
177
|
+
self.spin.setDecimals(3)
|
|
178
|
+
self.spin.setRange(
|
|
179
|
+
float(parameter.minimum) if parameter.minimum is not None else -1e9,
|
|
180
|
+
float(parameter.maximum) if parameter.maximum is not None else 1e9,
|
|
181
|
+
)
|
|
182
|
+
self.spin.setSingleStep(self._step())
|
|
183
|
+
if parameter.unit:
|
|
184
|
+
self.spin.setSuffix(f" {parameter.unit}")
|
|
185
|
+
self.spin.valueChanged.connect(self._on_spin)
|
|
186
|
+
|
|
187
|
+
self.slider: QSlider | None = None
|
|
188
|
+
if parameter.minimum is not None and parameter.maximum is not None:
|
|
189
|
+
self.slider = QSlider(Qt.Orientation.Horizontal)
|
|
190
|
+
self.slider.setRange(0, SLIDER_STEPS)
|
|
191
|
+
self.slider.valueChanged.connect(self._on_slider)
|
|
192
|
+
row.addWidget(self.slider, stretch=1)
|
|
193
|
+
row.addWidget(self.spin)
|
|
194
|
+
layout.addLayout(row)
|
|
195
|
+
|
|
196
|
+
def _step(self) -> float:
|
|
197
|
+
parameter = self.parameter
|
|
198
|
+
if parameter.minimum is None or parameter.maximum is None:
|
|
199
|
+
return 0.1
|
|
200
|
+
span = float(parameter.maximum) - float(parameter.minimum)
|
|
201
|
+
return max(0.001, span / 100.0)
|
|
202
|
+
|
|
203
|
+
def _on_spin(self, value: float) -> None:
|
|
204
|
+
if self.slider is not None:
|
|
205
|
+
self.slider.blockSignals(True)
|
|
206
|
+
self.slider.setValue(self._to_slider(float(value)))
|
|
207
|
+
self.slider.blockSignals(False)
|
|
208
|
+
self._announce()
|
|
209
|
+
|
|
210
|
+
def _on_slider(self, position: int) -> None:
|
|
211
|
+
self.spin.blockSignals(True)
|
|
212
|
+
self.spin.setValue(self._from_slider(position))
|
|
213
|
+
self.spin.blockSignals(False)
|
|
214
|
+
self._announce()
|
|
215
|
+
|
|
216
|
+
def _to_slider(self, value: float) -> int:
|
|
217
|
+
low, high = float(self.parameter.minimum), float(self.parameter.maximum)
|
|
218
|
+
if high <= low:
|
|
219
|
+
return 0
|
|
220
|
+
return int(round((value - low) / (high - low) * SLIDER_STEPS))
|
|
221
|
+
|
|
222
|
+
def _from_slider(self, position: int) -> Any:
|
|
223
|
+
low, high = float(self.parameter.minimum), float(self.parameter.maximum)
|
|
224
|
+
value = low + (high - low) * (position / SLIDER_STEPS)
|
|
225
|
+
return int(round(value)) if self.integral else value
|
|
226
|
+
|
|
227
|
+
def read(self) -> Any:
|
|
228
|
+
return int(self.spin.value()) if self.integral else float(self.spin.value())
|
|
229
|
+
|
|
230
|
+
def write(self, value: Any) -> None:
|
|
231
|
+
self.spin.setValue(int(value) if self.integral else float(value))
|
|
232
|
+
if self.slider is not None:
|
|
233
|
+
self.slider.blockSignals(True)
|
|
234
|
+
self.slider.setValue(self._to_slider(float(value)))
|
|
235
|
+
self.slider.blockSignals(False)
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
class IntEditor(NumberEditor):
|
|
239
|
+
integral = True
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
class FloatEditor(NumberEditor):
|
|
243
|
+
integral = False
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
class DurationEditor(NumberEditor):
|
|
247
|
+
"""Milliseconds, which are integers and want saying so."""
|
|
248
|
+
|
|
249
|
+
integral = True
|
|
250
|
+
|
|
251
|
+
def build(self, layout: QVBoxLayout) -> None:
|
|
252
|
+
super().build(layout)
|
|
253
|
+
if not self.parameter.unit:
|
|
254
|
+
self.spin.setSuffix(" ms")
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
class AngleEditor(ParameterEditor):
|
|
258
|
+
"""A dial, because a bearing is a bearing and a slider makes 359 and 0 far apart."""
|
|
259
|
+
|
|
260
|
+
def build(self, layout: QVBoxLayout) -> None:
|
|
261
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
262
|
+
row = QHBoxLayout()
|
|
263
|
+
row.setContentsMargins(0, 0, 0, 0)
|
|
264
|
+
|
|
265
|
+
self.dial = QDial()
|
|
266
|
+
self.dial.setRange(0, 359)
|
|
267
|
+
self.dial.setWrapping(True)
|
|
268
|
+
self.dial.setNotchesVisible(True)
|
|
269
|
+
self.dial.setFixedSize(64, 64)
|
|
270
|
+
self.dial.valueChanged.connect(self._on_dial)
|
|
271
|
+
|
|
272
|
+
self.spin = QDoubleSpinBox()
|
|
273
|
+
self.spin.setRange(0.0, 359.999)
|
|
274
|
+
self.spin.setDecimals(1)
|
|
275
|
+
self.spin.setSuffix(" °")
|
|
276
|
+
self.spin.valueChanged.connect(self._on_spin)
|
|
277
|
+
|
|
278
|
+
row.addWidget(self.dial)
|
|
279
|
+
row.addWidget(self.spin, stretch=1)
|
|
280
|
+
layout.addLayout(row)
|
|
281
|
+
|
|
282
|
+
def _on_dial(self, value: int) -> None:
|
|
283
|
+
self.spin.blockSignals(True)
|
|
284
|
+
self.spin.setValue(float(value))
|
|
285
|
+
self.spin.blockSignals(False)
|
|
286
|
+
self._announce()
|
|
287
|
+
|
|
288
|
+
def _on_spin(self, value: float) -> None:
|
|
289
|
+
self.dial.blockSignals(True)
|
|
290
|
+
self.dial.setValue(int(round(value)) % 360)
|
|
291
|
+
self.dial.blockSignals(False)
|
|
292
|
+
self._announce()
|
|
293
|
+
|
|
294
|
+
def read(self) -> Any:
|
|
295
|
+
return float(self.spin.value()) % 360.0
|
|
296
|
+
|
|
297
|
+
def write(self, value: Any) -> None:
|
|
298
|
+
degrees = float(value) % 360.0
|
|
299
|
+
self.spin.setValue(degrees)
|
|
300
|
+
self.dial.blockSignals(True)
|
|
301
|
+
self.dial.setValue(int(round(degrees)) % 360)
|
|
302
|
+
self.dial.blockSignals(False)
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
class EnumEditor(ParameterEditor):
|
|
306
|
+
def build(self, layout: QVBoxLayout) -> None:
|
|
307
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
308
|
+
self.combo = QComboBox()
|
|
309
|
+
self.combo.addItems([str(item) for item in self.parameter.enum_values])
|
|
310
|
+
self.combo.currentTextChanged.connect(self._announce)
|
|
311
|
+
layout.addWidget(self.combo)
|
|
312
|
+
|
|
313
|
+
def read(self) -> Any:
|
|
314
|
+
return self.combo.currentText()
|
|
315
|
+
|
|
316
|
+
def write(self, value: Any) -> None:
|
|
317
|
+
index = self.combo.findText(str(value))
|
|
318
|
+
if index >= 0:
|
|
319
|
+
self.combo.setCurrentIndex(index)
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
class ColorButton(QPushButton):
|
|
323
|
+
"""A swatch that opens a picker. Shows the colour rather than naming it."""
|
|
324
|
+
|
|
325
|
+
picked = Signal(str)
|
|
326
|
+
|
|
327
|
+
def __init__(self, value: Any = "#000000", parent: QWidget | None = None) -> None:
|
|
328
|
+
super().__init__(parent)
|
|
329
|
+
self.setFixedWidth(56)
|
|
330
|
+
self.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Preferred)
|
|
331
|
+
self._color = _qcolor(value)
|
|
332
|
+
self._paint()
|
|
333
|
+
self.clicked.connect(self._choose)
|
|
334
|
+
|
|
335
|
+
def color_hex(self) -> str:
|
|
336
|
+
return self._color.name().upper()
|
|
337
|
+
|
|
338
|
+
def set_color(self, value: Any) -> None:
|
|
339
|
+
self._color = _qcolor(value)
|
|
340
|
+
self._paint()
|
|
341
|
+
|
|
342
|
+
def _paint(self) -> None:
|
|
343
|
+
# A readable contrast against whichever colour is showing, so the hex
|
|
344
|
+
# stays legible from black through to white.
|
|
345
|
+
ink = "#000000" if self._color.lightnessF() > 0.55 else "#FFFFFF"
|
|
346
|
+
self.setStyleSheet(
|
|
347
|
+
f"background-color: {self._color.name()}; color: {ink};"
|
|
348
|
+
"border: 1px solid #555; padding: 4px;"
|
|
349
|
+
)
|
|
350
|
+
self.setText(self._color.name().upper()[1:])
|
|
351
|
+
|
|
352
|
+
def _choose(self) -> None:
|
|
353
|
+
chosen = QColorDialog.getColor(self._color, self, "Farbe wählen")
|
|
354
|
+
if chosen.isValid():
|
|
355
|
+
self._color = chosen
|
|
356
|
+
self._paint()
|
|
357
|
+
self.picked.emit(self.color_hex())
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
class ColorEditor(ParameterEditor):
|
|
361
|
+
"""A swatch and a text field, because both ways of saying a colour are useful.
|
|
362
|
+
|
|
363
|
+
The field accepts everything the normaliser does — ``#RRGGBB``, a named
|
|
364
|
+
colour from the catalogue — and only reports a change when what was typed
|
|
365
|
+
actually parses, so a half-typed name does not repaint the ring.
|
|
366
|
+
"""
|
|
367
|
+
|
|
368
|
+
def build(self, layout: QVBoxLayout) -> None:
|
|
369
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
370
|
+
row = QHBoxLayout()
|
|
371
|
+
row.setContentsMargins(0, 0, 0, 0)
|
|
372
|
+
|
|
373
|
+
self.button = ColorButton("#000000")
|
|
374
|
+
self.button.picked.connect(self._on_picked)
|
|
375
|
+
self.text = QLineEdit("#000000")
|
|
376
|
+
self.text.setPlaceholderText("#RRGGBB oder Farbname")
|
|
377
|
+
self.text.textEdited.connect(self._on_typed)
|
|
378
|
+
|
|
379
|
+
row.addWidget(self.button)
|
|
380
|
+
row.addWidget(self.text, stretch=1)
|
|
381
|
+
layout.addLayout(row)
|
|
382
|
+
|
|
383
|
+
def _on_picked(self, value: str) -> None:
|
|
384
|
+
self.text.blockSignals(True)
|
|
385
|
+
self.text.setText(value)
|
|
386
|
+
self.text.blockSignals(False)
|
|
387
|
+
self._announce()
|
|
388
|
+
|
|
389
|
+
def _on_typed(self, text: str) -> None:
|
|
390
|
+
try:
|
|
391
|
+
canonical = format_color(text)
|
|
392
|
+
except Exception:
|
|
393
|
+
return
|
|
394
|
+
self.button.set_color(canonical)
|
|
395
|
+
self._announce()
|
|
396
|
+
|
|
397
|
+
def read(self) -> Any:
|
|
398
|
+
return self.text.text().strip()
|
|
399
|
+
|
|
400
|
+
def write(self, value: Any) -> None:
|
|
401
|
+
canonical = format_color(value)
|
|
402
|
+
self.text.setText(canonical)
|
|
403
|
+
self.button.set_color(canonical)
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
class ColorListEditor(ParameterEditor):
|
|
407
|
+
"""A column of swatches, with the schema's length limits enforced live."""
|
|
408
|
+
|
|
409
|
+
def build(self, layout: QVBoxLayout) -> None:
|
|
410
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
411
|
+
layout.setSpacing(2)
|
|
412
|
+
self.rows: list[ColorButton] = []
|
|
413
|
+
self._rows_layout = QVBoxLayout()
|
|
414
|
+
self._rows_layout.setContentsMargins(0, 0, 0, 0)
|
|
415
|
+
self._rows_layout.setSpacing(2)
|
|
416
|
+
layout.addLayout(self._rows_layout)
|
|
417
|
+
|
|
418
|
+
controls = QHBoxLayout()
|
|
419
|
+
controls.setContentsMargins(0, 0, 0, 0)
|
|
420
|
+
self.add = QToolButton()
|
|
421
|
+
self.add.setText("+")
|
|
422
|
+
self.add.clicked.connect(lambda: self._add_row("#FFFFFF"))
|
|
423
|
+
self.remove = QToolButton()
|
|
424
|
+
self.remove.setText("−")
|
|
425
|
+
self.remove.clicked.connect(self._remove_row)
|
|
426
|
+
self.count = QLabel("0")
|
|
427
|
+
controls.addWidget(self.add)
|
|
428
|
+
controls.addWidget(self.remove)
|
|
429
|
+
controls.addWidget(self.count)
|
|
430
|
+
controls.addStretch(1)
|
|
431
|
+
layout.addLayout(controls)
|
|
432
|
+
|
|
433
|
+
def _limits(self) -> tuple[int, int]:
|
|
434
|
+
low = int(self.parameter.minimum) if self.parameter.minimum is not None else 1
|
|
435
|
+
high = int(self.parameter.maximum) if self.parameter.maximum is not None else 32
|
|
436
|
+
return max(1, low), max(low, high)
|
|
437
|
+
|
|
438
|
+
def _add_row(self, value: Any) -> None:
|
|
439
|
+
low, high = self._limits()
|
|
440
|
+
if len(self.rows) >= high:
|
|
441
|
+
return
|
|
442
|
+
button = ColorButton(value)
|
|
443
|
+
button.picked.connect(self._announce)
|
|
444
|
+
self.rows.append(button)
|
|
445
|
+
self._rows_layout.addWidget(button)
|
|
446
|
+
self._refresh_controls()
|
|
447
|
+
self._announce()
|
|
448
|
+
|
|
449
|
+
def _remove_row(self) -> None:
|
|
450
|
+
low, _ = self._limits()
|
|
451
|
+
if len(self.rows) <= low:
|
|
452
|
+
return
|
|
453
|
+
button = self.rows.pop()
|
|
454
|
+
button.setParent(None)
|
|
455
|
+
button.deleteLater()
|
|
456
|
+
self._refresh_controls()
|
|
457
|
+
self._announce()
|
|
458
|
+
|
|
459
|
+
def _refresh_controls(self) -> None:
|
|
460
|
+
low, high = self._limits()
|
|
461
|
+
self.count.setText(f"{len(self.rows)} ({low}–{high})")
|
|
462
|
+
self.add.setEnabled(len(self.rows) < high)
|
|
463
|
+
self.remove.setEnabled(len(self.rows) > low)
|
|
464
|
+
|
|
465
|
+
def read(self) -> Any:
|
|
466
|
+
return [button.color_hex() for button in self.rows]
|
|
467
|
+
|
|
468
|
+
def write(self, value: Any) -> None:
|
|
469
|
+
while self.rows:
|
|
470
|
+
button = self.rows.pop()
|
|
471
|
+
button.setParent(None)
|
|
472
|
+
button.deleteLater()
|
|
473
|
+
for item in list(value or []):
|
|
474
|
+
button = ColorButton(item)
|
|
475
|
+
button.picked.connect(self._announce)
|
|
476
|
+
self.rows.append(button)
|
|
477
|
+
self._rows_layout.addWidget(button)
|
|
478
|
+
low, _ = self._limits()
|
|
479
|
+
while len(self.rows) < low:
|
|
480
|
+
self._add_row("#FFFFFF")
|
|
481
|
+
self._refresh_controls()
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
class GradientEditor(ParameterEditor):
|
|
485
|
+
"""Stops as position-and-colour rows, with the ends pinned where the schema wants them.
|
|
486
|
+
|
|
487
|
+
A gradient must start at 0 and end at 1 and be sorted. Rather than let those
|
|
488
|
+
be typed wrongly and rejected on apply, the first and last positions are
|
|
489
|
+
fixed and the middle ones are sorted on read — the editor can only produce
|
|
490
|
+
gradients that validate.
|
|
491
|
+
"""
|
|
492
|
+
|
|
493
|
+
def build(self, layout: QVBoxLayout) -> None:
|
|
494
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
495
|
+
layout.setSpacing(2)
|
|
496
|
+
self.stops: list[tuple[QDoubleSpinBox, ColorButton]] = []
|
|
497
|
+
self._rows_layout = QVBoxLayout()
|
|
498
|
+
self._rows_layout.setContentsMargins(0, 0, 0, 0)
|
|
499
|
+
self._rows_layout.setSpacing(2)
|
|
500
|
+
layout.addLayout(self._rows_layout)
|
|
501
|
+
|
|
502
|
+
controls = QHBoxLayout()
|
|
503
|
+
controls.setContentsMargins(0, 0, 0, 0)
|
|
504
|
+
self.add = QToolButton()
|
|
505
|
+
self.add.setText("+")
|
|
506
|
+
self.add.clicked.connect(self._add_middle_stop)
|
|
507
|
+
self.remove = QToolButton()
|
|
508
|
+
self.remove.setText("−")
|
|
509
|
+
self.remove.clicked.connect(self._remove_middle_stop)
|
|
510
|
+
controls.addWidget(self.add)
|
|
511
|
+
controls.addWidget(self.remove)
|
|
512
|
+
controls.addStretch(1)
|
|
513
|
+
layout.addLayout(controls)
|
|
514
|
+
|
|
515
|
+
def _make_row(self, at: float, color: Any, *, pinned: bool) -> None:
|
|
516
|
+
row = QWidget()
|
|
517
|
+
line = QHBoxLayout(row)
|
|
518
|
+
line.setContentsMargins(0, 0, 0, 0)
|
|
519
|
+
|
|
520
|
+
position = QDoubleSpinBox()
|
|
521
|
+
position.setRange(0.0, 1.0)
|
|
522
|
+
position.setSingleStep(0.05)
|
|
523
|
+
position.setDecimals(2)
|
|
524
|
+
position.setValue(float(at))
|
|
525
|
+
position.setEnabled(not pinned)
|
|
526
|
+
position.valueChanged.connect(self._announce)
|
|
527
|
+
|
|
528
|
+
button = ColorButton(color)
|
|
529
|
+
button.picked.connect(self._announce)
|
|
530
|
+
|
|
531
|
+
line.addWidget(position)
|
|
532
|
+
line.addWidget(button, stretch=1)
|
|
533
|
+
self._rows_layout.addWidget(row)
|
|
534
|
+
self.stops.append((position, button))
|
|
535
|
+
|
|
536
|
+
def _add_middle_stop(self) -> None:
|
|
537
|
+
if len(self.stops) >= 16:
|
|
538
|
+
return
|
|
539
|
+
self.write([*self.read(), {"at": 0.5, "color": "#FFFFFF"}])
|
|
540
|
+
self._announce()
|
|
541
|
+
|
|
542
|
+
def _remove_middle_stop(self) -> None:
|
|
543
|
+
stops = self.read()
|
|
544
|
+
if len(stops) <= 2:
|
|
545
|
+
return
|
|
546
|
+
del stops[len(stops) // 2]
|
|
547
|
+
self.write(stops)
|
|
548
|
+
self._announce()
|
|
549
|
+
|
|
550
|
+
def _clear(self) -> None:
|
|
551
|
+
self.stops.clear()
|
|
552
|
+
while self._rows_layout.count():
|
|
553
|
+
item = self._rows_layout.takeAt(0)
|
|
554
|
+
widget = item.widget()
|
|
555
|
+
if widget is not None:
|
|
556
|
+
widget.setParent(None)
|
|
557
|
+
widget.deleteLater()
|
|
558
|
+
|
|
559
|
+
def read(self) -> Any:
|
|
560
|
+
stops = [
|
|
561
|
+
{"at": float(position.value()), "color": button.color_hex()}
|
|
562
|
+
for position, button in self.stops
|
|
563
|
+
]
|
|
564
|
+
if not stops:
|
|
565
|
+
return [{"at": 0.0, "color": "#000000"}, {"at": 1.0, "color": "#FFFFFF"}]
|
|
566
|
+
# Sorted here rather than refused on apply: a stop dragged past its
|
|
567
|
+
# neighbour is a reordering, not a mistake.
|
|
568
|
+
stops.sort(key=lambda stop: stop["at"])
|
|
569
|
+
stops[0]["at"] = 0.0
|
|
570
|
+
stops[-1]["at"] = 1.0
|
|
571
|
+
return stops
|
|
572
|
+
|
|
573
|
+
def write(self, value: Any) -> None:
|
|
574
|
+
stops = list(value or [])
|
|
575
|
+
if len(stops) < 2:
|
|
576
|
+
stops = [{"at": 0.0, "color": "#000000"}, {"at": 1.0, "color": "#FFFFFF"}]
|
|
577
|
+
stops = sorted(stops, key=lambda stop: float(stop["at"]))[:16]
|
|
578
|
+
self._clear()
|
|
579
|
+
for index, stop in enumerate(stops):
|
|
580
|
+
pinned = index in (0, len(stops) - 1)
|
|
581
|
+
at = 0.0 if index == 0 else 1.0 if index == len(stops) - 1 else float(stop["at"])
|
|
582
|
+
self._make_row(at, stop["color"], pinned=pinned)
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
class ColorRangeEditor(ParameterEditor):
|
|
586
|
+
"""Three min/max pairs, each on the scale its channel is actually measured in."""
|
|
587
|
+
|
|
588
|
+
CHANNELS = (("hue", 0.0, 360.0, "°"), ("saturation", 0.0, 1.0, ""), ("brightness", 0.0, 1.0, ""))
|
|
589
|
+
|
|
590
|
+
def build(self, layout: QVBoxLayout) -> None:
|
|
591
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
592
|
+
layout.setSpacing(2)
|
|
593
|
+
self.bounds: dict[str, tuple[QDoubleSpinBox, QDoubleSpinBox]] = {}
|
|
594
|
+
for name, low, high, unit in self.CHANNELS:
|
|
595
|
+
row = QHBoxLayout()
|
|
596
|
+
row.setContentsMargins(0, 0, 0, 0)
|
|
597
|
+
row.addWidget(QLabel(name[:3]))
|
|
598
|
+
pair = []
|
|
599
|
+
for _ in range(2):
|
|
600
|
+
spin = QDoubleSpinBox()
|
|
601
|
+
spin.setRange(low, high)
|
|
602
|
+
spin.setDecimals(0 if high > 1.0 else 2)
|
|
603
|
+
spin.setSingleStep(1.0 if high > 1.0 else 0.05)
|
|
604
|
+
if unit:
|
|
605
|
+
spin.setSuffix(f" {unit}")
|
|
606
|
+
spin.valueChanged.connect(self._on_bound_changed)
|
|
607
|
+
row.addWidget(spin)
|
|
608
|
+
pair.append(spin)
|
|
609
|
+
pair[1].setValue(high)
|
|
610
|
+
self.bounds[name] = (pair[0], pair[1])
|
|
611
|
+
holder = QWidget()
|
|
612
|
+
holder.setLayout(row)
|
|
613
|
+
layout.addWidget(holder)
|
|
614
|
+
|
|
615
|
+
def _on_bound_changed(self, *_: Any) -> None:
|
|
616
|
+
# A maximum below its minimum is not a range the schema accepts, so the
|
|
617
|
+
# pair is kept ordered as it is edited rather than rejected afterwards.
|
|
618
|
+
for low_spin, high_spin in self.bounds.values():
|
|
619
|
+
if high_spin.value() < low_spin.value():
|
|
620
|
+
high_spin.blockSignals(True)
|
|
621
|
+
high_spin.setValue(low_spin.value())
|
|
622
|
+
high_spin.blockSignals(False)
|
|
623
|
+
self._announce()
|
|
624
|
+
|
|
625
|
+
def read(self) -> Any:
|
|
626
|
+
return {
|
|
627
|
+
name: [float(low.value()), float(high.value())]
|
|
628
|
+
for name, (low, high) in self.bounds.items()
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
def write(self, value: Any) -> None:
|
|
632
|
+
payload = dict(value or {})
|
|
633
|
+
for name, (low_spin, high_spin) in self.bounds.items():
|
|
634
|
+
pair = payload.get(name)
|
|
635
|
+
if not pair:
|
|
636
|
+
continue
|
|
637
|
+
low_spin.setValue(float(pair[0]))
|
|
638
|
+
high_spin.setValue(float(pair[1]))
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
EDITORS: dict[ParamType, type[ParameterEditor]] = {
|
|
642
|
+
ParamType.BOOL: BoolEditor,
|
|
643
|
+
ParamType.INT: IntEditor,
|
|
644
|
+
ParamType.FLOAT: FloatEditor,
|
|
645
|
+
ParamType.DURATION_MS: DurationEditor,
|
|
646
|
+
ParamType.ANGLE_DEG: AngleEditor,
|
|
647
|
+
ParamType.ENUM: EnumEditor,
|
|
648
|
+
ParamType.COLOR: ColorEditor,
|
|
649
|
+
ParamType.COLOR_LIST: ColorListEditor,
|
|
650
|
+
ParamType.GRADIENT: GradientEditor,
|
|
651
|
+
ParamType.COLOR_RANGE: ColorRangeEditor,
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
def editor_for(parameter: ParamDefinition, parent: QWidget | None = None) -> ParameterEditor:
|
|
656
|
+
"""The editor a declared type gets. Every type has one, by construction.
|
|
657
|
+
|
|
658
|
+
The mapping is total and a test asserts that it stays total, so adding a
|
|
659
|
+
parameter type to the SDK without an editor here fails loudly rather than
|
|
660
|
+
producing an effect with an uneditable field.
|
|
661
|
+
"""
|
|
662
|
+
try:
|
|
663
|
+
factory = EDITORS[parameter.type]
|
|
664
|
+
except KeyError: # pragma: no cover - guarded by a test
|
|
665
|
+
raise KeyError(f"No editor for parameter type {parameter.type!r}") from None
|
|
666
|
+
return factory(parameter, parent)
|
|
667
|
+
|
|
668
|
+
|
|
669
|
+
class SchemaForm(QWidget):
|
|
670
|
+
"""Every parameter of one definition, as a form that reports on each change."""
|
|
671
|
+
|
|
672
|
+
changed = Signal()
|
|
673
|
+
|
|
674
|
+
def __init__(
|
|
675
|
+
self,
|
|
676
|
+
schema: Mapping[str, ParamDefinition],
|
|
677
|
+
parent: QWidget | None = None,
|
|
678
|
+
*,
|
|
679
|
+
on_change: Callable[[], None] | None = None,
|
|
680
|
+
) -> None:
|
|
681
|
+
super().__init__(parent)
|
|
682
|
+
self.editors: dict[str, ParameterEditor] = {}
|
|
683
|
+
|
|
684
|
+
layout = QFormLayout(self)
|
|
685
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
686
|
+
layout.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.AllNonFixedFieldsGrow)
|
|
687
|
+
layout.setLabelAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
|
|
688
|
+
|
|
689
|
+
for name, parameter in schema.items():
|
|
690
|
+
editor = editor_for(parameter, self)
|
|
691
|
+
editor.changed.connect(self.changed.emit)
|
|
692
|
+
label = QLabel(name)
|
|
693
|
+
if parameter.description:
|
|
694
|
+
label.setToolTip(parameter.description)
|
|
695
|
+
editor.setToolTip(parameter.description)
|
|
696
|
+
if parameter.required:
|
|
697
|
+
label.setText(f"{name} *")
|
|
698
|
+
layout.addRow(label, editor)
|
|
699
|
+
self.editors[name] = editor
|
|
700
|
+
|
|
701
|
+
if on_change is not None:
|
|
702
|
+
self.changed.connect(on_change)
|
|
703
|
+
|
|
704
|
+
def values(self) -> dict[str, Any]:
|
|
705
|
+
return {name: editor.value() for name, editor in self.editors.items()}
|
|
706
|
+
|
|
707
|
+
def set_values(self, values: Mapping[str, Any]) -> None:
|
|
708
|
+
for name, editor in self.editors.items():
|
|
709
|
+
if name in values:
|
|
710
|
+
editor.set_value(values[name])
|
|
711
|
+
|
|
712
|
+
|
|
713
|
+
__all__ = [
|
|
714
|
+
"EDITORS",
|
|
715
|
+
"SLIDER_STEPS",
|
|
716
|
+
"AngleEditor",
|
|
717
|
+
"BoolEditor",
|
|
718
|
+
"ColorButton",
|
|
719
|
+
"ColorEditor",
|
|
720
|
+
"ColorListEditor",
|
|
721
|
+
"ColorRangeEditor",
|
|
722
|
+
"DurationEditor",
|
|
723
|
+
"EnumEditor",
|
|
724
|
+
"FloatEditor",
|
|
725
|
+
"GradientEditor",
|
|
726
|
+
"IntEditor",
|
|
727
|
+
"ParameterEditor",
|
|
728
|
+
"SchemaForm",
|
|
729
|
+
"editor_for",
|
|
730
|
+
]
|