def test_nested_custom_lookup(tree: Group): tree._lookup = {str: lambda x: x.name} # first level g1 = tree[1] assert g1.name == 'g1' # index with integer as usual assert tree.index("g1") == 1 assert tree['g1'] == g1 # index with string also works # second level g1_2 = g1[2] assert tree[1, 2].name == '5' assert tree.index('5') == (1, 2) assert tree['5'] == g1_2
def _recursive_make_group(lst, level=0): """Make a Tree of Group/Node objects from a nested list.""" out = [] for item in lst: if isinstance(item, list): out.append(_recursive_make_group(item, level=level + 1)) else: out.append(Node(name=str(item))) return Group(out, name=f'g{level}')
def tree(): return Group( [ Node(name="1"), Group( [ Node(name="2"), Group([Node(name="3"), Node(name="4")], name="g2"), Node(name="5"), Node(name="6"), Node(name="7"), ], name="g1", ), Node(name="8"), Node(name="9"), ], name="root", )
def tree_model(): root = Group( [ Node(name="1"), Group( [ Node(name="2"), Group([Node(name="3"), Node(name="4")], name="g2"), Node(name="5"), Node(name="6"), Node(name="7"), ], name="g1", ), Node(name="8"), Node(name="9"), ], name="root", ) return QtNodeTreeModel(root)
def test_node_indexing(tree: Group): expected_indices = [ 0, 1, (1, 0), (1, 1), (1, 1, 0), (1, 1, 1), (1, 2), (1, 3), (1, 4), 2, 3, ] assert list(tree._iter_indices()) == expected_indices for index in tree._iter_indices(): assert tree.index(tree[index]) == index item = tree[index] if item.parent: assert item.parent.index(item) is not None
def _assert_models_synced(model: Group, qt_model: QtNodeTreeModel): for item in model.traverse(): model_idx = qt_model.nestedIndex(item.index_from_root()) node = qt_model.getItem(model_idx) assert item.name == node.name
def test_remove_selected(tree: Group): """Test remove_selected works, with nested""" node = tree[(1, 0)] tree.selection.active = node tree.remove_selected()
def test_deep_index(tree: Group): """Test deep indexing""" node = tree[(1, 0)] assert tree.index(node) == (1, 0)
# create some readable logging. Drag and drop the items in the tree to # see what sort of events are happening in the background. end = "\033[0m" Bold = "\033[1m" Dim = "\033[2m" ResetDim = "\033[22m" red = "\033[0;31m" green = "\033[0;32m" colorlog_format = f'{green}%(levelname)6s:{end} {Dim}%(name)43s.{ResetDim}{red}%(funcName)-18s{end}{"%(message)s"}' logging.basicConfig(level=logging.DEBUG, format=colorlog_format) get_app() tip = Node(name='tip') lg2 = Group([Node(name='2'), Node(name='3')], name="g2") lg1 = Group( [Node(name='1'), lg2, Node(name='4'), Node(name='5'), tip], name="g1", ) root = Group( [Node(name='6'), lg1, Node(name='7'), Node(name='8'), Node(name='9')], name="root", ) # pretty repr makes nested tree structure more interpretable print(root)
from napari._qt.containers import QtNodeTreeView from napari.utils.tree import Node, Group get_app() # create a group of nodes. root = Group( [ Node(name='6'), Group( [ Node(name='1'), Group([Node(name='2'), Node(name='3')], name="g2"), Node(name='4'), Node(name='5'), Node(name='tip'), ], name="g1", ), Node(name='7'), Node(name='8'), Node(name='9'), ], name="root", ) # create Qt view onto the Group view = QtNodeTreeView(root) # show the view view.show() # pretty __str__ makes nested tree structure more interpretable