1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2015 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 <QtQml/QJSValue> |
41 | #include <QtQml/QQmlEngine> |
42 | |
43 | #include <Qt3DQuickRender/private/quick3dbuffer_p.h> |
44 | #include <QtQml/private/qqmlengine_p.h> |
45 | #include <QtQml/private/qjsvalue_p.h> |
46 | #include <QtQml/private/qv4typedarray_p.h> |
47 | #include <QtQml/private/qv4arraybuffer_p.h> |
48 | #include <Qt3DRender/private/qurlhelper_p.h> |
49 | #include <QtCore/qfile.h> |
50 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | namespace Qt3DRender { |
54 | |
55 | namespace Render { |
56 | |
57 | namespace Quick { |
58 | |
59 | namespace { |
60 | const int jsValueTypeId = qMetaTypeId<QJSValue>(); |
61 | } |
62 | |
63 | Quick3DBuffer::Quick3DBuffer(Qt3DCore::QNode *parent) |
64 | : Qt3DRender::QBuffer(parent) |
65 | , m_engine(nullptr) |
66 | , m_v4engine(nullptr) |
67 | { |
68 | QObject::connect(sender: this, signal: &Qt3DRender::QBuffer::dataChanged, receiver: this, slot: &Quick3DBuffer::bufferDataChanged); |
69 | } |
70 | |
71 | QByteArray Quick3DBuffer::convertToRawData(const QJSValue &jsValue) |
72 | { |
73 | initEngines(); |
74 | Q_ASSERT(m_v4engine); |
75 | QV4::Scope scope(m_v4engine); |
76 | QV4::Scoped<QV4::TypedArray> typedArray(scope, |
77 | QJSValuePrivate::convertedToValue(e: m_v4engine, jsval: jsValue)); |
78 | if (!typedArray) |
79 | return QByteArray(); |
80 | |
81 | char *dataPtr = reinterpret_cast<char *>(typedArray->arrayData()->data()); |
82 | dataPtr += typedArray->d()->byteOffset; |
83 | uint byteLength = typedArray->byteLength(); |
84 | return QByteArray(dataPtr, byteLength); |
85 | } |
86 | |
87 | QVariant Quick3DBuffer::bufferData() const |
88 | { |
89 | return QVariant::fromValue(value: data()); |
90 | } |
91 | |
92 | void Quick3DBuffer::setBufferData(const QVariant &bufferData) |
93 | { |
94 | if (bufferData.userType() == QMetaType::QByteArray) { |
95 | QBuffer::setData(bufferData.toByteArray()); |
96 | } else if (bufferData.userType() == jsValueTypeId) { |
97 | QJSValue jsValue = bufferData.value<QJSValue>(); |
98 | QBuffer::setData(convertToRawData(jsValue)); |
99 | } |
100 | } |
101 | |
102 | void Quick3DBuffer::updateData(int offset, const QVariant &bufferData) |
103 | { |
104 | if (bufferData.userType() == QMetaType::QByteArray) { |
105 | QBuffer::updateData(offset, bytes: bufferData.toByteArray()); |
106 | } else if (bufferData.userType() == jsValueTypeId) { |
107 | QJSValue jsValue = bufferData.value<QJSValue>(); |
108 | QBuffer::updateData(offset, bytes: convertToRawData(jsValue)); |
109 | } |
110 | } |
111 | |
112 | /*! |
113 | \qmlmethod string Quick3DBuffer::readBinaryFile(url &fileUrl) |
114 | |
115 | Reads the binary at \a fileUrl and return it as a QByteArray wrapped in a |
116 | QVariant |
117 | |
118 | \note this is provided as convenience for QML where reading files and creating |
119 | QByteArray is not possible |
120 | */ |
121 | QVariant Quick3DBuffer::readBinaryFile(const QUrl &fileUrl) |
122 | { |
123 | QFile f(Qt3DRender::QUrlHelper::urlToLocalFileOrQrc(url: fileUrl)); |
124 | QByteArray data; |
125 | |
126 | if (f.open(flags: QIODevice::ReadOnly)) |
127 | data = f.readAll(); |
128 | return QVariant(data); |
129 | } |
130 | |
131 | void Quick3DBuffer::initEngines() |
132 | { |
133 | if (m_engine == nullptr) { |
134 | m_engine = qmlEngine(parent()); |
135 | m_v4engine = QQmlEnginePrivate::getV4Engine(e: m_engine); |
136 | } |
137 | } |
138 | |
139 | } // Quick |
140 | |
141 | } // Render |
142 | |
143 | } // Qt3DRender |
144 | |
145 | QT_END_NAMESPACE |
146 | |