spinetoolbox.spine_db_editor.mvcmodels.pivot_table_models

Provides pivot table models for the Tabular View.

Module Contents

Classes

TopLeftHeaderItem

Base class for all 'top left pivot headers'.

TopLeftEntityHeaderItem

A top left header for an entity class.

TopLeftParameterHeaderItem

A top left header for parameter_definition.

TopLeftParameterIndexHeaderItem

A top left header for parameter index.

TopLeftAlternativeHeaderItem

A top left header for alternative.

TopLeftScenarioHeaderItem

A top left header for scenario.

TopLeftDatabaseHeaderItem

A top left header for database.

PivotTableModelBase

param db_editor

ParameterValuePivotTableModel

A model for the pivot table in parameter_value input type.

IndexExpansionPivotTableModel

A model for the pivot table in parameter index expansion input type.

ElementPivotTableModel

A model for the pivot table in element input type.

ScenarioAlternativePivotTableModel

A model for the pivot table in scenario alternative input type.

PivotTableSortFilterProxy

Initialize class.

Functions

_make_get_id(action)

Returns a function to compute the db_map-id tuple of an item.

class spinetoolbox.spine_db_editor.mvcmodels.pivot_table_models.TopLeftHeaderItem(model)[source]

Base class for all ‘top left pivot headers’. Represents a header located in the top left area of the pivot table.

Parameters

model (PivotTableModelBase) –

property model[source]
property db_mngr[source]
_get_header_data_from_db(item_type, header_id, field_name, role)[source]
static accepts(header_id)[source]

Tests if header id is valid.

Parameters

header_id (Any) – header id

Returns

True if id is valid, False otherwise

Return type

bool

abstract header_data(header_id, role=Qt.ItemDataRole.DisplayRole)[source]

Returns header data for given id.

Parameters
  • header_id (Any) – header id

  • role (Qt.ItemDataRole) – data role

Returns

data corresponding to role

Return type

Any

abstract update_data(db_map_data)[source]

Updates database data.

Parameters

db_map_data (dict) – update data

Returns

True if data was successfully updated, False otherwise

Return type

bool

abstract add_data(names, db_map)[source]

Adds more data to database.

Parameters
  • names (set of str) – header names

  • db_map (DatabaseMapping) – database to add the data to

Returns

True if data was added successfully, False otherwise

Return type

bool

class spinetoolbox.spine_db_editor.mvcmodels.pivot_table_models.TopLeftEntityHeaderItem(model, rank, class_name, class_id)[source]

Bases: TopLeftHeaderItem

A top left header for an entity class.

Parameters

model (PivotTableModelBase) –

property header_type[source]
property name[source]
property class_id[source]
header_data(header_id, role=Qt.ItemDataRole.DisplayRole)[source]

See base class.

update_data(db_map_data)[source]

See base class.

add_data(names, db_map)[source]

See base class.

class spinetoolbox.spine_db_editor.mvcmodels.pivot_table_models.TopLeftParameterHeaderItem(model)[source]

Bases: TopLeftHeaderItem

A top left header for parameter_definition.

Parameters

model (PivotTableModelBase) –

property header_type[source]
property name[source]
header_data(header_id, role=Qt.ItemDataRole.DisplayRole)[source]

See base class.

update_data(db_map_data)[source]

See base class.

add_data(names, db_map)[source]

See base class.

class spinetoolbox.spine_db_editor.mvcmodels.pivot_table_models.TopLeftParameterIndexHeaderItem(model)[source]

Bases: TopLeftHeaderItem

A top left header for parameter index.

Parameters

model (PivotTableModelBase) –

property header_type[source]
property name[source]
header_data(header_id, role=Qt.ItemDataRole.DisplayRole)[source]

See base class.

update_data(db_map_data)[source]

See base class.

add_data(names, db_map)[source]

See base class.

class spinetoolbox.spine_db_editor.mvcmodels.pivot_table_models.TopLeftAlternativeHeaderItem(model)[source]

Bases: TopLeftHeaderItem

A top left header for alternative.

Parameters

model (PivotTableModelBase) –

property header_type[source]
property name[source]
header_data(header_id, role=Qt.ItemDataRole.DisplayRole)[source]

See base class.

