Skip to content

Extended Objects

List

src.drawpyo.diagram.extended_objects.List

Bases: Object

Source code in src/drawpyo/diagram/extended_objects.py
 6
 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
class List(Object):
    def __init__(self, title="List", list_items=[], **kwargs):
        """The List object wraps the basic Object type but allows easier managing of a list object and its members. All of the arguments and keyword arguments for Object are available here as well.

        Args:
            title (str, optional): The name in the heading of the list. Defaults to "List".
            list_items (list of strings, optional): A Python list of strings denoting the items. Defaults to empty.
        """
        super().__init__(value=title, **kwargs)
        self.format_as_library_object(library="general", obj_name="list")
        self.autosizing = kwargs.get("autosizing", True)
        self.width = kwargs.get("width", 120)
        self.list_items = list_items

    @property
    def list_items(self):
        """A Python list of strings of the objects in the list.

        Returns:
            list of strings: The list items
        """
        return [child.value for child in self.children]

    @list_items.setter
    def list_items(self, value):
        if not isinstance(value, list):
            raise TypeError("list_items must be a list!")
        self.children = []
        for item in value:
            self.add_item(item)

    def add_item(self, item_text):
        """This function creates a new Draw.io text item and adds it to the end of the list.

        Args:
            item_text (string): The name of the item to add.
        """
        new_item = object_from_library(
            library="general", obj_name="list_item", page=self.page
        )
        new_item.value = item_text
        new_item.parent = self
        new_item.width = self.width
        new_item.geometry.y = len(self.list_items) * new_item.height
        if self.autosizing:
            self.autosize()

    def remove_item(self, item_text):
        """This function removes any list items matching the text passed into the function.

        Args:
            item_text (string): The text of the list item to remove.
        """
        for child in self.children:
            if child.value == item_text:
                self.remove_object(child)
                self.page.remove_object(child)
                del child
        if self.autosizing:
            self.autosize()

    def autosize(self):
        """This function resizes the parent List object to match the length of the list of items. It also restacks the list items to fill any gaps from deleted items."""
        y_pos = self.startSize
        for child in self.children:
            child.geometry.y = y_pos
            y_pos = y_pos + child.height
        self.height = self.startSize + sum(child.height for child in self.children)
        self.width = min(child.width for child in self.children)

    @property
    def width(self):
        """The width of the object. The difference between List's width and Object's width is that when the List.width is set all of the child objects will be set to the same width.

        Returns:
            _type_: _description_
        """
        return self.geometry.width

    @width.setter
    def width(self, value):
        for child in self.children:
            child.width = value
        self.geometry.width = value
        self.update_parent()

list_items property writable

A Python list of strings of the objects in the list.

Returns:

Type Description

list of strings: The list items

width property writable

The width of the object. The difference between List's width and Object's width is that when the List.width is set all of the child objects will be set to the same width.

Returns:

Name Type Description
_type_

description

__init__(title='List', list_items=[], **kwargs)

The List object wraps the basic Object type but allows easier managing of a list object and its members. All of the arguments and keyword arguments for Object are available here as well.

Parameters:

Name Type Description Default
title str

The name in the heading of the list. Defaults to "List".

'List'
list_items list of strings

A Python list of strings denoting the items. Defaults to empty.

[]
Source code in src/drawpyo/diagram/extended_objects.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
def __init__(self, title="List", list_items=[], **kwargs):
    """The List object wraps the basic Object type but allows easier managing of a list object and its members. All of the arguments and keyword arguments for Object are available here as well.

    Args:
        title (str, optional): The name in the heading of the list. Defaults to "List".
        list_items (list of strings, optional): A Python list of strings denoting the items. Defaults to empty.
    """
    super().__init__(value=title, **kwargs)
    self.format_as_library_object(library="general", obj_name="list")
    self.autosizing = kwargs.get("autosizing", True)
    self.width = kwargs.get("width", 120)
    self.list_items = list_items

add_item(item_text)

This function creates a new Draw.io text item and adds it to the end of the list.

Parameters:

Name Type Description Default
item_text string

The name of the item to add.

required
Source code in src/drawpyo/diagram/extended_objects.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def add_item(self, item_text):
    """This function creates a new Draw.io text item and adds it to the end of the list.

    Args:
        item_text (string): The name of the item to add.
    """
    new_item = object_from_library(
        library="general", obj_name="list_item", page=self.page
    )
    new_item.value = item_text
    new_item.parent = self
    new_item.width = self.width
    new_item.geometry.y = len(self.list_items) * new_item.height
    if self.autosizing:
        self.autosize()

autosize()

This function resizes the parent List object to match the length of the list of items. It also restacks the list items to fill any gaps from deleted items.

Source code in src/drawpyo/diagram/extended_objects.py
67
68
69
70
71
72
73
74
def autosize(self):
    """This function resizes the parent List object to match the length of the list of items. It also restacks the list items to fill any gaps from deleted items."""
    y_pos = self.startSize
    for child in self.children:
        child.geometry.y = y_pos
        y_pos = y_pos + child.height
    self.height = self.startSize + sum(child.height for child in self.children)
    self.width = min(child.width for child in self.children)

remove_item(item_text)

This function removes any list items matching the text passed into the function.

Parameters:

Name Type Description Default
item_text string

The text of the list item to remove.

required
Source code in src/drawpyo/diagram/extended_objects.py
53
54
55
56
57
58
59
60
61
62
63
64
65
def remove_item(self, item_text):
    """This function removes any list items matching the text passed into the function.

    Args:
        item_text (string): The text of the list item to remove.
    """
    for child in self.children:
        if child.value == item_text:
            self.remove_object(child)
            self.page.remove_object(child)
            del child
    if self.autosizing:
        self.autosize()