|
9 | 9 | namespace QtDataSync { |
10 | 10 |
|
11 | 11 | class DataStoreModelPrivate; |
| 12 | +//! A passive item model for a datasync data store |
12 | 13 | class Q_DATASYNC_EXPORT DataStoreModel : public QAbstractListModel |
13 | 14 | { |
14 | 15 | Q_OBJECT |
15 | 16 | friend class DataStoreModelPrivate; |
16 | 17 |
|
| 18 | + //! Holds the type the model loads data for |
17 | 19 | Q_PROPERTY(int typeId READ typeId WRITE setTypeId) |
| 20 | + //! Specifies whether the model items can be edited |
18 | 21 | Q_PROPERTY(bool editable READ isEditable WRITE setEditable NOTIFY editableChanged) |
19 | 22 |
|
20 | 23 | public: |
| 24 | + //! Constructs a model for the default setup |
21 | 25 | explicit DataStoreModel(QObject *parent = nullptr); |
| 26 | + //! Constructs a model for the given setup |
22 | 27 | explicit DataStoreModel(const QString &setupName, QObject *parent = nullptr); |
| 28 | + //! Constructs a model on the given store |
23 | 29 | explicit DataStoreModel(AsyncDataStore *store, QObject *parent = nullptr); |
| 30 | + //! Destructor |
24 | 31 | ~DataStoreModel(); |
25 | 32 |
|
| 33 | + //! Returns the data store the model works on |
26 | 34 | AsyncDataStore *store() const; |
27 | 35 |
|
| 36 | + //! @readAcFn{DataStoreModel::typeId} |
28 | 37 | int typeId() const; |
| 38 | + //! @writeAcFn{DataStoreModel::typeId} |
29 | 39 | template <typename T> |
30 | 40 | inline bool setTypeId(); |
| 41 | + //! @readAcFn{DataStoreModel::editable} |
31 | 42 | bool isEditable() const; |
32 | 43 |
|
| 44 | + //! @inherit{QAbstractListModel::headerData} |
33 | 45 | QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; |
| 46 | + //! @inherit{QAbstractListModel::rowCount} |
34 | 47 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
35 | 48 |
|
| 49 | + //! Returns the index of the item with the given id |
36 | 50 | QModelIndex idIndex(const QString &id) const; |
| 51 | + //! Returns the index of the item with the given id |
37 | 52 | template <typename T> |
38 | 53 | inline QModelIndex idIndex(const T &id) const; |
39 | 54 |
|
| 55 | + //! Returns the key of the item at the given index |
40 | 56 | QString key(const QModelIndex &index) const; |
| 57 | + //! Returns the key of the item at the given index |
41 | 58 | template <typename T> |
42 | 59 | inline T key(const QModelIndex &index) const; |
43 | 60 |
|
| 61 | + //! @inherit{QAbstractListModel::data} |
44 | 62 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
| 63 | + //! @inherit{QAbstractListModel::setData} |
45 | 64 | bool setData(const QModelIndex &index, const QVariant &value, int role) override; |
46 | 65 |
|
| 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 | + */ |
47 | 80 | 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 | + */ |
48 | 96 | template <typename T> |
49 | 97 | inline T object(const QModelIndex &index) const; |
| 98 | + //! Loads the object at the given index from the store via AsyncDataStore::load |
50 | 99 | Task loadObject(const QModelIndex &index) const; |
| 100 | + //! Loads the object at the given index from the store via AsyncDataStore::load |
51 | 101 | template <typename T> |
52 | 102 | GenericTask<T> loadObject(const QModelIndex &index) const; |
53 | 103 |
|
| 104 | + //! @inherit{QAbstractListModel::flags} |
54 | 105 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
| 106 | + //! @inherit{QAbstractListModel::roleNames} |
55 | 107 | QHash<int, QByteArray> roleNames() const override; |
56 | 108 |
|
57 | 109 | public Q_SLOTS: |
| 110 | + //! @writeAcFn{DataStoreModel::typeId} |
58 | 111 | bool setTypeId(int typeId); |
| 112 | + //! @writeAcFn{DataStoreModel::editable} |
59 | 113 | void setEditable(bool editable); |
60 | 114 |
|
61 | 115 | Q_SIGNALS: |
| 116 | + //! Emitted when the model successfully loaded the current state from the store, and thus is "ready" |
62 | 117 | void storeLoaded(); |
| 118 | + //! Emitted when the store throws an exception |
63 | 119 | void storeError(const QException &exception); |
64 | 120 |
|
| 121 | + //! @notifyAcFn{DataStoreModel::editable} |
65 | 122 | void editableChanged(bool editable); |
66 | 123 |
|
67 | 124 | private Q_SLOTS: |
|
0 commit comments