QmlMaterial 0.1.0
Loading...
Searching...
No Matches
delegate_recycler.hpp
1#pragma once
2
3#include <QQmlComponent>
4#include <QQmlEngine>
5#include <QQuickItem>
6#include <QPointer>
7#include <QQmlIncubator>
8#include <QSet>
9#include <functional>
10#include "qml_material/model/lazy_row.hpp"
11
12namespace qml_material
13{
14class DelegateRecycler : public QObject {
15public:
16 struct Placement {
17 QPointer<QQuickItem> parent, view;
18 QRectF geometry;
19 Qt::Orientation orientation = Qt::Vertical;
20 LazyRow::Usage usage = LazyRow::Cached;
21 bool fixedExtent = false;
22 bool valid = false;
23 };
24 explicit DelegateRecycler(QQmlEngine*, QObject* parent = nullptr);
25 ~DelegateRecycler() override;
26 QQuickItem* acquire(QQmlComponent*, const ItemSnapshotPtr&, int index);
27 QQuickItem* acquire(QQmlComponent*, const ItemSnapshotPtr&, int index, const Placement&,
28 bool asynchronous);
29 bool rebind(QQuickItem*, const ItemSnapshotPtr&, int index);
30 bool rebind(QQuickItem*, const ItemSnapshotPtr&, int index, const Placement&);
31 void retainPreparations(QQmlComponent*, const ItemSnapshotPtr&, const QSet<QString>&);
32 void cancelPreparations();
33 bool pending(const QString&) const;
34 int pendingCount() const;
35 void setReadyCallback(std::function<void()> callback) { m_ready = std::move(callback); }
36 void attach(QQuickItem*, QQuickItem* parent);
37 void layout(QQuickItem*, const QRectF&, Qt::Orientation = Qt::Vertical,
38 bool fixedExtent = false);
39 void release(QQuickItem*);
40 void clear();
41 void setPoolLimit(int);
42 void beginUpdate();
43 void endUpdate();
44 int poolSize() const;
45 LazyRow* row(QQuickItem*) const;
46 QString errorString() const { return m_error; }
47
48private:
49 class Incubator;
50 struct Entry {
51 ~Entry();
52 QPointer<QQuickItem> item;
53 QPointer<QObject> object;
54 QPointer<QQuickItem> host;
55 QPointer<LazyRow> row;
56 QPointer<QQmlComponent> component;
57 QString type;
58 QString key, error;
59 QQuickItem* itemIdentity = nullptr;
60 Placement placement;
61 std::shared_ptr<Incubator> incubator;
62 bool preparing = false;
63 bool cancelled = false;
64 bool idle = false;
65 bool updating = false;
66 };
67 std::shared_ptr<Entry> entry(QQuickItem*) const;
68 bool place(const std::shared_ptr<Entry>&, const Placement&);
69 void remove(const std::shared_ptr<Entry>&);
70 void finishPreparation(const std::shared_ptr<Entry>&);
71 static void retire(const std::shared_ptr<Entry>&, QQmlEngine*);
72 void notifyReady();
73 void trim();
74 QPointer<QQmlEngine> m_engine;
75 QList<std::shared_ptr<Entry>> m_entries;
76 QHash<QQuickItem*, std::weak_ptr<Entry>> m_items;
77 QHash<QString, std::shared_ptr<Entry>> m_pending;
78 std::function<void()> m_ready;
79 QString m_error;
80 quint64 m_generation = 0;
81 int m_poolLimit = 16;
82 int m_updateDepth = 0;
83 bool m_clearing = false;
84 bool m_readyQueued = false;
85};
86} // namespace qml_material