diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2015-08-26 11:26:42 +0200 |
---|---|---|
committer | Paul Lemire <paul.lemire@kdab.com> | 2015-10-14 13:26:32 +0000 |
commit | 8d02ff205e65eaad4da851d1d997e317221126df (patch) | |
tree | 2ce7d7a360023f023717376082909be2a3968216 /src | |
parent | 7c971e0e7791ab38d7d3d8ebc25468c44cbe49c5 (diff) |
Quick3DBuffer extension class
QAbstractAttribute: remove the QByteArray data Q_PROPERTY as QByteArray isn't
properly handled from QML. Manually call QChangeArbiter notification when the
setData method is called.
Quick3DBuffer contains a QVariant data Q_PROPERTY. Using a method that
converts a QVariant to a QJSValue and then reads the proper JS typed array to
build a QByteBuffer all from QML.
Change-Id: Id777f34de2c166b2345faa25ecbc84d12a9ea352
Reviewed-by: Andy Nichols <andy.nichols@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/core/io/qabstractbuffer.cpp | 1 | ||||
-rw-r--r-- | src/core/io/qabstractbuffer.h | 6 | ||||
-rw-r--r-- | src/quick3d/quick3drenderer/items/items.pri | 6 | ||||
-rw-r--r-- | src/quick3d/quick3drenderer/items/quick3dbuffer.cpp | 102 | ||||
-rw-r--r-- | src/quick3d/quick3drenderer/items/quick3dbuffer.h | 87 |
5 files changed, 195 insertions, 7 deletions
diff --git a/src/core/io/qabstractbuffer.cpp b/src/core/io/qabstractbuffer.cpp index c53904747..d9a9ee845 100644 --- a/src/core/io/qabstractbuffer.cpp +++ b/src/core/io/qabstractbuffer.cpp @@ -79,6 +79,7 @@ void QAbstractBuffer::setData(const QByteArray &bytes) Q_D(QAbstractBuffer); if (bytes != d->m_data) { d->m_data = bytes; + QNodePrivate::get(this)->notifyPropertyChange("data", QVariant::fromValue(d->m_data)); emit dataChanged(); } } diff --git a/src/core/io/qabstractbuffer.h b/src/core/io/qabstractbuffer.h index 2a2120ec4..07723d476 100644 --- a/src/core/io/qabstractbuffer.h +++ b/src/core/io/qabstractbuffer.h @@ -50,17 +50,13 @@ class QAbstractBufferPrivate; class QT3DCORESHARED_EXPORT QAbstractBuffer : public QNode { Q_OBJECT - Q_PROPERTY(QByteArray data READ data WRITE setData NOTIFY dataChanged) public: QAbstractBuffer(QNode *parent = 0); virtual ~QAbstractBuffer(); - virtual void setData(const QByteArray &bytes); + void setData(const QByteArray &bytes); QByteArray data() const; - // TO DO: Add a functor here so that we can create CPU side buffer data - // in parallel with QAspectJobs - protected: QAbstractBuffer(QAbstractBufferPrivate &dd, QNode *parent = 0); void copy(const QNode *ref) Q_DECL_OVERRIDE; diff --git a/src/quick3d/quick3drenderer/items/items.pri b/src/quick3d/quick3drenderer/items/items.pri index 8c31f6f38..881923b59 100644 --- a/src/quick3d/quick3drenderer/items/items.pri +++ b/src/quick3d/quick3drenderer/items/items.pri @@ -17,7 +17,8 @@ HEADERS += \ $$PWD/quick3dshaderdataarray.h \ $$PWD/quick3dstateset.h \ $$PWD/quick3drendertargetselector.h \ - $$PWD/quick3dgeometry.h + $$PWD/quick3dgeometry.h \ + $$PWD/quick3dbuffer.h SOURCES += \ $$PWD/quick3drenderpassfilter.cpp \ @@ -37,6 +38,7 @@ SOURCES += \ $$PWD/quick3dshaderdataarray.cpp \ $$PWD/quick3dstateset.cpp \ $$PWD/quick3drendertargetselector.cpp \ - $$PWD/quick3dgeometry.cpp + $$PWD/quick3dgeometry.cpp \ + $$PWD/quick3dbuffer.cpp INCLUDEPATH += $$PWD diff --git a/src/quick3d/quick3drenderer/items/quick3dbuffer.cpp b/src/quick3d/quick3drenderer/items/quick3dbuffer.cpp new file mode 100644 index 000000000..aaa3645c1 --- /dev/null +++ b/src/quick3d/quick3drenderer/items/quick3dbuffer.cpp @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "quick3dbuffer.h" +#include <QQmlEngine> +#include <QJSValue> +#include <QtQml/private/qqmlengine_p.h> +#include <QtQml/private/qjsvalue_p.h> +#include <QtQml/private/qv4typedarray_p.h> +#include <QtQml/private/qv4arraybuffer_p.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3DRender { + +namespace Render { + +namespace Quick { + +Quick3DBuffer::Quick3DBuffer(QObject *parent) + : QObject(parent) + , m_engine(Q_NULLPTR) + , m_v4engine(Q_NULLPTR) +{ + QObject::connect(parentBuffer(), &Qt3DCore::QAbstractBuffer::dataChanged, this, &Quick3DBuffer::bufferDataChanged); +} + +QByteArray Quick3DBuffer::convertToRawData(const QJSValue &jsValue) +{ + initEngines(); + Q_ASSERT(m_v4engine); + QV4::Scope scope(m_v4engine); + QV4::Scoped<QV4::TypedArray> typedArray(scope, + QJSValuePrivate::convertedToValue(m_v4engine, jsValue)); + if (!typedArray) + return QByteArray(); + + char *dataPtr = reinterpret_cast<char *>(typedArray->arrayData()->data()); + dataPtr += typedArray->d()->byteOffset; + uint byteLength = typedArray->byteLength(); + return QByteArray::fromRawData(dataPtr, byteLength); +} + +QVariant Quick3DBuffer::bufferData() const +{ + return QVariant::fromValue(parentBuffer()->data()); +} + +void Quick3DBuffer::setBufferData(const QVariant &bufferData) +{ + QJSValue jsValue = bufferData.value<QJSValue>(); + parentBuffer()->setData(convertToRawData(jsValue)); +} + +void Quick3DBuffer::initEngines() +{ + if (m_engine == Q_NULLPTR) { + m_engine = qmlEngine(parent()); + m_v4engine = QQmlEnginePrivate::getV4Engine(m_engine); + } +} + +} // Quick + +} // Render + +} // Qt3DRender + +QT_END_NAMESPACE diff --git a/src/quick3d/quick3drenderer/items/quick3dbuffer.h b/src/quick3d/quick3drenderer/items/quick3dbuffer.h new file mode 100644 index 000000000..7fa3ad8cf --- /dev/null +++ b/src/quick3d/quick3drenderer/items/quick3dbuffer.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT3DRENDER_RENDER_QUICK_QUICK3DBUFFER_H +#define QT3DRENDER_RENDER_QUICK_QUICK3DBUFFER_H + +#include <Qt3DQuickRenderer/qt3dquickrenderer_global.h> +#include <Qt3DRenderer/QBuffer> + +QT_BEGIN_NAMESPACE + +class QQmlEngine; +class QJSValue; + +namespace QV4 { +struct ExecutionEngine; +} + +namespace Qt3DRender { + +namespace Render { + +namespace Quick { + +class QT3DQUICKRENDERERSHARED_EXPORT Quick3DBuffer : public QObject +{ + Q_OBJECT + Q_PROPERTY(QVariant data READ bufferData WRITE setBufferData NOTIFY bufferDataChanged) +public: + explicit Quick3DBuffer(QObject *parent = Q_NULLPTR); + inline QBuffer *parentBuffer() const { return qobject_cast<QBuffer *>(parent()); } + + QVariant bufferData() const; + void setBufferData(const QVariant &bufferData); + +Q_SIGNALS: + void bufferDataChanged(); + +private: + QQmlEngine *m_engine; + QV4::ExecutionEngine *m_v4engine; + void initEngines(); + QByteArray convertToRawData(const QJSValue &jsValue); +}; + +} // Quick + +} // Render + +} // Qt3DRender + +QT_END_NAMESPACE + +#endif // QT3DRENDER_RENDER_QUICK_QUICK3DBUFFER_H |