""" ui/config_dialog.py — Device configuration dialog (tabbed). """ from PyQt6.QtWidgets import ( QDialog, QVBoxLayout, QHBoxLayout, QTabWidget, QWidget, QFormLayout, QGroupBox, QScrollArea, QLabel, QLineEdit, QDoubleSpinBox, QCheckBox, QPushButton, ) from PyQt6.QtCore import Qt class DeviceConfigDialog(QDialog): def __init__(self, device, parent=None): super().__init__(parent) self.device = device self.setWindowTitle(f"Configure — {device.info.name} [{device.info.device_id}]") self.setMinimumSize(580, 520) self.resize(640, 580) self._build() def _build(self): layout = QVBoxLayout(self) layout.setSpacing(8) tabs = QTabWidget() # ── Tab 1: Device-specific widget ──────────────────────────── cfg_widget = self.device.get_config_widget() scroll = QScrollArea() scroll.setWidgetResizable(True) scroll.setHorizontalScrollBarPolicy( __import__("PyQt6.QtCore", fromlist=["Qt"]).Qt.ScrollBarPolicy.ScrollBarAsNeeded ) scroll.setMinimumWidth(460) scroll.setWidget(cfg_widget) tabs.addTab(scroll, "Hardware / Backend") # ── Tab 2: Channel settings ─────────────────────────────────── tabs.addTab(self._channel_tab(), "Channels") # ── Tab 3: Device info ──────────────────────────────────────── tabs.addTab(self._info_tab(), "Info") layout.addWidget(tabs) btn_row = QHBoxLayout() btn_row.addStretch() close_btn = QPushButton("Close") close_btn.setDefault(True) close_btn.clicked.connect(self.accept) btn_row.addWidget(close_btn) layout.addLayout(btn_row) def _channel_tab(self): w = QScrollArea() w.setWidgetResizable(True) container = QWidget() layout = QVBoxLayout(container) for ch in self.device.info.channels: grp = QGroupBox(f"{ch.channel_id} — {ch.name}") form = QFormLayout(grp) name_e = QLineEdit(ch.name) unit_e = QLineEdit(ch.unit) en_chk = QCheckBox() en_chk.setChecked(ch.enabled) form.addRow("Name:", name_e) form.addRow("Unit:", unit_e) form.addRow("Enabled:", en_chk) apply = QPushButton("Apply") apply.setObjectName("applyButton") def _make_apply(c, ne, ue, ec): def _do(): c.name = ne.text() c.unit = ue.text() c.enabled = ec.isChecked() return _do apply.clicked.connect(_make_apply(ch, name_e, unit_e, en_chk)) form.addRow(apply) layout.addWidget(grp) layout.addStretch() w.setWidget(container) return w def _info_tab(self): w = QWidget() form = QFormLayout(w) info = self.device.info def _ro(v): e = QLineEdit(str(v)); e.setReadOnly(True); return e form.addRow("Device ID:", _ro(info.device_id)) form.addRow("Name:", _ro(info.name)) form.addRow("Type:", _ro(info.device_type)) form.addRow("Description:", _ro(info.description)) form.addRow("Manufacturer:", _ro(info.manufacturer)) form.addRow("Model:", _ro(info.model)) form.addRow("Version:", _ro(info.version)) form.addRow("Status:", _ro(self.device.status.value)) form.addRow("Channels:", _ro(len(info.channels))) return w