spinetoolbox.mvcmodels.map_model

A model for maps, used by the parameter_value editors.

Module Contents

Classes

MapModel

A model for Map type parameter values.

Functions

_rows_to_dict(rows)

Turns table into nested dictionaries.

_reconstruct_map(tree)

Constructs a Map from a nested dictionary.

_data_length(row)

Counts the number of non-empty elements at the beginning of row.

_gather_index_names(map_value)

Collects index names from Map.

_apply_index_names(map_value, index_names)

Applies index names to Map.

_numpy_string_to_python_strings(rows)

Converts instances of numpy.str_ to regular Python strings.

Attributes

empty

Sentinel for empty cells.

spinetoolbox.mvcmodels.map_model.empty[source]

Sentinel for empty cells.

class spinetoolbox.mvcmodels.map_model.MapModel(map_value, parent)[source]

Bases: PySide6.QtCore.QAbstractTableModel

A model for Map type parameter values.

This model represents the Map as a 2D table. Each row consists of one or more index columns and a value column. The last columns of a row are padded with Nones.

Example

Map {
    "A": 1.0
    "B": Map {"a": -1.0}
    "C": 3.0
}

The table corresponding to the above map:

“A”

1.0

None

“B”

“a”

-1.0

“C”

3.0

None

Parameters
  • map_value (Map) – a map

  • parent (QObject) – parent object

append_column()[source]

Appends a new column to the right.

clear(indexes)[source]

Clears table cells.

Parameters

indexes (list of QModelIndex) – indexes to clear

columnCount(index=QModelIndex())[source]

Returns the number of columns in this model.

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

Returns the data associated with the given role.

flags(index)[source]

Returns flags at index.

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

Returns row numbers for vertical headers and column titles for horizontal ones.

insertColumns(column, count, parent=QModelIndex())[source]

Inserts new columns into the map.

Parameters
  • column (int) – column index where to insert

  • count (int) – number of new columns

  • parent (QModelIndex) – ignored

Returns

True if insertion was successful, False otherwise

Return type

bool

insertRows(row, count, parent=QModelIndex())[source]

Inserts new rows into the map.

Parameters
  • row (int) – an index where to insert the new data

  • count (int) – number of rows to insert

  • parent (QModelIndex) – an index to a parent model

Returns

True if the operation was successful

Return type

bool

is_leaf_value(index)[source]

Checks if given model index contains a leaf value.

Parameters

index (QModelIndex) – index to check

Returns

True if index points to leaf value, False otherwise

Return type

bool

_is_in_expanse(row, column)[source]

Returns True, if given row and column is in the right or bottom ‘expanding’ zone.

Parameters
  • row (int) – row index

  • column (int) – column index

Returns

True if the cell is in the expanse, False otherwise

Return type

bool

is_expanse_column(column)[source]

Returns True if given column is the expanse column.

Parameters

column (int) – column

Returns

True if column is expanse column, False otherwise

Return type

bool

is_expanse_row(row)[source]

Returns True if given row is the expanse row.

Parameters

row (int) – row

Returns

True if row is the expanse row, False otherwise

Return type

bool

removeColumns(column, count, parent=QModelIndex())[source]

Removes columns from the map.

Parameters
  • column (int) – first column to remove

  • count (int) – number of columns to remove

  • parent (QModelIndex) – an index to a parent model

Returns

True if the operation was successful

removeRows(row, count, parent=QModelIndex())[source]

Removes rows from the map.

Parameters
  • row (int) – first row to remove

  • count (int) – number of rows to remove

  • parent (QModelIndex) – an index to a parent model

Returns

True if the operation was successful

reset(map_value)[source]

Resets the model to given map_value.

rowCount(parent=QModelIndex())[source]

Returns the number of rows.

set_box(top_left, bottom_right, data)[source]

Sets data for several indexes at once.

Parameters
  • top_left (QModelIndex) – a sequence of model indexes

  • bottom_right (QModelIndex) – a sequence of values corresponding to the indexes

  • data (list of list) – box of data

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

Sets data in the map.

Parameters
  • index (QModelIndex) – an index to the model

  • value (object) – JSON representation of the value

  • role (int) – a role

Returns

True if the operation was successful

Return type

bool

setHeaderData(section, orientation, value, role=Qt.ItemDataRole.EditRole)[source]
trim_columns()[source]

Removes empty columns from the right.

value()[source]

Returns the Map.

index_name(index)[source]
spinetoolbox.mvcmodels.map_model._rows_to_dict(rows)[source]

Turns table into nested dictionaries.

Parameters

rows (list) – a list of row data

Returns

a nested dictionary

Return type

dict

spinetoolbox.mvcmodels.map_model._reconstruct_map(tree)[source]

Constructs a Map from a nested dictionary.

Parameters

tree (dict) – a nested dictionary

Returns

reconstructed Map

Return type

Map

spinetoolbox.mvcmodels.map_model._data_length(row)[source]

Counts the number of non-empty elements at the beginning of row.

Parameters

row (list) – a row of data

Returns

data length

Return type

int

spinetoolbox.mvcmodels.map_model._gather_index_names(map_value)[source]

Collects index names from Map.

Returns only the ‘first’ index name for nested maps at the same depth.

Parameters

map_value (Map) – map to investigate

Returns

index names

Return type

list of str

spinetoolbox.mvcmodels.map_model._apply_index_names(map_value, index_names)[source]

Applies index names to Map.

Parameters
  • map_value (Map) – target Map

  • index_names (list of str) – index names

spinetoolbox.mvcmodels.map_model._numpy_string_to_python_strings(rows)[source]

Converts instances of numpy.str_ to regular Python strings.

Parameters

rows (list of list) – table rows

Returns

converted rows

Return type

list of list