1 // Commit: acc903853d5ac54d646d324b7386c998bc07d464
2 /****************************************************************************
4 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5 ** All rights reserved.
6 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 ** This file is part of the QtDeclarative module of the Qt Toolkit.
10 ** $QT_BEGIN_LICENSE:LGPL$
11 ** No Commercial Usage
12 ** This file contains pre-release code and may not be distributed.
13 ** You may use this file in accordance with the terms and conditions
14 ** contained in the Technology Preview License Agreement accompanying
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
41 ****************************************************************************/
43 #include "qsgrectangle_p.h"
44 #include "qsgrectangle_p_p.h"
46 #include <private/qsgcontext_p.h>
47 #include <private/qsgadaptationlayer_p.h>
49 #include <QtGui/qpixmapcache.h>
50 #include <QtCore/qstringbuilder.h>
51 #include <QtCore/qmath.h>
55 // XXX todo - should we change rectangle to draw entirely within its width/height?
57 QSGPen::QSGPen(QObject *parent)
58 : QObject(parent), _width(1), _color("#000000"), _valid(false)
62 QColor QSGPen::color() const
67 void QSGPen::setColor(const QColor &c)
70 _valid = (_color.alpha() && _width >= 1) ? true : false;
74 int QSGPen::width() const
79 void QSGPen::setWidth(int w)
81 if (_width == w && _valid)
85 _valid = (_color.alpha() && _width >= 1) ? true : false;
89 bool QSGPen::isValid() const
94 QSGGradientStop::QSGGradientStop(QObject *parent)
99 qreal QSGGradientStop::position() const
104 void QSGGradientStop::setPosition(qreal position)
106 m_position = position; updateGradient();
109 QColor QSGGradientStop::color() const
114 void QSGGradientStop::setColor(const QColor &color)
116 m_color = color; updateGradient();
119 void QSGGradientStop::updateGradient()
121 if (QSGGradient *grad = qobject_cast<QSGGradient*>(parent()))
125 QSGGradient::QSGGradient(QObject *parent)
126 : QObject(parent), m_gradient(0)
130 QSGGradient::~QSGGradient()
135 QDeclarativeListProperty<QSGGradientStop> QSGGradient::stops()
137 return QDeclarativeListProperty<QSGGradientStop>(this, m_stops);
140 const QGradient *QSGGradient::gradient() const
142 if (!m_gradient && !m_stops.isEmpty()) {
143 m_gradient = new QLinearGradient(0,0,0,1.0);
144 for (int i = 0; i < m_stops.count(); ++i) {
145 const QSGGradientStop *stop = m_stops.at(i);
146 m_gradient->setCoordinateMode(QGradient::ObjectBoundingMode);
147 m_gradient->setColorAt(stop->position(), stop->color());
154 void QSGGradient::doUpdate()
161 int QSGRectanglePrivate::doUpdateSlotIdx = -1;
163 QSGRectangle::QSGRectangle(QSGItem *parent)
164 : QSGItem(*(new QSGRectanglePrivate), parent)
166 setFlag(ItemHasContents);
169 void QSGRectangle::doUpdate()
172 const int pw = d->pen && d->pen->isValid() ? d->pen->width() : 0;
173 d->setPaintMargin((pw+1)/2);
177 QSGPen *QSGRectangle::border()
183 QSGGradient *QSGRectangle::gradient() const
185 Q_D(const QSGRectangle);
189 void QSGRectangle::setGradient(QSGGradient *gradient)
192 if (d->gradient == gradient)
194 static int updatedSignalIdx = -1;
195 if (updatedSignalIdx < 0)
196 updatedSignalIdx = QSGGradient::staticMetaObject.indexOfSignal("updated()");
197 if (d->doUpdateSlotIdx < 0)
198 d->doUpdateSlotIdx = QSGRectangle::staticMetaObject.indexOfSlot("doUpdate()");
200 QMetaObject::disconnect(d->gradient, updatedSignalIdx, this, d->doUpdateSlotIdx);
201 d->gradient = gradient;
203 QMetaObject::connect(d->gradient, updatedSignalIdx, this, d->doUpdateSlotIdx);
207 qreal QSGRectangle::radius() const
209 Q_D(const QSGRectangle);
213 void QSGRectangle::setRadius(qreal radius)
216 if (d->radius == radius)
221 emit radiusChanged();
224 QColor QSGRectangle::color() const
226 Q_D(const QSGRectangle);
230 void QSGRectangle::setColor(const QColor &c)
241 QSGNode *QSGRectangle::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data)
246 if (width() <= 0 || height() <= 0) {
251 QSGRectangleNode *rectangle = static_cast<QSGRectangleNode *>(oldNode);
252 if (!rectangle) rectangle = d->sceneGraphContext()->createRectangleNode();
254 rectangle->setRect(QRectF(0, 0, width(), height()));
255 rectangle->setColor(d->color);
257 if (d->pen && d->pen->isValid()) {
258 rectangle->setPenColor(d->pen->color());
259 rectangle->setPenWidth(d->pen->width());
261 rectangle->setPenColor(QColor());
262 rectangle->setPenWidth(0);
265 rectangle->setRadius(d->radius);
267 QGradientStops stops;
269 QList<QSGGradientStop *> qxstops = d->gradient->m_stops;
270 for (int i = 0; i < qxstops.size(); ++i)
271 stops.append(QGradientStop(qxstops.at(i)->position(), qxstops.at(i)->color()));
273 rectangle->setGradientStops(stops);
280 QRectF QSGRectangle::boundingRect() const
282 Q_D(const QSGRectangle);
283 return QRectF(-d->paintmargin, -d->paintmargin, width()+d->paintmargin*2, height()+d->paintmargin*2);