diff options
22 files changed, 1019 insertions, 18 deletions
diff --git a/examples/quick/scenegraph/scenegraph.pro b/examples/quick/scenegraph/scenegraph.pro index 85ae25907a..1e7e74372d 100644 --- a/examples/quick/scenegraph/scenegraph.pro +++ b/examples/quick/scenegraph/scenegraph.pro @@ -6,4 +6,5 @@ SUBDIRS += \ simplematerial \ textureinsgnode \ textureinthread \ - threadedanimation + threadedanimation \ + twotextureproviders diff --git a/examples/quick/scenegraph/textureinthread/threadrenderer.cpp b/examples/quick/scenegraph/textureinthread/threadrenderer.cpp index 270a7f4323..d840d3989b 100644 --- a/examples/quick/scenegraph/textureinthread/threadrenderer.cpp +++ b/examples/quick/scenegraph/textureinthread/threadrenderer.cpp @@ -192,6 +192,8 @@ public slots: m_texture = m_window->createTextureFromId(newId, size); setTexture(m_texture); + markDirty(DirtyMaterial); + // This will notify the rendering thread that the texture is now being rendered // and it can start rendering to the other one. emit textureInUse(); diff --git a/examples/quick/scenegraph/twotextureproviders/doc/images/twotextureproviders-example.jpg b/examples/quick/scenegraph/twotextureproviders/doc/images/twotextureproviders-example.jpg Binary files differnew file mode 100644 index 0000000000..331233fa32 --- /dev/null +++ b/examples/quick/scenegraph/twotextureproviders/doc/images/twotextureproviders-example.jpg diff --git a/examples/quick/scenegraph/twotextureproviders/doc/src/twotextureproviders.qdoc b/examples/quick/scenegraph/twotextureproviders/doc/src/twotextureproviders.qdoc new file mode 100644 index 0000000000..3283bcf029 --- /dev/null +++ b/examples/quick/scenegraph/twotextureproviders/doc/src/twotextureproviders.qdoc @@ -0,0 +1,36 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Gunnar Sletta <gunnar@sletta.org> +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example scenegraph/twotextureproviders + \title Scene Graph - Two Texture Providers + \ingroup qtquickexamples + + \brief Shows how to combine two textures from two texture providers in a custom scene graph node. + + \image twotextureproviders-example.jpg + */ diff --git a/examples/quick/scenegraph/twotextureproviders/main.cpp b/examples/quick/scenegraph/twotextureproviders/main.cpp new file mode 100644 index 0000000000..49edd8b651 --- /dev/null +++ b/examples/quick/scenegraph/twotextureproviders/main.cpp @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Gunnar Sletta <gunnar@sletta.org> +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QGuiApplication> + +#include <QtQuick/QQuickView> + +#include "xorblender.h" + +int main(int argc, char **argv) +{ + QGuiApplication app(argc, argv); + + qmlRegisterType<XorBlender>("SceneGraphRendering", 1, 0, "XorBlender"); + + QQuickView view; + view.setResizeMode(QQuickView::SizeRootObjectToView); + view.setSource(QUrl("qrc:///scenegraph/twotextureproviders/main.qml")); + view.show(); + + return app.exec(); +} diff --git a/examples/quick/scenegraph/twotextureproviders/main.qml b/examples/quick/scenegraph/twotextureproviders/main.qml new file mode 100644 index 0000000000..c842cad554 --- /dev/null +++ b/examples/quick/scenegraph/twotextureproviders/main.qml @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Gunnar Sletta <gunnar@sletta.org> +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.2 +import SceneGraphRendering 1.0 + +Item { + id: root + + // The checkers background + ShaderEffect { + anchors.fill: parent + + property real tileSize: 16 + property color color1: Qt.rgba(0.9, 0.9, 0.9, 1); + property color color2: Qt.rgba(0.8, 0.8, 0.8, 1); + + property size pixelSize: Qt.size(width / tileSize, height / tileSize); + + fragmentShader: + " + uniform lowp vec4 color1; + uniform lowp vec4 color2; + uniform highp vec2 pixelSize; + varying highp vec2 qt_TexCoord0; + void main() { + highp vec2 tc = sign(sin(3.14152 * qt_TexCoord0 * pixelSize)); + if (tc.x != tc.y) + gl_FragColor = color1; + else + gl_FragColor = color2; + } + " + } + + width: 320 + height: 480 + + Item { + id: box + width: root.width * 0.9 + height: width + + Rectangle { + anchors.centerIn: parent + width: parent.width * 0.9 + height: parent.width * 0.4 + radius: width * 0.1; + gradient: Gradient { + GradientStop { position: 0; color: Qt.hsla(0.6, 0.9, 0.9, 1); } + GradientStop { position: 1; color: Qt.hsla(0.6, 0.6, 0.3, 1); } + } + RotationAnimator on rotation { from: 0; to: 360; duration: 10000; loops: Animation.Infinite } + } + + visible: false + layer.enabled: true + } + + Item { + id: text + width: box.width + height: width + Text { + anchors.centerIn: parent + color: "black" // Qt.hsla(0.8, 0.8, 0.8); + text: "Qt\nQuick" + + horizontalAlignment: Text.AlignHCenter + + font.bold: true + font.pixelSize: text.width * 0.25 + RotationAnimator on rotation { from: 360; to: 0; duration: 9000; loops: Animation.Infinite } + } + visible: false + layer.enabled: true + } + + XorBlender { + anchors.horizontalCenter: parent.horizontalCenter + y: root.height * 0.05; + width: box.width + height: box.height + source1: box + source2: text + } + + Rectangle { + id: labelFrame + anchors.margins: -10 + radius: 10 + color: "white" + border.color: "black" + opacity: 0.8 + anchors.fill: description + } + + Text { + id: description + anchors.left: parent.left + anchors.right: parent.right + anchors.bottom: parent.bottom + anchors.margins: 20 + wrapMode: Text.WordWrap + text: "This example creates two animated items and sets 'layer.enabled: true' on both of them. " + + "This turns the items into texture providers and we can access their texture from C++ in a custom material. " + + "The XorBlender is a custom C++ item which uses performs an Xor blend between them." + } +} diff --git a/examples/quick/scenegraph/twotextureproviders/twotextureproviders.pro b/examples/quick/scenegraph/twotextureproviders/twotextureproviders.pro new file mode 100644 index 0000000000..7b7c093b48 --- /dev/null +++ b/examples/quick/scenegraph/twotextureproviders/twotextureproviders.pro @@ -0,0 +1,12 @@ +QT += qml quick + +HEADERS += xorblender.h +SOURCES += xorblender.cpp main.cpp + +RESOURCES += twotextureproviders.qrc + +target.path = $$[QT_INSTALL_EXAMPLES]/quick/scenegraph/twotextureproviders +INSTALLS += target + +OTHER_FILES += \ + main.qml diff --git a/examples/quick/scenegraph/twotextureproviders/twotextureproviders.qrc b/examples/quick/scenegraph/twotextureproviders/twotextureproviders.qrc new file mode 100644 index 0000000000..1424333ce5 --- /dev/null +++ b/examples/quick/scenegraph/twotextureproviders/twotextureproviders.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/scenegraph/twotextureproviders"> + <file>main.qml</file> + </qresource> +</RCC> diff --git a/examples/quick/scenegraph/twotextureproviders/xorblender.cpp b/examples/quick/scenegraph/twotextureproviders/xorblender.cpp new file mode 100644 index 0000000000..c46a99ea67 --- /dev/null +++ b/examples/quick/scenegraph/twotextureproviders/xorblender.cpp @@ -0,0 +1,272 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Gunnar Sletta <gunnar@sletta.org> +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "xorblender.h" + +#include <QtCore/QPointer> + +#include <QtGui/QOpenGLContext> +#include <QtGui/QOpenGLFunctions> + +#include <QtQuick/QSGSimpleMaterial> +#include <QtQuick/QSGTexture> +#include <QtQuick/QSGGeometryNode> +#include <QtQuick/QSGTextureProvider> + +/* This example could just as well have been implemented all in QML using + * a ShaderEffect, and for 90% of all usecases, using a ShaderEffect will + * be sufficient. This example exists to illustrate how to consume + * texture providers from C++ and how to use multiple texture sources in + * a custom material. + */ + +struct XorBlendState { + QSGTexture *texture1; + QSGTexture *texture2; +}; + +class XorBlendShader : public QSGSimpleMaterialShader<XorBlendState> +{ + QSG_DECLARE_SIMPLE_SHADER(XorBlendShader, XorBlendState) +public: + + const char *vertexShader() const { + return + "attribute highp vec4 aVertex; \n" + "attribute highp vec2 aTexCoord; \n" + "uniform highp mat4 qt_Matrix; \n" + "varying highp vec2 vTexCoord; \n" + "void main() { \n" + " gl_Position = qt_Matrix * aVertex; \n" + " vTexCoord = aTexCoord; \n" + "}"; + } + + const char *fragmentShader() const { + return + "uniform lowp float qt_Opacity; \n" + "uniform lowp sampler2D uSource1; \n" + "uniform lowp sampler2D uSource2; \n" + "varying highp vec2 vTexCoord; \n" + "void main() { \n" + " lowp vec4 p1 = texture2D(uSource1, vTexCoord); \n" + " lowp vec4 p2 = texture2D(uSource2, vTexCoord); \n" + " gl_FragColor = (p1 * (1.0 - p2.a) + p2 * (1.0 - p1.a)) * qt_Opacity; \n" + "}"; + } + + QList<QByteArray> attributes() const { + return QList<QByteArray>() << "aVertex" << "aTexCoord"; + } + + void updateState(const XorBlendState *state, const XorBlendState *) { + QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); + // We bind the textures in inverse order so that we leave the updateState + // function with GL_TEXTURE0 as the active texture unit. This is maintain + // the "contract" that updateState should not mess up the GL state beyond + // what is needed for this material. + f->glActiveTexture(GL_TEXTURE1); + state->texture2->bind(); + f->glActiveTexture(GL_TEXTURE0); + state->texture1->bind(); + } + + void resolveUniforms() { + // The texture units never change, only the texturess we bind to them so + // we set these once and for all here. + program()->setUniformValue("uSource1", 0); // GL_TEXTURE0 + program()->setUniformValue("uSource2", 1); // GL_TEXTURE1 + } +}; + +/* The rendering is split into two nodes. The top-most node is not actually + * rendering anything, but is responsible for managing the texture providers. + * The XorNode also has a geometry node internally which it uses to render + * the texture providers using the XorBlendShader when all providers and + * textures are all present. + * + * The texture providers are updated solely on the render thread (when rendering + * is happening on a separate thread). This is why we are using preprocess + * and direct signals between the the texture providers and the node rather + * than updating state in updatePaintNode. + */ +class XorNode : public QObject, public QSGNode +{ + Q_OBJECT +public: + XorNode(QSGTextureProvider *p1, QSGTextureProvider *p2) + : m_provider1(p1) + , m_provider2(p2) + { + setFlag(QSGNode::UsePreprocess, true); + + // Set up material so it is all set for later.. + m_material = XorBlendShader::createMaterial(); + m_material->state()->texture1 = 0; + m_material->state()->texture2 = 0; + m_material->setFlag(QSGMaterial::Blending); + m_node.setMaterial(m_material); + m_node.setFlag(QSGNode::OwnsMaterial); + + // Set up geometry, actual vertices will be initialized in updatePaintNode + m_node.setGeometry(new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4)); + m_node.setFlag(QSGNode::OwnsGeometry); + + // If this node is used as in a shader effect source, we need to propegate + // changes that will occur in this node outwards. + connect(m_provider1, SIGNAL(textureChanged()), this, SLOT(textureChange()), Qt::DirectConnection); + connect(m_provider2, SIGNAL(textureChanged()), this, SLOT(textureChange()), Qt::DirectConnection); + } + + void preprocess() { + XorBlendState *state = m_material->state(); + // Update the textures from the providers, calling into QSGDynamicTexture if required + if (m_provider1) { + state->texture1 = m_provider1->texture(); + if (QSGDynamicTexture *dt1 = qobject_cast<QSGDynamicTexture *>(state->texture1)) + dt1->updateTexture(); + } + if (m_provider2) { + state->texture2 = m_provider2->texture(); + if (QSGDynamicTexture *dt2 = qobject_cast<QSGDynamicTexture *>(state->texture2)) + dt2->updateTexture(); + } + + // Remove node from the scene graph if it is there and either texture is missing... + if (m_node.parent() && (!state->texture1 || !state->texture2)) + removeChildNode(&m_node); + + // Add it if it is not already there and both textures are present.. + else if (!m_node.parent() && state->texture1 && state->texture2) + appendChildNode(&m_node); + } + + void setRect(const QRectF &rect) { + // Update geometry if it has changed and mark the change in the scene graph. + if (m_rect != rect) { + m_rect = rect; + QSGGeometry::updateTexturedRectGeometry(m_node.geometry(), m_rect, QRectF(0, 0, 1, 1)); + m_node.markDirty(QSGNode::DirtyGeometry); + } + } + +public slots: + void textureChange() { + // When our sources change, we will look different, so signal the change to the + // scene graph. + markDirty(QSGNode::DirtyMaterial); + } + +private: + QRectF m_rect; + QSGSimpleMaterial<XorBlendState> *m_material; + QSGGeometryNode m_node; + QPointer<QSGTextureProvider> m_provider1; + QPointer<QSGTextureProvider> m_provider2; +}; + +XorBlender::XorBlender(QQuickItem *parent) + : QQuickItem(parent) + , m_source1(0) + , m_source2(0) + , m_source1Changed(false) + , m_source2Changed(false) +{ + setFlag(ItemHasContents, true); +} + +void XorBlender::setSource1(QQuickItem *i) +{ + if (i == m_source1) + return; + m_source1 = i; + emit source1Changed(m_source1); + m_source1Changed = true; + update(); +} + +void XorBlender::setSource2(QQuickItem *i) +{ + if (i == m_source2) + return; + m_source2 = i; + emit source2Changed(m_source2); + m_source2Changed = true; + update(); +} + +QSGNode *XorBlender::updatePaintNode(QSGNode *old, UpdatePaintNodeData *) +{ + // Check if our input is valid and abort if not, deleting the old node. + bool abort = false; + if (!m_source1 || !m_source1->isTextureProvider()) { + qDebug() << "source1 is missing or not a texture provider"; + abort = true; + } + if (!m_source2 || !m_source2->isTextureProvider()) { + qDebug() << "source2 is missing or not a texture provider"; + abort = true; + } + if (abort) { + delete old; + return 0; + } + + XorNode *node = static_cast<XorNode *>(old); + + // If the sources have changed, recreate the nodes + if (m_source1Changed || m_source2Changed) { + delete node; + node = 0; + m_source1Changed = false; + m_source2Changed = false; + } + + // Create a new XorNode for us to render with. + if (!node) + node = new XorNode(m_source1->textureProvider(), m_source2->textureProvider()); + + // Update the geometry of the node to match the new bounding rect + node->setRect(boundingRect()); + + return node; +} + +#include "xorblender.moc" diff --git a/examples/quick/scenegraph/twotextureproviders/xorblender.h b/examples/quick/scenegraph/twotextureproviders/xorblender.h new file mode 100644 index 0000000000..d82352da15 --- /dev/null +++ b/examples/quick/scenegraph/twotextureproviders/xorblender.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Gunnar Sletta <gunnar@sletta.org> +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef XORBLENDER_H +#define XORBLENDER_H + +#include <QQuickItem> + +class XorBlender : public QQuickItem +{ + Q_OBJECT + Q_PROPERTY(QQuickItem *source1 READ source1 WRITE setSource1 NOTIFY source1Changed) + Q_PROPERTY(QQuickItem *source2 READ source2 WRITE setSource2 NOTIFY source2Changed) + +public: + explicit XorBlender(QQuickItem *parent = 0); + + QQuickItem *source1() const { return m_source1; } + QQuickItem *source2() const { return m_source2; } + + void setSource1(QQuickItem *i); + void setSource2(QQuickItem *i); + +protected: + QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *); + +signals: + void source1Changed(QQuickItem *item); + void source2Changed(QQuickItem *item); + +private: + QQuickItem *m_source1; + QQuickItem *m_source2; + + bool m_source1Changed; + bool m_source2Changed; +}; + +#endif // XORBLENDER_H diff --git a/src/imports/statemachine/signaltransition.cpp b/src/imports/statemachine/signaltransition.cpp index 9b64550678..634ec5a43b 100644 --- a/src/imports/statemachine/signaltransition.cpp +++ b/src/imports/statemachine/signaltransition.cpp @@ -38,6 +38,7 @@ #include <QQmlInfo> #include <QQmlEngine> #include <QQmlContext> +#include <QQmlExpression> #include <private/qv4qobjectwrapper_p.h> #include <private/qv8engine_p.h> @@ -46,7 +47,6 @@ SignalTransition::SignalTransition(QState *parent) : QSignalTransition(this, SIGNAL(invokeYourself()), parent) - , m_guard(true) { connect(this, SIGNAL(signalChanged()), SIGNAL(qmlSignalChanged())); } @@ -57,7 +57,23 @@ bool SignalTransition::eventTest(QEvent *event) if (!QSignalTransition::eventTest(event)) return false; - return m_guard; + if (m_guard.isEmpty()) + return true; + + QQmlContext context(QQmlEngine::contextForObject(this)); + + QStateMachine::SignalEvent *e = static_cast<QStateMachine::SignalEvent*>(event); + + // Set arguments as context properties + int count = e->arguments().count(); + QMetaMethod metaMethod = e->sender()->metaObject()->method(e->signalIndex()); + for (int i = 0; i < count; i++) + context.setContextProperty(metaMethod.parameterNames()[i], QVariant::fromValue(e->arguments().at(i))); + + QQmlExpression expr(m_guard, &context, this); + QVariant result = expr.evaluate(); + + return result.toBool(); } const QJSValue& SignalTransition::signal() @@ -89,17 +105,18 @@ void SignalTransition::setSignal(const QJSValue &signal) QSignalTransition::setSignal(metaMethod.methodSignature()); } -bool SignalTransition::guard() const +QQmlScriptString SignalTransition::guard() const { return m_guard; } -void SignalTransition::setGuard(bool guard) +void SignalTransition::setGuard(const QQmlScriptString &guard) { - if (guard != m_guard) { - m_guard = guard; - emit guardChanged(); - } + if (m_guard == guard) + return; + + m_guard = guard; + emit guardChanged(); } void SignalTransition::invoke() @@ -227,4 +244,12 @@ void SignalTransition::invoke() Guard conditions affect the behavior of a state machine by enabling transitions only when they evaluate to true and disabling them when they evaluate to false. + + When the signal associated with this signal transition is emitted the + guard condition is evaluated. In the guard condition the arguments + of the signal can be used as demonstrated in the example below. + + \snippet qml/statemachine/guardcondition.qml document + + \sa signal */ diff --git a/src/imports/statemachine/signaltransition.h b/src/imports/statemachine/signaltransition.h index 311195e8e9..3ac563b15e 100644 --- a/src/imports/statemachine/signaltransition.h +++ b/src/imports/statemachine/signaltransition.h @@ -38,19 +38,21 @@ #include <QtCore/QVariant> #include <QtQml/QJSValue> +#include <QtQml/qqmlscriptstring.h> + QT_BEGIN_NAMESPACE class SignalTransition : public QSignalTransition { Q_OBJECT Q_PROPERTY(QJSValue signal READ signal WRITE setSignal NOTIFY qmlSignalChanged) - Q_PROPERTY(bool guard READ guard WRITE setGuard NOTIFY guardChanged) + Q_PROPERTY(QQmlScriptString guard READ guard WRITE setGuard NOTIFY guardChanged) public: explicit SignalTransition(QState *parent = Q_NULLPTR); - bool guard() const; - void setGuard(bool guard); + QQmlScriptString guard() const; + void setGuard(const QQmlScriptString &guard); bool eventTest(QEvent *event); @@ -70,7 +72,7 @@ Q_SIGNALS: private: QByteArray m_data; QJSValue m_signal; - bool m_guard; + QQmlScriptString m_guard; }; QT_END_NAMESPACE diff --git a/src/qml/compiler/qv4ssa.cpp b/src/qml/compiler/qv4ssa.cpp index 7ea128d6ea..774d8fb88c 100644 --- a/src/qml/compiler/qv4ssa.cpp +++ b/src/qml/compiler/qv4ssa.cpp @@ -2212,10 +2212,14 @@ class TypeInference: public StmtVisitor, public ExprVisitor DiscoveredType type; bool fullyTyped; - TypingResult(const DiscoveredType &type = DiscoveredType()) - : type(type) - , fullyTyped(type.type != UnknownType) - {} + TypingResult(const DiscoveredType &type = DiscoveredType()) { +#if defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ == 6 + // avoid optimization bug in gcc 4.6.3 armhf + ((int volatile &) this->type.type) = type.type; +#endif + this->type = type; + fullyTyped = type.type != UnknownType; + } explicit TypingResult(MemberExpressionResolver memberResolver) : type(memberResolver) , fullyTyped(true) diff --git a/src/qml/doc/snippets/qml/statemachine/guardcondition.qml b/src/qml/doc/snippets/qml/statemachine/guardcondition.qml new file mode 100644 index 0000000000..f9ecf450bd --- /dev/null +++ b/src/qml/doc/snippets/qml/statemachine/guardcondition.qml @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Ford Motor Company +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [document] +import QtQml.StateMachine 1.0 +import QtQuick 2.0 + +Rectangle { + Button { + anchors.fill: parent + id: button + StateMachine { + StateBase { + SignalTransition { + targetState: finalState + signal: button.mysignal + // the guard condition uses the mystr string argument from mysignal + guard: mystr == "test" + } + } + FinalState { + id: finalState + } + } + // define the signal the SignalTransition is connected with + signal mysignal(string mystr) + // on clicking the button emit the signal with a single string argument + onClicked: button.mysignal("test") + } +} +//! [document] + diff --git a/src/qml/qml/qqmlscriptstring.cpp b/src/qml/qml/qqmlscriptstring.cpp index b18bd4d420..c8227070c5 100644 --- a/src/qml/qml/qqmlscriptstring.cpp +++ b/src/qml/qml/qqmlscriptstring.cpp @@ -114,6 +114,44 @@ QQmlScriptString &QQmlScriptString::operator=(const QQmlScriptString &other) } /*! +Returns \c true if this and the \a other QQmlScriptString objects are equal. + +\sa operator!=() +*/ +bool QQmlScriptString::operator==(const QQmlScriptString &other) const +{ + if (d == other.d) + return true; + + if (d->isNumberLiteral || other.d->isNumberLiteral) + return d->isNumberLiteral && other.d->isNumberLiteral && d->numberValue == other.d->numberValue; + + if (d->isStringLiteral || other.d->isStringLiteral) + return d->isStringLiteral && other.d->isStringLiteral && d->script == other.d->script; + + if (d->script == QStringLiteral("true") || + d->script == QStringLiteral("false") || + d->script == QStringLiteral("undefined") || + d->script == QStringLiteral("null")) + return d->script == other.d->script; + + return d->context == other.d->context && + d->scope == other.d->scope && + d->script == other.d->script && + d->bindingId == other.d->bindingId; +} + +/*! +Returns \c true if this and the \a other QQmlScriptString objects are different. + +\sa operator==() +*/ +bool QQmlScriptString::operator!=(const QQmlScriptString &other) const +{ + return !operator==(other); +} + +/*! Returns whether the QQmlScriptString is empty. */ bool QQmlScriptString::isEmpty() const diff --git a/src/qml/qml/qqmlscriptstring.h b/src/qml/qml/qqmlscriptstring.h index 1ff8903993..ccbe90535b 100644 --- a/src/qml/qml/qqmlscriptstring.h +++ b/src/qml/qml/qqmlscriptstring.h @@ -58,6 +58,9 @@ public: QQmlScriptString &operator=(const QQmlScriptString &); + bool operator==(const QQmlScriptString &) const; + bool operator!=(const QQmlScriptString &) const; + bool isEmpty() const; bool isUndefinedLiteral() const; diff --git a/src/quick/items/qquickitem.cpp b/src/quick/items/qquickitem.cpp index 7b031f3b5b..b6939ddaba 100644 --- a/src/quick/items/qquickitem.cpp +++ b/src/quick/items/qquickitem.cpp @@ -2161,6 +2161,17 @@ QQuickItem::~QQuickItem() d->changeListeners.clear(); + /* + Remove any references our transforms have to us, in case they try to + remove themselves from our list of transforms when that list has already + been destroyed after ~QQuickItem() has run. + */ + for (int ii = 0; ii < d->transforms.count(); ++ii) { + QQuickTransform *t = d->transforms.at(ii); + QQuickTransformPrivate *tp = QQuickTransformPrivate::get(t); + tp->items.removeOne(this); + } + if (d->extra.isAllocated()) { delete d->extra->contents; d->extra->contents = 0; delete d->extra->layer; d->extra->layer = 0; @@ -5166,6 +5177,8 @@ void QQuickItem::setScale(qreal s) rectangle has specified an opacity of 0.5, which affects the opacity of its blue child rectangle even though the child has not specified an opacity. + Values outside the range of 0 to 1 will be clamped. + \table \row \li \image declarative-item_opacity1.png @@ -5213,9 +5226,10 @@ qreal QQuickItem::opacity() const return d->opacity(); } -void QQuickItem::setOpacity(qreal o) +void QQuickItem::setOpacity(qreal newOpacity) { Q_D(QQuickItem); + qreal o = qBound<qreal>(0, newOpacity, 1); if (d->opacity() == o) return; diff --git a/src/quick/scenegraph/util/qsgtextureprovider.cpp b/src/quick/scenegraph/util/qsgtextureprovider.cpp index 05bc75d7a5..4a43f7149f 100644 --- a/src/quick/scenegraph/util/qsgtextureprovider.cpp +++ b/src/quick/scenegraph/util/qsgtextureprovider.cpp @@ -40,6 +40,8 @@ QT_BEGIN_NAMESPACE \brief The QSGTextureProvider class encapsulates texture based entities in QML. The QSGTextureProvider lives primarily in the scene graph rendering thread. + + \sa {Scene Graph - Two Texture Providers} */ diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index 363dd0244e..d6d2911285 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -140,6 +140,7 @@ private slots: void scriptString(); void scriptStringJs(); void scriptStringWithoutSourceCode(); + void scriptStringComparison(); void defaultPropertyListOrder(); void declaredPropertyValues(); void dontDoubleCallClassBegin(); @@ -2067,6 +2068,87 @@ void tst_qqmllanguage::scriptStringWithoutSourceCode() } } +// Test the QQmlScriptString comparison operators. The script strings are considered +// equal if there evaluation would produce the same result. +void tst_qqmllanguage::scriptStringComparison() +{ + QQmlComponent component1(&engine, testFileUrl("scriptString.qml")); + QVERIFY(!component1.isError() && component1.errors().isEmpty()); + MyTypeObject *object1 = qobject_cast<MyTypeObject*>(component1.create()); + QVERIFY(object1 != 0); + + QQmlComponent component2(&engine, testFileUrl("scriptString2.qml")); + QVERIFY(!component2.isError() && component2.errors().isEmpty()); + MyTypeObject *object2 = qobject_cast<MyTypeObject*>(component2.create()); + QVERIFY(object2 != 0); + + QQmlComponent component3(&engine, testFileUrl("scriptString3.qml")); + QVERIFY(!component3.isError() && component3.errors().isEmpty()); + MyTypeObject *object3 = qobject_cast<MyTypeObject*>(component3.create()); + QVERIFY(object3 != 0); + + //QJSValue inst1 = engine.newQObject(object1); + QJSValue inst2 = engine.newQObject(object2); + QJSValue inst3 = engine.newQObject(object3); + QJSValue func = engine.evaluate("function(value) { this.scriptProperty = value }"); + + const QString s = "hello\\n\\\"world\\\""; + const qreal n = 12.345; + bool ok; + + QVERIFY(object2->scriptProperty().stringLiteral() == s); + QVERIFY(object3->scriptProperty().numberLiteral(&ok) == n && ok); + QVERIFY(object1->scriptProperty() == object1->scriptProperty()); + QVERIFY(object2->scriptProperty() == object2->scriptProperty()); + QVERIFY(object3->scriptProperty() == object3->scriptProperty()); + QVERIFY(object2->scriptProperty() != object3->scriptProperty()); + QVERIFY(object1->scriptProperty() != object2->scriptProperty()); + QVERIFY(object1->scriptProperty() != object3->scriptProperty()); + + func.callWithInstance(inst2, QJSValueList() << n); + QVERIFY(object2->scriptProperty() == object3->scriptProperty()); + + func.callWithInstance(inst2, QJSValueList() << s); + QVERIFY(object2->scriptProperty() != object3->scriptProperty()); + func.callWithInstance(inst3, QJSValueList() << s); + QVERIFY(object2->scriptProperty() == object3->scriptProperty()); + + func.callWithInstance(inst2, QJSValueList() << QJSValue::UndefinedValue); + QVERIFY(object2->scriptProperty() != object3->scriptProperty()); + func.callWithInstance(inst3, QJSValueList() << QJSValue::UndefinedValue); + QVERIFY(object2->scriptProperty() == object3->scriptProperty()); + + func.callWithInstance(inst2, QJSValueList() << QJSValue::NullValue); + QVERIFY(object2->scriptProperty() != object3->scriptProperty()); + func.callWithInstance(inst3, QJSValueList() << QJSValue::NullValue); + QVERIFY(object2->scriptProperty() == object3->scriptProperty()); + + func.callWithInstance(inst2, QJSValueList() << false); + QVERIFY(object2->scriptProperty() != object3->scriptProperty()); + func.callWithInstance(inst3, QJSValueList() << false); + QVERIFY(object2->scriptProperty() == object3->scriptProperty()); + + func.callWithInstance(inst2, QJSValueList() << true); + QVERIFY(object2->scriptProperty() != object3->scriptProperty()); + func.callWithInstance(inst3, QJSValueList() << true); + QVERIFY(object2->scriptProperty() == object3->scriptProperty()); + + QVERIFY(object1->scriptProperty() != object2->scriptProperty()); + object2->setScriptProperty(object1->scriptProperty()); + QVERIFY(object1->scriptProperty() == object2->scriptProperty()); + + QVERIFY(object1->scriptProperty() != object3->scriptProperty()); + func.callWithInstance(inst3, QJSValueList() << engine.toScriptValue(object1->scriptProperty())); + QVERIFY(object1->scriptProperty() == object3->scriptProperty()); + + // While this are two instances of the same object they are still considered different + // because the (none literal) script string may access variables which have different + // values in both instances and hence evaluated to different results. + MyTypeObject *object1_2 = qobject_cast<MyTypeObject*>(component1.create()); + QVERIFY(object1_2 != 0); + QVERIFY(object1->scriptProperty() != object1_2->scriptProperty()); +} + // Check that default property assignments are correctly spliced into explicit // property assignments void tst_qqmllanguage::defaultPropertyListOrder() diff --git a/tests/auto/qmltest/statemachine/tst_guardcondition.qml b/tests/auto/qmltest/statemachine/tst_guardcondition.qml new file mode 100644 index 0000000000..71171b2079 --- /dev/null +++ b/tests/auto/qmltest/statemachine/tst_guardcondition.qml @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Ford Motor Company +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 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 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQml.StateMachine 1.0 +import QtTest 1.0 + +TestCase { + id: testCase + StateMachine { + id: machine + initialState: startState + StateBase { + id: startState + SignalTransition { + id: signalTrans + signal: testCase.mysignal + guard: mystr == "test1" + targetState: finalState + } + } + FinalState { + id: finalState + } + } + + SignalSpy { + id: finalStateActive + target: finalState + signalName: "activeChanged" + } + + signal mysignal(string mystr, bool mybool, int myint) + + name: "testGuardCondition" + function test_guardCondition() + { + // Start statemachine, should not have reached finalState yet. + machine.start() + tryCompare(finalStateActive, "count", 0) + tryCompare(machine, "running", true) + + // Emit the signalTrans.signal which will evaluate the guard. The + // guard should return true, finalState be reached and the + // statemachine be stopped. + testCase.mysignal("test1", true, 2) + tryCompare(finalStateActive, "count", 1) + tryCompare(machine, "running", false) + + // Restart machine. + machine.start() + tryCompare(machine, "running", true) + tryCompare(finalStateActive, "count", 2) + + // Emit signal that makes the signalTrans.guard return false. The + // finalState should not have been triggered. + testCase.mysignal("test2", true, 2) + tryCompare(finalStateActive, "count", 2) + tryCompare(machine, "running", true) + + // Change the guard in javascript to test that boolean true/false + // works as expected. + signalTrans.guard = false; + testCase.mysignal("test1", true, 2) + tryCompare(finalStateActive, "count", 2) + tryCompare(machine, "running", true) + signalTrans.guard = true; + testCase.mysignal("test1", true, 2) + tryCompare(finalStateActive, "count", 3) + tryCompare(machine, "running", false) + } +} diff --git a/tests/auto/quick/qquickitem/data/objectChildTransform.qml b/tests/auto/quick/qquickitem/data/objectChildTransform.qml new file mode 100644 index 0000000000..fecb9ac921 --- /dev/null +++ b/tests/auto/quick/qquickitem/data/objectChildTransform.qml @@ -0,0 +1,31 @@ +import QtQuick 2.3 + +Rectangle { + width: 360 + height: 360 + + property alias loaderSource: loader.source + + Component { + id: crashComponent + + Item { + Image { + transform: scale + } + + Scale { + id: scale + xScale: 1 + yScale: 1 + } + } + } + + Loader { + id: loader + anchors.fill: parent + sourceComponent: crashComponent + } +} + diff --git a/tests/auto/quick/qquickitem/tst_qquickitem.cpp b/tests/auto/quick/qquickitem/tst_qquickitem.cpp index a689887977..38dc8e0ac4 100644 --- a/tests/auto/quick/qquickitem/tst_qquickitem.cpp +++ b/tests/auto/quick/qquickitem/tst_qquickitem.cpp @@ -167,6 +167,8 @@ private slots: void testSGInitializeAndInvalidate(); + void objectChildTransform(); + private: enum PaintOrderOp { @@ -1906,7 +1908,18 @@ void tst_qquickitem::testSGInitializeAndInvalidate() delete view.take(); QCOMPARE(invalidateSpy.size(), expected); } +} + +void tst_qquickitem::objectChildTransform() +{ + QQuickView view; + view.setSource(testFileUrl("objectChildTransform.qml")); + + QQuickItem *root = qobject_cast<QQuickItem*>(view.rootObject()); + QVERIFY(root); + root->setProperty("source", QString()); + // Shouldn't crash. } QTEST_MAIN(tst_qquickitem) |