update_data(db_map_data)[source]

See base class.

add_data(names, db_map)[source]

See base class.

class spinetoolbox.spine_db_editor.mvcmodels.pivot_table_models.TopLeftScenarioHeaderItem(model)[source]

Bases: TopLeftHeaderItem

A top left header for scenario.

Parameters

model (PivotTableModelBase) –

property header_type[source]
property name[source]
header_data(header_id, role=Qt.ItemDataRole.DisplayRole)[source]

See base class.

update_data(db_map_data)[source]

See base class.

add_data(names, db_map)[source]

See base class.

class spinetoolbox.spine_db_editor.mvcmodels.pivot_table_models.TopLeftDatabaseHeaderItem(model)[source]

Bases: TopLeftHeaderItem

A top left header for database.

Parameters

model (PivotTableModelBase) –

property header_type[source]
property name[source]
header_data(header_id, role=Qt.ItemDataRole.DisplayRole)[source]

See base class.

update_data(db_map_data)[source]

See base class.

add_data(names, db_map)[source]

See base class.

set_data(codename)[source]

Sets database mapping’s codename.

Parameters

codename (str) – database codename

Returns

True if codename was acceptable, False otherwise

Return type

bool

take_suggested_db_map()[source]

Suggests database mapping resetting the suggestion afterwards.

Returns

database mapping

Return type

DatabaseMapping

suggest_db_map_codename()[source]

Suggests a database mapping codename.

Returns

codename

Return type

str

class spinetoolbox.spine_db_editor.mvcmodels.pivot_table_models.PivotTableModelBase(db_editor)[source]

Bases: PySide6.QtCore.QAbstractTableModel

Parameters

db_editor (SpineDBEditor) –

property db_maps[source]
abstract property item_type[source]

Returns the item type.

property plot_x_column[source]

Returns the index of the column designated as Y values for plotting or None.

_CHUNK_SIZE = 1000[source]
model_data_changed[source]
frozen_values_added[source]
frozen_values_removed[source]
reset_fetch_parents()[source]
set_fetch_parents_non_obsolete()[source]
abstract _fetch_parents()[source]

Yields fetch parents for this model.

Yields

FetchParent

canFetchMore(_)[source]
fetchMore(_)[source]
_reset_data_count()[source]
_collect_more_data()[source]
_collect_more_rows()[source]
_collect_more_columns()[source]
abstract call_reset_model(pivot=None)[source]
Parameters

pivot (tuple, optional) – list of rows, list of columns, list of frozen indexes, frozen value

abstract static make_delegate(parent)[source]
reset_model(data, index_ids, rows=(), columns=(), frozen=(), frozen_value=())[source]
clear_model()[source]
update_model(data)[source]

Update model with new data, but doesn’t grow the model.

Parameters

data (dict) –

add_to_model(db_map_data)[source]
remove_from_model(data)[source]
_emit_all_data_changed()[source]
set_pivot(rows, columns, frozen, frozen_value)[source]
set_frozen(frozen)[source]

Sets the order of frozen headers without changing model data.

Parameters

frozen (list of str) – new frozen

set_frozen_value(frozen_value)[source]

Sets frozen value resetting the model.

Parameters

frozen_value (tuple) – frozen value

Returns

True if value was set, False otherwise

Return type

bool

set_plot_x_column(column, is_x)[source]

Sets or clears the X flag on a column

x_value(index)[source]

Returns x value for given model index.

Parameters

index (QModelIndex) – model index

Returns

x value

Return type

Any

x_parameter_name()[source]

Returns x column’s parameter name.

Returns

parameter name

Return type

str

headerRowCount()[source]

Returns number of rows occupied by header.

headerColumnCount()[source]

Returns number of columns occupied by header.

dataRowCount()[source]

Returns number of rows that contain actual data.

dataColumnCount()[source]

Returns number of columns that contain actual data.

emptyRowCount()[source]
emptyColumnCount()[source]
rowCount(parent=QModelIndex())[source]

Number of rows in table, number of header rows + datarows + 1 empty row

columnCount(parent=QModelIndex())[source]

Number of columns in table, number of header columns + datacolumns + 1 empty columns

flags(index)[source]

Roles for data

top_left_indexes()[source]

