QmlMaterial 0.1.0
Loading...
Searching...
No Matches
item_source.hpp
1#pragma once
2
3#include <QObject>
4#include <QVariant>
5#include <QVector>
6#include <QtQml/qqmlregistration.h>
7#include "qml_material/model/item_snapshot.hpp"
8
9namespace qml_material
10{
11struct ItemChange {
12 enum Kind
13 {
14 Insert,
15 Remove,
16 Move,
17 Update
18 };
19 Kind kind = Update;
20 int index = 0;
21 int count = 0;
22 // Move destination is an index after the moved range has been removed.
23 int destination = 0;
24};
25
26struct ItemChangeSet {
27 ItemSnapshotPtr before;
28 ItemSnapshotPtr after;
29 QVector<ItemChange> changes;
30 bool reset = false;
31};
32
33struct ItemPublishResult {
34 enum Status
35 {
36 Accepted,
37 Rejected
38 };
39 Status status = Rejected;
40 QString error;
41 explicit operator bool() const { return status == Accepted; }
42};
43
44class QML_MATERIAL_API ItemSource : public QObject {
45 Q_OBJECT
46 QML_NAMED_ELEMENT(ItemSource)
47 QML_UNCREATABLE("Use an item source adapter")
48 Q_PROPERTY(int count READ count NOTIFY stateChanged FINAL)
49 Q_PROPERTY(quint64 revision READ revision NOTIFY stateChanged FINAL)
50 Q_PROPERTY(bool stableKeys READ stableKeys NOTIFY stateChanged FINAL)
51 Q_PROPERTY(QString errorString READ errorString NOTIFY stateChanged FINAL)
52public:
53 explicit ItemSource(QObject* parent = nullptr);
54 ItemSnapshotPtr snapshot() const { return m_snapshot; }
55 int count() const { return m_snapshot->count(); }
56 quint64 revision() const { return m_snapshot->revision; }
57 bool stableKeys() const { return m_snapshot->stableKeys(); }
58 QString errorString() const { return m_error; }
59 Q_INVOKABLE int indexOfKey(const QString& key) const { return m_snapshot->indexOfKey(key); }
60 Q_INVOKABLE QVariant valueAt(int index) const {
61 return index >= 0 && index < count() ? m_snapshot->valueAt(index) : QVariant {};
62 }
63 Q_SIGNAL void stateChanged();
64 Q_SIGNAL void committed(const qml_material::ItemChangeSet& change);
65
66protected:
67 ItemPublishResult publish(ItemDataSnapshotPtr, QVector<ItemChange> changes = {},
68 bool reset = true);
69 // Keeps row identity and structure; zero content revisions advance automatically.
70 ItemPublishResult publishValues(QVector<ItemValueUpdate>);
71 ItemPublishResult reject(QString error);
72 ItemSnapshotPtr publicationBase() const { return m_tail; }
73
74private:
75 struct Pending {
76 ItemChangeSet change;
77 QString error;
78 };
79 void drain();
80 ItemPublishResult commit(ItemSnapshotPtr, QVector<ItemChange>, bool reset);
81 ItemSnapshotPtr m_snapshot;
82 ItemSnapshotPtr m_tail;
83 QList<Pending> m_pending;
84 QString m_error;
85 bool m_publishing = false;
86};
87
88class QML_MATERIAL_API ListSnapshotSource : public ItemSource {
89 Q_OBJECT
90 QML_ELEMENT
91 Q_PROPERTY(QVariantList items READ items WRITE setItems NOTIFY itemsChanged FINAL)
92 Q_PROPERTY(QString keyRole READ keyRole WRITE setKeyRole NOTIFY keyRoleChanged FINAL)
93 Q_PROPERTY(QString typeRole READ typeRole WRITE setTypeRole NOTIFY typeRoleChanged FINAL)
94public:
95 using ItemSource::ItemSource;
96 QVariantList items() const { return m_items; }
97 QString keyRole() const { return m_keyRole; }
98 QString typeRole() const { return m_typeRole; }
99 void setItems(const QVariantList&);
100 void setKeyRole(const QString&);
101 void setTypeRole(const QString&);
102 Q_SIGNAL void itemsChanged();
103 Q_SIGNAL void keyRoleChanged();
104 Q_SIGNAL void typeRoleChanged();
105
106private:
107 void rebuild();
108 QVariantList m_items;
109 QString m_keyRole, m_typeRole;
110};
111} // namespace qml_material
112Q_DECLARE_METATYPE(qml_material::ItemChangeSet)