models

Classes for handling models in PySide2’s model/view framework. Note: These are Spine Toolbox internal data models.

authors:
  1. Savolainen (VTT), M. Marin (KTH), P. Vennström (VTT)
date:

23.1.2018

Module Contents

class models.ProjectItemModel(toolbox, root)[source]

Bases: PySide2.QtCore.QAbstractItemModel

Class to store project items, e.g. Data Stores, Data Connections, Tools, Views.

toolbox

QMainWindow instance

Type:ToolboxUI
root[source]

Root item for the project item tree

Type:ProjectItem
root(self)[source]

Returns root project item.

rowCount(self, parent=QModelIndex())[source]

Reimplemented rowCount method.

Parameters:parent (QModelIndex) – Index of parent item whose children are counted.
Returns:Number of children of given parent
Return type:int
columnCount(self, parent=QModelIndex())[source]

Returns model column count.

flags(self, index)[source]

Returns flags for the item at given index

Parameters:index (QModelIndex) – Flags of item at this index.
parent(self, index=QModelIndex())[source]

Returns index of the parent of given index.

Parameters:index (QModelIndex) – Index of item whose parent is returned
Returns:Index of parent item
Return type:QModelIndex
index(self, row, column, parent=QModelIndex())[source]

Returns index of item with given row, column, and parent.

Parameters:
  • row (int) – Item row
  • column (int) – Item column
  • parent (QModelIndex) – Parent item index
Returns:

Item index

Return type:

QModelIndex

data(self, index, role=None)[source]

Returns data in the given index according to requested role.

Parameters:
  • index (QModelIndex) – Index to query
  • role (int) – Role to return
Returns:

Data depending on role.

Return type:

object

project_item(self, index)[source]

Returns project item at given index.

Parameters:index (QModelIndex) – Index of project item
Returns:Item at given index or root project item if index is not valid
Return type:ProjectItem
find_category(self, category_name)[source]

Returns the index of the given category name.

Parameters:category_name (str) – Name of category item to find
Returns:index of a category item or None if it was not found
Return type:QModelIndex
find_item(self, name)[source]

Returns the QModelIndex of the project item with the given name

Parameters:name (str) – The searched project item (long) name
Returns:Index of a project item with the given name or None if not found
Return type:QModelIndex
insert_item(self, item, parent=QModelIndex())[source]

Adds a new item to model. Fails if given parent is not a category item nor a root item. New item is inserted as the last item.

Parameters:
  • item (ProjectItem) – Project item to add to model
  • parent (QModelIndex) – Parent project item
Returns:

True if successful, False otherwise

Return type:

bool

remove_item(self, item, parent=QModelIndex())[source]

Removes item from model.

Parameters:
  • item (ProjectItem) – Project item to remove
  • parent (QModelIndex) – Parent of item that is to be removed
Returns:

True if item removed successfully, False if item removing failed

Return type:

bool

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

Changes the name of the project item at given index to given value. # TODO: If the item is a Data Store the reference sqlite path must be updated.

Parameters:
  • index (QModelIndex) – Project item index
  • value (str) – New project item name
  • role (int) – Item data role to set
Returns:

True or False depending on whether the new name is acceptable.

Return type:

bool

items(self, category_name=None)[source]

Returns a list of items in model according to category name. If no category name given, returns all project items in a list.

Parameters:category_name (str) – Item category. Data Connections, Data Stores, Tools or Views permitted.
Returns:obj:’list’ of :obj:’ProjectItem’: Depending on category_name argument, returns all items or only items according to category. An empty list is returned if there are no items in the given category or if an unknown category name was given.
n_items(self)[source]

Returns the number of all project items in the model excluding category items and root.

Returns:Number of items
Return type:int
item_names(self)[source]

Returns all project item names in a list.

Returns:‘list’ of obj:’str’: Item names
Return type:obj
new_item_index(self, category)[source]

Returns the index where a new item can be appended according to category. This is needed for appending the connection model.

Parameters:category (str) – Display Role of the parent
Returns:Number of items according to category
Return type:int
short_name_reserved(self, short_name)[source]

Checks if the directory name derived from the name of the given item is in use.

Parameters:short_name (str) – Item short name
Returns:True if short name is taken, False if it is available.
Return type:bool
class models.ToolTemplateModel(toolbox=None)[source]

Bases: PySide2.QtCore.QAbstractListModel

Class to store tools that are available in a project e.g. GAMS or Julia models.

rowCount(self, parent=None)[source]

Must be reimplemented when subclassing. Returns the number of Tools in the model.

Parameters:parent (QModelIndex) – Not used (because this is a list)
Returns:Number of rows (available tools) in the model
data(self, index, role=None)[source]

Must be reimplemented when subclassing.

Parameters:
  • index (QModelIndex) – Requested index
  • role (int) – Data role
Returns:

Data according to requested role

flags(self, index)[source]

Returns enabled flags for the given index.

Parameters:index (QModelIndex) – Index of Tool
insertRow(self, tool, row=None, parent=QModelIndex())[source]

Insert row (tool) into model.

