Skip to content

commands

commands

Attributes

Classes

InsertChild(model, parent_path, row, key, value)

Bases: QUndoCommand

Add a dict key or list item.

Initialize the command.

Parameters:

Name Type Description Default
model JsonTreeModel

The model to edit.

required
parent_path ModelPath

ModelPath of the container node.

required
row int

Position to insert at.

required
key str | int

Dict key or list index of the new child.

required
value JsonValue

The child's value.

required
Source code in src/json_config_qt/tree/commands.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def __init__(
    self,
    model: JsonTreeModel,
    parent_path: ModelPath,
    row: int,
    key: str | int,
    value: JsonValue,
):
    """Initialize the command.

    Args:
        model (JsonTreeModel): The model to edit.
        parent_path (ModelPath): ModelPath of the container node.
        row (int): Position to insert at.
        key (str | int): Dict key or list index of the new child.
        value (JsonValue): The child's value.
    """
    super().__init__(f"Add {key}")
    self._model = model
    self._parent_path = parent_path
    self._row = row
    self._key = key
    self._value = value
Methods:
redo()

Insert the child.

Source code in src/json_config_qt/tree/commands.py
64
65
66
def redo(self) -> None:
    """Insert the child."""
    self._model.insert_at(self._parent_path, self._row, self._key, self._value)
undo()

Remove the inserted child.

Source code in src/json_config_qt/tree/commands.py
68
69
70
def undo(self) -> None:
    """Remove the inserted child."""
    self._model.remove_at((*self._parent_path, self._key))

RemoveChild(model, path)

Bases: QUndoCommand

Remove a dict key or list item.

Initialize the command.

Parameters:

Name Type Description Default
model JsonTreeModel

The model to edit.

required
path ModelPath

ModelPath of the node to remove.

required
Source code in src/json_config_qt/tree/commands.py
76
77
78
79
80
81
82
83
84
85
86
87
88
def __init__(self, model: JsonTreeModel, path: ModelPath):
    """Initialize the command.

    Args:
        model (JsonTreeModel): The model to edit.
        path (ModelPath): ModelPath of the node to remove.
    """
    super().__init__(f"Remove {path[-1]}")
    node = model.node_at(path)
    self._model = model
    self._path = path
    self._row = node.row
    self._value = node.to_python()
Methods:
redo()

Remove the child.

Source code in src/json_config_qt/tree/commands.py
90
91
92
def redo(self) -> None:
    """Remove the child."""
    self._model.remove_at(self._path)
undo()

Re-insert the removed child at its original position.

Source code in src/json_config_qt/tree/commands.py
94
95
96
def undo(self) -> None:
    """Re-insert the removed child at its original position."""
    self._model.insert_at(self._path[:-1], self._row, self._path[-1], self._value)

SetValue(model, path, value)

Bases: QUndoCommand

Replace the value at a path (also used to change a node's type).

Initialize the command.

Parameters:

Name Type Description Default
model JsonTreeModel

The model to edit.

required
path ModelPath

ModelPath of the node to replace.

required
value JsonValue

The new value.

required
Source code in src/json_config_qt/tree/commands.py
14
15
16
17
18
19
20
21
22
23
24
25
26
def __init__(self, model: JsonTreeModel, path: ModelPath, value: JsonValue):
    """Initialize the command.

    Args:
        model (JsonTreeModel): The model to edit.
        path (ModelPath): ModelPath of the node to replace.
        value (JsonValue): The new value.
    """
    super().__init__(f"Set {path[-1] if path else 'root'}")
    self._model = model
    self._path = path
    self._new = value
    self._old = model.node_at(path).to_python()
Methods:
redo()

Apply the new value.

Source code in src/json_config_qt/tree/commands.py
28
29
30
def redo(self) -> None:
    """Apply the new value."""
    self._model.assign_at(self._path, self._new)
undo()

Restore the previous value.

Source code in src/json_config_qt/tree/commands.py
32
33
34
def undo(self) -> None:
    """Restore the previous value."""
    self._model.assign_at(self._path, self._old)