spinetoolbox.spine_db_editor.mvcmodels.multi_db_tree_item

Base classes to represent items from multiple databases in a tree.

Module Contents

Classes

MultiDBTreeItem

A tree item that may belong in multiple databases.

class spinetoolbox.spine_db_editor.mvcmodels.multi_db_tree_item.MultiDBTreeItem(model, db_map_ids=None)[source]

Bases: spinetoolbox.mvcmodels.minimal_tree_model.TreeItem

A tree item that may belong in multiple databases.

Parameters
  • model (MinimalTreeModel, optional) – item’s model

  • db_map_ids (dict, optional) – maps instances of DatabaseMapping to the id of the item in that db

property visible_children[source]
property db_mngr[source]
abstract property child_item_class[source]

Returns the type of child items.

property display_id[source]

Returns an id for display based on the display key. This id must be the same across all db_maps. If it’s not, this property becomes None and measures need to be taken (see update_children_by_id).

property name[source]
property display_data[source]

Returns the name for display.

property display_database[source]

Returns the database for display.

property display_icon[source]

Returns an icon to display next to the name. Reimplement in subclasses to return something nice.

property first_db_map[source]

Returns the first associated db_map.

property db_maps[source]

Returns a list of all associated db_maps.

property db_map_ids[source]

Returns dict with db_map as key and id as value

property _children_sort_key[source]
property fetch_item_type[source]
item_type[source]

Item type identifier string. Should be set to a meaningful value by subclasses.

visual_key = ['name'][source]
_fetch_index[source]
row_count()[source]

Overriden to use visible_children.

child(row)[source]

Overriden to use visible_children.

child_number()[source]

Overriden to use find_row which is a dict-lookup rather than a list.index() call.

refresh_child_map()[source]

Recomputes the child map.

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

add_db_map_id(db_map, id_)[source]

Adds id for this item in the given db_map.

take_db_map(db_map)[source]

Removes the mapping for given db_map and returns it.

deep_refresh_children()[source]

Refreshes children after taking db_maps from them. Called after removing and updating children for this item.

deep_remove_db_map(db_map)[source]

Removes given db_map from this item and all its descendants.

deep_take_db_map(db_map)[source]

Removes given db_map from this item and all its descendants, and returns a new item from the db_map’s data.

Returns

MultiDBTreeItem, NoneType

deep_merge(other)[source]

Merges another item and all its descendants into this one.

db_map_id(db_map)[source]

Returns the id for this item in given db_map or None if not present.

db_map_data(db_map)[source]

Returns data for this item in given db_map or an empty dict if not present.

db_map_data_field(db_map, field, default=None)[source]

Returns field from data for this item in given db_map or None if not found.

_create_new_children(db_map, children_ids, **kwargs)[source]

Creates new items from ids associated to a db map.

Parameters
  • db_map (DiffDatabaseMapping) – create children for this db_map

  • children_ids (iter) – create children from these ids

Returns

new children

Return type

list of MultiDBTreeItem

make_or_restore_child(db_map, id_, **kwargs)[source]

Makes or restores a child if one was ever made using given db_map and id. The purpose of restoring is to keep using the same FetchParent, which is useful in case the user undoes a series of removal operations that would add items in cascade to the tree.

Parameters
  • db_map (DatabaseMapping) –

  • id (int) –

Returns

MultiDBTreemItem

restore(db_map_ids, **kwargs)[source]
_make_child(db_map_ids, **kwargs)[source]
_merge_children(new_children)[source]

Merges new children into this item. Ensures that each child has a valid display id afterwards.

_insert_children_sorted(new_children)[source]

Inserts and sorts children.

can_fetch_more()[source]

Returns whether this item can fetch more.

fetch_more()[source]

Fetches children from all associated databases.

fetch_more_if_possible()[source]
_key_for_index(db_map)[source]
accepts_item(item, db_map)[source]
handle_items_added(db_map_data)[source]
handle_items_removed(db_map_data)[source]
handle_items_updated(db_map_data)[source]
append_children_by_id(db_map_ids, **kwargs)[source]

Appends children by id.

Parameters

db_map_ids (dict) – maps DiffDatabaseMapping instances to list of ids

remove_children_by_id(db_map_ids)[source]

Removes children by id.

Parameters

db_map_ids (dict) – maps DiffDatabaseMapping instances to list of ids

is_valid()[source]

See base class.

update_children_by_id(db_map_ids, **kwargs)[source]

Updates children by id. Essentially makes sure all children have a valid display id after updating the underlying data. These may require ‘splitting’ a child into several for different dbs or merging two or more children from different dbs.

Examples of problems:

  • The user renames an object_class in one db but not in the others –> we need to split

  • The user renames an object_class and the new name is already ‘taken’ by another object_class in another db_map –> we need to merge

Parameters

db_map_ids (dict) – maps DiffDatabaseMapping instances to list of ids

insert_children(position, children)[source]

Inserts new children at given position.

Parameters
  • position (int) – insert new items here

  • children (Iterable of MultiDBTreeItem) – insert items from this iterable

Returns

True if children were inserted successfully, False otherwise

Return type

bool

remove_children(position, count)[source]

Removes count children starting from the given position.

reposition_child(row)[source]
find_row(db_map, id_)[source]
find_children_by_id(db_map, *ids, reverse=True)[source]

Generates children with the given ids in the given db_map. If the first id is None, then generates all children with the given db_map.

find_rows_by_id(db_map, *ids, reverse=True)[source]
_find_unsorted_rows_by_id(db_map, *ids)[source]

Generates rows corresponding to children with the given ids in the given db_map. If the only id given is None, then generates rows corresponding to all children with the given db_map.

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

Returns data for given column and role.

default_parameter_data()[source]

Returns data to set as default in a parameter table when this item is selected.

tear_down()[source]

Do stuff after the item has been removed.

register_fetch_parent()[source]

Registers item’s fetch parent for all model’s databases.