Parameters:
  • tool (Tool) – Tool added to the model
  • row (str) – Row to insert tool to
  • parent (QModelIndex) – Parent of child (not used)
Returns:

Void

removeRow(self, row, parent=QModelIndex())[source]

Remove row (tool) from model.

Parameters:
  • row (int) – Row to remove the tool from
  • parent (QModelIndex) – Parent of tool on row (not used)
Returns:

Boolean variable

update_tool_template(self, tool, row)[source]

Update tool template.

Parameters:
  • tool (ToolTemplate) – new tool, to replace the old one
  • row (int) – Position of the tool to be updated
Returns:

Boolean value depending on the result of the operation

tool_template(self, row)[source]

Returns tool template on given row.

Parameters:row (int) – Row of tool template
Returns:ToolTemplate from tool template list or None if given row is zero
find_tool_template(self, name)[source]

Returns tool template with the given name.

Parameters:name (str) – Name of tool template to find
tool_template_row(self, name)[source]

Returns the row on which the given template is located or -1 if it is not found.

tool_template_index(self, name)[source]

Returns the QModelIndex on which a tool template with the given name is located or invalid index if it is not found.

class models.ConnectionModel(toolbox=None)[source]

Bases: PySide2.QtCore.QAbstractTableModel

Table model for storing connections between items.

flags(self, index)[source]

Returns flags for table items.

rowCount(self, *args, **kwargs)[source]

Number of rows in the model. This should be the same as the number of items in the project.

columnCount(self, *args, **kwargs)[source]

Number of columns in the model. This should be the same as the number of items in the project.

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

Returns header data according to given role.

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

Sets the data for the given role and section in the header with the specified orientation to the value supplied.

data(self, index, role)[source]

Returns the data stored under the given role for the item referred to by the index. DisplayRole is a string “False” or “True” depending on if a Link is present.

Parameters:
  • index (QModelIndex) – Index of item
  • role (int) – Data role
Returns:

Item data for given role.

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

Set data of single cell in table. Toggles the checkbox state at index.

Parameters:
  • index (QModelIndex) – Index of data to edit
  • value (QVariant) – Value to write to index (Link instance)
  • role (int) – Role for editing
insertRows(self, row, count, parent=QModelIndex())[source]

Inserts count rows into the model before the given row. Items in the new row will be children of the item represented by the parent model index.

Parameters:
  • row (int) – Row number where new rows are inserted
  • count (int) – Number of inserted rows
  • parent (QModelIndex) – Parent index
Returns:

True if rows were inserted successfully, False otherwise

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

Inserts count columns into the model before the given column. Items in the new column will be children of the item represented by the parent model index.

Parameters:
  • column (int) – Column number where new columns are inserted
  • count (int) – Number of inserted columns
  • parent (QModelIndex) – Parent index
Returns:

True if columns were inserted successfully, False otherwise

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

Removes count rows starting with the given row under parent.

Parameters:
  • row (int) – Row number where to start removing rows
  • count (int) – Number of removed rows
  • parent (QModelIndex) – Parent index
Returns:

True if rows were removed successfully, False otherwise

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

Removes count columns starting with the given column under parent.

Parameters:
  • column (int) – Column number where to start removing columns
  • count (int) – Number of removed columns
  • parent (QModelIndex) – Parent index
Returns:

True if columns were removed successfully, False otherwise

append_item(self, name, index)[source]

Embiggens connections table by a new item.

Parameters:
  • name (str) – New item name
  • index (int) – Table row and column where the new item is appended
Returns:

True if successful, False otherwise

remove_item(self, name)[source]

Removes project item from connections table.

Parameters:name (str) – Name of removed item
Returns:True if successful, False otherwise
output_items(self, name)[source]

Returns a list of output items for the given item.

Parameters:name (str) – Project item name
Returns:Output project item names in a list if they exist or an empty list if they don’t.
Return type:(list)
input_items(self, name)[source]

Returns a list of input items for the given item.

Parameters:name (str) – Project item name
Returns:Input project item names in a list if they exist or an empty list if they don’t.
Return type:(list)
get_connections(self)[source]

Returns the internal data structure of the model.

Returns a list of connected links for the given item

reset_model(self, connection_table)[source]

Reset model. Used in replacing the current model with a boolean table that represents connections. Overwrites the current model with a True or False (boolean) table that is read from a project save file (.json). This table is updated by restore_links() method to add Link instances to True cells and Nones to False cells.

find_index_in_header(self, name)[source]

Returns the row or column (row==column) of the header item with the given text (item name).

Returns Link instance stored on row and column.

class models.MinimalTableModel(parent=None)[source]

Bases: PySide2.QtCore.QAbstractTableModel

Table model for outlining simple tabular data.

parent

the parent widget, usually an instance of TreeViewForm

Type:QMainWindow
clear(self)[source]

Clear all data in model.

flags(self, index)[source]

Return index flags.

rowCount(self, parent=QModelIndex())[source]

Number of rows in the model.

columnCount(self, parent=QModelIndex())[source]

Number of columns in the model.

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

Get headers.

set_horizontal_header_labels(self, labels)[source]

Set horizontal header labels.

