1/****************************************************************************
2**
3** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qshaderdata.h"
41#include "qshaderdata_p.h"
42
43QT_BEGIN_NAMESPACE
44
45namespace Qt3DRender {
46
47PropertyReaderInterface::~PropertyReaderInterface()
48{
49}
50
51QShaderDataPrivate::QShaderDataPrivate()
52 : QComponentPrivate()
53 , m_propertyReader(PropertyReaderInterfacePtr(new QShaderDataPropertyReader()))
54{
55}
56
57QShaderDataPrivate::QShaderDataPrivate(PropertyReaderInterfacePtr reader)
58 : QComponentPrivate()
59 , m_propertyReader(reader)
60{
61}
62
63/*!
64 * \class Qt3DRender::QShaderData
65 * \inheaderfile Qt3DRender/QShaderData
66 * \inmodule Qt3DRender
67 *
68 * \brief Provides a way of specifying values of a Uniform Block or a shader
69 * structure.
70 *
71 * \note When subclassing and adding properties to QShaderData, note that if
72 * you need to nest an inner Qt3DRender::QShaderData, the data type of the
73 * property should be Qt3DRender::QShaderData* instead of the name of your
74 * subclass.
75 *
76 * \since 5.5
77 */
78
79/*!
80 \fn Qt3DRender::PropertyReaderInterface::readProperty(const QVariant &v)
81 \return the property identified by \a v.
82 */
83
84/*!
85 * Constructs a new QShaderData with the specified \a parent.
86 */
87QShaderData::QShaderData(QNode *parent)
88 : QComponent(*new QShaderDataPrivate, parent)
89{
90}
91
92/*! \internal */
93QShaderData::~QShaderData()
94{
95}
96/*!
97 * \brief QShaderData::propertyReader
98 * Returns the PropertyReaderInterfacePtr for this shader data
99 */
100PropertyReaderInterfacePtr QShaderData::propertyReader() const
101{
102 Q_D(const QShaderData);
103 return d->m_propertyReader;
104}
105
106/*! \internal */
107QShaderData::QShaderData(QShaderDataPrivate &dd, QNode *parent)
108 : QComponent(dd, parent)
109{
110}
111
112/*! \internal */
113bool QShaderData::event(QEvent *event)
114{
115 Q_D(QShaderData);
116
117 if (event->type() == QEvent::DynamicPropertyChange) {
118 auto e = static_cast<QDynamicPropertyChangeEvent*>(event);
119 const auto propertyName = e->propertyName();
120
121 const QVariant data = property(propertyName);
122 if (data.canConvert<Qt3DCore::QNode*>()) {
123 const auto node = data.value<Qt3DCore::QNode*>();
124 const auto id = node ? node->id() : Qt3DCore::QNodeId();
125 d->notifyDynamicPropertyChange(propertyName, QVariant::fromValue(id));
126 } else {
127 d->notifyDynamicPropertyChange(propertyName, data);
128 }
129 }
130
131 return QComponent::event(event);
132}
133
134Qt3DCore::QNodeCreatedChangeBasePtr QShaderData::createNodeCreationChange() const
135{
136 Q_D(const QShaderData);
137
138 auto creationChange = Qt3DCore::QNodeCreatedChangePtr<QShaderDataData>::create(this);
139 QShaderDataData &data = creationChange->data;
140
141 data.propertyReader = d->m_propertyReader;
142
143 const QMetaObject *metaObj = metaObject();
144 const int propertyOffset = QShaderData::staticMetaObject.propertyOffset();
145 const int propertyCount = metaObj->propertyCount();
146
147 const auto propertyNames = dynamicPropertyNames();
148 data.properties.reserve(propertyCount - propertyOffset + propertyNames.size());
149
150 for (int i = propertyOffset; i < propertyCount; ++i) {
151 const QMetaProperty pro = metaObj->property(i);
152 if (pro.isWritable()) {
153 data.properties.push_back(qMakePair(pro.name(),
154 propertyReader()->readProperty(property(pro.name()))));
155 }
156 }
157
158 for (const QByteArray &propertyName : propertyNames) {
159 data.properties.push_back(qMakePair(propertyName,
160 propertyReader()->readProperty(property(propertyName))));
161 }
162
163 return creationChange;
164}
165
166} // namespace Qt3DRender
167
168QT_END_NAMESPACE
169