MeVisLab Scripting Reference
MLAbstractItemModelWrapper Class Reference
Inheritance diagram for MLAbstractItemModelWrapper:
MLPythonItemModelWrapper MLStandardItemModelWrapper

Public Slots

QObject * qmlModel () const
 
Item traversal interface


bool isFlat () const
 
bool hasChildren (const ModelIndexWrapper *parent)
 
int getChildCount (const ModelIndexWrapper *parent)
 
ModelIndexWrappergetChild (const ModelIndexWrapper *parent, unsigned int index)
 
ModelIndexWrappergetParent (const ModelIndexWrapper *child)
 
ModelIndexWrapperfindFirst (const QVariant &attribute, const QVariant &value)
 
ModelIndexWrappersearchItem (const QVariant &attribute, const QVariantList &values)
 
Attribute discovery


int getAttributeCount () const
 
QStringList getAttributeNames () const
 
QString getAttributeName (int index) const
 
QVariant getAttributeDefault (int index) const
 
int getAttributeIndex (const QString &name)
 
Data access


QVariant getData (const ModelIndexWrapper *item, int attributeIndex)
 
bool setData (const ModelIndexWrapper *item, int attributeIndex, const QVariant &data)
 
bool bulkSetData (const QList< ModelIndexWrapper * > &items, int attributeIndex, const QVariant &data)
 
Data access with attribute name instead of attribute index


QVariant getData (const ModelIndexWrapper *item, const QString &attributeName)
 
bool setData (const ModelIndexWrapper *item, const QString &attributeName, const QVariant &data)
 
bool bulkSetData (const QList< ModelIndexWrapper * > &items, const QString &attributeName, const QVariant &data)
 

Signals

Signals


void itemChanged (ModelIndexWrapper *item, bool after)
 
void itemsInserted (ModelIndexWrapper *parent, unsigned int childIndex, unsigned int itemsInserted, bool after)
 
void itemsRemoved (ModelIndexWrapper *parent, unsigned int childIndex, unsigned int itemsRemoved, bool after)
 
void dataChanged (const QList< ModelIndexWrapper * > &items, const QList< int > &attributeIndices)
 

Detailed Description

This class wraps the class ml::AbstractItemModel.

Member Function Documentation

◆ bulkSetData

bool MLAbstractItemModelWrapper::bulkSetData ( const QList< ModelIndexWrapper * > &  items,
int  attributeIndex,
const QVariant &  data 
)
slot

