1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
"""
ui/windows/settings_window.py
SETTINGS window — toolbar section 5.
Tabs:
General — theme (dark/light/system), font sizes, polling rate
Acquisition — sample rate, buffer size, CSV log directory
Display — default time window, legend, grid defaults
"""
from PyQt6.QtWidgets import (
QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton,
QTabWidget, QFrame, QFormLayout, QComboBox,
QSpinBox, QDoubleSpinBox, QCheckBox, QLineEdit,
QFileDialog, QScrollArea, QGroupBox, QMessageBox,
)
from PyQt6.QtCore import Qt, pyqtSignal
from PyQt6.QtGui import QCloseEvent, QFont
from devices.device_registry import DeviceRegistry
from core.acquisition import AcquisitionEngine
class SettingsWindow(QWidget):
theme_changed = pyqtSignal(str) # "dark" | "light"
settings_changed = pyqtSignal(dict)
plugin_enable_requested = pyqtSignal(str) # plugin_id
plugin_disable_requested= pyqtSignal(str) # plugin_id
closed = pyqtSignal()
# Shared settings dict — written on Apply, read by consumers
_defaults = {
"theme": "dark",
"poll_ms": 100,
"buffer_size": 20000,
"log_dir": "logs",
"time_window_s": 30.0,
"show_legend": True,
"show_grid": True,
"font_size": 12,
"antialias": True,
}
def __init__(self, registry: DeviceRegistry,
engine: AcquisitionEngine,
current: dict = None, parent=None, *,
plugin_manager=None):
super().__init__(parent, Qt.WindowType.Window | Qt.WindowType.Tool)
self.registry = registry
self.engine = engine
self._plugin_mgr = plugin_manager
self._plugin_buttons: dict = {} # plugin_id -> QPushButton
self.cfg = dict(self._defaults)
if current:
self.cfg.update(current)
self.setWindowTitle("Settings")
self.setMinimumSize(520, 440)
self.resize(560, 520)
self._build()
def _build(self):
root = QVBoxLayout(self); root.setContentsMargins(0,0,0,0); root.setSpacing(0)
# Header
hdr = QWidget(); hdr.setObjectName("devWindowTitleBar"); hdr.setFixedHeight(44)
hl = QHBoxLayout(hdr); hl.setContentsMargins(14,0,14,0)
hl.addWidget(QLabel("SETTINGS").also(lambda w: w.setObjectName("devWindowTitle")))
root.addWidget(hdr)
div = QFrame(); div.setFrameShape(QFrame.Shape.HLine)
div.setObjectName("devWindowDivider"); root.addWidget(div)
tabs = QTabWidget(); tabs.setObjectName("signalBuilderTabs")
root.addWidget(tabs, 1)
tabs.addTab(self._general_tab(), " General ")
tabs.addTab(self._acquisition_tab(), " Acquisition ")
tabs.addTab(self._display_tab(), " Display ")
tabs.addTab(self._plugins_tab(), " Plugins ")
# Bottom bar
btm = QWidget(); btm.setObjectName("cfgBottomBar")
bl = QHBoxLayout(btm); bl.setContentsMargins(12,8,12,8)
bl.addStretch()
ap = QPushButton("✓ Apply & Close"); ap.setObjectName("applyButton")
ap.clicked.connect(self._apply); bl.addWidget(ap)
root.addWidget(btm)
# ── Tabs ──────────────────────────────────────────────────────────────
def _general_tab(self):
w = QWidget()
scroll = QScrollArea(); scroll.setWidgetResizable(True)
scroll.setObjectName("deviceScroll")
cont = QWidget(); lay = QFormLayout(cont)
lay.setContentsMargins(16,14,16,14); lay.setSpacing(10)
self._theme_cb = QComboBox(); self._theme_cb.setObjectName("channelPickerCb")
self._theme_cb.addItems(["Dark", "Light"])
self._theme_cb.setCurrentText(self.cfg["theme"].title())
lay.addRow("Theme:", self._theme_cb)
self._font_sp = QSpinBox(); self._font_sp.setRange(8,18)
self._font_sp.setValue(self.cfg["font_size"]); self._font_sp.setSuffix(" pt")
self._font_sp.setObjectName("traceWidthSpin")
lay.addRow("Base font size:", self._font_sp)
self._aa_chk = QCheckBox(); self._aa_chk.setChecked(self.cfg["antialias"])
lay.addRow("Anti-alias plots:", self._aa_chk)
rst = QPushButton("Reset to Defaults"); rst.setObjectName("configButton")
rst.clicked.connect(self._reset_to_defaults)
lay.addRow("", rst)
scroll.setWidget(cont)
root = QVBoxLayout(w); root.setContentsMargins(0,0,0,0); root.addWidget(scroll)
return w
def _populate_from_cfg(self):
self._theme_cb.setCurrentText(self.cfg["theme"].title())
self._font_sp.setValue(self.cfg["font_size"])
self._aa_chk.setChecked(self.cfg["antialias"])
self._poll_sp.setValue(self.cfg["poll_ms"])
self._buf_sp.setValue(self.cfg["buffer_size"])
self._log_edit.setText(self.cfg["log_dir"])
self._tw_sp.setValue(self.cfg["time_window_s"])
self._legend_chk.setChecked(self.cfg["show_legend"])
self._grid_chk.setChecked(self.cfg["show_grid"])
def _acquisition_tab(self):
w = QWidget()
scroll = QScrollArea(); scroll.setWidgetResizable(True)
scroll.setObjectName("deviceScroll")
cont = QWidget(); lay = QFormLayout(cont)
lay.setContentsMargins(16,14,16,14); lay.setSpacing(10)
self._poll_sp = QSpinBox(); self._poll_sp.setRange(10,5000)
self._poll_sp.setValue(self.cfg["poll_ms"]); self._poll_sp.setSuffix(" ms")
self._poll_sp.setObjectName("traceWidthSpin")
lay.addRow("Poll interval:", self._poll_sp)
self._buf_sp = QSpinBox(); self._buf_sp.setRange(1000,500000)
self._buf_sp.setValue(self.cfg["buffer_size"])
self._buf_sp.setObjectName("traceWidthSpin")
lay.addRow("Buffer size (samples):", self._buf_sp)
log_row = QHBoxLayout()
self._log_edit = QLineEdit(self.cfg["log_dir"])
self._log_edit.setObjectName("traceLabel"); log_row.addWidget(self._log_edit,1)
br = QPushButton("Browse"); br.setObjectName("configButton")
br.clicked.connect(self._browse_log); log_row.addWidget(br)
lay.addRow("Log directory:", log_row)
scroll.setWidget(cont)
root = QVBoxLayout(w); root.setContentsMargins(0,0,0,0); root.addWidget(scroll)
return w
def _display_tab(self):
w = QWidget()
scroll = QScrollArea(); scroll.setWidgetResizable(True)
scroll.setObjectName("deviceScroll")
cont = QWidget(); lay = QFormLayout(cont)
lay.setContentsMargins(16,14,16,14); lay.setSpacing(10)
self._tw_sp = QDoubleSpinBox(); self._tw_sp.setRange(1,3600)
self._tw_sp.setValue(self.cfg["time_window_s"]); self._tw_sp.setSuffix(" s")
self._tw_sp.setObjectName("cfgGlobalSpin")
lay.addRow("Default time window:", self._tw_sp)
self._legend_chk = QCheckBox(); self._legend_chk.setChecked(self.cfg["show_legend"])
lay.addRow("Show legend by default:", self._legend_chk)
self._grid_chk = QCheckBox(); self._grid_chk.setChecked(self.cfg["show_grid"])
lay.addRow("Show grid by default:", self._grid_chk)
scroll.setWidget(cont)
root = QVBoxLayout(w); root.setContentsMargins(0,0,0,0); root.addWidget(scroll)
return w
def _plugins_tab(self):
w = QWidget()
scroll = QScrollArea(); scroll.setWidgetResizable(True)
scroll.setObjectName("deviceScroll")
cont = QWidget(); lay = QVBoxLayout(cont)
lay.setContentsMargins(14, 12, 14, 12); lay.setSpacing(10)
if self._plugin_mgr is None:
lay.addWidget(QLabel("Plugin manager not available."))
lay.addStretch()
scroll.setWidget(cont)
root = QVBoxLayout(w); root.setContentsMargins(0,0,0,0)
root.addWidget(scroll); return w
manifests = self._plugin_mgr.get_manifests()
if not manifests:
info = QLabel(
"No plugins found.\n\n"
"Drop a plugin folder into the plugins/ directory next to main.py.\n"
"Each plugin needs a manifest.json and a plugin.py."
)
info.setObjectName("traceSource"); info.setWordWrap(True)
lay.addWidget(info)
lay.addStretch()
scroll.setWidget(cont)
root = QVBoxLayout(w); root.setContentsMargins(0,0,0,0)
root.addWidget(scroll); return w
for manifest in manifests:
lay.addWidget(self._plugin_card(manifest))
lay.addStretch()
scroll.setWidget(cont)
root = QVBoxLayout(w); root.setContentsMargins(0,0,0,0)
root.addWidget(scroll); return w
def _plugin_card(self, manifest):
"""One card per discovered plugin."""
card = QGroupBox()
card.setObjectName("pluginCard")
cl = QVBoxLayout(card); cl.setContentsMargins(10, 8, 10, 8); cl.setSpacing(4)
# Header row: name + version + enable toggle
hdr = QHBoxLayout()
name_lbl = QLabel(f"<b>{manifest.name}</b> <small>v{manifest.version}</small>")
name_lbl.setObjectName("traceLabel")
hdr.addWidget(name_lbl, 1)
enabled = self._plugin_mgr.is_enabled(manifest.plugin_id)
toggle = QPushButton("Disable" if enabled else "Enable")
toggle.setObjectName("configButton")
toggle.setFixedWidth(72)
toggle.clicked.connect(
lambda _, pid=manifest.plugin_id, btn=toggle: self._toggle_plugin(pid, btn)
)
self._plugin_buttons[manifest.plugin_id] = toggle
hdr.addWidget(toggle)
cl.addLayout(hdr)
# Description / author
if manifest.description:
desc = QLabel(manifest.description)
desc.setObjectName("traceSource"); desc.setWordWrap(True)
cl.addWidget(desc)
if manifest.author:
author = QLabel(f"Author: {manifest.author}")
author.setObjectName("traceSource")
cl.addWidget(author)
# Plugin-specific settings widget (only when loaded)
plugin = self._plugin_mgr.get_plugin(manifest.plugin_id)
if plugin:
sw = plugin.get_settings_widget()
if sw is not None:
cl.addWidget(sw)
return card
def _toggle_plugin(self, plugin_id: str, btn: QPushButton):
if self._plugin_mgr.is_enabled(plugin_id):
self.plugin_disable_requested.emit(plugin_id)
btn.setText("Enable")
else:
# Don't flip to "Disable" yet — enabling can fail (missing
# dependencies, bad plugin code). main_window confirms the
# real outcome via sync_plugin_button() once enable() returns.
self.plugin_enable_requested.emit(plugin_id)
def sync_plugin_button(self, plugin_id: str):
"""Refresh one plugin's toggle button to match its actual enabled state."""
btn = self._plugin_buttons.get(plugin_id)
if btn is not None and self._plugin_mgr is not None:
btn.setText("Disable" if self._plugin_mgr.is_enabled(plugin_id) else "Enable")
# ── Actions ───────────────────────────────────────────────────────────
def _reset_to_defaults(self):
reply = QMessageBox.question(
self, "Reset Settings",
"Reset all settings to defaults?\nThis cannot be undone.",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No,
)
if reply == QMessageBox.StandardButton.Yes:
self.cfg = dict(self._defaults)
self._populate_from_cfg()
def _browse_log(self):
path = QFileDialog.getExistingDirectory(self, "Select Log Directory")
if path:
self._log_edit.setText(path)
def _apply(self):
theme = self._theme_cb.currentText().lower()
self.cfg.update({
"theme": theme,
"font_size": self._font_sp.value(),
"antialias": self._aa_chk.isChecked(),
"poll_ms": self._poll_sp.value(),
"buffer_size": self._buf_sp.value(),
"log_dir": self._log_edit.text(),
"time_window_s": self._tw_sp.value(),
"show_legend": self._legend_chk.isChecked(),
"show_grid": self._grid_chk.isChecked(),
})
self.theme_changed.emit(theme)
self.settings_changed.emit(dict(self.cfg))
self.hide()
def closeEvent(self, e: QCloseEvent):
self.closed.emit(); e.accept()
# monkey-patch QLabel.also
from PyQt6.QtWidgets import QLabel as _QL
def _also3(self, fn): fn(self); return self
if not hasattr(_QL, "also"): _QL.also = _also3
|