model
model
Attributes
COLUMNS = ('Key', 'Value', 'Type')
module-attribute
KIND_ROLE = QtCore.Qt.ItemDataRole.UserRole + 1
module-attribute
ModelPath = tuple[str | int, ...]
module-attribute
NODE_ROLE = QtCore.Qt.ItemDataRole.UserRole + 2
module-attribute
NON_EDITABLE_KINDS = (Kind.DICT, Kind.LIST, Kind.NULL, Kind.BINARY)
module-attribute
Classes
JsonTreeModel(undo_stack, parent=None)
Bases: QAbstractItemModel
Tree model over a JSON document.
Edits made through the view go through undo_stack; the *_at methods
mutate the tree directly and are meant to be called by those commands.
Initialize the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
undo_stack
|
QUndoStack
|
Stack that receives user edits. |
required |
parent
|
QObject | None
|
Parent object. |
None
|
Source code in src/json_config_qt/tree/model.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
Attributes
undo_stack = undo_stack
instance-attribute
Methods:
add_child(parent, key, value)
Push an undoable edit appending a child to a container node.
Source code in src/json_config_qt/tree/model.py
148 149 150 151 152 153 154 | |
assign_at(path, value)
Replace the value (and type) of the node at path, rebuilding children.
Source code in src/json_config_qt/tree/model.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
columnCount(parent=QtCore.QModelIndex())
Return the number of columns (key, value, type).
Source code in src/json_config_qt/tree/model.py
193 194 195 | |
data(index, role=QtCore.Qt.ItemDataRole.DisplayRole)
Return the data of index for role.
Besides the standard roles, KIND_ROLE returns the node's Kind and NODE_ROLE the JsonNode itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
QModelIndex
|
The cell. |
required |
role
|
int
|
The item data role. |
DisplayRole
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The data, or None if there is none for this role. |
Source code in src/json_config_qt/tree/model.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
flags(index)
Return item flags; only the value column of editable types is editable.
Bool values are additionally user-checkable.
Source code in src/json_config_qt/tree/model.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
headerData(section, orientation, role=QtCore.Qt.ItemDataRole.DisplayRole)
Return the column titles for the horizontal header.
Source code in src/json_config_qt/tree/model.py
197 198 199 200 201 202 203 204 | |
index(row, column, parent=QtCore.QModelIndex())
Return the index of a child row/column of parent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
int
|
Child row. |
required |
column
|
int
|
Child column. |
required |
parent
|
QModelIndex
|
Parent index; invalid for top-level rows. |
QModelIndex()
|
Returns:
| Type | Description |
|---|---|
QModelIndex
|
QtCore.QModelIndex: The child index, or an invalid index if out of range. |
Source code in src/json_config_qt/tree/model.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
index_for(node, column=0)
Return the index of node in a column; the root maps to the invalid index.
Source code in src/json_config_qt/tree/model.py
75 76 77 78 79 | |
insert_at(parent_path, row, key, value)
Insert a child into the container at parent_path at position row.
Source code in src/json_config_qt/tree/model.py
100 101 102 103 104 105 106 107 108 109 110 111 | |
load(data)
Replace the model's content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, JsonValue]
|
The JSON object to show. |
required |
Source code in src/json_config_qt/tree/model.py
40 41 42 43 44 45 46 47 48 | |
node_at(path)
Return the node at path (keys from the root).
Source code in src/json_config_qt/tree/model.py
55 56 57 58 59 60 | |
node_for(index)
Return the node of index; the invalid index maps to the root.
Source code in src/json_config_qt/tree/model.py
71 72 73 | |
parent(child)
Return the parent index of child, invalid for top-level rows.
Source code in src/json_config_qt/tree/model.py
181 182 183 184 185 | |
path_of(node)
staticmethod
Return the keys leading from the root to node.
Source code in src/json_config_qt/tree/model.py
62 63 64 65 66 67 68 69 | |
remove_at(path)
Remove the node at path from its parent.
Source code in src/json_config_qt/tree/model.py
113 114 115 116 117 118 119 120 121 122 | |
remove_node(node)
Push an undoable edit removing a node.
Source code in src/json_config_qt/tree/model.py
156 157 158 | |
rowCount(parent=QtCore.QModelIndex())
Return the number of children of parent; only column 0 has children.
Source code in src/json_config_qt/tree/model.py
187 188 189 190 191 | |
setData(index, value, role=QtCore.Qt.ItemDataRole.EditRole)
Push an undoable edit for the value cell at index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
QModelIndex
|
The cell; only the value column is editable. |
required |
value
|
Any
|
The new value, or a check state for bool values. |
required |
role
|
int
|
EditRole, or CheckStateRole for bool values. |
EditRole
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the edit was accepted. |
Source code in src/json_config_qt/tree/model.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
set_value(node, value)
Push an undoable edit replacing a node's value.
Source code in src/json_config_qt/tree/model.py
142 143 144 145 146 | |
to_python()
Return the current content as plain JSON data.
Source code in src/json_config_qt/tree/model.py
50 51 52 | |