QtDataSync  4.2.0
A simple offline-first synchronisation framework, to synchronize data of Qt applications between devices
Public Slots | Signals | Public Member Functions | Properties | List of all members
QtDataSync::DataStoreModel Class Reference

A passive item model for a datasync data store. More...

#include <datastoremodel.h>

+ Inheritance diagram for QtDataSync::DataStoreModel:

Public Slots

void setTypeId (int typeId)
 WRITE accessor for DataStoreModel::typeId.
 
void setTypeId (int typeId, bool resetColumns)
 WRITE accessor for DataStoreModel::typeId.
 
void setEditable (bool editable)
 WRITE accessor for DataStoreModel::editable.
 
void reload ()
 Reloads all data in the model.
 

Signals

void storeError (const QException &exception, QPrivateSignal)
 Emitted when the underlying DataStore throws an exception.
 
void typeIdChanged (int typeId, QPrivateSignal)
 NOTIFY accessor for DataStoreModel::typeId.
 
void editableChanged (bool editable, QPrivateSignal)
 NOTIFY accessor for DataStoreModel::editable.
 

Public Member Functions

 DataStoreModel (QObject *parent=nullptr)
 Constructs a model for the default setup. More...
 
 DataStoreModel (const QString &setupName, QObject *parent=nullptr)
 Constructs a model for the given setup. More...
 
 DataStoreModel (DataStore *store, QObject *parent=nullptr)
 Constructs a model on the given store. More...
 
DataStorestore () const
 Returns the data store the model works on. More...
 
QString setupName () const
 Returns the name of the setup this class operates on. More...
 
int typeId () const
 READ accessor for DataStoreModel::typeId.
 
template<typename T >
void setTypeId (bool resetColumns=true)
 WRITE accessor for DataStoreModel::typeId.
 
bool isEditable () const
 READ accessor for DataStoreModel::editable.
 
QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
 Inherits QAbstractTableModel::headerData.
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 Inherits QAbstractTableModel::rowCount.
 
int columnCount (const QModelIndex &parent) const override
 Inherits QAbstractTableModel::columnCount.
 
bool canFetchMore (const QModelIndex &parent) const override
 Inherits QAbstractTableModel::canFetchMore.
 
void fetchMore (const QModelIndex &parent) override
 Inherits QAbstractTableModel::fetchMore.
 
QModelIndex index (int row, int column=0, const QModelIndex &parent=QModelIndex()) const override
 Inherits QAbstractTableModel::index.
 
Q_INVOKABLE QModelIndex idIndex (const QString &id) const
 Returns the index of the item with the given id.
 
template<typename T >
QModelIndex idIndex (const T &id) const
 Returns the index of the item with the given id.
 
Q_INVOKABLE QString key (const QModelIndex &index) const
 Returns the key of the item at the given index.
 
template<typename T >
key (const QModelIndex &index) const
 Returns the key of the item at the given index.
 
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const override
 Inherits QAbstractTableModel::data.
 
bool setData (const QModelIndex &index, const QVariant &value, int role) override
 Inherits QAbstractTableModel::setData.
 
Q_INVOKABLE QVariant object (const QModelIndex &index) const
 Returns the object at the given index. More...
 
template<typename T >
object (const QModelIndex &index) const
 Returns the object at the given index. More...
 
Q_INVOKABLE QVariant loadObject (const QModelIndex &index) const
 Loads the object at the given index from the store via DataStore::load. More...
 
template<typename T >
loadObject (const QModelIndex &index) const
 Loads the object at the given index from the store via DataStore::load. More...
 
Qt::ItemFlags flags (const QModelIndex &index) const override
 Inherits QAbstractTableModel::flags.
 
QHash< int, QByteArrayroleNames () const override
 Inherits QAbstractTableModel::roleNames.
 
int addColumn (const QString &text)
 Add a new column with the given title. More...
 
int addColumn (const QString &text, const char *propertyName)
 Add a new column and set the given property as its Qt::DisplayRole. More...
 
void addRole (int column, int role, const char *propertyName)
 Adds the given property as a new role to the given column. More...
 
void clearColumns ()
 Removes all customly added columns and roles.
 
