models
¶
Classes for handling models in PySide2’s model/view framework. Note: These are Spine Toolbox internal data models.
authors: |
|
---|---|
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.
-
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
-
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
-
-
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
-
-
class
models.
ConnectionModel
(toolbox=None)[source]¶ Bases:
PySide2.QtCore.QAbstractTableModel
Table model for storing connections between 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
-
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
-
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)
-
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.
-
-
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
-
insert_horizontal_header_labels
(self, section, labels)[source]¶ Insert horizontal header labels at the given section.
-
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.
-
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
-
-
class
models.
EmptyRowModel
(parent=None)[source]¶ Bases:
models.MinimalTableModel
A table model with a last empty row.
-
_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.
-
-
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.
-
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.
-
-
class
models.
DatapackageResourcesModel
(parent)[source]¶ Bases:
models.MinimalTableModel
A model of datapackage resource data, used by SpineDatapackageWidget.
-
parent
¶ Type: SpineDatapackageWidget
-
-
class
models.
DatapackageFieldsModel
(parent)[source]¶ Bases:
models.MinimalTableModel
A model of datapackage field data, used by SpineDatapackageWidget.
-
parent
¶ Type: SpineDatapackageWidget
-
-
class
models.
DatapackageForeignKeysModel
(parent)[source]¶ Bases:
models.EmptyRowModel
A model of datapackage foreign key data, used by SpineDatapackageWidget.
-
parent
¶ Type: SpineDatapackageWidget
-