Autosave rule edits and sort centered table controls
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
"""Native table alignment and sorting for embedded rule editors."""
|
||||
import re
|
||||
|
||||
from PySide6 import QtCore as C, QtWidgets as W
|
||||
|
||||
SORT_ROLE = C.Qt.ItemDataRole.UserRole + 20
|
||||
|
||||
|
||||
def alignment(column):
|
||||
return C.Qt.AlignmentFlag.AlignVCenter | (C.Qt.AlignmentFlag.AlignLeft if column < 2 else C.Qt.AlignmentFlag.AlignHCenter)
|
||||
|
||||
|
||||
def sort_key(value):
|
||||
if isinstance(value, (int, float)):
|
||||
return (0, value)
|
||||
parts = re.split(r"(\d+)", str(value or "").casefold())
|
||||
return (1, tuple((0, int(part)) if part.isdigit() else (1, part) for part in parts))
|
||||
|
||||
|
||||
class TableItem(W.QTableWidgetItem):
|
||||
def __lt__(self, other):
|
||||
def value(item):
|
||||
result = item.data(SORT_ROLE)
|
||||
return item.data(C.Qt.ItemDataRole.DisplayRole) if result is None else result
|
||||
return sort_key(value(self)) < sort_key(value(other))
|
||||
|
||||
|
||||
class TreeItem(W.QTreeWidgetItem):
|
||||
def __lt__(self, other):
|
||||
column = self.treeWidget().sortColumn()
|
||||
def value(item):
|
||||
result = item.data(column, SORT_ROLE)
|
||||
return item.data(column, C.Qt.ItemDataRole.DisplayRole) if result is None else result
|
||||
return sort_key(value(self)) < sort_key(value(other))
|
||||
|
||||
|
||||
class CenteredCheckBox(W.QCheckBox):
|
||||
def paintEvent(self, event):
|
||||
painter = W.QStylePainter(self)
|
||||
option = W.QStyleOptionButton()
|
||||
self.initStyleOption(option)
|
||||
indicator = self.style().subElementRect(W.QStyle.SubElement.SE_CheckBoxIndicator, option, self)
|
||||
option.rect.setLeft((self.width() - indicator.width()) // 2)
|
||||
option.rect.setWidth(indicator.width())
|
||||
painter.drawControl(W.QStyle.ControlElement.CE_CheckBox, option)
|
||||
|
||||
def hitButton(self, position):
|
||||
return self.rect().contains(position)
|
||||
|
||||
|
||||
def set_value(item, column, value):
|
||||
if isinstance(item, W.QTreeWidgetItem):
|
||||
item.setData(column, C.Qt.ItemDataRole.DisplayRole, value)
|
||||
item.setTextAlignment(column, alignment(column))
|
||||
else:
|
||||
item.setData(C.Qt.ItemDataRole.DisplayRole, value)
|
||||
item.setTextAlignment(alignment(column))
|
||||
|
||||
|
||||
def table_item(value, column):
|
||||
item = TableItem()
|
||||
set_value(item, column, value)
|
||||
return item
|
||||
|
||||
|
||||
def bind_controls(view, row, controls):
|
||||
"""Keep a stable item reference: a control edit can move its row immediately."""
|
||||
for column, control in controls.items():
|
||||
item = row if isinstance(row, W.QTreeWidgetItem) else view.item(row, column)
|
||||
control._sort_binding = (item, column)
|
||||
if isinstance(control, W.QLineEdit):
|
||||
control.setAlignment(alignment(column))
|
||||
elif isinstance(control, W.QComboBox):
|
||||
control.setTextAlignment(alignment(column))
|
||||
if not getattr(control, "_sort_connected", False):
|
||||
def update(*args, control=control):
|
||||
item, column = control._sort_binding
|
||||
value = (int(control.isChecked()) if isinstance(control, W.QCheckBox) else
|
||||
control.currentText() if isinstance(control, W.QComboBox) else control.value())
|
||||
if isinstance(item, W.QTreeWidgetItem):
|
||||
item.setData(column, SORT_ROLE, value)
|
||||
else:
|
||||
item.setData(SORT_ROLE, value)
|
||||
signal = (control.toggled if isinstance(control, W.QCheckBox) else
|
||||
control.currentIndexChanged if isinstance(control, W.QComboBox) else control.valueChanged)
|
||||
signal.connect(update)
|
||||
control._sort_connected = True
|
||||
control._update_sort = update
|
||||
control._update_sort()
|
||||
Reference in New Issue
Block a user