From f39c22ecec295d44eb1604e0a5dd5400093f0322 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Tue, 3 Jul 2012 15:04:19 +1000 Subject: [PATCH] Allow resetting a Rectangle gradient. 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 --- src/quick/items/qquickrectangle.cpp | 24 ++---- src/quick/items/qquickrectangle_p.h | 6 +- tests/auto/quick/qquickrectangle/data/gradient.qml | 14 ++++ .../auto/quick/qquickrectangle/qquickrectangle.pro | 12 +++ .../quick/qquickrectangle/tst_qquickrectangle.cpp | 93 ++++++++++++++++++++++ tests/auto/quick/quick.pro | 1 + 6 files changed, 128 insertions(+), 22 deletions(-) create mode 100644 tests/auto/quick/qquickrectangle/data/gradient.qml create mode 100644 tests/auto/quick/qquickrectangle/qquickrectangle.pro create mode 100644 tests/auto/quick/qquickrectangle/tst_qquickrectangle.cpp diff --git a/src/quick/items/qquickrectangle.cpp b/src/quick/items/qquickrectangle.cpp index 3137ba9..608fd94 100644 --- a/src/quick/items/qquickrectangle.cpp +++ b/src/quick/items/qquickrectangle.cpp @@ -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 QQuickGradient::stops() @@ -254,24 +253,8 @@ QQmlListProperty QQuickGradient::stops() return QQmlListProperty(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. diff --git a/src/quick/items/qquickrectangle_p.h b/src/quick/items/qquickrectangle_p.h index f077e7f..0ac2ea6 100644 --- a/src/quick/items/qquickrectangle_p.h +++ b/src/quick/items/qquickrectangle_p.h @@ -120,8 +120,6 @@ public: QQmlListProperty stops(); - const QGradient *gradient() const; - Q_SIGNALS: void updated(); @@ -130,7 +128,6 @@ private: private: QList 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 index 0000000..6d2a3c4 --- /dev/null +++ b/tests/auto/quick/qquickrectangle/data/gradient.qml @@ -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 index 0000000..daefb5f --- /dev/null +++ b/tests/auto/quick/qquickrectangle/qquickrectangle.pro @@ -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 index 0000000..dd667c2 --- /dev/null +++ b/tests/auto/quick/qquickrectangle/tst_qquickrectangle.cpp @@ -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 + +#include +#include +#include + +#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(component.create()); + QVERIFY(rect); + + QQuickGradient *grad = rect->gradient(); + QVERIFY(grad); + + QQmlListProperty 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" diff --git a/tests/auto/quick/quick.pro b/tests/auto/quick/quick.pro index fd78da2..aed2ae9 100644 --- a/tests/auto/quick/quick.pro +++ b/tests/auto/quick/quick.pro @@ -60,6 +60,7 @@ QUICKTESTS = \ qquickpathview \ qquickpincharea \ qquickpositioners \ + qquickrectangle \ qquickrepeater \ qquickshadereffect \ qquickspritesequence \ -- 2.7.4