- Public Member Functions inherited from QAbstractTableModel
virtual const QMetaObjectmetaObject () const const
 
virtual void * qt_metacast (const char *)
 
virtual int qt_metacall (QMetaObject::Call, int, void **)
 
 QAbstractTableModel (QObject *parent)
 
virtual QModelIndex sibling (int row, int column, const QModelIndex &idx) const const override
 
virtual bool dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
 
- Public Member Functions inherited from QAbstractItemModel
 QAbstractItemModel (QObject *parent)
 
bool hasIndex (int row, int column, const QModelIndex &parent) const const
 
virtual QModelIndex parent (const QModelIndex &index) const const=0
 
virtual bool hasChildren (const QModelIndex &parent) const const
 
virtual bool setHeaderData (int section, Qt::Orientation orientation, const QVariant &value, int role)
 
virtual QMap< int, QVariantitemData (const QModelIndex &index) const const
 
virtual bool setItemData (const QModelIndex &index, const QMap< int, QVariant > &roles)
 
virtual QStringList mimeTypes () const const
 
virtual QMimeDatamimeData (const QModelIndexList &indexes) const const
 
virtual bool canDropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const const
 
virtual Qt::DropActions supportedDropActions () const const
 
virtual Qt::DropActions supportedDragActions () const const
 
void setSupportedDragActions (Qt::DropActions actions)
 
virtual bool insertRows (int row, int count, const QModelIndex &parent)
 
virtual bool insertColumns (int column, int count, const QModelIndex &parent)
 
virtual bool removeRows (int row, int count, const QModelIndex &parent)
 
virtual bool removeColumns (int column, int count, const QModelIndex &parent)
 
