treemodel¶
This module provides a generic interface for tree models
It centers around the TreeModel, which is used for all kinds of trees. The tree gets customized by the TreeItems. Each TreeItem holds a specific ItemData subclass. The ItemData is responsible for delivering the data. Make sure that all TreeItems in one hierarchy have the same ItemData subclasses or at least the same column_count. If not, make sure the data method can handle columns outside their column count. If you want to create a tree, create the needed itemdata classes, create a root tree item that is parent for all top-level items. The root item does not have to provide data, so the data might be None. It is advides to use ListItemData because the data in the list will be used for the headers. Then create the tree items with their appropriate data instances. Finally create a tree model instance with the root tree item.
- class jukeboxcore.gui.treemodel.ItemData[source]¶
Bases: object
An abstract class that holds data and is used as an interface for TreeItems
When subclassing implement ItemData.data() and ItemData.column_count(). It is advised to reimplement ItemData.internal_data() too. For editable models, check ItemData.set_data().
- data(column, role)[source]¶
Return the data for the specified column and role
The column addresses one attribute of the data. When used in a root item, the data should return the horizontal header data. When returning None, the section Number is used (starting at 1) by the treemodel. So if you want an empty header, return an empty string!
Parameters: - column (int) – the data column
- role (QtCore.Qt.ItemDataRole) – the data role
Returns: data depending on the role
Return type: Raises: None
- set_data(column, value, role)[source]¶
Set the data for the given column to value
The default implementation returns False
Parameters: - column (int) – the column to set
- value – the value to set
- role (QtCore.Qt.ItemDataRole) – the role, usually EditRole
Returns: True, if editing was successfull
Return type: Raises: None
- column_count()[source]¶
Return the number of columns that can be queried for data
Returns: the number of columns Return type: int Raises: None
- class jukeboxcore.gui.treemodel.ListItemData(liste, editable=False)[source]¶
Bases: jukeboxcore.gui.treemodel.ItemData
Item data for generic lists
Initialize it with a list of objects. Each element corresponds to a column. For DisplayRole the objects are converted to strings with str().
Initialize a new StringItemData with the given list
Parameters: - list (list of objects) – a list of objects, one for each column
- editable (bool) – If True, the list is editable
Raises: None
- data(column, role)[source]¶
Return the data for the specified column and role
For DisplayRole the element in the list will be converted to a sting and returned.
Parameters: - column (int) – the data column
- role (QtCore.Qt.ItemDataRole) – the data role
Returns: data depending on the role, or None if the column is out of range
Return type: depending on the role or None
Raises: None
- set_data(column, value, role)[source]¶
Set the data for the given column to value
The default implementation returns False
Parameters: - column (int) – the column to set
- value – the value to set
- role (QtCore.Qt.ItemDataRole) – the role, usually EditRole
Returns: True, if editing was successfull
Return type: Raises: None
- class jukeboxcore.gui.treemodel.TreeItem(data, parent=None)[source]¶
Bases: object
General TreeItem
You can represent a tree structure with these tree items. Each item should contain some data that it can give to the model. Note that each tree always has one root item. Even if you have multiple top level items, they are all grouped under one root. The data for the root item can be None but it is advised to use a ListItemData so you can provide horizontal headers.
TreeItems should always belong to only one model. Once a new TreeModel gets initialized all TreeItems will share the same model. When you add a new Item or delete one, the model gets automatically updated. You do not need to call TreeModel insertRow or removeRow. Just use add_child, remove_child or create a new TreeItem and provide a parent item to the constructor.
Initialize a new TreeItem that holds some data and might be parented under parent
The child count will be zero. Will automatically set the parent and update the model if the parent is not None.
Parameters: - data (ItemData) – the data item. if the tree item is the root, the data will be used for horizontal headers! It is recommended to use ListItemData in that case.
- parent (TreeItem) – the parent treeitem
Raises: None
- get_model()[source]¶
Return the model the item belongs to
Returns: the model the item belongs to or None if it belongs to none Return type: TreeModel | None Raises: None
- set_model(model)[source]¶
Set the model the item belongs to
A TreeItem can only belong to one model.
Parameters: model (Treemodel) – the model the item belongs to Returns: None Return type: None Raises: None
- add_child(child)[source]¶
Add child to children of this TreeItem
Parameters: child (TreeItem) – the child TreeItem Returns: None Return type: None Raises: None
- remove_child(child)[source]¶
Remove the child from this TreeItem
Parameters: child (TreeItem) – the child TreeItem Returns: None Return type: None Raises: ValueError
- child(row)[source]¶
Return the child at the specified row
Parameters: row (int) – the row number Returns: the child Return type: TreeItem Raises: IndexError
- row()[source]¶
Return the index of this tree item in the parent rows
Returns: the row of this TreeItem in the parent Return type: int Raises: None
- column_count()[source]¶
Return the number of columns that the children have
If there are no children, return the column count of its own data.
Returns: the column count of the children data Return type: int Raises: None
- data(column, role)[source]¶
Return the data for the column and role
Parameters: - column (int) – the data column
- role (QtCore.Qt.ItemDataRole) – the data role
Returns: data depending on the role
Return type: Raises: None
- set_data(column, value, role)[source]¶
Set the data of column to value
Parameters: - column (int) – the column to set
- value – the value to set
- role (QtCore.Qt.ItemDataRole) – the role, usually EditRole
Returns: True, if data successfully changed
Return type: Raises: None
- parent()[source]¶
Return the parent tree item
Returns: the parent or None if there is no parent Return type: TreeItem Raises: None
- set_parent(parent)[source]¶
Set the parent of the treeitem
Parameters: parent (TreeItem | None) – parent treeitem Returns: None Return type: None Raises: None
- itemdata()[source]¶
Return the internal ItemData
Returns: the internal ItemData Return type: ItemData Raises: None
- class jukeboxcore.gui.treemodel.TreeModel(root, parent=None)[source]¶
Bases: PySide.QtCore.QAbstractItemModel
A tree model that uses the TreeItem to represent a general tree.
The model uses TreeItem instances in an hierarchy to build a tree. Each tree item represents a row. The tree items can hold arbitrary ItemData instances.
The model will get automatically updated, when the hierarchy of the tree items changes. You rarely have to use model methods for that. Just use methods of the tree items.
All models need at least a root. The root is responsible for the headers. So the ItemData of the root should have a columns for each header and return a string for them when queried with QtCore.Qt.DisplayRole. Only horizontal headers are supported at the moment. Vertical headers get numbers.
Initialize a new tree model with the given root treeitem
Parameters: - root (TreeItem) – the root tree item. The root tree item is responsible for the headers. A ListItemData with the headers is suitable as data for the item.
- parent (QtCore.QObject) – the parent for the model
Raises: None
- index(row, column, parent=None)[source]¶
Return the index of the item in the model specified by the given row, column and parent index.
Parameters: Returns: the index of the item
Return type: QtCore.QModelIndex
Raises: None
- parent(index)[source]¶
Return the parent of the model item with the given index. If the item has no parent, return an invalid QModelIndex.
Parameters: index (QtCore.QModelIndex) – the index that you want to know the parent of Returns: parent index Return type: QtCore.QModelIndex Raises: None
- rowCount(parent)[source]¶
Return the number of rows under the given parent. When the parent is valid return rowCount the number of children of parent.
Parameters: parent (QtCore.QModelIndex:) – the parent index Returns: the row count Return type: int Raises: None
- columnCount(parent)[source]¶
Return the number of columns for the children of the given parent.
Parameters: parent (QtCore.QModelIndex:) – the parent index Returns: the column count Return type: int Raises: None
- data(index, role=PySide.QtCore.Qt.ItemDataRole.DisplayRole)[source]¶
Return the data stored under the given role for the item referred to by the index.
Parameters: - index (QtCore.QModelIndex) – the index
- role (QtCore.Qt.ItemDataRole) – the data role
Returns: some data depending on the role
Raises: None
- setData(index, value, role=PySide.QtCore.Qt.ItemDataRole.EditRole)[source]¶
Set the data of the given index to value
Parameters: - index (QtCore.QModelIndex) – the index to set
- value – the value to set
- role (QtCore.Qt.ItemDataRole) – the role, usually edit role
Returns: True, if successfull, False if unsuccessfull
Return type: Raises: None
- headerData(section, orientation, role)[source]¶
Return the header data
Will call TreeItem.data() of the root TreeItem with the given section (column) and role for horizontal orientations.
Vertical orientations are numbered.
Parameters: - section (int) – the section in the header view
- orientation (QtCore.Qt.Vertical | QtCore.Qt.Horizontal) – vertical or horizontal orientation
- role (QtCore.Qt.ItemDataRole) – the data role.
Returns: data for the header
Raises: None
- insertRow(row, item, parent)[source]¶
Insert a single item before the given row in the child items of the parent specified.
Parameters: Returns: Returns true if the row is inserted; otherwise returns false.
Return type: bool
Raises: None
- removeRow(row, parent)[source]¶
Remove row from parent
Parameters: - row (int) – the row index
- parent (QtCore.QModelIndex) – the parent index
Returns: True if row is inserted; otherwise returns false.
Return type: bool
Raises: None
- flags(index)[source]¶
Return the flags for the given index
This will call TreeItem.flags() for valid ones.
Parameters: index (QtCore.QModelIndex) – the index to query Returns: None Return type: None Raises: None
- index_of_item(item)[source]¶
Get the index for the given TreeItem
Parameters: item (TreeItem) – the treeitem to query Returns: the index of the item Return type: QtCore.QModelIndex Raises: ValueError
- staticMetaObject = <PySide.QtCore.QMetaObject object at 0x0000000003D1C488>¶