Skip to content
This repository has been archived by the owner on Jan 25, 2025. It is now read-only.

Added API to remove and insert children #10

Merged
merged 4 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,51 @@ def __init__(self, style, children=None):
self.assertEqual(child1.applicator.tasks, [])
self.assertEqual(child2.applicator.tasks, [])
self.assertEqual(child3.applicator.tasks, [])

def test_add(self):
"Nodes can be added as children to another node"

style = Style()
node = Node(style=style, children=[])

child = Node(style=style)
node.add(child)

self.assertTrue(child in node.children)
self.assertEqual(child.parent, node)
self.assertEqual(child.root, node.root)

def test_insert(self):
"Node can be inserted at a specific position as a child"

style = Style()
child1 = Node(style=style)
child2 = Node(style=style)
child3 = Node(style=style)
node = Node(style=style, children=[child1, child2, child3])

child4 = Node(style=style)

index = 2
node.insert(index, child4)

self.assertTrue(child4 in node.children)
self.assertEqual(child4.parent, node)
self.assertEqual(child4.root, node.root)

self.assertEqual(node.children.index(child4), index)

def test_remove(self):
"Children can be removed from node"

style = Style()
child1 = Node(style=style)
child2 = Node(style=style)
child3 = Node(style=style)
node = Node(style=style, children=[child1, child2, child3])

node.remove(child1)

self.assertFalse(child1 in node.children)
self.assertEqual(child1.parent, None)
self.assertEqual(child1.root, child1)
18 changes: 8 additions & 10 deletions travertino/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,21 @@ def content_top(self):

@content_top.setter
def content_top(self, value):
if value != self._content_top:
self._content_top = value
for child in self.node.children:
if child.layout:
child.layout._origin_top = self.absolute_content_top
self._content_top = value
for child in self.node.children:
if child.layout:
child.layout._origin_top = self.absolute_content_top

@property
def content_left(self):
return self._content_left

@content_left.setter
def content_left(self, value):
if value != self._content_left:
self._content_left = value
for child in self.node.children:
if child.layout:
child.layout._origin_left = self.absolute_content_left
self._content_left = value
for child in self.node.children:
if child.layout:
child.layout._origin_left = self.absolute_content_left

######################################################################
# Absolute content box position
Expand Down
31 changes: 31 additions & 0 deletions travertino/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,37 @@ def add(self, child):
child._parent = self
set_root(child, self.root)

def insert(self, index, child):
"""Insert a node as a child of this one.
Args:
index: Index of child position.
child: A node to insert as a child to this node.

Raises:
ValueError: If this node is a leaf, and cannot have children.
"""
if self._children is None:
raise ValueError('Cannot insert child')

self._children.insert(index, child)
child._parent = self
set_root(child, self.root)

def remove(self, child):
"""Remove child from this node.
Args:
child: The child to remove from this node.

Raises:
ValueError: If this node is a leaf, and cannot have children.
"""
if self._children is None:
raise ValueError('Cannot remove children')

self._children.remove(child)
child._parent = None
set_root(child, None)

def refresh(self, viewport):
"""Refresh the layout and appearance of the tree this node is contained in."""
if self._root:
Expand Down