Returns indexes in the top left area.

Returns

list(QModelIndex): top indexes (horizontal headers, associated to rows) list(QModelIndex): left indexes (vertical headers, associated to columns)

index_within_top_left(index)[source]
index_in_top(index)[source]
index_in_left(index)[source]
index_in_top_left(index)[source]

Returns whether the given index is in top left corner, where pivot names are displayed.

index_in_column_headers(index)[source]

Returns whether the given index is in column headers (horizontal) area.

index_in_row_headers(index)[source]

Returns whether the given index is in row headers (vertical) area.

index_in_headers(index)[source]
index_in_empty_column_headers(index)[source]

Returns whether the given index is in empty column headers (vertical) area.

index_in_empty_row_headers(index)[source]

Returns whether the given index is in empty row headers (vertical) area.

index_in_data(index)[source]

Returns whether the given index is in data area.

column_is_index_column(column)[source]

Returns True if column is the column containing expanded parameter_value indexes.

headerData(section, orientation, role=Qt.ItemDataRole.DisplayRole)[source]
map_to_pivot(index)[source]

Returns a tuple of row and column in the pivot model that corresponds to the given model index.

Parameters

index (QModelIndex) –

Returns

row int: column

Return type

int

top_left_id(index)[source]

Returns the id of the top left header corresponding to the given header index.

Parameters

index (QModelIndex) –

Returns

int, NoneType

_header_id(index)[source]

Returns the id of the given row or column header index.

Parameters

index (QModelIndex) –

Returns

tuple or DatabaseMapping or NoneType

_header_ids(row, column)[source]

Returns the ids for the headers at given row and column.

Parameters
  • row (int) –

  • column (int) –

Returns

tuple(int)

header_name(index)[source]

Returns the name corresponding to the given header index. Used by PivotTableView.

Parameters

index (QModelIndex) –

Returns

str

_color_data(index)[source]
_text_alignment_data(index)[source]
_header_data(index)[source]
_header_name(top_left_id, header_id)[source]
get_db_map_entities()[source]

Returns a dict mapping db maps to a list of dict entity items in the current class.

Returns

dict

abstract _data(index, role)[source]
data(index, role=Qt.ItemDataRole.DisplayRole)[source]
setData(index, value, role=Qt.ItemDataRole.EditRole)[source]
batch_set_data(indexes, values)[source]
_batch_set_inner_data(inner_data)[source]
abstract _do_batch_set_inner_data(row_map, column_map, data, values)[source]
_batch_set_header_data(header_data)[source]

Sets header data for multiple indexes at once.

Parameters

header_data (list of tuple) – mapping from index to data

Returns

True if data was set successfully, False otherwise

Return type

bool

_batch_set_empty_header_data(header_data, get_top_left_id)[source]
tear_down()[source]

Sets fetch parents obsolete preventing further updates.

class spinetoolbox.spine_db_editor.mvcmodels.pivot_table_models.ParameterValuePivotTableModel(parent)[source]

Bases: PivotTableModelBase

A model for the pivot table in parameter_value input type.

Parameters

parent (SpineDBEditor) –

property item_type[source]

Returns the item type.

_handle_entity_classes_added(db_map_data)[source]
_handle_entity_classes_removed(db_map_data)[source]
_handle_entities_added(db_map_data)[source]
_handle_entities_removed(db_map_data)[source]
_handle_parameter_definitions_added(db_map_data)[source]
_handle_parameter_definitions_removed(db_map_data)[source]
_handle_parameter_values_added(db_map_data)[source]
_handle_parameter_values_removed(db_map_data)[source]
_handle_alternatives_added(db_map_data)[source]
_handle_alternatives_removed(db_map_data)[source]
_load_empty_parameter_value_data(db_map_entities=None, db_map_parameter_ids=None, db_map_alternative_ids=None)[source]

Returns a dict containing all possible combinations of entities and parameters for the current class in all db_maps.

Parameters
  • db_map_entities (dict, optional) – if given, only load data for these db maps and entities

  • db_map_parameter_ids (dict, optional) – if given, only load data for these db maps and parameter definitions

  • db_map_alternative_ids (dict, optional) – if given, only load data for these db maps and alternatives

