Files
templates/python/set_devices/qt_ports/port_selection.py

24 lines
909 B
Python

"""Shared serial-port labels and selection for Qt project interfaces."""
def port_label(name: str, description: str = "") -> str:
"""Display the device description without changing the transport name."""
description = (description or "").strip()
return f"{name} · {description}" if description else name
def selected_port_name(combo) -> str:
"""Read item data, while allowing a manually entered port name."""
if combo.isEditable() and combo.currentText() != combo.itemText(combo.currentIndex()):
return combo.currentText().strip()
return str(combo.currentData() or combo.currentText()).strip()
def select_port_name(combo, name: str) -> None:
"""Restore by transport name; retain manually entered or absent ports."""
index = combo.findData(name)
if index >= 0:
combo.setCurrentIndex(index)
elif combo.isEditable():
combo.setEditText(name)