Update several items at once. The data can be a single value that is set on all items or a list of values (which must have the same length as the items list. This will return false if not all changes were accepted.

To react on such changes if you created the model yourself with MLStandardItemModelWrapper, connect to the signal MLAbstractItemModelWrapper::dataChanged

◆ dataChanged

void MLAbstractItemModelWrapper::dataChanged ( const QList< ModelIndexWrapper * > &  items,
const QList< int > &  attributeIndices 
)
signal

Attributes have changed on the given items.

Example:

model.dataChanged.connect(myDataChangedFunction)
...
def myDataChangedFunction(items, attributeIndices):
# find out if any attributes have changed that interest us:
nameChanged = False
for ai in attributeIndices:
if model.getAttributeName(ai) == "name":
nameChanged = True
# handle changes
for item in items:
name = model.getData(item, "name")
...
QVariant getData(const ModelIndexWrapper *item, int attributeIndex)
QString getAttributeName(int index) const
Get name of n-th attribute.

◆ findFirst

ModelIndexWrapper* MLAbstractItemModelWrapper::findFirst ( const QVariant &  attribute,
const QVariant &  value 
)
slot

Recursively find the first item in the model where the given attribute (given as index or name) has the given value.

The search is performed depth-first.

◆ getAttributeCount

int MLAbstractItemModelWrapper::getAttributeCount ( ) const
slot

Get number of defined attributes for this model.

◆ getAttributeDefault

QVariant MLAbstractItemModelWrapper::getAttributeDefault ( int  index) const
slot

Return default value of n-th attribute.

This is used to conserve space during serialization of items. This method returns an None by default.

◆ getAttributeIndex

int MLAbstractItemModelWrapper::getAttributeIndex ( const QString &  name)
slot

Get index of attribute with the given name.

Returns -1 if the attribute name doesn't exist in the model.

◆ getAttributeName

QString MLAbstractItemModelWrapper::getAttributeName ( int  index) const
slot

Get name of n-th attribute.

◆ getAttributeNames

QStringList MLAbstractItemModelWrapper::getAttributeNames ( ) const
slot

Get names of all attributes.

◆ getChild

ModelIndexWrapper* MLAbstractItemModelWrapper::getChild ( const ModelIndexWrapper parent,
unsigned int  index 
)
slot

Get n-th child of an item (or n-th top-level item if parent index is None). This will return None for indices out of range, but also see the description of getChildCount for important details.

Example for fully recursive traversal of a model in Python:

def getNames(model, parent):
names = []
for i in range(model.getChildCount(parent)):
item = model.getChild(parent, i)
names.append(model.getData(item, "name"))
names = names + getNames(model, item)
return names
// starting with root item:
print getNames(aModel, None)
ModelIndexWrapper * getChild(const ModelIndexWrapper *parent, unsigned int index)
int getChildCount(const ModelIndexWrapper *parent)

◆ getChildCount

int MLAbstractItemModelWrapper::getChildCount ( const ModelIndexWrapper parent)
slot

Get number of children of the given item, an invalid index (i.e. None in Python) means get the number of top-level items. This should be called before accessing any children, because this is the signal for dynamic models to actually provide the children.

◆ getData

QVariant MLAbstractItemModelWrapper::getData ( const ModelIndexWrapper item,
int  attributeIndex 
)
slot

Get attribute value of a certain item. This will return the default value if the attribute wasn't specified for the requested item and None if the attribute isn't specified in this model at all.

Note that you can't specify default values when creating the model yourself with the MLStandardItemModelWrapper.

◆ getParent

ModelIndexWrapper* MLAbstractItemModelWrapper::getParent ( const ModelIndexWrapper child)
slot

Get parent of an item. Will return None for top-level items.

◆ hasChildren

bool MLAbstractItemModelWrapper::hasChildren ( const ModelIndexWrapper parent)
slot

This method tells us if an item has children - at least in principle. This is used to display the item as having children, even if it might turn out that is has actually none.

You should prefer to call hasChildren instead of getChildCount if you only need to know if there are children, since dynamic models will delay providing the actual children until the number of children is requested.

◆ isFlat

bool MLAbstractItemModelWrapper::isFlat ( ) const
slot

This method tells you if you can expect items in this model to have no sub-items.

◆ itemChanged

void MLAbstractItemModelWrapper::itemChanged ( ModelIndexWrapper item,
bool  after 
)
signal

The children of an item will change completely/have completely changed.

If you are interested in such changes - most likely if you didn't create the model yourself and know that it can change - connect to this signal like this:

model.itemChanged.connect(myItemChangedFunction)
...
def myItemChangedFunction(item, after):
if after:
# after the item has changed
...
else:
# directly before the item will change
...

◆ itemsInserted

void MLAbstractItemModelWrapper::itemsInserted ( ModelIndexWrapper parent,
unsigned int  childIndex,
unsigned int  itemsInserted,
bool  after 
)
signal

An item gets/got additional children.

Example:

model.itemsInserted.connect(myItemsInsertedFunction)
...
def myItemsInsertedFunction(parent, childIndex, n, after):
if after:
# after children have been added
...
else:
# directly before children will be added
...

◆ itemsRemoved

void MLAbstractItemModelWrapper::itemsRemoved ( ModelIndexWrapper parent,
unsigned int  childIndex,
unsigned int  itemsRemoved,
bool  after 
)
signal

An item loses/lost additional children.

Example:

model.itemsRemoved.connect(myItemsRemovedFunction)
...
def myItemsRemovedFunction(parent, childIndex, n, after):
if after:
# after children have been removed
...
else:
# directly before children will be removed
...

◆ qmlModel

QObject* MLAbstractItemModelWrapper::qmlModel ( ) const
slot

Return a model for use in QML/the QuickView control.

Everything maps to the first column, the attributes directly map to roles.

Also see the Qt documentation on using QAbstractItemModel with QtQuick views

Note
Ownership is transferred to the caller. Each call returns a new object.

◆ searchItem

ModelIndexWrapper* MLAbstractItemModelWrapper::searchItem ( const QVariant &  attribute,
const QVariantList &  values 
)
slot

Almost as above, but the values give a hierarchical path to the item, i.e.

the first value is matched on a top-level item, then the search is continued from that item down with a value list reduced by the first element.

◆ setData

bool MLAbstractItemModelWrapper::setData ( const ModelIndexWrapper item,
int  attributeIndex,
const QVariant &  data 
)
slot

Update a single attribute on a single item. This will return false if the change wasn't accepted by the model.

Note: If you want to set a icon file path as attribute, you need to pass a dict with the keys "type" set to "FilePath", and "path" set to the path of the icon, like

{ "type": "FilePath", "path": myIconPath }

This is required because for some use cases the file path needs to be transformed before the icon can be displayed.