refactor(kit): rebase ComboBoxDelegate onto InstrumentDelegate (BL-DS-P3 carry-over)
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,19 +1,22 @@
|
||||
"""A QStyledItemDelegate that edits a cell with a dropdown of fixed labels.
|
||||
"""A kit delegate that edits a cell with a dropdown of fixed labels.
|
||||
|
||||
The label list is supplied by a callable (so it can reflect data that loads after
|
||||
construction — e.g. a device-type catalog streamed during a refresh). The chosen
|
||||
label is written back as the cell's display text; consumers map it to a value in
|
||||
their ``on_edit`` via a label->value lookup.
|
||||
Extends InstrumentDelegate so combo-edited columns keep every §5.4/§5.6 paint
|
||||
(hover pencil, edge bars, alarm tint, QUIET_ROLE). The label list is supplied by a
|
||||
callable (so it can reflect data that loads after construction — e.g. a device-type
|
||||
catalog streamed during a refresh). The chosen label is written back as the cell's
|
||||
display text; consumers map it to a value in their ``on_edit``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
|
||||
from PySide6.QtWidgets import QComboBox, QStyledItemDelegate
|
||||
from PySide6.QtWidgets import QComboBox
|
||||
|
||||
from .kit import InstrumentDelegate
|
||||
|
||||
|
||||
class ComboBoxDelegate(QStyledItemDelegate):
|
||||
class ComboBoxDelegate(InstrumentDelegate):
|
||||
def __init__(self, items_provider: Callable[[], list[str]], parent=None) -> None:
|
||||
super().__init__(parent)
|
||||
self._items_provider = items_provider
|
||||
@@ -23,11 +26,17 @@ class ComboBoxDelegate(QStyledItemDelegate):
|
||||
combo.addItems(self._items_provider())
|
||||
return combo
|
||||
|
||||
def setEditorData(self, editor: QComboBox, index) -> None: # noqa: N802 (Qt)
|
||||
def setEditorData(self, editor, index) -> None: # noqa: N802 (Qt)
|
||||
if not isinstance(editor, QComboBox):
|
||||
super().setEditorData(editor, index)
|
||||
return
|
||||
current = index.data() or ""
|
||||
pos = editor.findText(current)
|
||||
if pos >= 0:
|
||||
editor.setCurrentIndex(pos)
|
||||
|
||||
def setModelData(self, editor: QComboBox, model, index) -> None: # noqa: N802 (Qt)
|
||||
def setModelData(self, editor, model, index) -> None: # noqa: N802 (Qt)
|
||||
if not isinstance(editor, QComboBox):
|
||||
super().setModelData(editor, model, index)
|
||||
return
|
||||
model.setData(index, editor.currentText())
|
||||
|
||||
@@ -46,7 +46,7 @@ class DevicesTab(TableTab):
|
||||
self._ctrl = controller
|
||||
self._repo = repository
|
||||
self._devices = [] # parallel list of DeviceRecord for the current rows
|
||||
self._type_delegate = ComboBoxDelegate(self._type_labels, self)
|
||||
self._type_delegate = ComboBoxDelegate(self._type_labels, self.table)
|
||||
self.table.setItemDelegateForColumn(_TYPE_COL, self._type_delegate)
|
||||
controller.devicesChanged.connect(self.rebuild)
|
||||
controller.configChanged.connect(self.rebuild) # catalog arrives mid-refresh
|
||||
|
||||
@@ -34,3 +34,12 @@ def test_combo_delegate_preselects_current_value(qtbot):
|
||||
editor = delegate.createEditor(table, QStyleOptionViewItem(), idx)
|
||||
delegate.setEditorData(editor, idx)
|
||||
assert editor.currentText() == "CS-31"
|
||||
|
||||
|
||||
def test_combo_delegate_is_an_instrument_delegate(qtbot):
|
||||
from cim_suite.core.ui.kit import InstrumentDelegate
|
||||
|
||||
table = QTableWidget(1, 1)
|
||||
qtbot.addWidget(table)
|
||||
delegate = ComboBoxDelegate(lambda: ["A"], table)
|
||||
assert isinstance(delegate, InstrumentDelegate)
|
||||
|
||||
Reference in New Issue
Block a user