52 lines
1.9 KiB
Python
52 lines
1.9 KiB
Python
from PySide6.QtWidgets import QStyledItemDelegate, QSpinBox
|
||
from PySide6.QtCore import Qt, QEvent, QModelIndex
|
||
from PySide6.QtGui import QMouseEvent
|
||
|
||
class CommonTreeDelegate(QStyledItemDelegate):
|
||
def createEditor(self, parent, option, index: QModelIndex):
|
||
col = index.column()
|
||
if col == 0:
|
||
# Kolumna 0: QSpinBox z zakresem [0..10]
|
||
editor = QSpinBox(parent)
|
||
editor.setRange(0, 10)
|
||
return editor
|
||
elif col == 1:
|
||
# Kolumna 1: toggle "tak/nie" – obsługiwane double-clickiem
|
||
return None
|
||
return super().createEditor(parent, option, index)
|
||
|
||
def setEditorData(self, editor, index: QModelIndex):
|
||
col = index.column()
|
||
if col == 0:
|
||
val_str = index.data(Qt.EditRole)
|
||
if val_str is None:
|
||
val_str = index.data(Qt.DisplayRole)
|
||
try:
|
||
val = int(val_str)
|
||
except ValueError:
|
||
val = 0
|
||
editor.setValue(val)
|
||
else:
|
||
super().setEditorData(editor, index)
|
||
|
||
def setModelData(self, editor, model, index: QModelIndex):
|
||
col = index.column()
|
||
if col == 0:
|
||
val = editor.value()
|
||
model.setData(index, str(val), Qt.EditRole)
|
||
elif col == 1:
|
||
# Podwójny klik przełącza "tak"/"nie"
|
||
cur = index.data(Qt.DisplayRole)
|
||
new_val = "nie" if cur == "tak" else "tak"
|
||
model.setData(index, new_val, Qt.EditRole)
|
||
else:
|
||
super().setModelData(editor, model, index)
|
||
|
||
def editorEvent(self, event, model, option, index):
|
||
# Obsługa double-click w kolumnie 1 (toggle "tak"/"nie")
|
||
if index.column() == 1 and event.type() == QEvent.MouseButtonDblClick:
|
||
self.commitData.emit(None)
|
||
return True
|
||
return super().editorEvent(event, model, option, index)
|
||
|