QtDataSync  4.2.0
A simple offline-first synchronisation framework, to synchronize data of Qt applications between devices
datastoremodel.h
1 #ifndef QTDATASYNC_DATASTOREMODEL_H
2 #define QTDATASYNC_DATASTOREMODEL_H
3 
4 #include <QtCore/qabstractitemmodel.h>
5 
6 #include "QtDataSync/qtdatasync_global.h"
7 #include "QtDataSync/datastore.h"
8 
9 namespace QtDataSync {
10 
11 class DataStoreModelPrivate;
13 class Q_DATASYNC_EXPORT DataStoreModel : public QAbstractTableModel
14 {
15  Q_OBJECT
16  friend class DataStoreModelPrivate;
17 
19  Q_PROPERTY(int typeId READ typeId WRITE setTypeId NOTIFY typeIdChanged)
21  Q_PROPERTY(bool editable READ isEditable WRITE setEditable NOTIFY editableChanged)
22 
23 public:
25  explicit DataStoreModel(QObject *parent = nullptr);
27  explicit DataStoreModel(const QString &setupName, QObject *parent = nullptr);
29  explicit DataStoreModel(DataStore *store, QObject *parent = nullptr);
30  ~DataStoreModel() override;
31 
33  DataStore *store() const;
35  QString setupName() const;
36 
38  int typeId() const;
40  template <typename T>
41  inline void setTypeId(bool resetColumns = true);
43  bool isEditable() const;
44 
46  QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
48  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
50  int columnCount(const QModelIndex &parent) const override;
52  bool canFetchMore(const QModelIndex &parent) const override;
54  void fetchMore(const QModelIndex &parent) override;
55 
57  QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override;
59  Q_INVOKABLE QModelIndex idIndex(const QString &id) const;
61  template <typename T>
62  inline QModelIndex idIndex(const T &id) const;
63 
65  Q_INVOKABLE QString key(const QModelIndex &index) const;
67  template <typename T>
68  inline T key(const QModelIndex &index) const;
69 
71  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
73  bool setData(const QModelIndex &index, const QVariant &value, int role) override;
74 
76  Q_INVOKABLE QVariant object(const QModelIndex &index) const;
81  template <typename T>
82  inline T object(const QModelIndex &index) const;
84  Q_INVOKABLE QVariant loadObject(const QModelIndex &index) const;
89  template <typename T>
90  T loadObject(const QModelIndex &index) const;
91 
93  Qt::ItemFlags flags(const QModelIndex &index) const override;
95  QHash<int, QByteArray> roleNames() const override;
96 
98  int addColumn(const QString &text);
100  int addColumn(const QString &text, const char *propertyName);
102  void addRole(int column, int role, const char *propertyName);
104  void clearColumns();
105 
106 public Q_SLOTS:
108  void setTypeId(int typeId); //MAJOR merge methods
110  void setTypeId(int typeId, bool resetColumns);
112  void setEditable(bool editable);
113 
115  void reload();
116 
117 Q_SIGNALS:
119  void storeError(const QException &exception, QPrivateSignal);
121  void typeIdChanged(int typeId, QPrivateSignal);
123  void editableChanged(bool editable, QPrivateSignal);
124 
125 protected:
127  explicit DataStoreModel(QObject *parent, void*);
129  void initStore(DataStore *store);
130 
131 private Q_SLOTS:
132  void storeChanged(int metaTypeId, const QString &key, bool wasDeleted);
133  void storeResetted();
134 
135 private:
136  QScopedPointer<DataStoreModelPrivate> d;
137 };
138 
139 // ------------- Generic Implementation -------------
140 
141 template <typename T>
142 inline void DataStoreModel::setTypeId(bool resetColumns) {
143  QTDATASYNC_STORE_ASSERT(T);
144  setTypeId(qMetaTypeId<T>(), resetColumns);
145 }
146 
147 template <typename T>
148 inline QModelIndex DataStoreModel::idIndex(const T &id) const {
149  return idIndex(QVariant::fromValue<T>(id).toString());
150 }
151 
152 template <typename T>
153 inline T DataStoreModel::key(const QModelIndex &index) const {
154  return QVariant(key(index)).value<T>();
155 }
156 
157 template <typename T>
158 inline T DataStoreModel::object(const QModelIndex &index) const {
159  QTDATASYNC_STORE_ASSERT(T);
160  Q_ASSERT_X(qMetaTypeId<T>() == typeId(), Q_FUNC_INFO, "object must be used with the stores typeId");
161  return object(index).value<T>();
162 }
163 
164 template <typename T>
166  QTDATASYNC_STORE_ASSERT(T);
167  Q_ASSERT_X(qMetaTypeId<T>() == typeId(), Q_FUNC_INFO, "object must be used with the stores typeId");
168  return loadObject(index).value<T>();
169 }
170 
171 }
172 
173 #endif // QTDATASYNC_DATASTOREMODEL_H
Q_INVOKABLE QString key(const QModelIndex &index) const
Returns the key of the item at the given index.
Q_INVOKABLE QVariant loadObject(const QModelIndex &index) const
Loads the object at the given index from the store via DataStore::load.
Main store to generically access all stored data synchronously.
Definition: datastore.h:21
T value() const const
Q_INVOKABLE QVariant object(const QModelIndex &index) const
Returns the object at the given index.
The primary namespace of the QtDataSync library.
Q_INVOKABLE QModelIndex idIndex(const QString &id) const
Returns the index of the item with the given id.
A passive item model for a datasync data store.