Skip to content

delegate

delegate

Attributes

INT_LIMIT = 2 ** 31 - 1 module-attribute

Classes

ValueDelegate

Bases: QStyledItemDelegate

Creates the editor matching the type of each value.

Methods:
createEditor(parent, option, index)

Create the editor matching the value's type.

Color values open a color dialog instead and return None, as do types without an inline editor (dict, list, null, binary).

Parameters:

Name Type Description Default
parent QWidget

Parent of the editor.

required
option QStyleOptionViewItem

Style options of the cell.

required
index QModelIndex

The cell being edited.

required

Returns:

Type Description
QWidget | None

QtWidgets.QWidget | None: The editor, or None if there is no inline editor.

Source code in src/json_config_qt/tree/delegate.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def createEditor(self, parent, option, index) -> QtWidgets.QWidget | None:
    """Create the editor matching the value's type.

    Color values open a color dialog instead and return None, as do types
    without an inline editor (dict, list, null, binary).

    Args:
        parent (QtWidgets.QWidget): Parent of the editor.
        option (QtWidgets.QStyleOptionViewItem): Style options of the cell.
        index (QtCore.QModelIndex): The cell being edited.

    Returns:
        QtWidgets.QWidget | None: The editor, or None if there is no inline editor.
    """
    kind = index.data(KIND_ROLE)
    value = index.data(QtCore.Qt.ItemDataRole.EditRole)
    if kind == Kind.INT:
        if abs(value) <= INT_LIMIT:
            editor = QtWidgets.QSpinBox(parent)
            editor.setRange(-INT_LIMIT - 1, INT_LIMIT)
            return editor
        editor = QtWidgets.QLineEdit(parent)
        editor.setValidator(
            QtGui.QRegularExpressionValidator(QtCore.QRegularExpression(r"-?\d+"))
        )
        return editor
    if kind == Kind.FLOAT:
        editor = QtWidgets.QDoubleSpinBox(parent)
        editor.setRange(-1e308, 1e308)
        editor.setDecimals(6)
        editor.setStepType(
            QtWidgets.QAbstractSpinBox.StepType.AdaptiveDecimalStepType
        )
        return editor
    if kind == Kind.COLOR:
        self._pick_color(parent, index, value)
        return None
    if kind == Kind.STR:
        return QtWidgets.QLineEdit(parent)
    return None
initStyleOption(option, index)

Italicize null values.

Parameters:

Name Type Description Default
option QStyleOptionViewItem

The options to fill in.

required
index QModelIndex

The cell being painted.

required
Source code in src/json_config_qt/tree/delegate.py
125
126
127
128
129
130
131
132
133
134
def initStyleOption(self, option, index):
    """Italicize null values.

    Args:
        option (QtWidgets.QStyleOptionViewItem): The options to fill in.
        index (QtCore.QModelIndex): The cell being painted.
    """
    super().initStyleOption(option, index)
    if index.column() == VALUE_COLUMN and index.data(KIND_ROLE) == Kind.NULL:
        option.font.setItalic(True)
setEditorData(editor, index)

Load the cell's value into the editor.

Parameters:

Name Type Description Default
editor QWidget

The editor created by createEditor.

required
index QModelIndex

The cell being edited.

required
Source code in src/json_config_qt/tree/delegate.py
80
81
82
83
84
85
86
87
88
89
90
91
def setEditorData(self, editor, index):
    """Load the cell's value into the editor.

    Args:
        editor (QtWidgets.QWidget): The editor created by createEditor.
        index (QtCore.QModelIndex): The cell being edited.
    """
    value = index.data(QtCore.Qt.ItemDataRole.EditRole)
    if isinstance(editor, QtWidgets.QSpinBox | QtWidgets.QDoubleSpinBox):
        editor.setValue(value)
    elif isinstance(editor, QtWidgets.QLineEdit):
        editor.setText(str(value))
setModelData(editor, model, index)

Write the editor's value back to the model.

Invalid integer text is ignored.

Parameters:

Name Type Description Default
editor QWidget

The editor created by createEditor.

required
model QAbstractItemModel

The model to update.

required
index QModelIndex

The cell being edited.

required
Source code in src/json_config_qt/tree/delegate.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def setModelData(self, editor, model, index):
    """Write the editor's value back to the model.

    Invalid integer text is ignored.

    Args:
        editor (QtWidgets.QWidget): The editor created by createEditor.
        model (QtCore.QAbstractItemModel): The model to update.
        index (QtCore.QModelIndex): The cell being edited.
    """
    if isinstance(editor, QtWidgets.QSpinBox | QtWidgets.QDoubleSpinBox):
        editor.interpretText()
        model.setData(index, editor.value())
    elif isinstance(editor, QtWidgets.QLineEdit):
        text = editor.text()
        if index.data(KIND_ROLE) == Kind.INT:
            if not text.lstrip("-").isdigit():
                return
            model.setData(index, int(text))
        else:
            model.setData(index, text)
updateEditorGeometry(editor, option, index)

Make the editor fill its cell.

Parameters:

Name Type Description Default
editor QWidget

The editor created by createEditor.

required
option QStyleOptionViewItem

Style options of the cell.

required
index QModelIndex

The cell being edited.

required
Source code in src/json_config_qt/tree/delegate.py
115
116
117
118
119
120
121
122
123
def updateEditorGeometry(self, editor, option, index):
    """Make the editor fill its cell.

    Args:
        editor (QtWidgets.QWidget): The editor created by createEditor.
        option (QtWidgets.QStyleOptionViewItem): Style options of the cell.
        index (QtCore.QModelIndex): The cell being edited.
    """
    editor.setGeometry(option.rect)