File indexing completed on 2024-09-29 06:33:33

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 #include "quickcharts_export.h"
0019 
0020 class ItemIncubator;
0021 
0022 /**
0023  * A helper class that instantiates QML items from QML components.
0024  *
0025  * Effectively this is a C++ version of Repeater.
0026  */
0027 class QUICKCHARTS_EXPORT ItemBuilder : public QObject
0028 {
0029     Q_OBJECT
0030 
0031 public:
0032     ItemBuilder(QObject *parent = nullptr);
0033     ~ItemBuilder() override;
0034 
0035     QQmlComponent *component() const;
0036     void setComponent(QQmlComponent *newComponent);
0037 
0038     QQmlContext *context() const;
0039     void setContext(QQmlContext *newContext);
0040 
0041     int count() const;
0042     void setCount(int newCount);
0043 
0044     QQmlIncubator::IncubationMode incubationMode() const;
0045     void setIncubationMode(QQmlIncubator::IncubationMode newIncubationMode);
0046 
0047     QVariantMap initialProperties() const;
0048     void setInitialProperties(const QVariantMap &newInitialProperties);
0049 
0050     void build(QQuickItem *parent);
0051 
0052     Q_SIGNAL void beginCreate(int index, QQuickItem *item);
0053     Q_SIGNAL void endCreate(int index, QQuickItem *item);
0054 
0055     bool isFinished() const;
0056     Q_SIGNAL void finished();
0057 
0058     std::vector<std::shared_ptr<QQuickItem>> items() const;
0059 
0060     void clear();
0061 
0062 private:
0063     QQmlComponent *m_component = nullptr;
0064     QQmlContext *m_context = nullptr;
0065     int m_count = 0;
0066     int m_completed = 0;
0067     QQmlIncubator::IncubationMode m_incubationMode = QQmlIncubator::IncubationMode::AsynchronousIfNested;
0068     QVariantMap m_initialProperties;
0069 
0070     std::vector<std::unique_ptr<ItemIncubator>> m_incubators;
0071     std::vector<std::shared_ptr<QQuickItem>> m_items;
0072 };
0073 
0074 #endif // ITEMBUILDER_H