virtual bool moveRows (const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
 
virtual bool moveColumns (const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent, int destinationChild)
 
bool insertRow (int row, const QModelIndex &parent)
 
bool insertColumn (int column, const QModelIndex &parent)
 
bool removeRow (int row, const QModelIndex &parent)
 
bool removeColumn (int column, const QModelIndex &parent)
 
bool moveRow (const QModelIndex &sourceParent, int sourceRow, const QModelIndex &destinationParent, int destinationChild)
 
bool moveColumn (const QModelIndex &sourceParent, int sourceColumn, const QModelIndex &destinationParent, int destinationChild)
 
virtual void sort (int column, Qt::SortOrder order)
 
virtual QModelIndex buddy (const QModelIndex &index) const const
 
virtual QModelIndexList match (const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const const
 
virtual QSize span (const QModelIndex &index) const const
 
bool checkIndex (const QModelIndex &index, QAbstractItemModel::CheckIndexOptions options) const const
 
void dataChanged (const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles)
 
void headerDataChanged (Qt::Orientation orientation, int first, int last)
 
void layoutChanged (const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
 
void layoutAboutToBeChanged (const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
 
void rowsAboutToBeInserted (const QModelIndex &parent, int start, int end)
 
void rowsInserted (const QModelIndex &parent, int first, int last)
 
void rowsAboutToBeRemoved (const QModelIndex &parent, int first, int last)
 
void rowsRemoved (const QModelIndex &parent, int first, int last)
 
void columnsAboutToBeInserted (const QModelIndex &parent, int first, int last)
 
void columnsInserted (const QModelIndex &parent, int first, int last)
 
void columnsAboutToBeRemoved (const QModelIndex &parent, int first, int last)
 
void columnsRemoved (const QModelIndex &parent, int first, int last)
 
void modelAboutToBeReset ()
 
void modelReset ()
 
void rowsAboutToBeMoved (const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow)
 
void rowsMoved (const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row)
 
void columnsAboutToBeMoved (const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn)
 
void columnsMoved (const QModelIndex &parent, int start, int end, const QModelIndex &destination, int column)
 
virtual bool submit ()
 
virtual void revert ()
 
- Public Member Functions inherited from QObject
 QObject (QObject *parent)
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
QString objectName () const const
 
void setObjectName (const QString &name)
 
bool isWidgetType () const const
 
bool isWindowType () const const
 
bool signalsBlocked () const const
 
bool blockSignals (bool block)
 
QThreadthread () const const
 
void moveToThread (QThread *targetThread)
 
int startTimer (int interval, Qt::TimerType timerType)
 
int startTimer (std::chrono::milliseconds time, Qt::TimerType timerType)
 
void killTimer (int id)
 
findChild (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegExp &regExp, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
 
const QObjectList & children () const const
 
void setParent (QObject *parent)
 
void installEventFilter (QObject *filterObj)
 
void removeEventFilter (QObject *obj)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
 
bool disconnect (const QObject *receiver, const char *method) const const
 
void dumpObjectTree ()
 
void dumpObjectInfo ()
 
void dumpObjectTree () const const
 
void dumpObjectInfo () const const
 
bool setProperty (const char *name, const QVariant &value)
 
QVariant property (const char *name) const const
 
QList< QByteArraydynamicPropertyNames () const const
 
void destroyed (QObject *obj)
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const const
 
bool inherits (const char *className) const const
 
void deleteLater ()
 

Properties

int typeId
 Holds the type the model loads data for. More...
 
bool editable
 Specifies whether the model items can be edited. More...
 
- Properties inherited from QObject
 objectName
 

Additional Inherited Members

- Static Public Member Functions inherited from QAbstractTableModel
QString tr (const char *s, const char *c, int n)
 
QString trUtf8 (const char *s, const char *c, int n)
 
- Static Public Member Functions inherited from QAbstractItemModel
QString tr (const char *s, const char *c, int n)
 
QString trUtf8 (const char *s, const char *c, int n)
 
- Static Public Member Functions inherited from QObject
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
bool disconnect (const QMetaObject::Connection &connection)
 
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 
- Public Attributes inherited from QAbstractItemModel
typedef CheckIndexOptions
 
- Protected Member Functions inherited from QAbstractItemModel
void resetInternalData ()
 
QModelIndex createIndex (int row, int column, void *ptr) const const
 
QModelIndex createIndex (int row, int column, quintptr id) const const
 
void beginInsertRows (const QModelIndex &parent, int first, int last)
 
void endInsertRows ()
 
void beginRemoveRows (const QModelIndex &parent, int first, int last)
 
void endRemoveRows ()
 
bool beginMoveRows (const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild)
 
void endMoveRows ()
 
void beginInsertColumns (const QModelIndex &parent, int first, int last)
 
void endInsertColumns ()
 
void beginRemoveColumns (const QModelIndex &parent, int first, int last)
 
void endRemoveColumns ()
 
bool beginMoveColumns (const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild)
 
void endMoveColumns ()
 
void reset ()
 
void beginResetModel ()
 
void endResetModel ()
 
void changePersistentIndex (const QModelIndex &from, const QModelIndex &to)
 
void changePersistentIndexList (const QModelIndexList &from, const QModelIndexList &to)
 
QModelIndexList persistentIndexList () const const
 
void setRoleNames (const QHash< int, QByteArray > &roleNames)
 
- Protected Member Functions inherited from QObject
QObjectsender () const const
 
int senderSignalIndex () const const
 
int receivers (const char *signal) const const
 
bool isSignalConnected (const QMetaMethod &signal) const const
 
virtual void timerEvent (QTimerEvent *event)
 
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 

Detailed Description

A passive item model for a datasync data store.

This model can be used to display the data from a DataStore as model. The model itself is typically readonly, as the data is updated automatically as the contents in the store changes. Check out the example below to see how to use it.

After creating the model, you can set the datatype it should display via setTypeId(). This will reset the model and let it load the data from it's store. The model is generic and can handle any object or gadget type. The properties of those objects/gadgets are translated to display roles, and each object/gadget is represented by one row in the model.

This makes the model easy to use with QML, as there you access data via roles, but requires additional code to display it via a QTreeView or similar. Here, another project of mine can help: QObjectListModel Provides a class called QObjectProxyModel. This class can map roles to columns, making it possible to display a model like this one in widgets properly. The example below shows how to.

To "modify" the model, use one of the datasync stores and insert, updated or remove data. Once the change is successfully done in the engine, the model updates automatically. Sorting the model itself is not possible, but you can make use of a QSortFilterProxyModel to display the data sorted.

The model is readonly by default, but you can make exising items editable via DataStoreModel::editable. This does not allow inserting or removing items via the model, but allows you to change properties via the setData() method.

Items can be loaded from the model use loadObject(). This allows you to get the item at a specific index and pass it to other components, update it and other. This method loads a new instance from the store, and this is safe in any case. With the object() method this can be done faster, but is not** safe for all types. Read the DataStoreModel::object documentation for details.

Example

Assuming your data class looks like this:

class Person
{
Q_GADGET
Q_PROPERTY(QString name MEMBER name)
Q_PROPERTY(int age MEMBER age)
//...
};

You can instanciate a model for this data as follows:

auto model = new QtDataSync::DataStoreModel(this);
model->setTypeId<Person>();

One additional feature of the model is, that it can also serve as model for table/tree views by delegating properties to multiple columns. This way you can use the model from QML by using the property names, and from widgets by delegating the properties to extra columns. For the above sample, the following code will result in a model with 2 columns, presenting the name and age properties as the Qt::DisplayRole of each column:

// setup first
auto model = new QtDataSync::DataStoreModel(this);
model->setTypeId<Person>();
// add the column config:
auto col0 = model->addColumn("Name");
model->addRole(col0, Qt::DisplayRole, "name");
// or as shortcut for the above:
model->addColumn("Age", "age");
See also
DataStore

Definition at line 13 of file datastoremodel.h.

Constructor & Destructor Documentation

◆ DataStoreModel() [1/3]

QtDataSync::DataStoreModel::DataStoreModel ( QObject parent = nullptr)
explicit

Constructs a model for the default setup.

Parameters
parentThe parent object
Exceptions
SetupDoesNotExistExceptionThrown if the default setup was not created yet

◆ DataStoreModel() [2/3]

QtDataSync::DataStoreModel::DataStoreModel ( const QString setupName,
QObject parent = nullptr 
)
explicit

Constructs a model for the given setup.

Parameters
setupNameThe name of the setup to connect to
parentThe parent object
Exceptions
SetupDoesNotExistExceptionThrown if the given setup was not created yet

◆ DataStoreModel() [3/3]

QtDataSync::DataStoreModel::DataStoreModel ( DataStore store,
QObject parent = nullptr 
)
explicit

Constructs a model on the given store.

Parameters
storeThe store to be used by the model
parentThe parent object
Attention
The model does not take ownership of the passed store. Thus, the store must stay valid as long as the model exists.

Member Function Documentation

◆ addColumn() [1/2]

QVariant QtDataSync::DataStoreModel::addColumn ( const QString text)

Add a new column with the given title.

Parameters
textThe display name of the column header
Returns
The index of the newly created column

The method add a column to the model that can be used to present propeties from the underlying data as specific roles.

Attention
If no custom columns have been added yet, the model with have a single column with the typeId class name as title. By calling this method the first time, this default column will be replaced by whatever column you specify. All subsequent columns are simply added as new columns.
See also
DataStoreModel, DataStoreModel::typeId, DataStore::addRole, DataStore::clearColumns

◆ addColumn() [2/2]

QVariant QtDataSync::DataStoreModel::addColumn ( const QString text,
const char *  propertyName 
)

Add a new column and set the given property as its Qt::DisplayRole.

Parameters
textThe display name of the column header
propertyNameThe name of the property to presented as the display role of that column
Returns
The index of the newly created column

This method is basically a shortcut for:

auto column = model->addColumn(text);
model->addRole(column, Qt::DisplayRole, propertyName
See also
DataStoreModel, DataStoreModel::typeId, DataStore::addRole, DataStore::clearColumns

◆ addRole()

QVariant QtDataSync::DataStoreModel::addRole ( int  column,
int  role,
const char *  propertyName 
)

Adds the given property as a new role to the given column.

Parameters
columnThe existing column to add the role to
roleThe role that should be added to the column
propertyNameThe name of the property to presented as the give role of that column

This method adds a new role to an existing column. The new role will simply the the value of the given property of the object in each row. It's basically a delegate to the properties role.

See also
DataStoreModel, DataStoreModel::typeId, DataStore::addColumn, DataStore::clearColumns

◆ loadObject() [1/2]

QVariant QtDataSync::DataStoreModel::loadObject ( const QModelIndex index) const

Loads the object at the given index from the store via DataStore::load.

Parameters
indexThe model index to return the object for
Returns
The data at the index, or a default constructed value
Exceptions
NoDataExceptionIn case no dataset for the given type and key was found
LocalStoreExceptionIn case of an internal error

The method uses the store() to load a new instance of the data using the very key of the dataset the given index identifies. The returned data is owned by the caller. For more details, check DataStore::load.

See also
DataStoreModel::object, DataStore::load

◆ loadObject() [2/2]

template<typename T >
T QtDataSync::DataStoreModel::loadObject ( const QModelIndex index) const

Loads the object at the given index from the store via DataStore::load.

Template Parameters
TThe type of object to return. Must match DataStoreModel::typeId
Parameters
indexThe model index to return the object for
Returns
The data at the index, or a default constructed value
Exceptions
NoDataExceptionIn case no dataset for the given type and key was found
LocalStoreExceptionIn case of an internal error

The method uses the store() to load a new instance of the data using the very key of the dataset the given index identifies. The returned data is owned by the caller. For more details, check DataStore::load.

See also
DataStoreModel::object, DataStore::load

Definition at line 165 of file datastoremodel.h.

◆ object() [1/2]

QVariant QtDataSync::DataStoreModel::object ( const QModelIndex index) const

Returns the object at the given index.

Parameters
indexThe model index to return the object for
Returns
The data at the index, or a default constructed value
Warning
When working with QObjects, the method is potentially dangerous, as the returned object is owend by the model, and can be deleted any time. It is fine to use the returned object in a local scope. Do not leave a local scope, or use QPointer to be able to react in case the object gets deleted. To get a copy that you own, use the loadObject() method.
See also
DataStoreModel::loadObject, DataStore::data

◆ object() [2/2]

template<typename T >
T QtDataSync::DataStoreModel::object ( const QModelIndex index) const
inline

Returns the object at the given index.

Template Parameters
TThe type of object to return. Must match DataStoreModel::typeId
Parameters
indexThe model index to return the object for
Returns
The data at the index, or a default constructed value
Warning
When working with QObjects, the method is potentially dangerous, as the returned object is owend by the model, and can be deleted any time. It is fine to use the returned object in a local scope. Do not leave a local scope, or use QPointer to be able to react in case the object gets deleted. To get a copy that you own, use the loadObject() method.
See also
DataStoreModel::loadObject, DataStore::data

Definition at line 158 of file datastoremodel.h.

◆ setupName()

QString QtDataSync::DataStoreModel::setupName ( ) const

Returns the name of the setup this class operates on.

Returns
The name of the setup
See also
QtDataSync::Setup

◆ store()

QtDataSync::DataStoreModel::store ( ) const

Returns the data store the model works on.

Returns
A reference to the internally used store
Note
Most models do own their own datastore (only not the case if the store was passed via the constructor). The most returned stores are thus deleted as soon as the model gets destroyed.

Property Documentation

◆ editable

QtDataSync::DataStoreModel::editable
readwrite

Specifies whether the model items can be edited.

Default: false

By default, the model is not editable and servers as a normal read only model. By enabeling this property, the setData() method allows modification of items. These modifications will be automatically passed to the store and thus are permanent data changes.

Accessors
READisEditable()
WRITEsetEditable()
NOTIFYeditableChanged()
See also
DataStoreModel::setData

Definition at line 21 of file datastoremodel.h.

◆ typeId

QtDataSync::DataStoreModel::typeId
readwrite

Holds the type the model loads data for.

Default: QMetaType::UnknownType

The type id is essential for the model, and defines what data should be loaded. When changing the property, the model resets and the loads all data from it's store for the given type, and then reacts on changes for that type.

Setting it to an unknown or invalid type will lead to errors. The type should be set before passing the model to a view or proxy model, as these need the models roles to be defined to work properly.

Accessors
READtypeId()
WRITEsetTypeId()
NOTIFYtypeIdChanged()
See also
DataStoreModel::storeError, DataStoreModel::reload, DataStoreModel::addColumn, DataStoreModel::addRole

Definition at line 19 of file datastoremodel.h.


The documentation for this class was generated from the following files: