Bases: StyledItemDelegate
Edits a dtype with an editable combo box (each entry with its dtype icon).
The chosen text is written back with setData(index, text, role); the model
converts the column (PandasColumnListModel does astype). A failed conversion
leaves the column unchanged.
Source code in src/cutepandas/delegates/dtypedelegate.py
| class DTypeDelegate(widgets.StyledItemDelegate):
"""Edits a dtype with an editable combo box (each entry with its dtype icon).
The chosen text is written back with `setData(index, text, role)`; the model
converts the column (`PandasColumnListModel` does `astype`). A failed conversion
leaves the column unchanged.
"""
ID = "dtype"
def __init__(
self,
dtypes: Sequence[str] = PANDAS_DTYPES,
role: constants.ItemDataRole = constants.EDIT_ROLE,
**kwargs,
):
super().__init__(**kwargs)
self.dtypes = list(dtypes)
self.role = role
def createEditor(self, parent, option, index: core.ModelIndex):
combo = widgets.ComboBox(parent)
combo.set_editable(True)
for dtype in self.dtypes:
try:
icon = icons.icon_for_dtype(dtype)
except TypeError, ValueError:
icon = None
combo.add(dtype, icon=icon)
combo.activated.connect(lambda _: self.commitData.emit(combo))
return combo
def setEditorData(self, editor, index: core.ModelIndex):
editor.setCurrentText(str(index.data(constants.DISPLAY_ROLE)))
def setModelData(self, editor, model, index: core.ModelIndex):
text = editor.currentText().strip()
if text and text != index.data(constants.DISPLAY_ROLE):
model.setData(index, text, self.role)
|