summaryrefslogtreecommitdiffstats
path: root/src/declarative/graphicsitems/qdeclarativerectangle.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/declarative/graphicsitems/qdeclarativerectangle.cpp')
-rw-r--r--src/declarative/graphicsitems/qdeclarativerectangle.cpp123
1 files changed, 82 insertions, 41 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativerectangle.cpp b/src/declarative/graphicsitems/qdeclarativerectangle.cpp
index 0328f9163d..2756877004 100644
--- a/src/declarative/graphicsitems/qdeclarativerectangle.cpp
+++ b/src/declarative/graphicsitems/qdeclarativerectangle.cpp
@@ -43,6 +43,7 @@
#include "private/qdeclarativerectangle_p_p.h"
#include <QPainter>
+#include <QStringBuilder>
#include <QtCore/qmath.h>
QT_BEGIN_NAMESPACE
@@ -113,11 +114,11 @@ void QDeclarativeGradientStop::updateGradient()
rectangle with a gradient starting with red, blending to yellow at 1/3 of the
size of the rectangle, and ending with Green:
- \table
- \row
- \o \image gradient.png
- \o \quotefile doc/src/snippets/declarative/gradient.qml
- \endtable
+ \snippet doc/src/snippets/declarative/gradient.qml code
+
+ Note that this item is not a visual representation of a gradient. To display a
+ gradient use a visual item (like rectangle) which supports having a gradient set
+ on it for display.
\sa GradientStop
*/
@@ -155,10 +156,12 @@ void QDeclarativeGradient::doUpdate()
\brief The Rectangle item allows you to add rectangles to a scene.
\inherits Item
- A Rectangle is painted having a solid fill (color) and an optional border.
- You can also create rounded rectangles using the radius property.
+ A Rectangle is painted using a solid fill (color) and an optional border.
+ You can also create rounded rectangles using the \l radius property.
\qml
+ import Qt 4.7
+
Rectangle {
width: 100
height: 100
@@ -201,8 +204,20 @@ void QDeclarativeRectangle::doUpdate()
A width of 1 creates a thin line. For no line, use a width of 0 or a transparent color.
- To keep the border smooth (rather than blurry), odd widths cause the rectangle to be painted at
- a half-pixel offset;
+ If \c border.width is an odd number, the rectangle is painted at a half-pixel offset to retain
+ border smoothness. Also, the border is rendered evenly on either side of the
+ rectangle's boundaries, and the spare pixel is rendered to the right and below the
+ rectangle (as documented for QRect rendering). This can cause unintended effects if
+ \c border.width is 1 and the rectangle is \l{Item::clip}{clipped} by a parent item:
+
+ \table
+ \row
+ \o \snippet doc/src/snippets/declarative/rect-border-width.qml 0
+ \o \image rect-border-width.png
+ \endtable
+
+ Here, the innermost rectangle's border is clipped on the bottom and right edges by its
+ parent. To avoid this, the border width can be set to two instead of one.
*/
QDeclarativePen *QDeclarativeRectangle::border()
{
@@ -223,14 +238,22 @@ QDeclarativePen *QDeclarativeRectangle::border()
\o \image declarative-rect_gradient.png
\o
\qml
- Rectangle { y: 0; width: 80; height: 80; color: "lightsteelblue" }
- Rectangle { y: 100; width: 80; height: 80
+ Rectangle {
+ y: 0; width: 80; height: 80
+ color: "lightsteelblue"
+ }
+
+ Rectangle {
+ y: 100; width: 80; height: 80
gradient: Gradient {
GradientStop { position: 0.0; color: "lightsteelblue" }
GradientStop { position: 1.0; color: "blue" }
}
}
- Rectangle { rotation: 90; y: 200; width: 80; height: 80
+
+ Rectangle {
+ y: 200; width: 80; height: 80
+ rotation: 90
gradient: Gradient {
GradientStop { position: 0.0; color: "lightsteelblue" }
GradientStop { position: 1.0; color: "blue" }
@@ -334,21 +357,29 @@ void QDeclarativeRectangle::generateRoundedRect()
if (d->rectImage.isNull()) {
const int pw = d->pen && d->pen->isValid() ? d->pen->width() : 0;
const int radius = qCeil(d->radius); //ensure odd numbered width/height so we get 1-pixel center
- d->rectImage = QPixmap(radius*2 + 3 + pw*2, radius*2 + 3 + pw*2);
- d->rectImage.fill(Qt::transparent);
- QPainter p(&(d->rectImage));
- p.setRenderHint(QPainter::Antialiasing);
- if (d->pen && d->pen->isValid()) {
- QPen pn(QColor(d->pen->color()), d->pen->width());
- p.setPen(pn);
- } else {
- p.setPen(Qt::NoPen);
+
+ QString key = QLatin1String("q_") % QString::number(pw) % d->color.name() % QString::number(d->color.alpha(), 16) % QLatin1Char('_') % QString::number(radius);
+ if (d->pen && d->pen->isValid())
+ key += d->pen->color().name() % QString::number(d->pen->color().alpha(), 16);
+
+ if (!QPixmapCache::find(key, &d->rectImage)) {
+ d->rectImage = QPixmap(radius*2 + 3 + pw*2, radius*2 + 3 + pw*2);
+ d->rectImage.fill(Qt::transparent);
+ QPainter p(&(d->rectImage));
+ p.setRenderHint(QPainter::Antialiasing);
+ if (d->pen && d->pen->isValid()) {
+ QPen pn(QColor(d->pen->color()), d->pen->width());
+ p.setPen(pn);
+ } else {
+ p.setPen(Qt::NoPen);
+ }
+ p.setBrush(d->color);
+ if (pw%2)
+ 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);
+ else
+ p.drawRoundedRect(QRectF(qreal(pw)/2, qreal(pw)/2, d->rectImage.width()-pw, d->rectImage.height()-pw), d->radius, d->radius);
+ QPixmapCache::insert(key, d->rectImage);
}
- p.setBrush(d->color);
- if (pw%2)
- 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);
- else
- p.drawRoundedRect(QRectF(qreal(pw)/2, qreal(pw)/2, d->rectImage.width()-pw, d->rectImage.height()-pw), d->radius, d->radius);
}
}
@@ -357,22 +388,32 @@ void QDeclarativeRectangle::generateBorderedRect()
Q_D(QDeclarativeRectangle);
if (d->rectImage.isNull()) {
const int pw = d->pen && d->pen->isValid() ? d->pen->width() : 0;
- d->rectImage = QPixmap(pw*2 + 3, pw*2 + 3);
- d->rectImage.fill(Qt::transparent);
- QPainter p(&(d->rectImage));
- p.setRenderHint(QPainter::Antialiasing);
- if (d->pen && d->pen->isValid()) {
- QPen pn(QColor(d->pen->color()), d->pen->width());
- pn.setJoinStyle(Qt::MiterJoin);
- p.setPen(pn);
- } else {
- p.setPen(Qt::NoPen);
+
+ QString key = QLatin1String("q_") % QString::number(pw) % d->color.name() % QString::number(d->color.alpha(), 16);
+ if (d->pen && d->pen->isValid())
+ key += d->pen->color().name() % QString::number(d->pen->color().alpha(), 16);
+
+ if (!QPixmapCache::find(key, &d->rectImage)) {
+ // Adding 5 here makes qDrawBorderPixmap() paint correctly with smooth: true
+ // See QTBUG-7999 and QTBUG-10765 for more details.
+ d->rectImage = QPixmap(pw*2 + 5, pw*2 + 5);
+ d->rectImage.fill(Qt::transparent);
+ QPainter p(&(d->rectImage));
+ p.setRenderHint(QPainter::Antialiasing);
+ if (d->pen && d->pen->isValid()) {
+ QPen pn(QColor(d->pen->color()), d->pen->width());
+ pn.setJoinStyle(Qt::MiterJoin);
+ p.setPen(pn);
+ } else {
+ p.setPen(Qt::NoPen);
+ }
+ p.setBrush(d->color);
+ if (pw%2)
+ p.drawRect(QRectF(qreal(pw)/2+1, qreal(pw)/2+1, d->rectImage.width()-(pw+1), d->rectImage.height()-(pw+1)));
+ else
+ p.drawRect(QRectF(qreal(pw)/2, qreal(pw)/2, d->rectImage.width()-pw, d->rectImage.height()-pw));
+ QPixmapCache::insert(key, d->rectImage);
}
- p.setBrush(d->color);
- if (pw%2)
- p.drawRect(QRectF(qreal(pw)/2+1, qreal(pw)/2+1, d->rectImage.width()-(pw+1), d->rectImage.height()-(pw+1)));
- else
- p.drawRect(QRectF(qreal(pw)/2, qreal(pw)/2, d->rectImage.width()-pw, d->rectImage.height()-pw));
}
}