insert_horizontal_header_labels(self, section, labels)[source]

Insert horizontal header labels at the given section.

horizontal_header_labels(self)[source]
setHeaderData(self, section, orientation, value, role=Qt.EditRole)[source]

Sets the data for the given role and section in the header with the specified orientation to the value supplied.

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

Returns the data stored under the given role for the item referred to by the index.

Parameters:
  • index (QModelIndex) – Index of item
  • role (int) – Data role
Returns:

Item data for given role.

row_data(self, row, role=Qt.DisplayRole)[source]

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

Parameters:
  • row (int) – Item row
  • role (int) – Data role
Returns:

Row data for given role.

column_data(self, column, role=Qt.DisplayRole)[source]

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

Parameters:
  • column (int) – Item column
  • role (int) – Data role
Returns:

Column data for given role.

model_data(self, role=Qt.DisplayRole)[source]

Returns the data stored under the given role in the entire model.

Parameters:role (int) – Data role
Returns:Model data for given role.
setData(self, index, value, role=Qt.EditRole)[source]

Set data in model.

batch_set_data(self, indexes, data)[source]

Batch set data for indexes.

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

Inserts count rows into the model before the given row. Items in the new row will be children of the item represented by the parent model index.

Parameters:
  • row (int) – Row number where new rows are inserted
  • count (int) – Number of inserted rows
  • parent (QModelIndex) – Parent index
Returns:

True if rows were inserted successfully, False otherwise

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

Inserts count columns into the model before the given column. Items in the new column will be children of the item represented by the parent model index.

Parameters:
  • column (int) – Column number where new columns are inserted
  • count (int) – Number of inserted columns
  • parent (QModelIndex) – Parent index
Returns:

True if columns were inserted successfully, False otherwise

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

Removes count rows starting with the given row under parent.

Parameters:
  • row (int) – Row number where to start removing rows
  • count (int) – Number of removed rows
  • parent (QModelIndex) – Parent index
Returns:

True if rows were removed successfully, False otherwise

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

Removes count columns starting with the given column under parent.

Parameters:
  • column (int) – Column number where to start removing columns
  • count (int) – Number of removed columns
  • parent (QModelIndex) – Parent index
Returns:

True if columns were removed successfully, False otherwise

reset_model(self, main_data=None)[source]

Reset model.

class models.EmptyRowModel(parent=None)[source]

Bases: models.MinimalTableModel

A table model with a last empty row.

flags(self, index)[source]

Return default flags except if forcing defaults.

set_default_row(self, **kwargs)[source]

Set default row data.

clear(self)[source]
reset_model(self, data)[source]
_handle_data_changed(self, top_left, bottom_right, roles=None)[source]

Insert a new last empty row in case the previous one has been filled with any data other than the defaults.

_handle_rows_removed(self, parent, first, last)[source]

Insert a new empty row in case it’s been removed.

_handle_rows_inserted(self, parent, first, last)[source]

Handle rowsInserted signal.

set_rows_to_default(self, first, last)[source]

Set default data in newly inserted rows.

class models.HybridTableModel(parent=None)[source]

Bases: models.MinimalTableModel

A model that concatenates two models, one for existing items and another one for new items.

flags(self, index)[source]

Return flags for given index. Depending on the index’s row we will land on one of the two models.

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

Return data for given index and role. Depending on the index’s row we will land on one of the two models.

rowCount(self, parent=QModelIndex())[source]

Return the sum of rows in the two models.

batch_set_data(self, indexes, data)[source]

Batch set data for indexes. Distribute indexes and data among the two models and call batch_set_data on each of them.

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

Find the right sub-model (or the empty model) and call insertRows on it.

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

Find the right sub-models (or empty model) and call removeRows on them.

set_horizontal_header_labels(self, labels)[source]
reset_model(self, data)[source]

Reset model data.

_handle_new_item_model_rows_inserted(self, parent, first, last)[source]
class models.DatapackageResourcesModel(parent)[source]

Bases: models.MinimalTableModel

A model of datapackage resource data, used by SpineDatapackageWidget.

parent
Type:SpineDatapackageWidget
reset_model(self, resources)[source]
flags(self, index)[source]
class models.DatapackageFieldsModel(parent)[source]

Bases: models.MinimalTableModel

A model of datapackage field data, used by SpineDatapackageWidget.

parent
Type:SpineDatapackageWidget
reset_model(self, schema)[source]
class models.DatapackageForeignKeysModel(parent)[source]

Bases: models.EmptyRowModel

A model of datapackage foreign key data, used by SpineDatapackageWidget.

parent
Type:SpineDatapackageWidget
reset_model(self, foreign_keys)[source]
class models.TableModel(headers=None, data=None)[source]

Bases: PySide2.QtCore.QAbstractItemModel

Used by custom_qtableview.FrozenTableView

parent(self, child=None)[source]
index(self, row, column, parent=QModelIndex())[source]
set_data(self, data, headers)[source]
rowCount(self, parent=QModelIndex())[source]
columnCount(self, parent=QModelIndex())[source]
headerData(self, section, orientation, role)[source]
row(self, index)[source]
data(self, index, role)[source]