Returns

Key is a tuple object_id, …, parameter_id, value is None.

Return type

dict

_all_combination_for_empty_parameter_value(db_map_entities, db_map_parameter_ids, db_map_alternative_ids)[source]
_load_full_parameter_value_data(db_map_parameter_values=None, action='add')[source]

Returns a dict of parameter values for the current class.

Parameters
  • db_map_parameter_values (list, optional) –

  • action (str) –

Returns

Key is a tuple object_id, …, parameter_id, value is the parameter_value.

Return type

dict

_fetch_parents()[source]

Yields fetch parents for this model.

Yields

FetchParent

_db_map_element_ids(header_ids)[source]
db_map_entity_ids(indexes)[source]

Returns db_map and entity ids for given indexes. Used by PivotTableView.

Parameters

indexes (list of QModelIndex) – indexes corresponding to entity items

Returns

mapping DatabaseMapping to set of entity ids

Return type

dict

all_header_names(index)[source]

Returns the entity, parameter, alternative, and db names corresponding to the given data index.

Parameters

index (QModelIndex) –

Returns

object names str: parameter name str: alternative name str: db name

Return type

list(str)

index_name(index)[source]

Returns a string that concatenates the object and parameter names corresponding to the given data index. Used by plotting and ParameterValueEditor.

Parameters

index (QModelIndex) –

Returns

str

column_name(column)[source]

Returns a string that concatenates the object and parameter names corresponding to the given column. Used by plotting.

Parameters

column (int) –

Returns

str

call_reset_model(pivot=None)[source]

See base class.

static make_delegate(parent)[source]
_default_pivot(data)[source]
_data(index, role)[source]
_do_batch_set_inner_data(row_map, column_map, data, values)[source]
_entity_parameter_value_to_add(db_map, header_ids, value_and_type, ent_id_lookup=None)[source]
_make_parameter_value_to_add()[source]
static _parameter_value_to_update(id_, header_ids, value_and_type)[source]
_batch_set_parameter_value_data(row_map, column_map, data, values)[source]

Sets parameter values in batch.

_add_parameter_values(db_map_data)[source]
_update_parameter_values(db_map_data)[source]
get_set_data_delayed(index)[source]

Returns a function that ParameterValueEditor can call to set data for the given index at any later time, even if the model changes.

Parameters

index (QModelIndex) –

Returns

function

_get_db_map_parameter_value_or_def_ids(item_type)[source]

Returns a dict mapping db maps to a list of integer parameter (value or def) ids from the current class.

Parameters

item_type (str) – either “parameter_value” or “parameter_definition”

Returns

dict

_get_db_map_parameter_values_or_defs(item_type)[source]

Returns a dict mapping db maps to list of dict parameter (value or def) items from the current class.

Parameters

item_type (str) – either “parameter_value” or “parameter_definition”

Returns

dict

load_full_parameter_value_data(db_map_parameter_values=None, action='add')[source]

Returns a dict of parameter values for the current class.

Parameters
  • db_map_parameter_values (list, optional) –

  • action (str) –

Returns

Key is a tuple object_id, …, parameter_id, value is the parameter_value.

Return type

dict

class spinetoolbox.spine_db_editor.mvcmodels.pivot_table_models.IndexExpansionPivotTableModel(parent)[source]

Bases: ParameterValuePivotTableModel

A model for the pivot table in parameter index expansion input type.

Parameters

parent (SpineDBEditor) –

INDEX_INSERTION_POINT[source]
call_reset_model(pivot=None)[source]

See base class.

emptyRowCount()[source]
emptyColumnCount()[source]
flags(index)[source]

Roles for data

column_is_index_column(column)[source]

Returns True if column is the column containing expanded parameter_value indexes.

_handle_parameter_values_removed(db_map_data)[source]
_load_empty_parameter_value_data(db_map_entities=None, db_map_parameter_ids=None, db_map_alternative_ids=None)[source]

Does not load the data since adding values in index expansion mode is disabled.

Parameters
  • db_map_entities (dict, optional) – mapping from database map to iterable of entity items

  • db_map_parameter_ids (dict, optional) – mapping from database map to iterable of parameter definition id tuples

  • db_map_alternative_ids (dict, optional) – mapping from database map to iterable of alternative id tuples

