Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Commit ae2a02d

Browse files
committed
added model doc
1 parent e73a870 commit ae2a02d

File tree

3 files changed

+161
-1
lines changed

3 files changed

+161
-1
lines changed

doc/datastoremodel.dox

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*!
2+
@class QtDataSync::DataStoreModel
3+
4+
This model can be used to display the data from a AsyncDataStore as model. The model itself
5+
is typically readonly, as the data is updated automatically as the contents in the store changes.
6+
Check out the example below to see how to use it.
7+
8+
After creating the model, you can set the datatype it should display via setTypeId(). This will
9+
reset the model and let it load the data from it's store. The model is generic and can handle
10+
any object or gadget type. The properties of those objects/gadgets are translated to display roles,
11+
and each object/gadget is represented by one row in the model.
12+
13+
This makes the model easy to use with QML, as there you access data via roles, but requires additional
14+
code to display it via a QTreeView or similar. Here, another project of mine can help:
15+
[QObjectListModel](https://github.com/Skycoder42/QObjectListModel) Provides a class called `QObjectProxyModel`.
16+
This class can map roles to columns, making it possible to display a model like this one in widgets
17+
properly. The example below shows how to.
18+
19+
To "modify" the model, use one of the datasync stores and insert, updated or remove data. Once the
20+
change is successfully done in the engine, the model updates automatically. Sorting the model itself
21+
is not possible, but you can make use of a QSortFilterProxyModel to display the data sorted.
22+
23+
The model is readonly by default, but you can make exising items editable via DataStoreModel::editable.
24+
This does not allow inserting or removing items via the model, but allows you to change properties
25+
via the setData() method.
26+
27+
Items can be loaded from the model use loadObject(). This allows you to get the item at a specific index
28+
and pass it to other components, update it and other. This method loads a new instance from the store, and
29+
this is safe in any case. With the object() method this can be done faster, but is <b>not safe</b> for all
30+
types. Read the DataStoreModel::object documentation for details.
31+
32+
<h3>Example</h3>
33+
Assuming your data class looks like this:
34+
@code{.cpp}
35+
class Person
36+
{
37+
Q_GADGET
38+
39+
Q_PROPERTY(QString name MEMBER name)
40+
Q_PROPERTY(int age MEMBER age)
41+
42+
//...
43+
};
44+
@endcode
45+
46+
You can instanciate a model for this data as follows:
47+
@code{.cpp}
48+
auto model = new QtDataSync::DataStoreModel(this);
49+
model->setTypeId<Person>();
50+
@endcode
51+
52+
To properly use it with widgets, you can use [QObjectListModel's](https://github.com/Skycoder42/QObjectListModel) `QObjectProxyModel`.
53+
The code below creates a model with 2 columns, where column 0 shows the "name" role and
54+
column 1 shows the "age" role:
55+
@code{.cpp}
56+
auto proxy = new QObjectProxyModel({"name", "age"}, this);
57+
proxy->setSourceModel(model);
58+
proxy->addMapping(0, Qt::DisplayRole, "name");
59+
proxy->addMapping(1, Qt::DisplayRole, "age");
60+
@endcode
61+
62+
@sa AsyncDataStore, CachingDataStore, [QObjectListModel](https://github.com/Skycoder42/QObjectListModel)
63+
*/
64+
65+
/*!
66+
@property QtDataSync::DataStoreModel::typeId
67+
68+
@default{`QMetaType::UnknownType`}
69+
70+
The type id is essential for the model, and defines what data should be loaded.
71+
When changing the property, the model resets and the loads all data from it's store
72+
for the given type, and then reacts on changes for that type.
73+
74+
Setting it to an unknown or invalid will lead to errors. The type should be set before
75+
passing the model to a view or proxy model.
76+
77+
@accessors{
78+
@readAc{typeId()}
79+
@writeAc{setTypeId()}
80+
}
81+
82+
@sa DataStoreModel::storeLoaded, DataStoreModel::storeError
83+
*/
84+
85+
/*!
86+
@property QtDataSync::DataStoreModel::editable
87+
88+
@default{`false`}
89+
90+
By default, the model is not editable and servers as a normal read only model.
91+
By enabeling this property, the setData() method allows modification of items.
92+
These modifications will be automatically passed to the store and thus are
93+
permanent data changes.
94+
95+
@accessors{
96+
@readAc{isEditable()}
97+
@writeAc{setEditable()}
98+
@notifyAc{editableChanged()}
99+
}
100+
101+
@sa DataStoreModel::setData
102+
*/

doc/qtdatasync.dox

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ The following classes are "user classes". They are what you need to work with da
88
- CachingDataStore
99
- GenericTask (and Task, UpdateTask)
1010
- UserDataNetworkExchange (and UserInfo)
11+
- DataStoreModel
1112
- WsAuthenticator (for the default remote connector)
1213

1314
The other classes are "developer classes". They are interfaces, base classes and utilities you
1415
need if you want to extend datasync:
1516
- LocalStore
1617
- StateHolder
17-
- DataMerger
18+
- DataMerger2 (replacing DataMerger until the next major release)
1819
- Encryptor
1920
- RemoteConnector
2021
- Authenticator

src/datasync/datastoremodel.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,116 @@
99
namespace QtDataSync {
1010

1111
class DataStoreModelPrivate;
12+
//! A passive item model for a datasync data store
1213
class Q_DATASYNC_EXPORT DataStoreModel : public QAbstractListModel
1314
{
1415
Q_OBJECT
1516
friend class DataStoreModelPrivate;
1617

18+
//! Holds the type the model loads data for
1719
Q_PROPERTY(int typeId READ typeId WRITE setTypeId)
20+
//! Specifies whether the model items can be edited
1821
Q_PROPERTY(bool editable READ isEditable WRITE setEditable NOTIFY editableChanged)
1922

2023
public:
24+
//! Constructs a model for the default setup
2125
explicit DataStoreModel(QObject *parent = nullptr);
26+
//! Constructs a model for the given setup
2227
explicit DataStoreModel(const QString &setupName, QObject *parent = nullptr);
28+
//! Constructs a model on the given store
2329
explicit DataStoreModel(AsyncDataStore *store, QObject *parent = nullptr);
30+
//! Destructor
2431
~DataStoreModel();
2532

33+
//! Returns the data store the model works on
2634
AsyncDataStore *store() const;
2735

36+
//! @readAcFn{DataStoreModel::typeId}
2837
int typeId() const;
38+
//! @writeAcFn{DataStoreModel::typeId}
2939
template <typename T>
3040
inline bool setTypeId();
41+
//! @readAcFn{DataStoreModel::editable}
3142
bool isEditable() const;
3243

44+
//! @inherit{QAbstractListModel::headerData}
3345
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
46+
//! @inherit{QAbstractListModel::rowCount}
3447
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
3548

49+
//! Returns the index of the item with the given id
3650
QModelIndex idIndex(const QString &id) const;
51+
//! Returns the index of the item with the given id
3752
template <typename T>
3853
inline QModelIndex idIndex(const T &id) const;
3954

55+
//! Returns the key of the item at the given index
4056
QString key(const QModelIndex &index) const;
57+
//! Returns the key of the item at the given index
4158
template <typename T>
4259
inline T key(const QModelIndex &index) const;
4360

61+
//! @inherit{QAbstractListModel::data}
4462
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
63+
//! @inherit{QAbstractListModel::setData}
4564
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
4665

66+
/*!
67+
Returns the object at the given index
68+
69+
@param index The model index to return the object for
70+
@returns The data at the index, or an invalid variant
71+
72+
@warning When working with QObjects, the method is potentially dangerous, as the
73+
returned object is owend by the model, and can be deleted any time. It is fine
74+
to use the returned object in a local scope. Do not leave a local scope,
75+
or use QPointer to be able to react in case the object gets deleted. To get
76+
a copy that you own, use the loadObject() method.
77+
78+
@sa DataStoreModel::loadObject
79+
*/
4780
QVariant object(const QModelIndex &index) const;
81+
/*!
82+
Returns the object at the given index
83+
84+
@tparam The type of object to return. Must match DataStoreModel::typeId
85+
@param index The model index to return the object for
86+
@returns The data at the index, or an invalid variant
87+
88+
@warning When working with QObjects, the method is potentially dangerous, as the
89+
returned object is owend by the model, and can be deleted any time. It is fine
90+
to use the returned object in a local scope. Do not leave a local scope,
91+
or use QPointer to be able to react in case the object gets deleted. To get
92+
a copy that you own, use the loadObject() method.
93+
94+
@sa DataStoreModel::loadObject
95+
*/
4896
template <typename T>
4997
inline T object(const QModelIndex &index) const;
98+
//! Loads the object at the given index from the store via AsyncDataStore::load
5099
Task loadObject(const QModelIndex &index) const;
100+
//! Loads the object at the given index from the store via AsyncDataStore::load
51101
template <typename T>
52102
GenericTask<T> loadObject(const QModelIndex &index) const;
53103

104+
//! @inherit{QAbstractListModel::flags}
54105
Qt::ItemFlags flags(const QModelIndex &index) const override;
106+
//! @inherit{QAbstractListModel::roleNames}
55107
QHash<int, QByteArray> roleNames() const override;
56108

57109
public Q_SLOTS:
110+
//! @writeAcFn{DataStoreModel::typeId}
58111
bool setTypeId(int typeId);
112+
//! @writeAcFn{DataStoreModel::editable}
59113
void setEditable(bool editable);
60114

61115
Q_SIGNALS:
116+
//! Emitted when the model successfully loaded the current state from the store, and thus is "ready"
62117
void storeLoaded();
118+
//! Emitted when the store throws an exception
63119
void storeError(const QException &exception);
64120

121+
//! @notifyAcFn{DataStoreModel::editable}
65122
void editableChanged(bool editable);
66123

67124
private Q_SLOTS:

0 commit comments

Comments
 (0)