QtRestClient  3.0.0
A library for generic JSON-based REST-APIs, with a mechanism to map JSON to Qt objects
pagingmodel.h
1 #ifndef QTRESTCLIENT_PAGINGMODEL_H
2 #define QTRESTCLIENT_PAGINGMODEL_H
3 
4 #include "QtRestClient/qtrestclient_global.h"
5 #include "QtRestClient/ipaging.h"
6 #include "QtRestClient/restreply.h"
7 #include "QtRestClient/restclass.h"
8 
9 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
10 #include "QtRestClient/paging_fwd.h"
11 #include "QtRestClient/genericrestreply.h"
12 #endif
13 
14 #include <QtCore/qabstractitemmodel.h>
15 #include <QtCore/qscopedpointer.h>
16 #include <QtCore/qpointer.h>
17 
18 namespace QtRestClient {
19 
21 class Q_RESTCLIENT_EXPORT IPagingModelFetcher
22 {
23  Q_DISABLE_COPY(IPagingModelFetcher)
24 public:
26  virtual ~IPagingModelFetcher();
28  virtual RestClient *client() const = 0;
30  virtual RestReply *fetch(const QUrl &url) const = 0;
31 };
32 
33 class PagingModelPrivate;
35 class Q_RESTCLIENT_EXPORT PagingModel : public QAbstractTableModel
36 {
37  Q_OBJECT
38 
40  Q_PROPERTY(int typeId READ typeId NOTIFY typeIdChanged)
41 
42 public:
44  static constexpr int ModelDataRole = Qt::UserRole;
45 
47  explicit PagingModel(QObject *parent = nullptr);
48 
50  Q_INVOKABLE void initialize(const QUrl &initialUrl, QtRestClient::IPagingModelFetcher *fetcher, int typeId = QMetaType::UnknownType);
52  Q_INVOKABLE void initialize(const QUrl &initialUrl, QtRestClient::RestClass *restClass, int typeId = QMetaType::UnknownType);
54  Q_INVOKABLE void initialize(QtRestClient::RestReply *reply, QtRestClient::IPagingModelFetcher *fetcher, int typeId = QMetaType::UnknownType);
56  Q_INVOKABLE void initialize(QtRestClient::RestReply *reply, QtRestClient::RestClass *restClass, int typeId = QMetaType::UnknownType);
58  Q_INVOKABLE void initialize(QtRestClient::IPaging *paging, QtRestClient::IPagingModelFetcher *fetcher, int typeId = QMetaType::UnknownType);
60  Q_INVOKABLE void initialize(QtRestClient::IPaging *paging, QtRestClient::RestClass *restClass, int typeId = QMetaType::UnknownType);
61 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
62  template <typename T>
64  inline void initialize(const QUrl &initialUrl, IPagingModelFetcher *fetcher);
66  template <typename T>
67  inline void initialize(const QUrl &initialUrl, RestClass *restClass);
69  template <typename T>
70  inline void initialize(const Paging<T> &paging, IPagingModelFetcher *fetcher);
72  template <typename T>
73  inline void initialize(const Paging<T> &paging, RestClass *restClass);
75  template <typename DataClassType, typename ErrorClassType = QObject*>
76  inline void initialize(GenericRestReply<Paging<DataClassType>, ErrorClassType> *reply, IPagingModelFetcher *fetcher);
78  template <typename DataClassType, typename ErrorClassType = QObject*>
79  inline void initialize(GenericRestReply<Paging<DataClassType>, ErrorClassType> *reply, RestClass *restClass);
80 #endif
81 
83  int typeId() const;
84 
86  QVariant headerData(int section, Qt::Orientation orientation = Qt::Horizontal, int role = Qt::DisplayRole) const override;
88  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
90  int columnCount(const QModelIndex &parent = QModelIndex()) const override;
92  bool canFetchMore(const QModelIndex &parent) const override;
94  void fetchMore(const QModelIndex &parent) override;
95 
97  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
98 
100  Q_INVOKABLE QVariant object(const QModelIndex &index) const;
105  template <typename T>
106  inline T object(const QModelIndex &index) const;
107 
109  Qt::ItemFlags flags(const QModelIndex &index) const override;
111  QHash<int, QByteArray> roleNames() const override;
112 
114  int addColumn(const QString &text);
116  int addColumn(const QString &text, const char *propertyName);
118  void addRole(int column, int role, const char *propertyName);
120  void clearColumns();
121 
122 Q_SIGNALS:
124  void fetchError(QPrivateSignal);
125 
127  void typeIdChanged(int typeId, QPrivateSignal);
128 
129 protected:
131  explicit PagingModel(PagingModelPrivate &dd, QObject *parent = nullptr);
132 
133 private:
134  Q_DECLARE_PRIVATE(PagingModel)
135 };
136 
138 class Q_RESTCLIENT_EXPORT RestClassFetcher : public IPagingModelFetcher
139 {
140 public:
142  RestClassFetcher(RestClass *restClass);
143  RestClient *client() const override;
144  RestReply *fetch(const QUrl &url) const override;
145 
146 private:
147  QPointer<RestClass> _restClass;
148 };
149 
150 // ------------- Generic implementation -------------
151 
152 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
153 template<typename T>
154 inline void PagingModel::initialize(const QUrl &initialUrl, IPagingModelFetcher *fetcher)
155 {
156  initialize(initialUrl, fetcher, qMetaTypeId<T>());
157 }
158 
159 template<typename T>
160 inline void PagingModel::initialize(const QUrl &initialUrl, RestClass *restClass)
161 {
162  initialize(initialUrl, restClass, qMetaTypeId<T>());
163 }
164 
165 template<typename T>
166 inline void PagingModel::initialize(const Paging<T> &paging, IPagingModelFetcher *fetcher)
167 {
168  initialize(paging.iPaging(), fetcher, qMetaTypeId<T>());
169 }
170 
171 template<typename T>
172 inline void PagingModel::initialize(const Paging<T> &paging, RestClass *restClass)
173 {
174  initialize(paging.iPaging(), restClass, qMetaTypeId<T>());
175 }
176 
177 template<typename DataClassType, typename ErrorClassType>
179 {
180  initialize(reply, fetcher, qMetaTypeId<DataClassType>());
181 }
182 
183 template<typename DataClassType, typename ErrorClassType>
184 inline void PagingModel::initialize(GenericRestReply<Paging<DataClassType>, ErrorClassType> *reply, RestClass *restClass)
185 {
186  initialize(reply, restClass, qMetaTypeId<DataClassType>());
187 }
188 #endif
189 
190 template <typename T>
191 inline T PagingModel::object(const QModelIndex &index) const
192 {
193  Q_ASSERT_X(typeId() == QMetaType::UnknownType || qMetaTypeId<T>() == typeId(), Q_FUNC_INFO, "object must be used with the stores typeId");
194  return object(index).value<T>();
195 }
196 
197 }
198 
199 Q_DECLARE_METATYPE(QtRestClient::IPagingModelFetcher*)
200 
201 #endif // QTRESTCLIENT_PAGINGMODEL_H
QPointer
QtRestClient::IPaging
Interface to parse generic paging objects and operate on them.
Definition: ipaging.h:22
QtRestClient::PagingModel::object
Q_INVOKABLE QVariant object(const QModelIndex &index) const
Returns the object at the given index.
QtRestClient
The Namespace containing all classes of the QtRestClient module.
Definition: genericrestreply.h:14
QUrl
QtRestClient::RestClassFetcher
A default implementation for a IPagingModelFetcher, using a RestClass to send the requests.
Definition: pagingmodel.h:138
QtRestClient::PagingModel::initialize
Q_INVOKABLE void initialize(const QUrl &initialUrl, QtRestClient::IPagingModelFetcher *fetcher, int typeId=QMetaType::UnknownType)
Initialize the model using a type, a initial URL and a fetcher to use to send requests.
QVariant::value
T value() const const
QtRestClient::RestClass
A class to perform requests to an API.
Definition: restclass.h:19
Qt::ItemFlags
typedef ItemFlags
QString
QAbstractTableModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
QtRestClient::GenericRestReply
A class to handle generic replies for generic requests.
Definition: genericrestreply.h:20
QtRestClient::PagingModel
A Qt item model that fills itself from a Paging object obtained via the network.
Definition: pagingmodel.h:35
QtRestClient::IPagingModelFetcher
Interface for a component that fetches entries for the PagingModel.
Definition: pagingmodel.h:21
QAbstractTableModel
QtRestClient::Paging::iPaging
IPaging * iPaging() const
Returns the internally used IPaging instance.
Definition: paging.h:64
QtRestClient::Paging
A class to access generic paging objects.
Definition: paging_fwd.h:30
QModelIndex
QVariant
QObject
QtRestClient::PagingModel::typeId
int typeId() const
READ accessor for PagingModel::typeId.
QtRestClient::RestReply
A class to handle replies for JSON requests.
Definition: restreply.h:23
QtRestClient::RestClient
A class to define access to an API, with general settings.
Definition: restclient.h:30
QHash