Returns

empty data

Return type

dict

_load_full_parameter_value_data(db_map_parameter_values=None, action='add')[source]

Makes a dict of expanded parameter values for the current class.

Parameters
  • db_map_parameter_values (list, optional) –

  • action (str) –

Returns

mapping from unique value id tuple to value tuple

Return type

dict

_data(index, role)[source]
static _parameter_value_to_update(id_, header_ids, value_and_type)[source]
_update_parameter_values(db_map_data)[source]
_indexes(value)[source]
class spinetoolbox.spine_db_editor.mvcmodels.pivot_table_models.ElementPivotTableModel(parent)[source]

Bases: PivotTableModelBase

A model for the pivot table in element input type.

Parameters

parent (SpineDBEditor) –

property item_type[source]

Returns the item type.

_handle_entities_added(db_map_data)[source]
_handle_entities_removed(db_map_data)[source]
_load_empty_element_data(db_map_data)[source]
_handle_elements_added(db_map_data)[source]
_handle_elements_removed(db_map_data)[source]
_fetch_parents()[source]

Yields fetch parents for this model.

Yields

FetchParent

call_reset_model(pivot=None)[source]

See base class.

static make_delegate(parent)[source]
_default_pivot(data)[source]
_data(index, role)[source]
_do_batch_set_inner_data(row_map, column_map, data, values)[source]
_batch_set_entity_data(row_map, column_map, data, values)[source]
_load_full_element_data(db_map_entities=None, action='add')[source]

Returns a dict of entity elements in the current class.

Parameters
  • db_map_entities (dict, optional) – a mapping from database map to entities in the current entity class

  • action (str) – ‘add’ or ‘remove’

Returns

Key is db_map-object id tuple, value is relationship id.

Return type

dict

class spinetoolbox.spine_db_editor.mvcmodels.pivot_table_models.ScenarioAlternativePivotTableModel(parent)[source]

Bases: PivotTableModelBase

A model for the pivot table in scenario alternative input type.

Parameters

parent (SpineDBEditor) –

property item_type[source]

Returns the item type.

_handle_scenarios_added(db_map_data)[source]
_handle_scenarios_removed(db_map_data)[source]
_handle_alternatives_added(db_map_data)[source]
_handle_alternatives_removed(db_map_data)[source]
_handle_scenario_alternatives_changed(db_map_data)[source]
_fetch_parents()[source]

Yields fetch parents for this model.

Yields

FetchParent

call_reset_model(pivot=None)[source]

See base class.

static make_delegate(parent)[source]
_default_pivot(data)[source]
_data(index, role)[source]
_do_batch_set_inner_data(row_map, column_map, data, values)[source]
_batch_set_scenario_alternative_data(row_map, column_map, data, values)[source]
_load_scenario_alternative_data(db_map_scenarios=None, db_map_alternatives=None)[source]

Returns a dict containing all scenario alternatives.

Returns

Key is db_map-id tuple, value is None or rank.

Return type

dict

class spinetoolbox.spine_db_editor.mvcmodels.pivot_table_models.PivotTableSortFilterProxy(parent=None)[source]

Bases: PySide6.QtCore.QSortFilterProxyModel

Initialize class.

model_data_changed[source]
setSourceModel(model)[source]
set_filter(identifier, filter_value)[source]

Sets filter for a given index (object_class) name.

Parameters
  • identifier (int) – index identifier

  • filter_value (set, None) – A set of accepted values, or None if no filter (all pass)

clear_filter()[source]
accept_index(index, index_ids)[source]
filterAcceptsRow(source_row, source_parent)[source]

Returns true if the item in the row indicated by the given source_row and source_parent should be included in the model; otherwise returns false.

filterAcceptsColumn(source_column, source_parent)[source]

Returns true if the item in the column indicated by the given source_column and source_parent should be included in the model; otherwise returns false.

batch_set_data(indexes, values)[source]
spinetoolbox.spine_db_editor.mvcmodels.pivot_table_models._make_get_id(action)[source]

Returns a function to compute the db_map-id tuple of an item.

Parameters

action (str) – “add” or “remove”

Returns

function to compute db_map-id tuples

Return type

Callable