File indexing completed on 2024-04-21 15:02:55

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  * 
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #ifndef ITEMBUILDER_H
0008 #define ITEMBUILDER_H
0009 
0010 #include <vector>
0011 #include <memory>
0012 
0013 #include <QObject>
0014 #include <QQmlComponent>
0015 #include <QQmlIncubator>
0016 #include <QQuickItem>
0017 
0018 class ItemIncubator;
0019 
0020 /**
0021  * A helper class that instantiates QML items from QML components.
0022  *
0023  * Effectively this is a C++ version of Repeater.
0024  */
0025 class ItemBuilder : public QObject
0026 {
0027     Q_OBJECT
0028 
0029 public:
0030     ItemBuilder(QObject *parent = nullptr);
0031     ~ItemBuilder() override;
0032 
0033     QQmlComponent *component() const;
0034     void setComponent(QQmlComponent *newComponent);
0035 
0036     QQmlContext *context() const;
0037     void setContext(QQmlContext *newContext);
0038 
0039     int count() const;
0040     void setCount(int newCount);
0041 
0042     QQmlIncubator::IncubationMode incubationMode() const;
0043     void setIncubationMode(QQmlIncubator::IncubationMode newIncubationMode);
0044 
0045     QVariantMap initialProperties() const;
0046     void setInitialProperties(const QVariantMap &newInitialProperties);
0047 
0048     void build(QQuickItem *parent);
0049 
0050     Q_SIGNAL void beginCreate(int index, QQuickItem *item);
0051     Q_SIGNAL void endCreate(int index, QQuickItem *item);
0052 
0053     bool isFinished() const;
0054     Q_SIGNAL void finished();
0055 
0056     std::vector<std::shared_ptr<QQuickItem>> items() const;
0057 
0058     void clear();
0059 
0060 private:
0061     QQmlComponent *m_component = nullptr;
0062     QQmlContext *m_context = nullptr;
0063     int m_count = 0;
0064     int m_completed = 0;
0065     QQmlIncubator::IncubationMode m_incubationMode = QQmlIncubator::IncubationMode::AsynchronousIfNested;
0066     QVariantMap m_initialProperties;
0067 
0068     std::vector<std::unique_ptr<ItemIncubator>> m_incubators;
0069     std::vector<std::shared_ptr<QQuickItem>> m_items;
0070 };
0071 
0072 #endif // ITEMBUILDER_H