class ItemEditorFactory(widgets.QItemEditorFactory):
"""Widgets for editing item data in views and delegates."""
creators = []
@classmethod
def register_default_editor(
cls,
editor_cls: type[widgets.QWidget],
typ: int | None | type = None,
property_name: str = "",
):
factory = cls.defaultFactory()
creator = get_creator_class(editor_cls, property_name)()
cls.creators.append(creator)
match typ:
case None:
typ = editor_cls.staticMetaObject.userProperty().userType()
case type() if typ in TYPES:
typ = TYPES[typ].value
case int():
pass
case _:
raise TypeError(typ)
factory.registerEditor(typ, creator)
cls.setDefaultFactory(factory)
def register_editor(
self,
editor_cls: type[widgets.QWidget],
typ: int | None | type = None,
property_name: str = "",
):
creator = get_creator_class(editor_cls, property_name)()
self.creators.append(creator)
match typ:
case None:
typ = editor_cls.staticMetaObject.userProperty().userType()
case type() if typ in TYPES:
typ = TYPES[typ].value
case int():
pass
case _:
raise TypeError(typ)
self.registerEditor(typ, creator)
@classmethod
def create_extended(cls) -> Self:
factory = cls()
factory.register_editor(widgets.CheckBox, bool, "")
factory.register_editor(widgets.SpinBox, int, "value")
factory.register_editor(widgets.LineEdit, str, "text")
factory.register_editor(widgets.DoubleSpinBox, float, "value")
factory.register_editor(widgets.DateEdit, QtCore.QDate, "date")
factory.register_editor(widgets.TimeEdit, QtCore.QTime, "time")
factory.register_editor(widgets.DateTimeEdit, QtCore.QDateTime, "dateTime")
factory.register_editor(custom_widgets.PointEdit, QtCore.QPoint, "value")
factory.register_editor(custom_widgets.SizeEdit, QtCore.QSize, "value")
factory.register_editor(custom_widgets.RectEdit, QtCore.QRect, "value")
# factory.register_editor(custom_widgets.EnumFlagWidget, 66231, "value")
# factory.register_editor(custom_widgets.EnumComboBox, 20001, "value")
factory.register_editor(custom_widgets.RegionEdit, QtGui.QRegion, "value")
factory.register_editor(
custom_widgets.SizePolicyEdit, widgets.QSizePolicy, "value"
)
factory.register_editor(
widgets.KeySequenceEdit, QtGui.QKeySequence, "keySequence"
)
factory.register_editor(
custom_widgets.ColorComboBox, QtGui.QColor, "current_color"
)
factory.register_editor(widgets.FontComboBox, QtGui.QFont, "currentFont")
factory.register_editor(widgets.LineEdit, QtCore.QUrl, "text")
factory.register_editor(widgets.LineEdit, QtCore.QRegularExpression, "text")
return factory