1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4//
5// W A R N I N G
6// -------------
7//
8// This file is not part of the Qt API. It exists purely as an
9// implementation detail. This header file may change from version to
10// version without notice, or even be removed.
11//
12// We mean it.
13//
14
15#ifndef INSTANCEREPEATER_P_H
16#define INSTANCEREPEATER_P_H
17
18#include <QtQuick3D/qquick3dinstancing.h>
19#include <QtQuick3D/private/qquick3drepeater_p.h>
20#include <QAbstractListModel>
21
22// Workaround for QTBUG-94099, ensures qml_register_types...() is exported
23#include "qtquick3dhelpersglobal_p.h"
24
25QT_BEGIN_NAMESPACE
26
27class InstanceModel : public QAbstractListModel
28{
29 Q_OBJECT
30 Q_PROPERTY(QQuick3DInstancing *instancingTable READ instancing WRITE setInstancing NOTIFY instancingChanged)
31 QML_NAMED_ELEMENT(InstanceModel)
32 QML_ADDED_IN_VERSION(6, 4)
33
34public:
35 explicit InstanceModel(QObject *parent = nullptr);
36
37 QVariant data(const QModelIndex &index, int role) const override;
38 int rowCount(const QModelIndex &parent) const override;
39
40 QQuick3DInstancing *instancing() const { return m_instancing; }
41 void setInstancing(QQuick3DInstancing *instancing);
42
43 const QQuick3DInstancing::InstanceTableEntry *instanceData(int index) const;
44
45 enum Roles {
46 PositionRole, RotationRole, ScaleRole, ColorRole, CustomDataRole
47 };
48
49 QHash<int, QByteArray> roleNames() const override {
50 return {
51 { ColorRole, "modelColor" },
52 { PositionRole, "modelPosition" },
53 { RotationRole, "modelRotation"},
54 { ScaleRole, "modelScale"},
55 { CustomDataRole, "modelData"}
56 };
57 }
58
59private slots:
60 void reset();
61
62signals:
63 void instancingChanged();
64
65private:
66 void ensureTable() const;
67
68 QQuick3DInstancing *m_instancing = nullptr;
69
70 QByteArray m_instanceData;
71 int m_count = 0;
72 QMetaObject::Connection m_tableConnection;
73};
74
75class InstanceRepeater : public QQuick3DRepeater
76{
77 Q_OBJECT
78 Q_PROPERTY(QQuick3DInstancing *instancingTable READ instancing WRITE setInstancing NOTIFY instancingChanged)
79 QML_NAMED_ELEMENT(InstanceRepeater)
80 QML_ADDED_IN_VERSION(6, 4)
81
82public:
83 explicit InstanceRepeater(QQuick3DNode *parent = nullptr);
84 QQuick3DInstancing *instancing() const;
85 void setInstancing(QQuick3DInstancing *instancing);
86signals:
87 void instancingChanged();
88protected:
89 void initDelegate(int index, QQuick3DNode *node) override;
90private:
91 InstanceModel *m_model = nullptr;
92};
93QT_END_NAMESPACE
94#endif // INSTANCEREPEATER_P_H
95

source code of qtquick3d/src/helpers/instancerepeater_p.h