refactored connection tab

This commit is contained in:
2026-05-24 11:59:34 +02:00
parent 561d4ad77f
commit 45ef58ea8d
+44 -41
View File
@@ -44,19 +44,6 @@ class ConnectionRow(QWidget):
layout.setContentsMargins(4, 2, 4, 2)
layout.setSpacing(6)
# ── History dropdown ─────────────────────────────────────────────────
self._hist_combo = QComboBox()
self._hist_combo.setPlaceholderText("History ▾")
self._hist_combo.setMinimumWidth(150)
self._hist_combo.setSizeAdjustPolicy(QComboBox.AdjustToContents)
self._hist_combo.currentIndexChanged.connect(self._load_history)
layout.addWidget(self._hist_combo)
sep = QFrame()
sep.setFrameShape(QFrame.VLine)
sep.setFrameShadow(QFrame.Sunken)
layout.addWidget(sep)
# ── Receiver ID ──────────────────────────────────────────────────────
layout.addWidget(QLabel("ID:"))
self._id_edit = QLineEdit()
@@ -127,7 +114,6 @@ class ConnectionRow(QWidget):
if config:
self.apply_config(config)
self.refresh_history()
# ── Serial port detection ────────────────────────────────────────────────
@@ -166,14 +152,6 @@ class ConnectionRow(QWidget):
def get_receiver_id(self) -> str:
return self._rid
def refresh_history(self):
self._hist_combo.blockSignals(True)
self._hist_combo.clear()
self._hist_combo.addItem("History ▾")
for cfg in self._model.get_lru():
self._hist_combo.addItem(cfg.display_str(), userData=cfg)
self._hist_combo.blockSignals(False)
def set_connected(self, connected: bool):
self._connected = connected
self._dot.set_connected(connected)
@@ -187,16 +165,6 @@ class ConnectionRow(QWidget):
def _on_type_changed(self, idx: int):
self._stack.setCurrentIndex(idx)
def _load_history(self, idx: int):
if idx <= 0:
return
cfg = self._hist_combo.itemData(idx)
if cfg:
self.apply_config(cfg)
self._hist_combo.blockSignals(True)
self._hist_combo.setCurrentIndex(0)
self._hist_combo.blockSignals(False)
def _toggle(self):
if self._connected:
if self._rid:
@@ -235,7 +203,7 @@ class ConnectionTab(QWidget):
outer.setContentsMargins(8, 8, 8, 8)
outer.setSpacing(6)
# Scroll area
# ── Scroll area with rows ─────────────────────────────────────────────
self._scroll = QScrollArea()
self._scroll.setWidgetResizable(True)
self._scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
@@ -249,17 +217,53 @@ class ConnectionTab(QWidget):
self._scroll.setWidget(self._inner)
outer.addWidget(self._scroll)
add_btn = QPushButton(" Add Connection")
add_btn.setMaximumWidth(180)
add_btn.clicked.connect(lambda: self._add_row())
outer.addWidget(add_btn, alignment=Qt.AlignLeft)
# ── Bottom bar: history combo (left) + Add button (right) ─────────────
bottom = QHBoxLayout()
bottom.setSpacing(8)
self._hist_combo = QComboBox()
self._hist_combo.setPlaceholderText("Select from history…")
self._hist_combo.setMinimumWidth(260)
self._hist_combo.setSizeAdjustPolicy(QComboBox.AdjustToContents)
self._refresh_history_combo()
bottom.addWidget(self._hist_combo)
add_btn = QPushButton(" Add")
add_btn.setFixedWidth(90)
add_btn.clicked.connect(self._on_add)
bottom.addWidget(add_btn)
bottom.addStretch()
outer.addLayout(bottom)
model.connection_changed.connect(self._on_connection_changed)
# ── History combo ─────────────────────────────────────────────────────────
def _refresh_history_combo(self):
self._hist_combo.blockSignals(True)
current = self._hist_combo.currentData()
self._hist_combo.clear()
for cfg in self._model.get_lru():
self._hist_combo.addItem(cfg.display_str(), userData=cfg)
# restore selection if still present
if current:
idx = self._hist_combo.findText(current.display_str())
if idx >= 0:
self._hist_combo.setCurrentIndex(idx)
self._hist_combo.blockSignals(False)
# ── Row management ────────────────────────────────────────────────────────
def _on_add(self):
cfg = self._hist_combo.currentData() # None when placeholder shown
self._add_row(cfg)
self._hist_combo.setCurrentIndex(-1) # reset to placeholder
def _add_row(self, config: ConnectionConfig = None):
row = ConnectionRow(self._model, config)
row.remove_requested.connect(self._remove_row)
# Insert before the trailing stretch
self._vbox.insertWidget(self._vbox.count() - 1, row)
self._rows.append(row)
@@ -276,7 +280,6 @@ class ConnectionTab(QWidget):
for row in self._rows:
if row.get_receiver_id() == rid:
row.set_connected(connected)
if connected:
for r in self._rows:
r.refresh_history()
break
if connected:
self._refresh_history_combo()