Skip to content

NodeObject

src.drawpyo.diagram_types.tree.NodeObject

Bases: Object

This class defines one of the nodes on a tree graph. It inherits from Object and performs the same in most regards. It also tracks the tree-specific parameters like the tree, children, parent, etc.

Source code in src/drawpyo/diagram_types/tree.py
 7
 8
 9
10
11
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class NodeObject(Object):
    """This class defines one of the nodes on a tree graph. It inherits from Object and performs the same in most regards. It also tracks the tree-specific parameters like the tree, children, parent, etc."""

    def __init__(self, tree=None, **kwargs):
        """The NodeObject should be instantiated with an owning tree object. A NodeObject can only have a single parent but can have any number of children.

        Args:
            tree (TreeDiagram, optional): The owning tree diagram. Defaults to None.

        Keyword Args:
            tree_children (list, optional): A list of other NodeObjects
            parent (list, optional): The parent NodeObject
        """
        super().__init__(**kwargs)
        self.tree = tree
        self.tree_children = kwargs.get("tree_children", [])
        self.tree_parent = kwargs.get("tree_parent", None)
        self.peers = []
        # self.level = kwargs.get("level", None)
        # self.peers = kwargs.get("peers", [])

    @property
    def tree(self):
        """The TreeDiagram that owns the NodeObject

        Returns:
            TreeDiagram
        """
        return self._tree

    @tree.setter
    def tree(self, value):
        if value is not None:
            value.add_object(self)
        self._tree = value

    @property
    def tree_parent(self):
        """The parent NodeObject in the tree

        Returns:
            NodeObject
        """
        return self._tree_parent

    @tree_parent.setter
    def tree_parent(self, value):
        if value is not None:
            value.tree_children.append(self)
        self._tree_parent = value

    def add_child(self, obj):
        """Add a new child to the object

        Args:
            obj (NodeObject)
        """
        self.tree_children.append(obj)
        obj._tree_parent = self

    def add_peer(self, obj):
        if obj not in self.peers:
            self.peers.append(obj)
        if self not in obj.peers:
            obj.peers.append(self)

    @property
    def size_of_level(self):
        """The height or the width of the level, depending on tree orientation.

        Returns:
            int
        """
        if self.tree is not None:
            if self.tree.direction in ["up", "down"]:
                return self.geometry.height
            elif self.tree.direction in ["left", "right"]:
                return self.geometry.width

    @property
    def size_in_level(self):
        """The size of the object within its level, either its width or height depending on tree orientation.

        Returns:
            int
        """
        if self.tree is not None:
            if self.tree.direction in ["up", "down"]:
                return self.geometry.width
            elif self.tree.direction in ["left", "right"]:
                return self.geometry.height

size_in_level property

The size of the object within its level, either its width or height depending on tree orientation.

Returns:

Type Description

int

size_of_level property

The height or the width of the level, depending on tree orientation.

Returns:

Type Description

int

tree property writable

The TreeDiagram that owns the NodeObject

Returns:

Type Description

TreeDiagram

tree_parent property writable

The parent NodeObject in the tree

Returns:

Type Description

NodeObject

__init__(tree=None, **kwargs)

The NodeObject should be instantiated with an owning tree object. A NodeObject can only have a single parent but can have any number of children.

Parameters:

Name Type Description Default
tree TreeDiagram

The owning tree diagram. Defaults to None.

None

Other Parameters:

Name Type Description
tree_children list

A list of other NodeObjects

parent list

The parent NodeObject

Source code in src/drawpyo/diagram_types/tree.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def __init__(self, tree=None, **kwargs):
    """The NodeObject should be instantiated with an owning tree object. A NodeObject can only have a single parent but can have any number of children.

    Args:
        tree (TreeDiagram, optional): The owning tree diagram. Defaults to None.

    Keyword Args:
        tree_children (list, optional): A list of other NodeObjects
        parent (list, optional): The parent NodeObject
    """
    super().__init__(**kwargs)
    self.tree = tree
    self.tree_children = kwargs.get("tree_children", [])
    self.tree_parent = kwargs.get("tree_parent", None)
    self.peers = []

add_child(obj)

Add a new child to the object

Source code in src/drawpyo/diagram_types/tree.py
58
59
60
61
62
63
64
65
def add_child(self, obj):
    """Add a new child to the object

    Args:
        obj (NodeObject)
    """
    self.tree_children.append(obj)
    obj._tree_parent = self