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,67 @@
|
|
|
1
|
+
"""Autarke Einzeldatei-Widgets.
|
|
2
|
+
|
|
3
|
+
Jede Datei in diesem Ordner ist für sich vollständig: Sie benötigt nur die
|
|
4
|
+
Python-Standardbibliothek und PySide6 und importiert keine andere Datei des
|
|
5
|
+
Projekts. Zum Übernehmen in ein anderes Projekt genügt es, die eine Datei zu
|
|
6
|
+
kopieren (siehe docs/EINZELWIDGETS.md).
|
|
7
|
+
|
|
8
|
+
Dieses Paket-``__init__`` dient nur der Bequemlichkeit innerhalb des Frameworks;
|
|
9
|
+
die Dateien selbst wissen nichts voneinander.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from .action_button import ActionButton
|
|
13
|
+
from .auth_widget import AuthWidget
|
|
14
|
+
from .check_box_editor import CheckBoxEditor
|
|
15
|
+
from .choice_buttons_editor import ChoiceButtonsEditor, FlowLayout
|
|
16
|
+
from .color_editor import ColorEditor
|
|
17
|
+
from .dropdown_editor import DropdownEditor
|
|
18
|
+
from .editable_label import EditableLabel
|
|
19
|
+
from .float_spin_box import FloatSpinBox
|
|
20
|
+
from .font_editor import FontEditor
|
|
21
|
+
from .hotkey_editor import HotkeyEditor
|
|
22
|
+
from .info_label import InfoLabel
|
|
23
|
+
from .int_spin_box import IntSpinBox
|
|
24
|
+
from .multi_choice_editor import MultiChoiceEditor
|
|
25
|
+
from .password_editor import STORED_SECRET, PasswordEditor
|
|
26
|
+
from .path_editor import PathEditor, check_path
|
|
27
|
+
from .protected_group_box import ProtectedGroupBox
|
|
28
|
+
from .server_connection_editor import ConnectionInputError, ServerConnectionEditor, compose_url
|
|
29
|
+
from .slider_spin_box import SliderSpinBox
|
|
30
|
+
from .sound_selection_widget import SoundSelectionWidget
|
|
31
|
+
from .status_label import StatusLabel
|
|
32
|
+
from .switch_editor import SwitchEditor
|
|
33
|
+
from .text_editor import TextEditor
|
|
34
|
+
|
|
35
|
+
#: Datei → öffentliche Klasse(n); Grundlage für Übersicht und Isolationstests.
|
|
36
|
+
FILES: dict[str, tuple[str, ...]] = {
|
|
37
|
+
"text_editor.py": ("TextEditor",),
|
|
38
|
+
"password_editor.py": ("PasswordEditor",),
|
|
39
|
+
"int_spin_box.py": ("IntSpinBox",),
|
|
40
|
+
"float_spin_box.py": ("FloatSpinBox",),
|
|
41
|
+
"slider_spin_box.py": ("SliderSpinBox",),
|
|
42
|
+
"hotkey_editor.py": ("HotkeyEditor",),
|
|
43
|
+
"check_box_editor.py": ("CheckBoxEditor",),
|
|
44
|
+
"switch_editor.py": ("SwitchEditor",),
|
|
45
|
+
"dropdown_editor.py": ("DropdownEditor",),
|
|
46
|
+
"choice_buttons_editor.py": ("ChoiceButtonsEditor", "FlowLayout"),
|
|
47
|
+
"multi_choice_editor.py": ("MultiChoiceEditor",),
|
|
48
|
+
"color_editor.py": ("ColorEditor",),
|
|
49
|
+
"font_editor.py": ("FontEditor",),
|
|
50
|
+
"path_editor.py": ("PathEditor",),
|
|
51
|
+
"server_connection_editor.py": ("ServerConnectionEditor",),
|
|
52
|
+
"editable_label.py": ("EditableLabel",),
|
|
53
|
+
"sound_selection_widget.py": ("SoundSelectionWidget",),
|
|
54
|
+
"action_button.py": ("ActionButton",),
|
|
55
|
+
"info_label.py": ("InfoLabel",),
|
|
56
|
+
"status_label.py": ("StatusLabel",),
|
|
57
|
+
"auth_widget.py": ("AuthWidget",),
|
|
58
|
+
"protected_group_box.py": ("ProtectedGroupBox",),
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
__all__ = [
|
|
62
|
+
"ActionButton", "AuthWidget", "CheckBoxEditor", "ChoiceButtonsEditor", "ColorEditor", "ConnectionInputError",
|
|
63
|
+
"DropdownEditor", "EditableLabel", "FILES", "FloatSpinBox", "FlowLayout", "FontEditor", "HotkeyEditor", "InfoLabel",
|
|
64
|
+
"IntSpinBox", "MultiChoiceEditor", "PasswordEditor", "PathEditor", "ProtectedGroupBox", "STORED_SECRET",
|
|
65
|
+
"ServerConnectionEditor", "SliderSpinBox", "SoundSelectionWidget", "StatusLabel", "SwitchEditor", "TextEditor",
|
|
66
|
+
"check_path", "compose_url",
|
|
67
|
+
] # fmt: skip
|
|
@@ -0,0 +1,200 @@
|
|
|
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
|
+
"""ActionButton – Schaltfläche mit Busy-Zustand und sichtbarer Rückmeldung.
|
|
24
|
+
|
|
25
|
+
Autarke Einzeldatei: benötigt nur Python ≥ 3.11 und PySide6 (Essentials).
|
|
26
|
+
|
|
27
|
+
Beispiel::
|
|
28
|
+
|
|
29
|
+
from action_button import ActionButton
|
|
30
|
+
|
|
31
|
+
button = ActionButton("Mikrofon testen")
|
|
32
|
+
def run():
|
|
33
|
+
button.set_busy(True) # verhindert Doppelauslösung
|
|
34
|
+
... # Arbeit (z. B. in einem Thread)
|
|
35
|
+
button.set_busy(False)
|
|
36
|
+
button.show_result(True, "Pegel in Ordnung")
|
|
37
|
+
button.triggered.connect(run)
|
|
38
|
+
|
|
39
|
+
``set_allowed(False)`` sperrt die Aktion (z. B. fehlende Berechtigung). Die Rückmeldung
|
|
40
|
+
trägt ein Symbol (✔/✖/⟳), nicht nur eine Farbe.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
from __future__ import annotations
|
|
44
|
+
|
|
45
|
+
from PySide6.QtCore import QEvent, Qt, QTimer, Signal
|
|
46
|
+
from PySide6.QtGui import QColor, QGuiApplication, QPalette
|
|
47
|
+
from PySide6.QtWidgets import QHBoxLayout, QLabel, QPushButton, QWidget
|
|
48
|
+
|
|
49
|
+
__all__ = ["ActionButton"]
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
# ── Statustext ─ identisch in allen Dateien mit Statusanzeige (bewusst kopiert, Test prüft Gleichheit) ──
|
|
53
|
+
_ROLE_COLORS = {
|
|
54
|
+
# Rolle: (hell, dunkel) – Kontrast ≥ 4,5:1 auf Standardhintergründen
|
|
55
|
+
"accent": ("#1f6feb", "#5aa2ff"),
|
|
56
|
+
"muted": ("#5f6773", "#a3abb8"),
|
|
57
|
+
"success": ("#1a7a37", "#6fd08c"),
|
|
58
|
+
"warning": ("#8a5a00", "#f0b44c"),
|
|
59
|
+
"error": ("#b3261e", "#ff8f87"),
|
|
60
|
+
"busy": ("#1f5fbf", "#7db4ff"),
|
|
61
|
+
"locked": ("#5f6773", "#a3abb8"),
|
|
62
|
+
}
|
|
63
|
+
_ROLE_SYMBOLS = {"success": "✔", "error": "✖", "warning": "⚠", "busy": "⟳", "info": "ℹ", "locked": "🔒", "plain": ""}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _base_palette(widget: QWidget) -> QPalette:
|
|
67
|
+
"""Ausdrücklich gesetzte Fensterpalette, sonst die der Anwendung (Stylesheets verfälschen die eigene)."""
|
|
68
|
+
window = widget.window()
|
|
69
|
+
return window.palette() if window.testAttribute(Qt.WidgetAttribute.WA_SetPalette) else QGuiApplication.palette()
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _role_color(widget: QWidget, role: str) -> QColor:
|
|
73
|
+
light, dark = _ROLE_COLORS.get(role, _ROLE_COLORS["muted"])
|
|
74
|
+
return QColor(dark if _base_palette(widget).color(QPalette.ColorRole.Window).lightness() < 128 else light)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class _StatusText(QLabel):
|
|
78
|
+
"""Einzeiliger oder umbrechender Statustext: ``show_message(text, role)``."""
|
|
79
|
+
|
|
80
|
+
_role = "plain" # Klassenwert: Qt kann Ereignisse schon während der Konstruktion liefern
|
|
81
|
+
|
|
82
|
+
def __init__(self, parent: QWidget | None = None) -> None:
|
|
83
|
+
super().__init__(parent)
|
|
84
|
+
self._role = "plain"
|
|
85
|
+
self.setTextFormat(Qt.TextFormat.PlainText)
|
|
86
|
+
self.setWordWrap(True)
|
|
87
|
+
hints = QGuiApplication.styleHints()
|
|
88
|
+
if hasattr(hints, "colorSchemeChanged"):
|
|
89
|
+
hints.colorSchemeChanged.connect(self._scheme_changed)
|
|
90
|
+
|
|
91
|
+
def _scheme_changed(self, *_: object) -> None:
|
|
92
|
+
# Paletten werden je nach Stil verzögert aktualisiert; deshalb zweimal abgleichen.
|
|
93
|
+
for delay in (0, 150):
|
|
94
|
+
QTimer.singleShot(delay, self, self._recolor)
|
|
95
|
+
|
|
96
|
+
def show_message(self, text: str, role: str = "plain") -> None:
|
|
97
|
+
symbol = _ROLE_SYMBOLS.get(role, "")
|
|
98
|
+
self.setText(f"{symbol} {text}".strip() if text else "")
|
|
99
|
+
self._role = role
|
|
100
|
+
self._recolor()
|
|
101
|
+
|
|
102
|
+
def role(self) -> str:
|
|
103
|
+
return self._role
|
|
104
|
+
|
|
105
|
+
def _recolor(self) -> None:
|
|
106
|
+
sheet = ""
|
|
107
|
+
if self._role not in ("plain", ""):
|
|
108
|
+
sheet = f"color: {_role_color(self, 'accent' if self._role == 'info' else self._role).name()};"
|
|
109
|
+
if sheet != self.styleSheet():
|
|
110
|
+
self.setStyleSheet(sheet)
|
|
111
|
+
|
|
112
|
+
def changeEvent(self, event: QEvent) -> None:
|
|
113
|
+
super().changeEvent(event)
|
|
114
|
+
if event.type() == QEvent.Type.PaletteChange:
|
|
115
|
+
self._recolor()
|
|
116
|
+
|
|
117
|
+
def showEvent(self, event: QEvent) -> None:
|
|
118
|
+
super().showEvent(event)
|
|
119
|
+
self._recolor()
|
|
120
|
+
# ── Ende Statustext ──
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
class ActionButton(QWidget):
|
|
124
|
+
"""Schaltfläche mit Busy-Zustand und Ergebnisanzeige."""
|
|
125
|
+
|
|
126
|
+
triggered = Signal()
|
|
127
|
+
|
|
128
|
+
def __init__(
|
|
129
|
+
self, text: str = "Ausführen", parent: QWidget | None = None, *, show_result: bool = True, primary: bool = False
|
|
130
|
+
) -> None:
|
|
131
|
+
super().__init__(parent)
|
|
132
|
+
self._text = text
|
|
133
|
+
self._allowed = True
|
|
134
|
+
self._busy = False
|
|
135
|
+
layout = QHBoxLayout(self)
|
|
136
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
137
|
+
layout.setSpacing(8)
|
|
138
|
+
self._button = QPushButton(text.replace("&", "&&"), self)
|
|
139
|
+
self._button.setAutoDefault(False)
|
|
140
|
+
if primary:
|
|
141
|
+
font = self._button.font()
|
|
142
|
+
font.setBold(True)
|
|
143
|
+
self._button.setFont(font)
|
|
144
|
+
self._button.clicked.connect(self._on_clicked)
|
|
145
|
+
self._result = _StatusText(self)
|
|
146
|
+
self._result.setVisible(show_result)
|
|
147
|
+
layout.addWidget(self._button)
|
|
148
|
+
layout.addWidget(self._result, 1)
|
|
149
|
+
if not show_result:
|
|
150
|
+
layout.addStretch(1)
|
|
151
|
+
self.setFocusProxy(self._button)
|
|
152
|
+
|
|
153
|
+
@property
|
|
154
|
+
def button(self) -> QPushButton:
|
|
155
|
+
return self._button
|
|
156
|
+
|
|
157
|
+
def _on_clicked(self) -> None:
|
|
158
|
+
if not self._busy and self._allowed:
|
|
159
|
+
self.triggered.emit()
|
|
160
|
+
|
|
161
|
+
def set_busy(self, busy: bool, message: str = "Läuft …") -> None:
|
|
162
|
+
self._busy = busy
|
|
163
|
+
if busy:
|
|
164
|
+
self._result.show_message(message, "busy")
|
|
165
|
+
self._button.setEnabled(self._allowed and not busy)
|
|
166
|
+
|
|
167
|
+
def is_busy(self) -> bool:
|
|
168
|
+
return self._busy
|
|
169
|
+
|
|
170
|
+
def set_allowed(self, allowed: bool) -> None:
|
|
171
|
+
self._allowed = allowed
|
|
172
|
+
self._button.setEnabled(allowed and not self._busy)
|
|
173
|
+
|
|
174
|
+
def show_result(self, ok: bool, message: str) -> None:
|
|
175
|
+
self._result.show_message(message or ("Erledigt" if ok else "Fehlgeschlagen"), "success" if ok else "error")
|
|
176
|
+
|
|
177
|
+
def clear_result(self) -> None:
|
|
178
|
+
self._result.show_message("")
|
|
179
|
+
|
|
180
|
+
def result_text(self) -> str:
|
|
181
|
+
return self._result.text()
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def _demo() -> None: # pragma: no cover – python action_button.py
|
|
185
|
+
from PySide6.QtWidgets import QApplication
|
|
186
|
+
|
|
187
|
+
app = QApplication.instance() or QApplication([])
|
|
188
|
+
button = ActionButton("Test starten")
|
|
189
|
+
|
|
190
|
+
def run() -> None:
|
|
191
|
+
button.set_busy(True)
|
|
192
|
+
QTimer.singleShot(800, lambda: (button.set_busy(False), button.show_result(True, "Fertig")))
|
|
193
|
+
|
|
194
|
+
button.triggered.connect(run)
|
|
195
|
+
button.show()
|
|
196
|
+
app.exec()
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
if __name__ == "__main__":
|
|
200
|
+
_demo()
|
|
@@ -0,0 +1,377 @@
|
|
|
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
|
+
"""AuthWidget – Eingabe von Zugangsdaten, Prüfaktion und sichtbarer Sperrstatus.
|
|
24
|
+
|
|
25
|
+
Autarke Einzeldatei: benötigt nur Python ≥ 3.11 und PySide6 (Essentials). Enthält keine
|
|
26
|
+
Prüflogik: Die Anwendung prüft die Zugangsdaten selbst und meldet das Ergebnis zurück.
|
|
27
|
+
|
|
28
|
+
Beispiel::
|
|
29
|
+
|
|
30
|
+
from auth_widget import AuthWidget
|
|
31
|
+
|
|
32
|
+
auth = AuthWidget([("user", "Benutzer", False), ("key", "Schlüssel", True)])
|
|
33
|
+
def check(credentials: dict[str, str]) -> None:
|
|
34
|
+
auth.set_state("checking")
|
|
35
|
+
ok = my_server_check(credentials) # eigene Prüfung, z. B. in einem Thread
|
|
36
|
+
auth.set_state("granted" if ok else "failed", "" if ok else "Schlüssel abgelehnt")
|
|
37
|
+
auth.auth_requested.connect(check)
|
|
38
|
+
auth.credentials_edited.connect(lambda: auth.set_state("locked")) # Freigabe aufheben
|
|
39
|
+
auth.lock_requested.connect(lambda: auth.set_state("locked", "Manuell gesperrt"))
|
|
40
|
+
|
|
41
|
+
Felder: ``(name, beschriftung, geheim)``, ``{"name", "label", "secret"}`` oder Objekte mit
|
|
42
|
+
diesen Attributen. Geheimfelder sind verdeckt und werden nach dem Absenden geleert; das
|
|
43
|
+
Widget speichert nichts. Zustände: ``locked``, ``checking``, ``granted``, ``failed``.
|
|
44
|
+
Bei einem einzelnen Feld stehen Feld, Knopf und Status in einer Zeile (kompakt).
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
from __future__ import annotations
|
|
48
|
+
|
|
49
|
+
from collections.abc import Iterable
|
|
50
|
+
from typing import Any
|
|
51
|
+
|
|
52
|
+
from PySide6.QtCore import QByteArray, QEvent, QObject, QTimer, Qt, Signal
|
|
53
|
+
from PySide6.QtGui import QColor, QGuiApplication, QIcon, QPainter, QPalette, QPixmap
|
|
54
|
+
from PySide6.QtSvg import QSvgRenderer
|
|
55
|
+
from PySide6.QtWidgets import QGridLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QToolButton, QVBoxLayout, QWidget
|
|
56
|
+
|
|
57
|
+
__all__ = ["AuthWidget"]
|
|
58
|
+
|
|
59
|
+
_EYE = (
|
|
60
|
+
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path d="M1 8s2.6-5 7-5 7 5 7 5-2.6 5-7 5-7-5-7-5z" '
|
|
61
|
+
'fill="none" stroke="currentColor" stroke-width="1.4" stroke-linejoin="round"/>'
|
|
62
|
+
'<circle cx="8" cy="8" r="2.2" fill="currentColor"/></svg>'
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# ── Symbole ─ identisch in allen Dateien mit eingebetteten Symbolen (bewusst kopiert) ──
|
|
67
|
+
def _svg_icon(svg: str, widget: QWidget, size: int = 16) -> QIcon:
|
|
68
|
+
"""Färbt ein eingebettetes SVG (``currentColor``) mit der Palette ein, auch für „deaktiviert“."""
|
|
69
|
+
icon = QIcon()
|
|
70
|
+
ratio = max(1.0, widget.devicePixelRatioF())
|
|
71
|
+
palette = widget.palette()
|
|
72
|
+
for mode, group in ((QIcon.Mode.Normal, QPalette.ColorGroup.Active), (QIcon.Mode.Disabled, QPalette.ColorGroup.Disabled)):
|
|
73
|
+
color = palette.color(group, QPalette.ColorRole.ButtonText).name()
|
|
74
|
+
renderer = QSvgRenderer(QByteArray(svg.replace("currentColor", color).encode()))
|
|
75
|
+
pixmap = QPixmap(int(size * ratio), int(size * ratio))
|
|
76
|
+
pixmap.fill(Qt.GlobalColor.transparent)
|
|
77
|
+
painter = QPainter(pixmap)
|
|
78
|
+
renderer.render(painter)
|
|
79
|
+
painter.end()
|
|
80
|
+
pixmap.setDevicePixelRatio(ratio)
|
|
81
|
+
icon.addPixmap(pixmap, mode)
|
|
82
|
+
return icon
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class _SchemeFollower(QObject):
|
|
86
|
+
"""Ruft ``callback`` nach einem Wechsel des Farbschemas erneut auf.
|
|
87
|
+
|
|
88
|
+
Paletten werden je nach Stil verzögert aktualisiert, deshalb zweimal (sofort und nach
|
|
89
|
+
150 ms). Als Kindobjekt mit gebundener Methode verbunden: Nach dem Löschen des Widgets
|
|
90
|
+
wird nichts mehr aufgerufen.
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
def __init__(self, parent: QWidget, callback) -> None:
|
|
94
|
+
super().__init__(parent)
|
|
95
|
+
self._callback = callback
|
|
96
|
+
hints = QGuiApplication.styleHints()
|
|
97
|
+
if hasattr(hints, "colorSchemeChanged"):
|
|
98
|
+
hints.colorSchemeChanged.connect(self._changed)
|
|
99
|
+
|
|
100
|
+
def _changed(self, *_: object) -> None:
|
|
101
|
+
for delay in (0, 150):
|
|
102
|
+
QTimer.singleShot(delay, self, self._callback)
|
|
103
|
+
# ── Ende Symbole ──
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
# ── Statustext ─ identisch in allen Dateien mit Statusanzeige (bewusst kopiert, Test prüft Gleichheit) ──
|
|
107
|
+
_ROLE_COLORS = {
|
|
108
|
+
# Rolle: (hell, dunkel) – Kontrast ≥ 4,5:1 auf Standardhintergründen
|
|
109
|
+
"accent": ("#1f6feb", "#5aa2ff"),
|
|
110
|
+
"muted": ("#5f6773", "#a3abb8"),
|
|
111
|
+
"success": ("#1a7a37", "#6fd08c"),
|
|
112
|
+
"warning": ("#8a5a00", "#f0b44c"),
|
|
113
|
+
"error": ("#b3261e", "#ff8f87"),
|
|
114
|
+
"busy": ("#1f5fbf", "#7db4ff"),
|
|
115
|
+
"locked": ("#5f6773", "#a3abb8"),
|
|
116
|
+
}
|
|
117
|
+
_ROLE_SYMBOLS = {"success": "✔", "error": "✖", "warning": "⚠", "busy": "⟳", "info": "ℹ", "locked": "🔒", "plain": ""}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _base_palette(widget: QWidget) -> QPalette:
|
|
121
|
+
"""Ausdrücklich gesetzte Fensterpalette, sonst die der Anwendung (Stylesheets verfälschen die eigene)."""
|
|
122
|
+
window = widget.window()
|
|
123
|
+
return window.palette() if window.testAttribute(Qt.WidgetAttribute.WA_SetPalette) else QGuiApplication.palette()
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _role_color(widget: QWidget, role: str) -> QColor:
|
|
127
|
+
light, dark = _ROLE_COLORS.get(role, _ROLE_COLORS["muted"])
|
|
128
|
+
return QColor(dark if _base_palette(widget).color(QPalette.ColorRole.Window).lightness() < 128 else light)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
class _StatusText(QLabel):
|
|
132
|
+
"""Einzeiliger oder umbrechender Statustext: ``show_message(text, role)``."""
|
|
133
|
+
|
|
134
|
+
_role = "plain" # Klassenwert: Qt kann Ereignisse schon während der Konstruktion liefern
|
|
135
|
+
|
|
136
|
+
def __init__(self, parent: QWidget | None = None) -> None:
|
|
137
|
+
super().__init__(parent)
|
|
138
|
+
self._role = "plain"
|
|
139
|
+
self.setTextFormat(Qt.TextFormat.PlainText)
|
|
140
|
+
self.setWordWrap(True)
|
|
141
|
+
hints = QGuiApplication.styleHints()
|
|
142
|
+
if hasattr(hints, "colorSchemeChanged"):
|
|
143
|
+
hints.colorSchemeChanged.connect(self._scheme_changed)
|
|
144
|
+
|
|
145
|
+
def _scheme_changed(self, *_: object) -> None:
|
|
146
|
+
# Paletten werden je nach Stil verzögert aktualisiert; deshalb zweimal abgleichen.
|
|
147
|
+
for delay in (0, 150):
|
|
148
|
+
QTimer.singleShot(delay, self, self._recolor)
|
|
149
|
+
|
|
150
|
+
def show_message(self, text: str, role: str = "plain") -> None:
|
|
151
|
+
symbol = _ROLE_SYMBOLS.get(role, "")
|
|
152
|
+
self.setText(f"{symbol} {text}".strip() if text else "")
|
|
153
|
+
self._role = role
|
|
154
|
+
self._recolor()
|
|
155
|
+
|
|
156
|
+
def role(self) -> str:
|
|
157
|
+
return self._role
|
|
158
|
+
|
|
159
|
+
def _recolor(self) -> None:
|
|
160
|
+
sheet = ""
|
|
161
|
+
if self._role not in ("plain", ""):
|
|
162
|
+
sheet = f"color: {_role_color(self, 'accent' if self._role == 'info' else self._role).name()};"
|
|
163
|
+
if sheet != self.styleSheet():
|
|
164
|
+
self.setStyleSheet(sheet)
|
|
165
|
+
|
|
166
|
+
def changeEvent(self, event: QEvent) -> None:
|
|
167
|
+
super().changeEvent(event)
|
|
168
|
+
if event.type() == QEvent.Type.PaletteChange:
|
|
169
|
+
self._recolor()
|
|
170
|
+
|
|
171
|
+
def showEvent(self, event: QEvent) -> None:
|
|
172
|
+
super().showEvent(event)
|
|
173
|
+
self._recolor()
|
|
174
|
+
# ── Ende Statustext ──
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
class _SecretField(QWidget):
|
|
178
|
+
"""Verdecktes Eingabefeld mit Knopf zum bewussten Sichtbarmachen."""
|
|
179
|
+
|
|
180
|
+
edited = Signal()
|
|
181
|
+
|
|
182
|
+
def __init__(self, parent: QWidget | None = None) -> None:
|
|
183
|
+
super().__init__(parent)
|
|
184
|
+
layout = QHBoxLayout(self)
|
|
185
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
186
|
+
layout.setSpacing(2)
|
|
187
|
+
self._edit = QLineEdit(self)
|
|
188
|
+
self._edit.setEchoMode(QLineEdit.EchoMode.Password)
|
|
189
|
+
self._edit.setMinimumWidth(80)
|
|
190
|
+
self._edit.textEdited.connect(self.edited)
|
|
191
|
+
self._reveal = QToolButton(self)
|
|
192
|
+
self._reveal.setCheckable(True)
|
|
193
|
+
self._reveal.setAutoRaise(True)
|
|
194
|
+
self._reveal.toggled.connect(self._on_reveal)
|
|
195
|
+
layout.addWidget(self._edit, 1)
|
|
196
|
+
layout.addWidget(self._reveal)
|
|
197
|
+
self.setFocusProxy(self._edit)
|
|
198
|
+
_SchemeFollower(self, lambda: self._on_reveal(self._reveal.isChecked()))
|
|
199
|
+
self._on_reveal(False)
|
|
200
|
+
|
|
201
|
+
def _on_reveal(self, visible: bool) -> None:
|
|
202
|
+
self._edit.setEchoMode(QLineEdit.EchoMode.Normal if visible else QLineEdit.EchoMode.Password)
|
|
203
|
+
text = "Eingabe verbergen" if visible else "Eingabe anzeigen"
|
|
204
|
+
self._reveal.setToolTip(text)
|
|
205
|
+
self._reveal.setAccessibleName(text)
|
|
206
|
+
self._reveal.setIcon(_svg_icon(_EYE, self))
|
|
207
|
+
|
|
208
|
+
def changeEvent(self, event: QEvent) -> None:
|
|
209
|
+
super().changeEvent(event)
|
|
210
|
+
if event.type() == QEvent.Type.PaletteChange and hasattr(self, "_reveal"):
|
|
211
|
+
self._reveal.setIcon(_svg_icon(_EYE, self))
|
|
212
|
+
|
|
213
|
+
def text(self) -> str:
|
|
214
|
+
return self._edit.text()
|
|
215
|
+
|
|
216
|
+
def clear(self) -> None:
|
|
217
|
+
self._edit.clear()
|
|
218
|
+
self._reveal.setChecked(False)
|
|
219
|
+
|
|
220
|
+
def line_edit(self) -> QLineEdit:
|
|
221
|
+
return self._edit
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _parse_field(raw: Any) -> tuple[str, str, bool]:
|
|
225
|
+
if isinstance(raw, dict):
|
|
226
|
+
return str(raw["name"]), str(raw.get("label", raw["name"])), bool(raw.get("secret", True))
|
|
227
|
+
if isinstance(raw, (tuple, list)):
|
|
228
|
+
name = str(raw[0])
|
|
229
|
+
return name, str(raw[1]) if len(raw) > 1 else name, bool(raw[2]) if len(raw) > 2 else True
|
|
230
|
+
return str(raw.name), str(getattr(raw, "label", raw.name)), bool(getattr(raw, "secret", True))
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
class AuthWidget(QWidget):
|
|
234
|
+
"""Verdeckte Zugangsdaten, Prüfaktion und Sperrstatus.
|
|
235
|
+
|
|
236
|
+
Signale: ``auth_requested(dict)`` (alle Felder ausgefüllt), ``lock_requested()``,
|
|
237
|
+
``credentials_edited()`` (jede Eingabe – eine bestehende Freigabe ist aufzuheben).
|
|
238
|
+
"""
|
|
239
|
+
|
|
240
|
+
auth_requested = Signal(dict)
|
|
241
|
+
lock_requested = Signal()
|
|
242
|
+
credentials_edited = Signal()
|
|
243
|
+
|
|
244
|
+
STATES = ("locked", "checking", "granted", "failed")
|
|
245
|
+
|
|
246
|
+
def __init__(self, fields: Iterable[Any] = (("key", "Schlüssel", True),), parent: QWidget | None = None) -> None:
|
|
247
|
+
super().__init__(parent)
|
|
248
|
+
self._state = "locked"
|
|
249
|
+
self._allowed = True
|
|
250
|
+
self._inputs: dict[str, _SecretField | QLineEdit] = {}
|
|
251
|
+
specs = [_parse_field(f) for f in fields]
|
|
252
|
+
layout = QVBoxLayout(self)
|
|
253
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
254
|
+
layout.setSpacing(4)
|
|
255
|
+
grid = QGridLayout()
|
|
256
|
+
grid.setHorizontalSpacing(8)
|
|
257
|
+
grid.setVerticalSpacing(4)
|
|
258
|
+
for row, (name, label_text, secret) in enumerate(specs):
|
|
259
|
+
label = QLabel(label_text, self)
|
|
260
|
+
if secret:
|
|
261
|
+
editor: _SecretField | QLineEdit = _SecretField(self)
|
|
262
|
+
editor.edited.connect(self.credentials_edited)
|
|
263
|
+
line = editor.line_edit()
|
|
264
|
+
else:
|
|
265
|
+
editor = QLineEdit(self)
|
|
266
|
+
editor.textEdited.connect(lambda *_: self.credentials_edited.emit())
|
|
267
|
+
line = editor
|
|
268
|
+
line.setAccessibleName(label_text)
|
|
269
|
+
line.returnPressed.connect(self.request)
|
|
270
|
+
label.setBuddy(line)
|
|
271
|
+
grid.addWidget(label, row, 0)
|
|
272
|
+
grid.addWidget(editor, row, 1)
|
|
273
|
+
self._inputs[name] = editor
|
|
274
|
+
grid.setColumnStretch(1, 1)
|
|
275
|
+
layout.addLayout(grid)
|
|
276
|
+
self._unlock = QPushButton("Freischalten", self)
|
|
277
|
+
self._lock = QPushButton("Sperren", self)
|
|
278
|
+
self._unlock.clicked.connect(self.request)
|
|
279
|
+
self._lock.clicked.connect(self.lock_requested)
|
|
280
|
+
for button in (self._unlock, self._lock):
|
|
281
|
+
button.setAutoDefault(False)
|
|
282
|
+
self._status = _StatusText(self)
|
|
283
|
+
self._status.setMinimumWidth(1)
|
|
284
|
+
actions = QHBoxLayout()
|
|
285
|
+
actions.setSpacing(6)
|
|
286
|
+
actions.addWidget(self._unlock)
|
|
287
|
+
actions.addWidget(self._lock)
|
|
288
|
+
if len(specs) == 1:
|
|
289
|
+
# Kompakt: Feld, Knöpfe und Status in einer Zeile.
|
|
290
|
+
grid.addLayout(actions, 0, 2)
|
|
291
|
+
grid.addWidget(self._status, 0, 3)
|
|
292
|
+
grid.setColumnStretch(3, 1)
|
|
293
|
+
grid.setColumnStretch(1, 2)
|
|
294
|
+
else:
|
|
295
|
+
actions.addWidget(self._status, 1)
|
|
296
|
+
layout.addLayout(actions)
|
|
297
|
+
self.set_state("locked", "")
|
|
298
|
+
|
|
299
|
+
def credentials(self) -> dict[str, str]:
|
|
300
|
+
"""Aktuelle Eingaben (Klartext – nicht protokollieren)."""
|
|
301
|
+
return {name: editor.text() for name, editor in self._inputs.items()}
|
|
302
|
+
|
|
303
|
+
def input(self, name: str) -> _SecretField | QLineEdit:
|
|
304
|
+
return self._inputs[name]
|
|
305
|
+
|
|
306
|
+
def clear(self) -> None:
|
|
307
|
+
for editor in self._inputs.values():
|
|
308
|
+
editor.clear()
|
|
309
|
+
self._refresh()
|
|
310
|
+
|
|
311
|
+
def request(self) -> None:
|
|
312
|
+
"""Prüfung anfordern (Knopf oder Eingabetaste)."""
|
|
313
|
+
if not self._unlock.isEnabled():
|
|
314
|
+
return
|
|
315
|
+
credentials = self.credentials()
|
|
316
|
+
if not all(credentials.values()):
|
|
317
|
+
self._status.show_message("Bitte alle Felder ausfüllen", "warning")
|
|
318
|
+
return
|
|
319
|
+
for editor in self._inputs.values():
|
|
320
|
+
if isinstance(editor, _SecretField):
|
|
321
|
+
editor.clear()
|
|
322
|
+
self.auth_requested.emit(credentials)
|
|
323
|
+
|
|
324
|
+
def set_state(self, state: str, message: str = "") -> None:
|
|
325
|
+
"""``locked``, ``checking``, ``granted`` oder ``failed``, optional mit Erläuterung."""
|
|
326
|
+
if state not in self.STATES:
|
|
327
|
+
raise ValueError(f"Unbekannter Zustand {state!r}")
|
|
328
|
+
self._state = state
|
|
329
|
+
defaults = {"locked": "Gesperrt", "checking": "Wird geprüft …", "granted": "Freigeschaltet", "failed": "Fehlgeschlagen"}
|
|
330
|
+
role = {"locked": "locked", "checking": "busy", "granted": "success", "failed": "error"}[state]
|
|
331
|
+
text = defaults[state]
|
|
332
|
+
if message and message != text:
|
|
333
|
+
text = message if state == "checking" else f"{text} – {message}"
|
|
334
|
+
self._status.show_message(text, role)
|
|
335
|
+
self._refresh()
|
|
336
|
+
|
|
337
|
+
def state(self) -> str:
|
|
338
|
+
return self._state
|
|
339
|
+
|
|
340
|
+
def status_text(self) -> str:
|
|
341
|
+
return self._status.text()
|
|
342
|
+
|
|
343
|
+
def set_allowed(self, allowed: bool) -> None:
|
|
344
|
+
"""Sperrt den Bereich selbst (z. B. innerhalb eines noch gesperrten äußeren Bereichs)."""
|
|
345
|
+
self._allowed = allowed
|
|
346
|
+
self._refresh()
|
|
347
|
+
|
|
348
|
+
def _refresh(self) -> None:
|
|
349
|
+
for editor in self._inputs.values():
|
|
350
|
+
editor.setEnabled(self._allowed)
|
|
351
|
+
self._unlock.setEnabled(self._allowed and self._state in ("locked", "failed"))
|
|
352
|
+
self._lock.setVisible(self._state in ("granted", "checking"))
|
|
353
|
+
self._lock.setEnabled(self._allowed)
|
|
354
|
+
self._lock.setText("Abbrechen" if self._state == "checking" else "Sperren")
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def _demo() -> None: # pragma: no cover – python auth_widget.py
|
|
358
|
+
from PySide6.QtWidgets import QApplication
|
|
359
|
+
|
|
360
|
+
app = QApplication.instance() or QApplication([])
|
|
361
|
+
auth = AuthWidget()
|
|
362
|
+
|
|
363
|
+
def check(credentials: dict[str, str]) -> None:
|
|
364
|
+
auth.set_state("checking")
|
|
365
|
+
ok = credentials.get("key") == "demo" # nur Beispiel
|
|
366
|
+
QTimer.singleShot(400, lambda: auth.set_state("granted" if ok else "failed", "" if ok else "Schlüssel abgelehnt"))
|
|
367
|
+
|
|
368
|
+
auth.auth_requested.connect(check)
|
|
369
|
+
auth.credentials_edited.connect(lambda: auth.state() == "granted" and auth.set_state("locked", "Eingabe geändert"))
|
|
370
|
+
auth.lock_requested.connect(lambda: auth.set_state("locked", "Manuell gesperrt"))
|
|
371
|
+
auth.resize(520, 40)
|
|
372
|
+
auth.show()
|
|
373
|
+
app.exec()
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
if __name__ == "__main__":
|
|
377
|
+
_demo()
|