QmlMaterial 0.1.0
Loading...
Searching...
No Matches
item_model_source.hpp
1#pragma once
2
3#include <QAbstractItemModel>
4#include <QPointer>
5#include "qml_material/model/item_source.hpp"
6
7namespace qml_material
8{
9class QML_MATERIAL_API ItemModelSource : public ItemSource {
10 Q_OBJECT
11 QML_ELEMENT
12 Q_PROPERTY(QAbstractItemModel* model READ model WRITE setModel NOTIFY modelChanged FINAL)
13 Q_PROPERTY(QString keyRole READ keyRole WRITE setKeyRole NOTIFY keyRoleChanged FINAL)
14 Q_PROPERTY(QString typeRole READ typeRole WRITE setTypeRole NOTIFY typeRoleChanged FINAL)
15 Q_PROPERTY(QStringList roles READ roles WRITE setRoles NOTIFY rolesChanged FINAL)
16public:
17 using ItemSource::ItemSource;
18 QAbstractItemModel* model() const { return m_model; }
19 QString keyRole() const { return m_keyRole; }
20 QString typeRole() const { return m_typeRole; }
21 QStringList roles() const { return m_roles; }
22 void setModel(QAbstractItemModel*);
23 void setKeyRole(const QString&);
24 void setTypeRole(const QString&);
25 void setRoles(const QStringList&);
26 Q_INVOKABLE void requestMore() override;
27 // QAbstractItemModel has no notification for canFetchMore changes without row changes.
28 Q_INVOKABLE void notifyFetchStateChanged();
29 Q_SIGNAL void modelChanged();
30 Q_SIGNAL void keyRoleChanged();
31 Q_SIGNAL void typeRoleChanged();
32 Q_SIGNAL void rolesChanged();
33
34private:
35 void update(QVector<ItemChange> changes = {}, bool reset = true);
36 QPointer<QAbstractItemModel> m_model;
37 QList<QMetaObject::Connection> m_connections;
38 QString m_keyRole, m_typeRole;
39 QStringList m_roles;
40 quint64 m_generation = 0;
41 quint64 m_modelGeneration = 0;
42 bool m_fetchQueued = false;
43};
44} // namespace qml_material