1 | /* |
2 | * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl> |
3 | * |
4 | * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
5 | */ |
6 | |
7 | #ifndef ITEMBUILDER_H |
8 | #define ITEMBUILDER_H |
9 | |
10 | #include <vector> |
11 | #include <memory> |
12 | |
13 | #include <QObject> |
14 | #include <QQmlComponent> |
15 | #include <QQmlIncubator> |
16 | #include <QQuickItem> |
17 | |
18 | #include "quickcharts_export.h" |
19 | |
20 | class ItemIncubator; |
21 | |
22 | /** |
23 | * A helper class that instantiates QML items from QML components. |
24 | * |
25 | * Effectively this is a C++ version of Repeater. |
26 | */ |
27 | class QUICKCHARTS_EXPORT ItemBuilder : public QObject |
28 | { |
29 | Q_OBJECT |
30 | |
31 | public: |
32 | ItemBuilder(QObject *parent = nullptr); |
33 | ~ItemBuilder() override; |
34 | |
35 | QQmlComponent *component() const; |
36 | void setComponent(QQmlComponent *newComponent); |
37 | |
38 | QQmlContext *context() const; |
39 | void setContext(QQmlContext *newContext); |
40 | |
41 | int count() const; |
42 | void setCount(int newCount); |
43 | |
44 | QQmlIncubator::IncubationMode incubationMode() const; |
45 | void setIncubationMode(QQmlIncubator::IncubationMode newIncubationMode); |
46 | |
47 | QVariantMap initialProperties() const; |
48 | void setInitialProperties(const QVariantMap &newInitialProperties); |
49 | |
50 | void build(QQuickItem *parent); |
51 | |
52 | Q_SIGNAL void beginCreate(int index, QQuickItem *item); |
53 | Q_SIGNAL void endCreate(int index, QQuickItem *item); |
54 | |
55 | bool isFinished() const; |
56 | Q_SIGNAL void finished(); |
57 | |
58 | std::vector<std::shared_ptr<QQuickItem>> items() const; |
59 | |
60 | void clear(); |
61 | |
62 | private: |
63 | QQmlComponent *m_component = nullptr; |
64 | QQmlContext *m_context = nullptr; |
65 | int m_count = 0; |
66 | int m_completed = 0; |
67 | QQmlIncubator::IncubationMode m_incubationMode = QQmlIncubator::IncubationMode::AsynchronousIfNested; |
68 | QVariantMap m_initialProperties; |
69 | |
70 | std::vector<std::unique_ptr<ItemIncubator>> m_incubators; |
71 | std::vector<std::shared_ptr<QQuickItem>> m_items; |
72 | }; |
73 | |
74 | #endif // ITEMBUILDER_H |
75 | |