[844] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
| 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the QtDeclarative module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
| 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at qt-info@nokia.com.
|
---|
| 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #include "private/qdeclarativerectangle_p.h"
|
---|
| 43 | #include "private/qdeclarativerectangle_p_p.h"
|
---|
| 44 |
|
---|
| 45 | #include <QPainter>
|
---|
| 46 | #include <QStringBuilder>
|
---|
| 47 | #include <QtCore/qmath.h>
|
---|
| 48 |
|
---|
| 49 | QT_BEGIN_NAMESPACE
|
---|
| 50 |
|
---|
| 51 | /*!
|
---|
| 52 | \internal
|
---|
| 53 | \class QDeclarativePen
|
---|
| 54 | \brief The QDeclarativePen class provides a pen used for drawing rectangle borders on a QDeclarativeView.
|
---|
| 55 |
|
---|
| 56 | By default, the pen is invalid and nothing is drawn. You must either set a color (then the default
|
---|
| 57 | width is 1) or a width (then the default color is black).
|
---|
| 58 |
|
---|
| 59 | A width of 1 indicates is a single-pixel line on the border of the item being painted.
|
---|
| 60 |
|
---|
| 61 | Example:
|
---|
| 62 | \qml
|
---|
| 63 | Rectangle { border.width: 2; border.color: "red" ... }
|
---|
| 64 | \endqml
|
---|
| 65 | */
|
---|
| 66 |
|
---|
| 67 | void QDeclarativePen::setColor(const QColor &c)
|
---|
| 68 | {
|
---|
| 69 | _color = c;
|
---|
| 70 | _valid = _color.alpha() ? true : false;
|
---|
| 71 | emit penChanged();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | void QDeclarativePen::setWidth(int w)
|
---|
| 75 | {
|
---|
| 76 | if (_width == w && _valid)
|
---|
| 77 | return;
|
---|
| 78 |
|
---|
| 79 | _width = w;
|
---|
| 80 | _valid = (_width < 1) ? false : true;
|
---|
| 81 | emit penChanged();
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | /*!
|
---|
| 86 | \qmlclass GradientStop QDeclarativeGradientStop
|
---|
| 87 | \ingroup qml-basic-visual-elements
|
---|
| 88 | \since 4.7
|
---|
| 89 | \brief The GradientStop item defines the color at a position in a Gradient.
|
---|
| 90 |
|
---|
| 91 | \sa Gradient
|
---|
| 92 | */
|
---|
| 93 |
|
---|
| 94 | /*!
|
---|
| 95 | \qmlproperty real GradientStop::position
|
---|
| 96 | \qmlproperty color GradientStop::color
|
---|
| 97 |
|
---|
| 98 | The position and color properties describe the color used at a given
|
---|
| 99 | position in a gradient, as represented by a gradient stop.
|
---|
| 100 |
|
---|
| 101 | The default position is 0.0; the default color is black.
|
---|
| 102 |
|
---|
| 103 | \sa Gradient
|
---|
| 104 | */
|
---|
| 105 |
|
---|
| 106 | void QDeclarativeGradientStop::updateGradient()
|
---|
| 107 | {
|
---|
| 108 | if (QDeclarativeGradient *grad = qobject_cast<QDeclarativeGradient*>(parent()))
|
---|
| 109 | grad->doUpdate();
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /*!
|
---|
| 113 | \qmlclass Gradient QDeclarativeGradient
|
---|
| 114 | \ingroup qml-basic-visual-elements
|
---|
| 115 | \since 4.7
|
---|
| 116 | \brief The Gradient item defines a gradient fill.
|
---|
| 117 |
|
---|
| 118 | A gradient is defined by two or more colors, which will be blended seamlessly.
|
---|
| 119 |
|
---|
| 120 | The colors are specified as a set of GradientStop child items, each of
|
---|
| 121 | which defines a position on the gradient from 0.0 to 1.0 and a color.
|
---|
| 122 | The position of each GradientStop is defined by setting its
|
---|
| 123 | \l{GradientStop::}{position} property; its color is defined using its
|
---|
| 124 | \l{GradientStop::}{color} property.
|
---|
| 125 |
|
---|
| 126 | A gradient without any gradient stops is rendered as a solid white fill.
|
---|
| 127 |
|
---|
| 128 | Note that this item is not a visual representation of a gradient. To display a
|
---|
| 129 | gradient, use a visual element (like \l Rectangle) which supports the use
|
---|
| 130 | of gradients.
|
---|
| 131 |
|
---|
| 132 | \section1 Example Usage
|
---|
| 133 |
|
---|
| 134 | \beginfloatright
|
---|
| 135 | \inlineimage qml-gradient.png
|
---|
| 136 | \endfloat
|
---|
| 137 |
|
---|
| 138 | The following example declares a \l Rectangle item with a gradient starting
|
---|
| 139 | with red, blending to yellow at one third of the height of the rectangle,
|
---|
| 140 | and ending with green:
|
---|
| 141 |
|
---|
| 142 | \snippet doc/src/snippets/declarative/gradient.qml code
|
---|
| 143 |
|
---|
| 144 | \clearfloat
|
---|
| 145 | \section1 Performance and Limitations
|
---|
| 146 |
|
---|
| 147 | Calculating gradients can be computationally expensive compared to the use
|
---|
| 148 | of solid color fills or images. Consider using gradients for static items
|
---|
| 149 | in a user interface.
|
---|
| 150 |
|
---|
| 151 | In Qt 4.7, only vertical, linear gradients can be applied to items. If you
|
---|
| 152 | need to apply different orientations of gradients, a combination of rotation
|
---|
| 153 | and clipping will need to be applied to the relevant items. This can
|
---|
| 154 | introduce additional performance requirements for your application.
|
---|
| 155 |
|
---|
| 156 | The use of animations involving gradient stops may not give the desired
|
---|
| 157 | result. An alternative way to animate gradients is to use pre-generated
|
---|
| 158 | images or SVG drawings containing gradients.
|
---|
| 159 |
|
---|
| 160 | \sa GradientStop
|
---|
| 161 | */
|
---|
| 162 |
|
---|
| 163 | /*!
|
---|
| 164 | \qmlproperty list<GradientStop> Gradient::stops
|
---|
| 165 | This property holds the gradient stops describing the gradient.
|
---|
| 166 |
|
---|
| 167 | By default, this property contains an empty list.
|
---|
| 168 |
|
---|
| 169 | To set the gradient stops, define them as children of the Gradient element.
|
---|
| 170 | */
|
---|
| 171 |
|
---|
| 172 | const QGradient *QDeclarativeGradient::gradient() const
|
---|
| 173 | {
|
---|
| 174 | if (!m_gradient && !m_stops.isEmpty()) {
|
---|
| 175 | m_gradient = new QLinearGradient(0,0,0,1.0);
|
---|
| 176 | for (int i = 0; i < m_stops.count(); ++i) {
|
---|
| 177 | const QDeclarativeGradientStop *stop = m_stops.at(i);
|
---|
| 178 | m_gradient->setCoordinateMode(QGradient::ObjectBoundingMode);
|
---|
| 179 | m_gradient->setColorAt(stop->position(), stop->color());
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | return m_gradient;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | void QDeclarativeGradient::doUpdate()
|
---|
| 187 | {
|
---|
| 188 | delete m_gradient;
|
---|
| 189 | m_gradient = 0;
|
---|
| 190 | emit updated();
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 |
|
---|
| 194 | /*!
|
---|
| 195 | \qmlclass Rectangle QDeclarativeRectangle
|
---|
| 196 | \ingroup qml-basic-visual-elements
|
---|
| 197 | \since 4.7
|
---|
| 198 | \brief The Rectangle item provides a filled rectangle with an optional border.
|
---|
| 199 | \inherits Item
|
---|
| 200 |
|
---|
| 201 | Rectangle items are used to fill areas with solid color or gradients, and are
|
---|
| 202 | often used to hold other items.
|
---|
| 203 |
|
---|
| 204 | \section1 Appearance
|
---|
| 205 |
|
---|
| 206 | Each Rectangle item is painted using either a solid fill color, specified using
|
---|
| 207 | the \l color property, or a gradient, defined using a Gradient element and set
|
---|
| 208 | using the \l gradient property. If both a color and a gradient are specified,
|
---|
| 209 | the gradient is used.
|
---|
| 210 |
|
---|
| 211 | You can add an optional border to a rectangle with its own color and thickness
|
---|
| 212 | by settting the \l border.color and \l border.width properties.
|
---|
| 213 |
|
---|
| 214 | You can also create rounded rectangles using the \l radius property. Since this
|
---|
| 215 | introduces curved edges to the corners of a rectangle, it may be appropriate to
|
---|
| 216 | set the \l smooth property to improve its appearance.
|
---|
| 217 |
|
---|
| 218 | \section1 Example Usage
|
---|
| 219 |
|
---|
| 220 | \beginfloatright
|
---|
| 221 | \inlineimage declarative-rect.png
|
---|
| 222 | \endfloat
|
---|
| 223 |
|
---|
| 224 | The following example shows the effects of some of the common properties on a
|
---|
| 225 | Rectangle item, which in this case is used to create a square:
|
---|
| 226 |
|
---|
| 227 | \snippet doc/src/snippets/declarative/rectangle/rectangle.qml document
|
---|
| 228 |
|
---|
| 229 | \clearfloat
|
---|
| 230 | \section1 Performance
|
---|
| 231 |
|
---|
| 232 | Using the \l smooth property improves the appearance of a rounded rectangle at
|
---|
| 233 | the cost of rendering performance. You should consider unsetting this property
|
---|
| 234 | for rectangles in motion, and only set it when they are stationary.
|
---|
| 235 |
|
---|
| 236 | \sa Image
|
---|
| 237 | */
|
---|
| 238 |
|
---|
| 239 | int QDeclarativeRectanglePrivate::doUpdateSlotIdx = -1;
|
---|
| 240 |
|
---|
| 241 | QDeclarativeRectangle::QDeclarativeRectangle(QDeclarativeItem *parent)
|
---|
| 242 | : QDeclarativeItem(*(new QDeclarativeRectanglePrivate), parent)
|
---|
| 243 | {
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | void QDeclarativeRectangle::doUpdate()
|
---|
| 247 | {
|
---|
| 248 | Q_D(QDeclarativeRectangle);
|
---|
| 249 | d->rectImage = QPixmap();
|
---|
| 250 | const int pw = d->pen && d->pen->isValid() ? d->pen->width() : 0;
|
---|
| 251 | d->setPaintMargin((pw+1)/2);
|
---|
| 252 | update();
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | /*!
|
---|
| 256 | \qmlproperty int Rectangle::border.width
|
---|
| 257 | \qmlproperty color Rectangle::border.color
|
---|
| 258 |
|
---|
| 259 | The width and color used to draw the border of the rectangle.
|
---|
| 260 |
|
---|
| 261 | A width of 1 creates a thin line. For no line, use a width of 0 or a transparent color.
|
---|
| 262 |
|
---|
| 263 | \note The width of the rectangle's border does not affect the geometry of the
|
---|
| 264 | rectangle itself or its position relative to other items if anchors are used.
|
---|
| 265 |
|
---|
| 266 | If \c border.width is an odd number, the rectangle is painted at a half-pixel offset to retain
|
---|
| 267 | border smoothness. Also, the border is rendered evenly on either side of the
|
---|
| 268 | rectangle's boundaries, and the spare pixel is rendered to the right and below the
|
---|
| 269 | rectangle (as documented for QRect rendering). This can cause unintended effects if
|
---|
| 270 | \c border.width is 1 and the rectangle is \l{Item::clip}{clipped} by a parent item:
|
---|
| 271 |
|
---|
| 272 | \beginfloatright
|
---|
| 273 | \inlineimage rect-border-width.png
|
---|
| 274 | \endfloat
|
---|
| 275 |
|
---|
| 276 | \snippet doc/src/snippets/declarative/rectangle/rect-border-width.qml 0
|
---|
| 277 |
|
---|
| 278 | \clearfloat
|
---|
| 279 | Here, the innermost rectangle's border is clipped on the bottom and right edges by its
|
---|
| 280 | parent. To avoid this, the border width can be set to two instead of one.
|
---|
| 281 | */
|
---|
| 282 | QDeclarativePen *QDeclarativeRectangle::border()
|
---|
| 283 | {
|
---|
| 284 | Q_D(QDeclarativeRectangle);
|
---|
| 285 | return d->getPen();
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | /*!
|
---|
| 289 | \qmlproperty Gradient Rectangle::gradient
|
---|
| 290 |
|
---|
| 291 | The gradient to use to fill the rectangle.
|
---|
| 292 |
|
---|
| 293 | This property allows for the construction of simple vertical gradients.
|
---|
| 294 | Other gradients may by formed by adding rotation to the rectangle.
|
---|
| 295 |
|
---|
| 296 | \beginfloatleft
|
---|
| 297 | \inlineimage declarative-rect_gradient.png
|
---|
| 298 | \endfloat
|
---|
| 299 |
|
---|
| 300 | \snippet doc/src/snippets/declarative/rectangle/rectangle-gradient.qml rectangles
|
---|
| 301 | \clearfloat
|
---|
| 302 |
|
---|
| 303 | If both a gradient and a color are specified, the gradient will be used.
|
---|
| 304 |
|
---|
| 305 | \sa Gradient, color
|
---|
| 306 | */
|
---|
| 307 | QDeclarativeGradient *QDeclarativeRectangle::gradient() const
|
---|
| 308 | {
|
---|
| 309 | Q_D(const QDeclarativeRectangle);
|
---|
| 310 | return d->gradient;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | void QDeclarativeRectangle::setGradient(QDeclarativeGradient *gradient)
|
---|
| 314 | {
|
---|
| 315 | Q_D(QDeclarativeRectangle);
|
---|
| 316 | if (d->gradient == gradient)
|
---|
| 317 | return;
|
---|
| 318 | static int updatedSignalIdx = -1;
|
---|
| 319 | if (updatedSignalIdx < 0)
|
---|
| 320 | updatedSignalIdx = QDeclarativeGradient::staticMetaObject.indexOfSignal("updated()");
|
---|
| 321 | if (d->doUpdateSlotIdx < 0)
|
---|
| 322 | d->doUpdateSlotIdx = QDeclarativeRectangle::staticMetaObject.indexOfSlot("doUpdate()");
|
---|
| 323 | if (d->gradient)
|
---|
| 324 | QMetaObject::disconnect(d->gradient, updatedSignalIdx, this, d->doUpdateSlotIdx);
|
---|
| 325 | d->gradient = gradient;
|
---|
| 326 | if (d->gradient)
|
---|
| 327 | QMetaObject::connect(d->gradient, updatedSignalIdx, this, d->doUpdateSlotIdx);
|
---|
| 328 | update();
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 |
|
---|
| 332 | /*!
|
---|
| 333 | \qmlproperty real Rectangle::radius
|
---|
| 334 | This property holds the corner radius used to draw a rounded rectangle.
|
---|
| 335 |
|
---|
| 336 | If radius is non-zero, the rectangle will be painted as a rounded rectangle, otherwise it will be
|
---|
| 337 | painted as a normal rectangle. The same radius is used by all 4 corners; there is currently
|
---|
| 338 | no way to specify different radii for different corners.
|
---|
| 339 | */
|
---|
| 340 | qreal QDeclarativeRectangle::radius() const
|
---|
| 341 | {
|
---|
| 342 | Q_D(const QDeclarativeRectangle);
|
---|
| 343 | return d->radius;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | void QDeclarativeRectangle::setRadius(qreal radius)
|
---|
| 347 | {
|
---|
| 348 | Q_D(QDeclarativeRectangle);
|
---|
| 349 | if (d->radius == radius)
|
---|
| 350 | return;
|
---|
| 351 |
|
---|
| 352 | d->radius = radius;
|
---|
| 353 | d->rectImage = QPixmap();
|
---|
| 354 | update();
|
---|
| 355 | emit radiusChanged();
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | /*!
|
---|
| 359 | \qmlproperty color Rectangle::color
|
---|
| 360 | This property holds the color used to fill the rectangle.
|
---|
| 361 |
|
---|
| 362 | The default color is white.
|
---|
| 363 |
|
---|
| 364 | \beginfloatright
|
---|
| 365 | \inlineimage rect-color.png
|
---|
| 366 | \endfloat
|
---|
| 367 |
|
---|
| 368 | The following example shows rectangles with colors specified
|
---|
| 369 | using hexadecimal and named color notation:
|
---|
| 370 |
|
---|
| 371 | \snippet doc/src/snippets/declarative/rectangle/rectangle-colors.qml rectangles
|
---|
| 372 |
|
---|
| 373 | \clearfloat
|
---|
| 374 | If both a gradient and a color are specified, the gradient will be used.
|
---|
| 375 |
|
---|
| 376 | \sa gradient
|
---|
| 377 | */
|
---|
| 378 | QColor QDeclarativeRectangle::color() const
|
---|
| 379 | {
|
---|
| 380 | Q_D(const QDeclarativeRectangle);
|
---|
| 381 | return d->color;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | void QDeclarativeRectangle::setColor(const QColor &c)
|
---|
| 385 | {
|
---|
| 386 | Q_D(QDeclarativeRectangle);
|
---|
| 387 | if (d->color == c)
|
---|
| 388 | return;
|
---|
| 389 |
|
---|
| 390 | d->color = c;
|
---|
| 391 | d->rectImage = QPixmap();
|
---|
| 392 | update();
|
---|
| 393 | emit colorChanged();
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | void QDeclarativeRectangle::generateRoundedRect()
|
---|
| 397 | {
|
---|
| 398 | Q_D(QDeclarativeRectangle);
|
---|
| 399 | if (d->rectImage.isNull()) {
|
---|
| 400 | const int pw = d->pen && d->pen->isValid() ? d->pen->width() : 0;
|
---|
| 401 | const int radius = qCeil(d->radius); //ensure odd numbered width/height so we get 1-pixel center
|
---|
| 402 |
|
---|
| 403 | QString key = QLatin1String("q_") % QString::number(pw) % d->color.name() % QString::number(d->color.alpha(), 16) % QLatin1Char('_') % QString::number(radius);
|
---|
| 404 | if (d->pen && d->pen->isValid())
|
---|
| 405 | key += d->pen->color().name() % QString::number(d->pen->color().alpha(), 16);
|
---|
| 406 |
|
---|
| 407 | if (!QPixmapCache::find(key, &d->rectImage)) {
|
---|
| 408 | d->rectImage = QPixmap(radius*2 + 3 + pw*2, radius*2 + 3 + pw*2);
|
---|
| 409 | d->rectImage.fill(Qt::transparent);
|
---|
| 410 | QPainter p(&(d->rectImage));
|
---|
| 411 | p.setRenderHint(QPainter::Antialiasing);
|
---|
| 412 | if (d->pen && d->pen->isValid()) {
|
---|
| 413 | QPen pn(QColor(d->pen->color()), d->pen->width());
|
---|
| 414 | p.setPen(pn);
|
---|
| 415 | } else {
|
---|
| 416 | p.setPen(Qt::NoPen);
|
---|
| 417 | }
|
---|
| 418 | p.setBrush(d->color);
|
---|
| 419 | if (pw%2)
|
---|
| 420 | p.drawRoundedRect(QRectF(qreal(pw)/2+1, qreal(pw)/2+1, d->rectImage.width()-(pw+1), d->rectImage.height()-(pw+1)), d->radius, d->radius);
|
---|
| 421 | else
|
---|
| 422 | p.drawRoundedRect(QRectF(qreal(pw)/2, qreal(pw)/2, d->rectImage.width()-pw, d->rectImage.height()-pw), d->radius, d->radius);
|
---|
| 423 |
|
---|
| 424 | // end painting before inserting pixmap
|
---|
| 425 | // to pixmap cache to avoid a deep copy
|
---|
| 426 | p.end();
|
---|
| 427 | QPixmapCache::insert(key, d->rectImage);
|
---|
| 428 | }
|
---|
| 429 | }
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | void QDeclarativeRectangle::generateBorderedRect()
|
---|
| 433 | {
|
---|
| 434 | Q_D(QDeclarativeRectangle);
|
---|
| 435 | if (d->rectImage.isNull()) {
|
---|
| 436 | const int pw = d->pen && d->pen->isValid() ? d->pen->width() : 0;
|
---|
| 437 |
|
---|
| 438 | QString key = QLatin1String("q_") % QString::number(pw) % d->color.name() % QString::number(d->color.alpha(), 16);
|
---|
| 439 | if (d->pen && d->pen->isValid())
|
---|
| 440 | key += d->pen->color().name() % QString::number(d->pen->color().alpha(), 16);
|
---|
| 441 |
|
---|
| 442 | if (!QPixmapCache::find(key, &d->rectImage)) {
|
---|
| 443 | // Adding 5 here makes qDrawBorderPixmap() paint correctly with smooth: true
|
---|
| 444 | // See QTBUG-7999 and QTBUG-10765 for more details.
|
---|
| 445 | d->rectImage = QPixmap(pw*2 + 5, pw*2 + 5);
|
---|
| 446 | d->rectImage.fill(Qt::transparent);
|
---|
| 447 | QPainter p(&(d->rectImage));
|
---|
| 448 | p.setRenderHint(QPainter::Antialiasing);
|
---|
| 449 | if (d->pen && d->pen->isValid()) {
|
---|
| 450 | QPen pn(QColor(d->pen->color()), d->pen->width());
|
---|
| 451 | pn.setJoinStyle(Qt::MiterJoin);
|
---|
| 452 | p.setPen(pn);
|
---|
| 453 | } else {
|
---|
| 454 | p.setPen(Qt::NoPen);
|
---|
| 455 | }
|
---|
| 456 | p.setBrush(d->color);
|
---|
| 457 | if (pw%2)
|
---|
| 458 | p.drawRect(QRectF(qreal(pw)/2+1, qreal(pw)/2+1, d->rectImage.width()-(pw+1), d->rectImage.height()-(pw+1)));
|
---|
| 459 | else
|
---|
| 460 | p.drawRect(QRectF(qreal(pw)/2, qreal(pw)/2, d->rectImage.width()-pw, d->rectImage.height()-pw));
|
---|
| 461 |
|
---|
| 462 | // end painting before inserting pixmap
|
---|
| 463 | // to pixmap cache to avoid a deep copy
|
---|
| 464 | p.end();
|
---|
| 465 | QPixmapCache::insert(key, d->rectImage);
|
---|
| 466 | }
|
---|
| 467 | }
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | void QDeclarativeRectangle::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
|
---|
| 471 | {
|
---|
| 472 | Q_D(QDeclarativeRectangle);
|
---|
| 473 | if (width() <= 0 || height() <= 0)
|
---|
| 474 | return;
|
---|
| 475 | if (d->radius > 0 || (d->pen && d->pen->isValid())
|
---|
| 476 | || (d->gradient && d->gradient->gradient()) ) {
|
---|
| 477 | drawRect(*p);
|
---|
| 478 | }
|
---|
| 479 | else {
|
---|
| 480 | bool oldAA = p->testRenderHint(QPainter::Antialiasing);
|
---|
| 481 | if (d->smooth)
|
---|
| 482 | p->setRenderHints(QPainter::Antialiasing, true);
|
---|
| 483 | p->fillRect(QRectF(0, 0, width(), height()), d->color);
|
---|
| 484 | if (d->smooth)
|
---|
| 485 | p->setRenderHint(QPainter::Antialiasing, oldAA);
|
---|
| 486 | }
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | void QDeclarativeRectangle::drawRect(QPainter &p)
|
---|
| 490 | {
|
---|
| 491 | Q_D(QDeclarativeRectangle);
|
---|
| 492 | if ((d->gradient && d->gradient->gradient())
|
---|
| 493 | || d->radius > width()/2 || d->radius > height()/2
|
---|
| 494 | || width() < 3 || height() < 3) {
|
---|
| 495 | // XXX This path is still slower than the image path
|
---|
| 496 | // Image path won't work for gradients or invalid radius though
|
---|
| 497 | bool oldAA = p.testRenderHint(QPainter::Antialiasing);
|
---|
| 498 | if (d->smooth)
|
---|
| 499 | p.setRenderHint(QPainter::Antialiasing);
|
---|
| 500 | if (d->pen && d->pen->isValid()) {
|
---|
| 501 | QPen pn(QColor(d->pen->color()), d->pen->width());
|
---|
| 502 | pn.setJoinStyle(Qt::MiterJoin);
|
---|
| 503 | p.setPen(pn);
|
---|
| 504 | } else {
|
---|
| 505 | p.setPen(Qt::NoPen);
|
---|
| 506 | }
|
---|
| 507 | if (d->gradient && d->gradient->gradient())
|
---|
| 508 | p.setBrush(*d->gradient->gradient());
|
---|
| 509 | else
|
---|
| 510 | p.setBrush(d->color);
|
---|
| 511 | const int pw = d->pen && d->pen->isValid() ? d->pen->width() : 0;
|
---|
| 512 | QRectF rect;
|
---|
| 513 | if (pw%2)
|
---|
| 514 | rect = QRectF(0.5, 0.5, width()-1, height()-1);
|
---|
| 515 | else
|
---|
| 516 | rect = QRectF(0, 0, width(), height());
|
---|
| 517 | qreal radius = d->radius;
|
---|
| 518 | if (radius > width()/2 || radius > height()/2)
|
---|
| 519 | radius = qMin(width()/2, height()/2);
|
---|
| 520 | if (radius > 0.)
|
---|
| 521 | p.drawRoundedRect(rect, radius, radius);
|
---|
| 522 | else
|
---|
| 523 | p.drawRect(rect);
|
---|
| 524 | if (d->smooth)
|
---|
| 525 | p.setRenderHint(QPainter::Antialiasing, oldAA);
|
---|
| 526 | } else {
|
---|
| 527 | bool oldAA = p.testRenderHint(QPainter::Antialiasing);
|
---|
| 528 | bool oldSmooth = p.testRenderHint(QPainter::SmoothPixmapTransform);
|
---|
| 529 | if (d->smooth)
|
---|
| 530 | p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, d->smooth);
|
---|
| 531 |
|
---|
| 532 | const int pw = d->pen && d->pen->isValid() ? (d->pen->width()+1)/2*2 : 0;
|
---|
| 533 |
|
---|
| 534 | if (d->radius > 0)
|
---|
| 535 | generateRoundedRect();
|
---|
| 536 | else
|
---|
| 537 | generateBorderedRect();
|
---|
| 538 |
|
---|
| 539 | int xOffset = (d->rectImage.width()-1)/2;
|
---|
| 540 | int yOffset = (d->rectImage.height()-1)/2;
|
---|
| 541 | Q_ASSERT(d->rectImage.width() == 2*xOffset + 1);
|
---|
| 542 | Q_ASSERT(d->rectImage.height() == 2*yOffset + 1);
|
---|
| 543 |
|
---|
| 544 | // check whether we've eliminated the center completely
|
---|
| 545 | if (2*xOffset > width()+pw)
|
---|
| 546 | xOffset = (width()+pw)/2;
|
---|
| 547 | if (2*yOffset > height()+pw)
|
---|
| 548 | yOffset = (height()+pw)/2;
|
---|
| 549 |
|
---|
| 550 | QMargins margins(xOffset, yOffset, xOffset, yOffset);
|
---|
| 551 | QTileRules rules(Qt::StretchTile, Qt::StretchTile);
|
---|
| 552 | //NOTE: even though our item may have qreal-based width and height, qDrawBorderPixmap only supports QRects
|
---|
| 553 | qDrawBorderPixmap(&p, QRect(-pw/2, -pw/2, width()+pw, height()+pw), margins, d->rectImage, d->rectImage.rect(), margins, rules);
|
---|
| 554 |
|
---|
| 555 | if (d->smooth) {
|
---|
| 556 | p.setRenderHint(QPainter::Antialiasing, oldAA);
|
---|
| 557 | p.setRenderHint(QPainter::SmoothPixmapTransform, oldSmooth);
|
---|
| 558 | }
|
---|
| 559 | }
|
---|
| 560 | }
|
---|
| 561 |
|
---|
| 562 | /*!
|
---|
| 563 | \qmlproperty bool Rectangle::smooth
|
---|
| 564 |
|
---|
| 565 | Set this property if you want the item to be smoothly scaled or
|
---|
| 566 | transformed. Smooth filtering gives better visual quality, but is slower. If
|
---|
| 567 | the item is displayed at its natural size, this property has no visual or
|
---|
| 568 | performance effect.
|
---|
| 569 |
|
---|
| 570 | \note Generally scaling artifacts are only visible if the item is stationary on
|
---|
| 571 | the screen. A common pattern when animating an item is to disable smooth
|
---|
| 572 | filtering at the beginning of the animation and reenable it at the conclusion.
|
---|
| 573 |
|
---|
| 574 | \image rect-smooth.png
|
---|
| 575 | On this image, smooth is turned off on the top half and on on the bottom half.
|
---|
| 576 | */
|
---|
| 577 |
|
---|
| 578 | QRectF QDeclarativeRectangle::boundingRect() const
|
---|
| 579 | {
|
---|
| 580 | Q_D(const QDeclarativeRectangle);
|
---|
| 581 | return QRectF(-d->paintmargin, -d->paintmargin, d->width()+d->paintmargin*2, d->height()+d->paintmargin*2);
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | QT_END_NAMESPACE
|
---|