dynamic-settings-widget 2.0.0__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.
- dynamic_settings_widget/__init__.py +56 -0
- dynamic_settings_widget/adapters/__init__.py +42 -0
- dynamic_settings_widget/adapters/base.py +179 -0
- dynamic_settings_widget/adapters/memory.py +201 -0
- dynamic_settings_widget/adapters/runner.py +63 -0
- dynamic_settings_widget/core/__init__.py +1 -0
- dynamic_settings_widget/core/catalog.py +201 -0
- dynamic_settings_widget/core/common.py +214 -0
- dynamic_settings_widget/core/conditions.py +146 -0
- dynamic_settings_widget/core/datatypes.py +414 -0
- dynamic_settings_widget/core/diagnostics.py +69 -0
- dynamic_settings_widget/core/loader.py +101 -0
- dynamic_settings_widget/core/model.py +245 -0
- dynamic_settings_widget/core/overrides.py +353 -0
- dynamic_settings_widget/core/parser.py +552 -0
- dynamic_settings_widget/core/presentation.py +68 -0
- dynamic_settings_widget/core/registry.py +258 -0
- dynamic_settings_widget/core/state.py +314 -0
- dynamic_settings_widget/data/example_catalog.json +23 -0
- dynamic_settings_widget/data/example_layout.json +23 -0
- dynamic_settings_widget/data/settings_catalog.schema.json +388 -0
- dynamic_settings_widget/data/settings_definition.schema.json +1041 -0
- dynamic_settings_widget/data/settings_layout.schema.json +701 -0
- dynamic_settings_widget/data/settings_override.schema.json +406 -0
- dynamic_settings_widget/py.typed +0 -0
- dynamic_settings_widget/session.py +1537 -0
- dynamic_settings_widget/standalone/__init__.py +67 -0
- dynamic_settings_widget/standalone/action_button.py +200 -0
- dynamic_settings_widget/standalone/auth_widget.py +377 -0
- dynamic_settings_widget/standalone/check_box_editor.py +233 -0
- dynamic_settings_widget/standalone/choice_buttons_editor.py +394 -0
- dynamic_settings_widget/standalone/color_editor.py +316 -0
- dynamic_settings_widget/standalone/dropdown_editor.py +356 -0
- dynamic_settings_widget/standalone/editable_label.py +428 -0
- dynamic_settings_widget/standalone/float_spin_box.py +621 -0
- dynamic_settings_widget/standalone/font_editor.py +348 -0
- dynamic_settings_widget/standalone/hotkey_editor.py +681 -0
- dynamic_settings_widget/standalone/info_label.py +190 -0
- dynamic_settings_widget/standalone/int_spin_box.py +627 -0
- dynamic_settings_widget/standalone/multi_choice_editor.py +428 -0
- dynamic_settings_widget/standalone/password_editor.py +348 -0
- dynamic_settings_widget/standalone/path_editor.py +615 -0
- dynamic_settings_widget/standalone/protected_group_box.py +287 -0
- dynamic_settings_widget/standalone/server_connection_editor.py +642 -0
- dynamic_settings_widget/standalone/slider_spin_box.py +840 -0
- dynamic_settings_widget/standalone/sound_selection_widget.py +710 -0
- dynamic_settings_widget/standalone/status_label.py +148 -0
- dynamic_settings_widget/standalone/switch_editor.py +293 -0
- dynamic_settings_widget/standalone/text_editor.py +265 -0
- dynamic_settings_widget/testlab/__init__.py +1 -0
- dynamic_settings_widget/testlab/__main__.py +5 -0
- dynamic_settings_widget/testlab/app.py +699 -0
- dynamic_settings_widget/testlab/assets/extra_sounds/Signal.wav +0 -0
- dynamic_settings_widget/testlab/assets/sounds/Benachrichtigung.wav +0 -0
- dynamic_settings_widget/testlab/assets/sounds/feedback/Signal.wav +0 -0
- dynamic_settings_widget/testlab/definitions/broken_base.json +25 -0
- dynamic_settings_widget/testlab/definitions/demo.json +634 -0
- dynamic_settings_widget/testlab/definitions/gallery.json +255 -0
- dynamic_settings_widget/testlab/definitions/override_example.json +41 -0
- dynamic_settings_widget/testlab/definitions/override_invalid.json +11 -0
- dynamic_settings_widget/testlab/definitions/override_syntax_error.json +5 -0
- dynamic_settings_widget/testlab/definitions/override_weaken.json +10 -0
- dynamic_settings_widget/testlab/definitions/voice_v2/01_left_top.json +1190 -0
- dynamic_settings_widget/testlab/definitions/voice_v2/catalog.json +1930 -0
- dynamic_settings_widget/testlab/definitions/voice_v2/models.json +141 -0
- dynamic_settings_widget/testlab/definitions/voice_v2/wakewords.json +161 -0
- dynamic_settings_widget/testlab/dummy.py +353 -0
- dynamic_settings_widget/testlab/extensions.py +149 -0
- dynamic_settings_widget/testlab/gallery.py +223 -0
- dynamic_settings_widget/testlab/http_adapter.py +430 -0
- dynamic_settings_widget/testlab/http_server.py +493 -0
- dynamic_settings_widget/testlab/voice_v2.py +251 -0
- dynamic_settings_widget/ui/__init__.py +23 -0
- dynamic_settings_widget/ui/base.py +63 -0
- dynamic_settings_widget/ui/builders.py +226 -0
- dynamic_settings_widget/ui/containers.py +441 -0
- dynamic_settings_widget/ui/dialog.py +231 -0
- dynamic_settings_widget/ui/editing.py +51 -0
- dynamic_settings_widget/ui/row.py +810 -0
- dynamic_settings_widget/ui/view.py +248 -0
- dynamic_settings_widget/widgets/__init__.py +74 -0
- dynamic_settings_widget/widgets/base.py +145 -0
- dynamic_settings_widget/widgets/bridge.py +222 -0
- dynamic_settings_widget/widgets/theme.py +107 -0
- dynamic_settings_widget-2.0.0.dist-info/METADATA +237 -0
- dynamic_settings_widget-2.0.0.dist-info/RECORD +90 -0
- dynamic_settings_widget-2.0.0.dist-info/WHEEL +5 -0
- dynamic_settings_widget-2.0.0.dist-info/entry_points.txt +5 -0
- dynamic_settings_widget-2.0.0.dist-info/licenses/LICENSE +21 -0
- dynamic_settings_widget-2.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
#
|
|
3
|
+
# Copyright (c) 2026 marcosudau-vps
|
|
4
|
+
#
|
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
# furnished to do so, subject to the following conditions:
|
|
11
|
+
#
|
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
# copies or substantial portions of the Software.
|
|
14
|
+
#
|
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
# SOFTWARE.
|
|
22
|
+
|
|
23
|
+
"""IntSpinBox – Ganzzahleingabe mit Grenzen, Schrittweite, Einheit und „nicht gesetzt“.
|
|
24
|
+
|
|
25
|
+
Autarke Einzeldatei: benötigt nur Python ≥ 3.11 und PySide6 (Essentials).
|
|
26
|
+
|
|
27
|
+
Beispiel::
|
|
28
|
+
|
|
29
|
+
from int_spin_box import IntSpinBox
|
|
30
|
+
|
|
31
|
+
spin = IntSpinBox()
|
|
32
|
+
spin.configure(minimum=0, maximum=5000, step=50, unit="ms")
|
|
33
|
+
spin.set_value(250)
|
|
34
|
+
spin.value_edited.connect(print) # nur Benutzereingaben
|
|
35
|
+
spin.configure(inline_edit=True) # kompakte Wertanzeige, Klick/F2 öffnet die Spinbox
|
|
36
|
+
|
|
37
|
+
Exakt bis ±2⁵³. Werte werden nie still gekappt: Ein programmatisch gesetzter Wert
|
|
38
|
+
außerhalb der Grenzen bleibt erhalten und sichtbar. Optionen (``configure``):
|
|
39
|
+
``optional``, ``minimum``, ``maximum``, ``step``, ``precision`` (ohne Wirkung), ``unit``,
|
|
40
|
+
``inline_edit``.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
from __future__ import annotations
|
|
44
|
+
|
|
45
|
+
import math
|
|
46
|
+
from contextlib import contextmanager
|
|
47
|
+
from decimal import Decimal
|
|
48
|
+
from typing import Any
|
|
49
|
+
|
|
50
|
+
from PySide6.QtCore import QEvent, QObject, QSize, Qt, Signal
|
|
51
|
+
from PySide6.QtGui import QFont, QKeyEvent, QValidator
|
|
52
|
+
from PySide6.QtWidgets import QDoubleSpinBox, QHBoxLayout, QToolButton, QWidget
|
|
53
|
+
|
|
54
|
+
__all__ = ["IntSpinBox", "MAX_SAFE_INT"]
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
# ── Wertbasis ─ identisch in allen Editor-Dateien (bewusst kopiert, Test prüft Gleichheit) ──
|
|
58
|
+
def _same(a: Any, b: Any) -> bool:
|
|
59
|
+
"""Gleichheit ohne Pythons ``True == 1``-Vermischung."""
|
|
60
|
+
if a is b:
|
|
61
|
+
return True
|
|
62
|
+
if isinstance(a, bool) != isinstance(b, bool):
|
|
63
|
+
return False
|
|
64
|
+
if isinstance(a, list) and isinstance(b, list):
|
|
65
|
+
return len(a) == len(b) and all(_same(x, y) for x, y in zip(a, b))
|
|
66
|
+
if isinstance(a, dict) and isinstance(b, dict):
|
|
67
|
+
return a.keys() == b.keys() and all(_same(a[k], b[k]) for k in a)
|
|
68
|
+
try:
|
|
69
|
+
return bool(a == b)
|
|
70
|
+
except Exception: # noqa: BLE001 – fremde Typen ohne Vergleich
|
|
71
|
+
return False
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class _ValueWidget(QWidget):
|
|
75
|
+
"""Gemeinsamer Vertrag aller Editoren.
|
|
76
|
+
|
|
77
|
+
* ``value()`` / ``set_value(v)`` – ``None`` bedeutet „nicht gesetzt“; ein Wert falschen
|
|
78
|
+
Typs löst ``ValueError`` aus und wird nie still umgewandelt.
|
|
79
|
+
* ``value_changed(object)`` – jede Wertänderung, auch programmatisch.
|
|
80
|
+
* ``value_edited(object)`` – nur Änderungen durch den Benutzer.
|
|
81
|
+
* ``configure(**optionen)`` – Schlüssel aus ``OPTIONS``; unbekannte → ``TypeError``.
|
|
82
|
+
* ``set_read_only(bool)``, ``input_error()`` / ``input_error_changed(str)``, ``commit()``.
|
|
83
|
+
"""
|
|
84
|
+
|
|
85
|
+
value_changed = Signal(object)
|
|
86
|
+
value_edited = Signal(object)
|
|
87
|
+
input_error_changed = Signal(str)
|
|
88
|
+
|
|
89
|
+
#: Schlüssel, die ``configure()`` annimmt.
|
|
90
|
+
OPTIONS: tuple[str, ...] = ("optional",)
|
|
91
|
+
|
|
92
|
+
def __init__(self, parent: QWidget | None = None) -> None:
|
|
93
|
+
super().__init__(parent)
|
|
94
|
+
self._optional = False
|
|
95
|
+
self._read_only = False
|
|
96
|
+
self._silent = 0
|
|
97
|
+
self._last_error = ""
|
|
98
|
+
|
|
99
|
+
# -- von Unterklassen bereitzustellen ------------------------------------------------
|
|
100
|
+
def value(self) -> Any:
|
|
101
|
+
raise NotImplementedError
|
|
102
|
+
|
|
103
|
+
def _apply_value(self, value: Any) -> None:
|
|
104
|
+
raise NotImplementedError
|
|
105
|
+
|
|
106
|
+
def _apply_options(self) -> None:
|
|
107
|
+
"""Darstellung nach ``configure()`` nachführen."""
|
|
108
|
+
|
|
109
|
+
def _apply_read_only(self, read_only: bool) -> None:
|
|
110
|
+
self.setEnabled(not read_only)
|
|
111
|
+
|
|
112
|
+
def input_error(self) -> str:
|
|
113
|
+
"""Grund, warum die aktuelle Eingabe keinen gültigen Wert ergibt (sonst leer)."""
|
|
114
|
+
return ""
|
|
115
|
+
|
|
116
|
+
def commit(self) -> None:
|
|
117
|
+
"""Schließt eine angefangene Eingabe ab."""
|
|
118
|
+
|
|
119
|
+
# -- öffentliche Schnittstelle --------------------------------------------------------
|
|
120
|
+
def set_value(self, value: Any) -> None:
|
|
121
|
+
"""Setzt den Wert programmatisch (ohne ``value_edited``)."""
|
|
122
|
+
old = self.value()
|
|
123
|
+
with self._programmatic():
|
|
124
|
+
self._apply_value(value)
|
|
125
|
+
new = self.value()
|
|
126
|
+
if not _same(old, new):
|
|
127
|
+
self.value_changed.emit(new)
|
|
128
|
+
self._check_error()
|
|
129
|
+
|
|
130
|
+
def configure(self, **options: Any) -> None:
|
|
131
|
+
"""Setzt mehrere Optionen auf einmal, z. B. ``configure(optional=True)``."""
|
|
132
|
+
unknown = sorted(set(options) - set(self.OPTIONS))
|
|
133
|
+
if unknown:
|
|
134
|
+
raise TypeError(f"{type(self).__name__}: unbekannte Option(en) {', '.join(unknown)}")
|
|
135
|
+
for key, value in options.items():
|
|
136
|
+
self._set_option(key, value)
|
|
137
|
+
with self._programmatic():
|
|
138
|
+
self._apply_options()
|
|
139
|
+
self._check_error()
|
|
140
|
+
|
|
141
|
+
def options(self) -> dict[str, Any]:
|
|
142
|
+
"""Aktuelle Optionen."""
|
|
143
|
+
return {key: getattr(self, f"_{key}") for key in self.OPTIONS}
|
|
144
|
+
|
|
145
|
+
def _set_option(self, key: str, value: Any) -> None:
|
|
146
|
+
if key == "optional":
|
|
147
|
+
value = bool(value)
|
|
148
|
+
setattr(self, f"_{key}", value)
|
|
149
|
+
|
|
150
|
+
def set_optional(self, optional: bool) -> None:
|
|
151
|
+
self.configure(optional=optional)
|
|
152
|
+
|
|
153
|
+
def is_optional(self) -> bool:
|
|
154
|
+
return self._optional
|
|
155
|
+
|
|
156
|
+
def set_read_only(self, read_only: bool) -> None:
|
|
157
|
+
self._read_only = bool(read_only)
|
|
158
|
+
self._apply_read_only(self._read_only)
|
|
159
|
+
|
|
160
|
+
def is_read_only(self) -> bool:
|
|
161
|
+
return self._read_only
|
|
162
|
+
|
|
163
|
+
# -- Hilfen für Unterklassen ----------------------------------------------------------
|
|
164
|
+
@contextmanager
|
|
165
|
+
def _programmatic(self):
|
|
166
|
+
self._silent += 1
|
|
167
|
+
try:
|
|
168
|
+
yield
|
|
169
|
+
finally:
|
|
170
|
+
self._silent -= 1
|
|
171
|
+
|
|
172
|
+
def _user_changed(self, *_: Any) -> None:
|
|
173
|
+
"""Nach einer echten Benutzereingabe aufzurufen."""
|
|
174
|
+
if self._silent:
|
|
175
|
+
return
|
|
176
|
+
value = self.value()
|
|
177
|
+
self._check_error()
|
|
178
|
+
self.value_changed.emit(value)
|
|
179
|
+
self.value_edited.emit(value)
|
|
180
|
+
|
|
181
|
+
def _check_error(self) -> None:
|
|
182
|
+
error = self.input_error()
|
|
183
|
+
if error != self._last_error:
|
|
184
|
+
self._last_error = error
|
|
185
|
+
self.input_error_changed.emit(error)
|
|
186
|
+
# ── Ende Wertbasis ──
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
# ── Mausrad-Schutz ─ identisch in allen Dateien mit Rad-empfindlichen Feldern (bewusst kopiert) ──
|
|
190
|
+
class _WheelGuard(QObject):
|
|
191
|
+
"""Verhindert versehentliche Wertänderungen beim Scrollen einer Seite (nur mit Fokus)."""
|
|
192
|
+
|
|
193
|
+
def eventFilter(self, obj: QObject, event: QEvent) -> bool:
|
|
194
|
+
if event.type() == QEvent.Type.Wheel and isinstance(obj, QWidget) and not obj.hasFocus():
|
|
195
|
+
event.ignore()
|
|
196
|
+
return True
|
|
197
|
+
return False
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _guard_wheel(widget: QWidget) -> None:
|
|
201
|
+
guard = widget.findChild(_WheelGuard, "_wheel_guard")
|
|
202
|
+
if guard is None:
|
|
203
|
+
guard = _WheelGuard(widget)
|
|
204
|
+
guard.setObjectName("_wheel_guard")
|
|
205
|
+
widget.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
|
|
206
|
+
widget.installEventFilter(guard)
|
|
207
|
+
# ── Ende Mausrad-Schutz ──
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
# ── Zahlenkern ─ identisch in int_spin_box.py, float_spin_box.py, slider_spin_box.py (bewusst kopiert) ──
|
|
211
|
+
#: Größter exakt darstellbarer Ganzzahlbetrag (Basis ist QDoubleSpinBox, QSpinBox hätte nur 32 Bit).
|
|
212
|
+
MAX_SAFE_INT = 2**53
|
|
213
|
+
_FLOAT_LIMIT = 1e15
|
|
214
|
+
_DEFAULT_PRECISION = 2
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def _format_number(value: float | int) -> str:
|
|
218
|
+
return str(value) if isinstance(value, int) else f"{value:.12g}"
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
class _SpinBox(QDoubleSpinBox):
|
|
222
|
+
"""Spinbox mit „nicht gesetzt“-Zustand; Punkt und Komma gelten als Dezimaltrennzeichen."""
|
|
223
|
+
|
|
224
|
+
null_changed = Signal()
|
|
225
|
+
|
|
226
|
+
def __init__(self, parent: QWidget | None = None) -> None:
|
|
227
|
+
self._null = False
|
|
228
|
+
self.allow_null = False
|
|
229
|
+
super().__init__(parent)
|
|
230
|
+
self._unit = ""
|
|
231
|
+
self.setGroupSeparatorShown(False)
|
|
232
|
+
self.setKeyboardTracking(True)
|
|
233
|
+
self.setAccelerated(True)
|
|
234
|
+
self.setAlignment(Qt.AlignmentFlag.AlignRight)
|
|
235
|
+
self.lineEdit().textEdited.connect(self._on_text_edited)
|
|
236
|
+
self.editingFinished.connect(self._restore_suffix)
|
|
237
|
+
|
|
238
|
+
def sizeHint(self) -> QSize:
|
|
239
|
+
hint = super().sizeHint()
|
|
240
|
+
return QSize(min(max(hint.width(), 110), 170), hint.height())
|
|
241
|
+
|
|
242
|
+
def minimumSizeHint(self) -> QSize:
|
|
243
|
+
hint = super().minimumSizeHint()
|
|
244
|
+
return QSize(min(hint.width(), 90), hint.height())
|
|
245
|
+
|
|
246
|
+
def is_null(self) -> bool:
|
|
247
|
+
return self._null
|
|
248
|
+
|
|
249
|
+
def set_null(self, null: bool) -> None:
|
|
250
|
+
self._null = null
|
|
251
|
+
# setSuffix() erneuert die Anzeige über textFromValue().
|
|
252
|
+
super().setSuffix("")
|
|
253
|
+
super().setSuffix("" if null or not self._unit else f" {self._unit}")
|
|
254
|
+
|
|
255
|
+
def set_unit(self, unit: str) -> None:
|
|
256
|
+
self._unit = unit
|
|
257
|
+
self.set_null(self._null)
|
|
258
|
+
|
|
259
|
+
def textFromValue(self, value: float) -> str:
|
|
260
|
+
return "" if self._null else super().textFromValue(value)
|
|
261
|
+
|
|
262
|
+
def _normalize(self, text: str) -> str:
|
|
263
|
+
point = self.locale().decimalPoint()
|
|
264
|
+
return text.replace(".", point).replace(",", point)
|
|
265
|
+
|
|
266
|
+
def validate(self, text: str, pos: int):
|
|
267
|
+
suffix = self.suffix()
|
|
268
|
+
body = text[: -len(suffix)] if suffix and text.endswith(suffix) else text
|
|
269
|
+
normalized = self._normalize(body) + (suffix if body is not text else "")
|
|
270
|
+
if self.allow_null and not body.strip():
|
|
271
|
+
return QValidator.State.Intermediate, normalized, pos
|
|
272
|
+
state = super().validate(normalized, pos)
|
|
273
|
+
return (state[0], normalized, pos) if isinstance(state, tuple) else (state, normalized, pos)
|
|
274
|
+
|
|
275
|
+
def valueFromText(self, text: str) -> float:
|
|
276
|
+
suffix = self.suffix()
|
|
277
|
+
body = text[: -len(suffix)] if suffix and text.endswith(suffix) else text
|
|
278
|
+
return super().valueFromText(self._normalize(body) + (suffix if body is not text else ""))
|
|
279
|
+
|
|
280
|
+
def _body(self) -> str:
|
|
281
|
+
text = self.lineEdit().text()
|
|
282
|
+
suffix = self.suffix().strip()
|
|
283
|
+
return text.removesuffix(suffix).strip() if suffix else text.strip()
|
|
284
|
+
|
|
285
|
+
def _on_text_edited(self, _: str) -> None:
|
|
286
|
+
body = self._body()
|
|
287
|
+
if not body:
|
|
288
|
+
if self.allow_null and not self._null:
|
|
289
|
+
self.set_null(True)
|
|
290
|
+
self.null_changed.emit()
|
|
291
|
+
elif self._null and self.hasAcceptableInput():
|
|
292
|
+
self._null = False
|
|
293
|
+
self.null_changed.emit()
|
|
294
|
+
|
|
295
|
+
def leave_null(self) -> None:
|
|
296
|
+
"""Ein Wert wurde eingegeben; laufende Texteingaben werden dabei nicht umformatiert."""
|
|
297
|
+
self._null = False
|
|
298
|
+
if not self.lineEdit().text():
|
|
299
|
+
self.set_null(False)
|
|
300
|
+
|
|
301
|
+
def _restore_suffix(self) -> None:
|
|
302
|
+
if not self._null and self._unit and not self.suffix():
|
|
303
|
+
self.set_null(False)
|
|
304
|
+
|
|
305
|
+
def stepBy(self, steps: int) -> None:
|
|
306
|
+
if self._null:
|
|
307
|
+
start = 0.0 if self.minimum() <= 0 <= self.maximum() else self.minimum()
|
|
308
|
+
self.blockSignals(True)
|
|
309
|
+
self.setValue(start)
|
|
310
|
+
self.blockSignals(False)
|
|
311
|
+
self.set_null(False)
|
|
312
|
+
self.null_changed.emit()
|
|
313
|
+
return
|
|
314
|
+
super().stepBy(steps)
|
|
315
|
+
|
|
316
|
+
def stepEnabled(self):
|
|
317
|
+
if self._null and not self.isReadOnly():
|
|
318
|
+
return self.StepEnabledFlag.StepUpEnabled | self.StepEnabledFlag.StepDownEnabled
|
|
319
|
+
return super().stepEnabled()
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
class _ValueButton(QToolButton):
|
|
323
|
+
"""Kompakte Wertanzeige des Inline-Modus; Klick, Eingabe, Leertaste oder F2 öffnen den Editor."""
|
|
324
|
+
|
|
325
|
+
activated = Signal()
|
|
326
|
+
|
|
327
|
+
def __init__(self, parent: QWidget | None = None) -> None:
|
|
328
|
+
super().__init__(parent)
|
|
329
|
+
self.setAutoRaise(True)
|
|
330
|
+
self.setCursor(Qt.CursorShape.IBeamCursor)
|
|
331
|
+
self.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
|
|
332
|
+
self.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextOnly)
|
|
333
|
+
font = self.font()
|
|
334
|
+
font.setWeight(QFont.Weight.DemiBold)
|
|
335
|
+
self.setFont(font)
|
|
336
|
+
self.setToolTip("Zum Bearbeiten klicken (oder Eingabe/F2)")
|
|
337
|
+
self.clicked.connect(self.activated)
|
|
338
|
+
|
|
339
|
+
def keyPressEvent(self, event: QKeyEvent) -> None:
|
|
340
|
+
if event.key() in (Qt.Key.Key_Return, Qt.Key.Key_Enter, Qt.Key.Key_F2):
|
|
341
|
+
self.activated.emit()
|
|
342
|
+
return
|
|
343
|
+
super().keyPressEvent(event)
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
class _NumberEditor(_ValueWidget):
|
|
347
|
+
"""Gemeinsame Logik der Zahleneditoren.
|
|
348
|
+
|
|
349
|
+
Programmatisch gesetzte Werte bleiben exakt erhalten, auch wenn die Anzeige weniger
|
|
350
|
+
Nachkommastellen hat. Ein gesetzter Wert außerhalb der Grenzen wird nicht gekappt; der
|
|
351
|
+
Eingabebereich wird vorübergehend erweitert.
|
|
352
|
+
"""
|
|
353
|
+
|
|
354
|
+
integer = False
|
|
355
|
+
OPTIONS: tuple[str, ...] = ("optional", "minimum", "maximum", "step", "precision", "unit", "inline_edit")
|
|
356
|
+
|
|
357
|
+
def __init__(self, parent: QWidget | None = None) -> None:
|
|
358
|
+
super().__init__(parent)
|
|
359
|
+
self._exact: int | float | None = 0
|
|
360
|
+
self._minimum: int | float | None = None
|
|
361
|
+
self._maximum: int | float | None = None
|
|
362
|
+
self._step: int | float | None = None
|
|
363
|
+
self._precision: int | None = None
|
|
364
|
+
self._unit = ""
|
|
365
|
+
self._inline_edit = False
|
|
366
|
+
self._inline_start: int | float | None = None
|
|
367
|
+
self._layout = QHBoxLayout(self)
|
|
368
|
+
self._layout.setContentsMargins(0, 0, 0, 0)
|
|
369
|
+
self._layout.setSpacing(8)
|
|
370
|
+
self._spin = _SpinBox(self)
|
|
371
|
+
_guard_wheel(self._spin)
|
|
372
|
+
self._display = _ValueButton(self)
|
|
373
|
+
self._display.activated.connect(self.start_inline_edit)
|
|
374
|
+
self._display.hide()
|
|
375
|
+
self._value_host = QWidget(self)
|
|
376
|
+
host = QHBoxLayout(self._value_host)
|
|
377
|
+
host.setContentsMargins(0, 0, 0, 0)
|
|
378
|
+
host.setSpacing(0)
|
|
379
|
+
host.addWidget(self._spin)
|
|
380
|
+
host.addWidget(self._display)
|
|
381
|
+
self._layout.addWidget(self._value_host)
|
|
382
|
+
self._layout.addStretch(1)
|
|
383
|
+
self.setFocusProxy(self._spin)
|
|
384
|
+
self._spin.installEventFilter(self)
|
|
385
|
+
with self._programmatic():
|
|
386
|
+
self._apply_options()
|
|
387
|
+
self._spin.valueChanged.connect(self._on_value_changed)
|
|
388
|
+
self._spin.null_changed.connect(self._on_spin)
|
|
389
|
+
self._spin.editingFinished.connect(self._finish_inline_edit)
|
|
390
|
+
|
|
391
|
+
# -- Konfiguration -------------------------------------------------------------------
|
|
392
|
+
@property
|
|
393
|
+
def spin_box(self) -> QDoubleSpinBox:
|
|
394
|
+
"""Die eigentliche Spinbox (auch bei Ganzzahlen eine ``QDoubleSpinBox``)."""
|
|
395
|
+
return self._spin
|
|
396
|
+
|
|
397
|
+
def set_range(self, minimum: int | float | None, maximum: int | float | None) -> None:
|
|
398
|
+
self.configure(minimum=minimum, maximum=maximum)
|
|
399
|
+
|
|
400
|
+
def minimum(self) -> int | float | None:
|
|
401
|
+
return self._minimum
|
|
402
|
+
|
|
403
|
+
def maximum(self) -> int | float | None:
|
|
404
|
+
return self._maximum
|
|
405
|
+
|
|
406
|
+
def set_step(self, step: int | float | None) -> None:
|
|
407
|
+
self.configure(step=step)
|
|
408
|
+
|
|
409
|
+
def set_precision(self, precision: int | None) -> None:
|
|
410
|
+
self.configure(precision=precision)
|
|
411
|
+
|
|
412
|
+
def set_unit(self, unit: str) -> None:
|
|
413
|
+
self.configure(unit=unit)
|
|
414
|
+
|
|
415
|
+
def unit(self) -> str:
|
|
416
|
+
return self._unit
|
|
417
|
+
|
|
418
|
+
def _set_option(self, key: str, value: Any) -> None:
|
|
419
|
+
if key in ("minimum", "maximum", "step") and value is not None:
|
|
420
|
+
if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value):
|
|
421
|
+
raise ValueError(f"{key}: Zahl oder None erwartet")
|
|
422
|
+
if key == "step" and value <= 0:
|
|
423
|
+
raise ValueError("step muss größer als 0 sein")
|
|
424
|
+
if key == "precision" and value is not None:
|
|
425
|
+
if isinstance(value, bool) or not isinstance(value, int) or not 0 <= value <= 12:
|
|
426
|
+
raise ValueError("precision: Ganzzahl von 0 bis 12 oder None erwartet")
|
|
427
|
+
if key == "unit":
|
|
428
|
+
value = "" if value is None else str(value)
|
|
429
|
+
if key == "inline_edit":
|
|
430
|
+
value = bool(value)
|
|
431
|
+
super()._set_option(key, value)
|
|
432
|
+
if self._minimum is not None and self._maximum is not None and self._minimum > self._maximum:
|
|
433
|
+
raise ValueError("minimum ist größer als maximum")
|
|
434
|
+
|
|
435
|
+
def precision(self) -> int:
|
|
436
|
+
"""Angezeigte Nachkommastellen (aus ``precision`` oder abgeleitet aus ``step``)."""
|
|
437
|
+
if self.integer:
|
|
438
|
+
return 0
|
|
439
|
+
if self._precision is not None:
|
|
440
|
+
return self._precision
|
|
441
|
+
if self._step is not None:
|
|
442
|
+
exponent = Decimal(str(self._step)).normalize().as_tuple().exponent
|
|
443
|
+
return max(_DEFAULT_PRECISION if self._step >= 1 else 0, -int(exponent))
|
|
444
|
+
return _DEFAULT_PRECISION
|
|
445
|
+
|
|
446
|
+
def step(self) -> float:
|
|
447
|
+
if self._step is not None:
|
|
448
|
+
return self._step
|
|
449
|
+
return 1 if self.integer else 10 ** -self.precision()
|
|
450
|
+
|
|
451
|
+
def bounds(self) -> tuple[float, float]:
|
|
452
|
+
limit = MAX_SAFE_INT if self.integer else _FLOAT_LIMIT
|
|
453
|
+
return (-limit if self._minimum is None else self._minimum, limit if self._maximum is None else self._maximum)
|
|
454
|
+
|
|
455
|
+
def _apply_options(self) -> None:
|
|
456
|
+
value = self._exact
|
|
457
|
+
self._spin.allow_null = self._optional
|
|
458
|
+
self._spin.setDecimals(self.precision())
|
|
459
|
+
self._spin.setSingleStep(self.step())
|
|
460
|
+
self._spin.set_unit(self._unit)
|
|
461
|
+
self._apply_value(value)
|
|
462
|
+
if self._minimum is not None and self._maximum is not None:
|
|
463
|
+
self._spin.setToolTip(f"{_format_number(self._minimum)} bis {_format_number(self._maximum)} {self._unit}".strip())
|
|
464
|
+
else:
|
|
465
|
+
self._spin.setToolTip("")
|
|
466
|
+
if not self._inline_edit:
|
|
467
|
+
self._display.hide()
|
|
468
|
+
self._spin.show()
|
|
469
|
+
self.setFocusProxy(self._spin)
|
|
470
|
+
elif not self._spin.hasFocus():
|
|
471
|
+
self._show_display()
|
|
472
|
+
|
|
473
|
+
# -- Wert ----------------------------------------------------------------------------
|
|
474
|
+
def value(self) -> int | float | None:
|
|
475
|
+
return self._exact
|
|
476
|
+
|
|
477
|
+
def commit(self) -> None:
|
|
478
|
+
self._spin.interpretText()
|
|
479
|
+
|
|
480
|
+
def _from_spin(self) -> int | float:
|
|
481
|
+
raw = self._spin.value()
|
|
482
|
+
return int(round(raw)) if self.integer else round(raw, self.precision())
|
|
483
|
+
|
|
484
|
+
def _on_value_changed(self, _: float) -> None:
|
|
485
|
+
if not self._silent:
|
|
486
|
+
self._spin.leave_null()
|
|
487
|
+
self._on_spin()
|
|
488
|
+
|
|
489
|
+
def _on_spin(self) -> None:
|
|
490
|
+
if self._silent:
|
|
491
|
+
return
|
|
492
|
+
self._exact = None if self._spin.is_null() else self._from_spin()
|
|
493
|
+
self._restore_range()
|
|
494
|
+
self._after_spin()
|
|
495
|
+
self._update_display()
|
|
496
|
+
self._user_changed()
|
|
497
|
+
|
|
498
|
+
def _after_spin(self) -> None:
|
|
499
|
+
"""Erweiterungspunkt: weitere Darstellungen nachführen."""
|
|
500
|
+
|
|
501
|
+
def _restore_range(self) -> None:
|
|
502
|
+
low, high = self.bounds()
|
|
503
|
+
if self._exact is not None and low <= self._exact <= high:
|
|
504
|
+
if (self._spin.minimum(), self._spin.maximum()) != (low, high):
|
|
505
|
+
with self._programmatic():
|
|
506
|
+
self._spin.setRange(low, high)
|
|
507
|
+
|
|
508
|
+
def _check_type(self, value: Any) -> None:
|
|
509
|
+
if value is None:
|
|
510
|
+
return
|
|
511
|
+
if isinstance(value, bool) or not isinstance(value, int if self.integer else (int, float)):
|
|
512
|
+
raise ValueError("Ganzzahl erwartet" if self.integer else "Zahl erwartet")
|
|
513
|
+
if not math.isfinite(value):
|
|
514
|
+
raise ValueError("Endliche Zahl erwartet")
|
|
515
|
+
if abs(value) > (MAX_SAFE_INT if self.integer else _FLOAT_LIMIT):
|
|
516
|
+
raise ValueError("Zahl außerhalb des darstellbaren Bereichs")
|
|
517
|
+
|
|
518
|
+
def _apply_value(self, value: Any) -> None:
|
|
519
|
+
self._check_type(value)
|
|
520
|
+
self._exact = value
|
|
521
|
+
if value is None:
|
|
522
|
+
self._spin.set_null(True)
|
|
523
|
+
else:
|
|
524
|
+
low, high = self.bounds()
|
|
525
|
+
self._spin.setRange(min(low, value), max(high, value))
|
|
526
|
+
self._spin.setValue(value)
|
|
527
|
+
self._spin.set_null(False)
|
|
528
|
+
self._update_display()
|
|
529
|
+
|
|
530
|
+
def _apply_read_only(self, read_only: bool) -> None:
|
|
531
|
+
if read_only:
|
|
532
|
+
self._finish_inline_edit()
|
|
533
|
+
self._spin.setReadOnly(read_only)
|
|
534
|
+
buttons = QDoubleSpinBox.ButtonSymbols
|
|
535
|
+
self._spin.setButtonSymbols(buttons.NoButtons if read_only else buttons.UpDownArrows)
|
|
536
|
+
self._display.setEnabled(not read_only)
|
|
537
|
+
|
|
538
|
+
# -- Inline-Modus --------------------------------------------------------------------
|
|
539
|
+
def display_text(self) -> str:
|
|
540
|
+
"""Angezeigter Wert samt Einheit („–“, wenn nicht gesetzt)."""
|
|
541
|
+
if self._exact is None:
|
|
542
|
+
return "–"
|
|
543
|
+
number = self._spin.locale().toString(float(self._exact), "f", self.precision())
|
|
544
|
+
return f"{number} {self._unit}" if self._unit else number
|
|
545
|
+
|
|
546
|
+
def _update_display(self) -> None:
|
|
547
|
+
text = self.display_text()
|
|
548
|
+
self._display.setText(text)
|
|
549
|
+
self._display.setAccessibleName(f"Wert {text}, zum Bearbeiten aktivieren")
|
|
550
|
+
|
|
551
|
+
def _show_display(self) -> None:
|
|
552
|
+
self._update_display()
|
|
553
|
+
self._spin.hide()
|
|
554
|
+
self._display.show()
|
|
555
|
+
self.setFocusProxy(self._display)
|
|
556
|
+
|
|
557
|
+
def is_inline_editing(self) -> bool:
|
|
558
|
+
"""Im Inline-Modus: ist der Editor gerade geöffnet?"""
|
|
559
|
+
return self._inline_edit and not self._spin.isHidden()
|
|
560
|
+
|
|
561
|
+
def start_inline_edit(self) -> None:
|
|
562
|
+
"""Öffnet im Inline-Modus die Spinbox (Eingabe übernimmt, Esc stellt den alten Wert her)."""
|
|
563
|
+
if not self._inline_edit or self._read_only:
|
|
564
|
+
return
|
|
565
|
+
self._inline_start = self._exact
|
|
566
|
+
self._display.hide()
|
|
567
|
+
self._spin.show()
|
|
568
|
+
self.setFocusProxy(self._spin)
|
|
569
|
+
self._spin.setFocus(Qt.FocusReason.OtherFocusReason)
|
|
570
|
+
self._spin.selectAll()
|
|
571
|
+
|
|
572
|
+
def _finish_inline_edit(self) -> None:
|
|
573
|
+
if self._inline_edit and not self._spin.isHidden():
|
|
574
|
+
had_focus = self._spin.hasFocus()
|
|
575
|
+
self._show_display()
|
|
576
|
+
if had_focus:
|
|
577
|
+
self._display.setFocus(Qt.FocusReason.OtherFocusReason)
|
|
578
|
+
|
|
579
|
+
def _cancel_inline_edit(self) -> None:
|
|
580
|
+
start = self._inline_start
|
|
581
|
+
if not _same(start, self._exact):
|
|
582
|
+
with self._programmatic():
|
|
583
|
+
self._apply_value(start)
|
|
584
|
+
self._after_spin()
|
|
585
|
+
self._user_changed()
|
|
586
|
+
self._finish_inline_edit()
|
|
587
|
+
|
|
588
|
+
def eventFilter(self, obj: QObject, event: QEvent) -> bool:
|
|
589
|
+
spin = getattr(self, "_spin", None)
|
|
590
|
+
if obj is spin and spin is not None and self._inline_edit and event.type() == QEvent.Type.KeyPress:
|
|
591
|
+
if event.key() == Qt.Key.Key_Escape:
|
|
592
|
+
self._cancel_inline_edit()
|
|
593
|
+
return True
|
|
594
|
+
if event.key() in (Qt.Key.Key_Return, Qt.Key.Key_Enter):
|
|
595
|
+
self.commit()
|
|
596
|
+
self._finish_inline_edit()
|
|
597
|
+
return True
|
|
598
|
+
return super().eventFilter(obj, event)
|
|
599
|
+
# ── Ende Zahlenkern ──
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
class IntSpinBox(_NumberEditor):
|
|
603
|
+
"""Ganzzahlen mit Grenzen, Schrittweite und Einheit."""
|
|
604
|
+
|
|
605
|
+
integer = True
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
def _demo() -> None: # pragma: no cover – python int_spin_box.py
|
|
609
|
+
from PySide6.QtWidgets import QApplication, QFormLayout
|
|
610
|
+
|
|
611
|
+
app = QApplication.instance() or QApplication([])
|
|
612
|
+
host = QWidget()
|
|
613
|
+
form = QFormLayout(host)
|
|
614
|
+
normal, inline = IntSpinBox(), IntSpinBox()
|
|
615
|
+
normal.configure(minimum=0, maximum=5000, step=50, unit="ms")
|
|
616
|
+
inline.configure(minimum=1, maximum=16, inline_edit=True)
|
|
617
|
+
normal.set_value(250)
|
|
618
|
+
inline.set_value(4)
|
|
619
|
+
for label, editor in (("Verzögerung", normal), ("Sitzungen (inline)", inline)):
|
|
620
|
+
editor.value_edited.connect(lambda v, n=label: print(n, v))
|
|
621
|
+
form.addRow(label, editor)
|
|
622
|
+
host.show()
|
|
623
|
+
app.exec()
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
if __name__ == "__main__":
|
|
627
|
+
_demo()
|