spinetoolbox.mvcmodels.minimal_tree_model

Models to represent items in a tree.

Module Contents

Classes

TreeItem

A tree item that can fetch its children.

MinimalTreeModel

Base class for all tree models.

class spinetoolbox.mvcmodels.minimal_tree_model.TreeItem(model)[source]

A tree item that can fetch its children.

Parameters

model (MinimalTreeModel) – The model where the item belongs.

property model[source]
property children[source]
property parent_item[source]
property display_data[source]
property edit_data[source]
set_has_children_initially(has_children_initially)[source]
has_children()[source]

Returns whether this item has or could have children.

is_valid()[source]

Tests if item is valid.

Returns

True if item is valid, False otherwise

Return type

bool

child(row)[source]

Returns the child at given row or None if out of bounds.

last_child()[source]

Returns the last child.

child_count()[source]

Returns the number of children.

row_count()[source]

Returns the number of rows, which may be different from the number of children. This allows subclasses to hide children.

child_number()[source]

Returns the rank of this item within its parent or -1 if it’s an orphan.

find_children(cond=lambda child: ...)[source]

Returns children that meet condition expressed as a lambda function.

find_child(cond=lambda child: ...)[source]

Returns first child that meet condition expressed as a lambda function or None.

next_sibling()[source]

Returns the next sibling or None if it’s the last.

previous_sibling()[source]

Returns the previous sibling or None if it’s the first.

index()[source]
set_up()[source]
_do_set_up()[source]

Do stuff after the item has been inserted.

_polish_children(children)[source]

Polishes children just before inserting them.

insert_children(position, children)[source]

Insert new children at given position. Returns a boolean depending on how it went.

Parameters
  • position (int) – insert new items here

  • children (list of TreeItem) – insert items from this iterable

Returns

True if the children were inserted successfully, False otherwise

Return type

bool

append_children(children)[source]

Append children at the end.

tear_down()[source]

Do stuff after the item has been removed.

tear_down_recursively()[source]
remove_children(position, count)[source]

Removes count children starting from the given position.

Parameters
  • position (int) – position of the first child to remove

  • count (int) – number of children to remove

Returns

True if operation was successful, False otherwise

Return type

bool

flags(column)[source]

Enables the item and makes it selectable.

data(column, role=Qt.ItemDataRole.DisplayRole)[source]

Returns data for given column and role.

can_fetch_more()[source]

Returns whether this item can fetch more.

fetch_more()[source]

Fetches more children.

abstract set_data(column, value, role)[source]

Sets data for this item.

Parameters
  • column (int) – column index

  • value (object) – a new value

  • role (int) – role of the new value

Returns

True if data was set successfully, False otherwise

Return type

bool

class spinetoolbox.mvcmodels.minimal_tree_model.MinimalTreeModel(parent)[source]

Bases: PySide6.QtCore.QAbstractItemModel

Base class for all tree models.

Init class.

Parameters

parent (SpineDBEditor) –

visit_all(index=QModelIndex(), view=None)[source]

Iterates all items in the model including and below the given index. Iterative implementation so we don’t need to worry about Python recursion limits.

Parameters
  • index (QModelIndex) – an index to start. If not given, we start at the root

  • view (QTreeView) – a tree view. If given, we only yield items that are visible to that view. So for example, if a tree item is not expanded then we don’t yield its children.

Yields

TreeItem

item_from_index(index)[source]

Return the item corresponding to the given index.

Parameters

index (QModelIndex) – model index

Returns

item at index

Return type

TreeItem

index_from_item(item)[source]

Return a model index corresponding to the given item.

Parameters

item (StandardTreeItem) – item

Returns

item’s index

Return type

QModelIndex

index(row, column, parent=QModelIndex())[source]

Returns the index of the item in the model specified by the given row, column and parent index.

parent(index)[source]

Returns the parent of the model item with the given index.

columnCount(parent=QModelIndex())[source]
rowCount(parent=QModelIndex())[source]
data(index, role=Qt.ItemDataRole.DisplayRole)[source]

Returns the data stored under the given role for the index.

setData(index, value, role=Qt.ItemDataRole.EditRole)[source]

Sets data for given index and role. Returns True if successful; otherwise returns False.

flags(index)[source]

Returns the item flags for the given index.

hasChildren(parent)[source]
canFetchMore(parent)[source]
fetchMore(parent)[source]