Allow resetting a Rectangle gradient.
authorMartin Jones <martin.jones@nokia.com>
Tue, 3 Jul 2012 05:04:19 +0000 (15:04 +1000)
committerQt by Nokia <qt-info@nokia.com>
Tue, 3 Jul 2012 05:27:22 +0000 (07:27 +0200)
If a gradient and a color are set on a rectangle, the gradient is used.
This means that if you wish to override the gradient on a component with
a color, you have to unset the gradient.

Also remove the unused QQuickGradient::gradient() method.

Task-number: QTBUG-23238
Change-Id: Ibd43cfe1bd4b867e4f6103f1d0dc0ed6176ab5c1
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
src/quick/items/qquickrectangle.cpp
src/quick/items/qquickrectangle_p.h
tests/auto/quick/qquickrectangle/data/gradient.qml [new file with mode: 0644]
tests/auto/quick/qquickrectangle/qquickrectangle.pro [new file with mode: 0644]
tests/auto/quick/qquickrectangle/tst_qquickrectangle.cpp [new file with mode: 0644]
tests/auto/quick/quick.pro

index 3137ba9..608fd94 100644 (file)
@@ -240,13 +240,12 @@ void QQuickGradientStop::updateGradient()
     To set the gradient stops, define them as children of the Gradient element.
 */
 QQuickGradient::QQuickGradient(QObject *parent)
-: QObject(parent), m_gradient(0)
+: QObject(parent)
 {
 }
 
 QQuickGradient::~QQuickGradient()
 {
-    delete m_gradient;
 }
 
 QQmlListProperty<QQuickGradientStop> QQuickGradient::stops()
@@ -254,24 +253,8 @@ QQmlListProperty<QQuickGradientStop> QQuickGradient::stops()
     return QQmlListProperty<QQuickGradientStop>(this, m_stops);
 }
 
-const QGradient *QQuickGradient::gradient() const
-{
-    if (!m_gradient && !m_stops.isEmpty()) {
-        m_gradient = new QLinearGradient(0,0,0,1.0);
-        for (int i = 0; i < m_stops.count(); ++i) {
-            const QQuickGradientStop *stop = m_stops.at(i);
-            m_gradient->setCoordinateMode(QGradient::ObjectBoundingMode);
-            m_gradient->setColorAt(stop->position(), stop->color());
-        }
-    }
-
-    return m_gradient;
-}
-
 void QQuickGradient::doUpdate()
 {
-    delete m_gradient;
-    m_gradient = 0;
     emit updated();
 }
 
@@ -426,6 +409,11 @@ void QQuickRectangle::setGradient(QQuickGradient *gradient)
     update();
 }
 
+void QQuickRectangle::resetGradient()
+{
+    setGradient(0);
+}
+
 /*!
     \qmlproperty real QtQuick2::Rectangle::radius
     This property holds the corner radius used to draw a rounded rectangle.
index f077e7f..0ac2ea6 100644 (file)
@@ -120,8 +120,6 @@ public:
 
     QQmlListProperty<QQuickGradientStop> stops();
 
-    const QGradient *gradient() const;
-
 Q_SIGNALS:
     void updated();
 
@@ -130,7 +128,6 @@ private:
 
 private:
     QList<QQuickGradientStop *> m_stops;
-    mutable QGradient *m_gradient;
     friend class QQuickRectangle;
     friend class QQuickGradientStop;
 };
@@ -141,7 +138,7 @@ class Q_AUTOTEST_EXPORT QQuickRectangle : public QQuickItem
     Q_OBJECT
 
     Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
-    Q_PROPERTY(QQuickGradient *gradient READ gradient WRITE setGradient)
+    Q_PROPERTY(QQuickGradient *gradient READ gradient WRITE setGradient RESET resetGradient)
     Q_PROPERTY(QQuickPen * border READ border CONSTANT)
     Q_PROPERTY(qreal radius READ radius WRITE setRadius NOTIFY radiusChanged)
 public:
@@ -154,6 +151,7 @@ public:
 
     QQuickGradient *gradient() const;
     void setGradient(QQuickGradient *gradient);
+    void resetGradient();
 
     qreal radius() const;
     void setRadius(qreal radius);
diff --git a/tests/auto/quick/qquickrectangle/data/gradient.qml b/tests/auto/quick/qquickrectangle/data/gradient.qml
new file mode 100644 (file)
index 0000000..6d2a3c4
--- /dev/null
@@ -0,0 +1,14 @@
+import QtQuick 2.0
+
+Rectangle {
+
+    function resetGradient() {
+        gradient = undefined
+    }
+
+    gradient: Gradient {
+        GradientStop { position: 0.0; color: "gray" }
+        GradientStop { position: 1.0; color: "white" }
+    }
+}
+
diff --git a/tests/auto/quick/qquickrectangle/qquickrectangle.pro b/tests/auto/quick/qquickrectangle/qquickrectangle.pro
new file mode 100644 (file)
index 0000000..daefb5f
--- /dev/null
@@ -0,0 +1,12 @@
+CONFIG += testcase
+TARGET = tst_qquickrectangle
+macx:CONFIG -= app_bundle
+
+SOURCES += tst_qquickrectangle.cpp
+
+include (../../shared/util.pri)
+include (../shared/util.pri)
+
+TESTDATA = data/*
+
+QT += core-private gui-private qml-private quick-private testlib
diff --git a/tests/auto/quick/qquickrectangle/tst_qquickrectangle.cpp b/tests/auto/quick/qquickrectangle/tst_qquickrectangle.cpp
new file mode 100644 (file)
index 0000000..dd667c2
--- /dev/null
@@ -0,0 +1,93 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** 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, Nokia gives you certain additional
+** rights. These rights are described in the Nokia 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.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+#include <qtest.h>
+
+#include <QtQml/qqmlengine.h>
+#include <QtQml/qqmlcomponent.h>
+#include <private/qquickrectangle_p.h>
+
+#include "../../shared/util.h"
+
+class tst_qquickrectangle : public QQmlDataTest
+{
+    Q_OBJECT
+public:
+    tst_qquickrectangle();
+
+private slots:
+    void gradient();
+
+private:
+    QQmlEngine engine;
+};
+
+tst_qquickrectangle::tst_qquickrectangle()
+{
+}
+
+void tst_qquickrectangle::gradient()
+{
+    QQmlComponent component(&engine, testFileUrl("gradient.qml"));
+    QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(component.create());
+    QVERIFY(rect);
+
+    QQuickGradient *grad = rect->gradient();
+    QVERIFY(grad);
+
+    QQmlListProperty<QQuickGradientStop> stops = grad->stops();
+    QCOMPARE(stops.count(&stops), 2);
+    QCOMPARE(stops.at(&stops, 0)->position(), 0.0);
+    QCOMPARE(stops.at(&stops, 0)->color(), QColor("gray"));
+    QCOMPARE(stops.at(&stops, 1)->position(), 1.0);
+    QCOMPARE(stops.at(&stops, 1)->color(), QColor("white"));
+
+    QMetaObject::invokeMethod(rect, "resetGradient");
+
+    grad = rect->gradient();
+    QVERIFY(!grad);
+
+    delete rect;
+}
+
+
+QTEST_MAIN(tst_qquickrectangle)
+
+#include "tst_qquickrectangle.moc"
index fd78da2..aed2ae9 100644 (file)
@@ -60,6 +60,7 @@ QUICKTESTS =  \
     qquickpathview \
     qquickpincharea \
     qquickpositioners \
+    qquickrectangle \
     qquickrepeater \
     qquickshadereffect \
     qquickspritesequence \