Allow Item components to be assigned to Item.layer.effect.
authorKim Motoyoshi Kalland <kim.kalland@nokia.com>
Tue, 17 Jan 2012 16:06:08 +0000 (17:06 +0100)
committerQt by Nokia <qt-info@nokia.com>
Tue, 7 Feb 2012 13:04:37 +0000 (14:04 +0100)
Some complex effects are easier to implement as Items using
ShaderEffects internally rather than with a top-level ShaderEffect.
Auto-tests added.

Change-Id: I4b99811b87e7ca5054bf119b99207b7f5a7c666e
Reviewed-by: Gunnar Sletta <gunnar.sletta@nokia.com>
src/quick/items/qquickitem.cpp
src/quick/items/qquickitem_p.h
tests/auto/qtquick2/qquickitemlayer/data/ItemEffect.qml [new file with mode: 0644]
tests/auto/qtquick2/qquickitemlayer/data/RectangleEffect.qml [new file with mode: 0644]
tests/auto/qtquick2/qquickitemlayer/qquickitemlayer.pro
tests/auto/qtquick2/qquickitemlayer/tst_qquickitemlayer.cpp

index 7195e8a..3e59bee 100644 (file)
@@ -5497,9 +5497,9 @@ void QQuickItemLayer::activateEffect()
     Q_ASSERT(!m_effect);
 
     QObject *created = m_effectComponent->create();
-    m_effect = qobject_cast<QQuickShaderEffect *>(created);
+    m_effect = qobject_cast<QQuickItem *>(created);
     if (!m_effect) {
-        qWarning("Item: layer.effect is not a ShaderEffect.");
+        qWarning("Item: layer.effect is not a QML Item.");
         delete created;
         return;
     }
@@ -5527,7 +5527,8 @@ void QQuickItemLayer::deactivateEffect()
 
     Holds the effect that is applied to this layer.
 
-    The effect must be a \l ShaderEffect.
+    The effect is typically a \l ShaderEffect component, although any \l Item component can be
+    assigned. The effect should have a source texture property with a name matching \l samplerName.
 
     \sa samplerName
  */
index 2383ae1..1f634be 100644 (file)
@@ -225,7 +225,7 @@ private:
     QRectF m_sourceRect;
     QString m_name;
     QDeclarativeComponent *m_effectComponent;
-    QQuickShaderEffect *m_effect;
+    QQuickItem *m_effect;
     QQuickShaderEffectSource *m_effectSource;
 };
 
diff --git a/tests/auto/qtquick2/qquickitemlayer/data/ItemEffect.qml b/tests/auto/qtquick2/qquickitemlayer/data/ItemEffect.qml
new file mode 100644 (file)
index 0000000..2f17d78
--- /dev/null
@@ -0,0 +1,23 @@
+import QtQuick 2.0
+
+Item {
+    width: 200
+    height: 200
+    Rectangle {
+        anchors.fill: parent
+        anchors.margins: 99
+        gradient: Gradient {
+            GradientStop { position: 0.3; color: "red" }
+            GradientStop { position: 0.7; color: "blue" }
+        }
+        layer.enabled: true
+        layer.effect: Item {
+            property variant source
+            ShaderEffect {
+                anchors.fill: parent
+                anchors.margins: -99
+                property variant source: parent.source
+            }
+        }
+    }
+}
diff --git a/tests/auto/qtquick2/qquickitemlayer/data/RectangleEffect.qml b/tests/auto/qtquick2/qquickitemlayer/data/RectangleEffect.qml
new file mode 100644 (file)
index 0000000..94c43f2
--- /dev/null
@@ -0,0 +1,22 @@
+import QtQuick 2.0
+
+Item {
+    width: 200
+    height: 200
+    Rectangle {
+        width: 100
+        height: 100
+        x: 50
+        y: 50
+        scale: 1.5
+        z: 1
+        rotation: 45
+        color: "#ff0000"
+        layer.enabled: true
+        layer.effect: Rectangle { color: "#0000ff" }
+    }
+    Rectangle {
+        anchors.fill: parent
+        color: "#00ff00"
+    }
+}
index 5c34dec..557e23d 100644 (file)
@@ -25,7 +25,9 @@ OTHER_FILES += \
     data/ZOrderChange.qml \
     data/ToggleLayerAndEffect.qml \
     data/DisableLayer.qml \
-    data/SamplerNameChange.qml
+    data/SamplerNameChange.qml \
+    data/ItemEffect.qml \
+    data/RectangleEffect.qml
 
 
 
index 1e55924..2a85670 100644 (file)
@@ -86,6 +86,8 @@ private slots:
     void toggleLayerAndEffect();
     void disableLayer();
     void changeSamplerName();
+    void itemEffect();
+    void rectangleEffect();
 
 private:
     bool m_isMesaSoftwareRasterizer;
@@ -403,6 +405,32 @@ void tst_QQuickItemLayer::changeSamplerName()
     QCOMPARE(fb.pixel(0, 0), qRgb(0, 0, 0xff));
 }
 
+void tst_QQuickItemLayer::itemEffect()
+{
+    if (m_isMesaSoftwareRasterizer && m_mesaVersion < QT_VERSION_CHECK(7, 11, 0))
+        QSKIP("Mesa Software Rasterizer below version 7.11 does not render this test correctly.");
+    QImage fb = runTest(testFile("ItemEffect.qml"));
+    QCOMPARE(fb.pixel(0, 0), qRgb(0xff, 0, 0));
+    QCOMPARE(fb.pixel(199, 0), qRgb(0xff, 0, 0));
+    QCOMPARE(fb.pixel(0, 199), qRgb(0, 0, 0xff));
+    QCOMPARE(fb.pixel(199, 199), qRgb(0, 0, 0xff));
+}
+
+void tst_QQuickItemLayer::rectangleEffect()
+{
+    QImage fb = runTest(testFile("RectangleEffect.qml"));
+    QCOMPARE(fb.pixel(0, 0), qRgb(0, 0xff, 0));
+    QCOMPARE(fb.pixel(199, 0), qRgb(0, 0xff, 0));
+    QCOMPARE(fb.pixel(0, 199), qRgb(0, 0xff, 0));
+    QCOMPARE(fb.pixel(199, 199), qRgb(0, 0xff, 0));
+
+    QCOMPARE(fb.pixel(100, 0), qRgb(0, 0, 0xff));
+    QCOMPARE(fb.pixel(199, 100), qRgb(0, 0, 0xff));
+    QCOMPARE(fb.pixel(100, 199), qRgb(0, 0, 0xff));
+    QCOMPARE(fb.pixel(0, 100), qRgb(0, 0, 0xff));
+}
+
+
 QTEST_MAIN(tst_QQuickItemLayer)
 
 #include "tst_qquickitemlayer.moc"