summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMartin Jones <martin.jones@nokia.com>2009-05-27 07:00:37 (GMT)
committerMartin Jones <martin.jones@nokia.com>2009-05-27 07:00:37 (GMT)
commited5d5f6f04cda1f7aa3ea66be5fefc0a85429794 (patch)
treeb8244c7f0cd8b50e18faf64508a32c94f274a698 /src
parent411678df3e25304b105e5f3afc849b529baa0d6f (diff)
downloadQt-ed5d5f6f04cda1f7aa3ea66be5fefc0a85429794.zip
Qt-ed5d5f6f04cda1f7aa3ea66be5fefc0a85429794.tar.gz
Qt-ed5d5f6f04cda1f7aa3ea66be5fefc0a85429794.tar.bz2
Better gradient support for Rect.
Diffstat (limited to 'src')
-rw-r--r--src/declarative/fx/qfxrect.cpp157
-rw-r--r--src/declarative/fx/qfxrect.h51
-rw-r--r--src/declarative/fx/qfxrect_p.h6
3 files changed, 153 insertions, 61 deletions
diff --git a/src/declarative/fx/qfxrect.cpp b/src/declarative/fx/qfxrect.cpp
index cc5afc3..4271022 100644
--- a/src/declarative/fx/qfxrect.cpp
+++ b/src/declarative/fx/qfxrect.cpp
@@ -45,6 +45,8 @@
QT_BEGIN_NAMESPACE
QML_DEFINE_TYPE(QFxPen,Pen)
+QML_DEFINE_TYPE(QFxGradientStop,GradientStop)
+QML_DEFINE_TYPE(QFxGradient,Gradient)
/*!
\internal
@@ -117,6 +119,57 @@ void QFxPen::setWidth(int w)
}
+/*!
+ \qmlclass GradientStop QFxGradientStop
+ \brief The GradientStop item defines the color at a position in a Gradient
+
+ \sa Gradient
+*/
+
+/*!
+ \qmlproperty real GradientStop::position
+ \qmlproperty color GradientStop::color
+
+ Sets a \e color at a \e position in a gradient.
+*/
+
+/*!
+ \qmlclass Gradient QFxGradient
+ \brief The Gradient item defines a gradient fill.
+
+ A gradient is defined by two or more colors, which will be blended seemlessly. The
+ colors are specified at their position in the range 0.0 - 1.0 via
+ the GradientStop item. For example, the following code paints a
+ Rect with a gradient starting with red, blending to yellow at 1/3 of the
+ size of the Rect, and ending with Green:
+
+ \table
+ \row
+ \o \image gradient.png
+ \o \quotefile doc/src/snippets/declarative/gradient.qml
+ \endtable
+
+ \sa GradientStop
+*/
+
+/*!
+ \qmlproperty list<GradientStop> Gradient::stops
+ This property holds the gradient stops describing the gradient.
+*/
+
+const QGradient *QFxGradient::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 QFxGradientStop *stop = m_stops.at(i);
+ m_gradient->setCoordinateMode(QGradient::ObjectBoundingMode);
+ m_gradient->setColorAt(stop->position(), stop->color());
+ }
+ }
+
+ return m_gradient;
+}
QML_DEFINE_TYPE(QFxRect,Rect)
@@ -215,6 +268,51 @@ QFxPen *QFxRect::pen()
}
/*!
+ \qmlproperty Gradient Rect::gradient
+
+ The gradient to use to fill the rect.
+
+ This property allows for the construction of simple vertical gradients.
+ Other gradients may by formed by adding rotation to the rect.
+
+ \table
+ \row
+ \o \image declarative-rect_gradient.png
+ \o
+ \qml
+ Rect { y: 0; width: 80; height: 80; color: "lightsteelblue" }
+ Rect { y: 100; width: 80; height: 80
+ gradient: Gradient {
+ GradientStop { position: 0.0; color: "lightsteelblue" }
+ GradientStop { position: 1.0; color: "blue" }
+ }
+ }
+ Rect { rotation: 90; x: 80; y: 200; width: 80; height: 80
+ gradient: Gradient {
+ GradientStop { position: 0.0; color: "lightsteelblue" }
+ GradientStop { position: 1.0; color: "blue" }
+ }
+ }
+ // The x offset is needed because the rotation is from the top left corner
+ \endqml
+ \endtable
+
+ \sa Gradient, color
+*/
+QFxGradient *QFxRect::gradient() const
+{
+ Q_D(const QFxRect);
+ return d->gradient;
+}
+
+void QFxRect::setGradient(QFxGradient *gradient)
+{
+ Q_D(QFxRect);
+ d->gradient = gradient;
+}
+
+
+/*!
\qmlproperty real Rect::radius
This property holds the corner radius used to draw a rounded rect.
@@ -358,42 +456,6 @@ QColor QFxRectPrivate::getColor()
}
}
-/*!
- \qmlproperty color Rect::gradientColor
- This property holds the color to use at the base of the rectangle and blend upwards.
-
- This property allows for the easy construction of simple horizontal gradients. Other gradients may by formed by adding rotation to the rect. The gradient will blend linearly from the rect's main color to the color specified for gradient color.
-
- \qml
- Rect { y: 0; width: 80; height: 80; color: "lightsteelblue" }
- Rect { y: 100; width: 80; height: 80; color: "lightsteelblue"; gradientColor="blue" }
- Rect { rotation: 90; x: 80; y: 200; width: 80; height: 80; color="lightsteelblue"
- gradientColor: "blue" }
- // The x offset is needed because the rotation is from the top left corner
- \endqml
- \image declarative-rect_gradient.png
-*/
-
-/*!
- \property QFxRect::gradientColor
- \brief The color to use at the base of the rectangle and blend upwards.
-*/
-
-QColor QFxRect::gradientColor() const
-{
- Q_D(const QFxRect);
- return d->_gradcolor;
-}
-
-void QFxRect::setGradientColor(const QColor &c)
-{
- Q_D(QFxRect);
- if (d->_gradcolor == c)
- return;
-
- d->_gradcolor = c;
- update();
-}
#if defined(QFX_RENDER_QPAINTER)
void QFxRect::generateRoundedRect()
@@ -485,16 +547,8 @@ void QFxRect::paintContents(QPainter &p)
{
Q_D(QFxRect);
if (d->_radius > 0 || (d->_pen && d->_pen->isValid())
- || d->_gradcolor.isValid())
+ || (d->gradient && d->gradient->gradient()) )
drawRect(p);
- /*
- QLinearGradient grad(0, 0, 0, height());
- grad.setColorAt(0, d->_color);
- grad.setColorAt(1, d->_gradcolor);
- p.setBrush(grad);
- p.drawRect(0, 0, width(), height());
- p.setBrush(QBrush());
- */
else
p.fillRect(QRect(0, 0, width(), height()), d->getColor());
}
@@ -502,7 +556,7 @@ void QFxRect::paintContents(QPainter &p)
void QFxRect::drawRect(QPainter &p)
{
Q_D(QFxRect);
- if (d->_gradcolor.isValid() /*|| p.usingQt() */) {
+ if (d->gradient && d->gradient->gradient() /*|| p.usingQt() */) {
// XXX This path is still slower than the image path
// Image path won't work for gradients though
QPainter::RenderHints oldHints = p.renderHints();
@@ -513,15 +567,8 @@ void QFxRect::drawRect(QPainter &p)
} else {
p.setPen(Qt::NoPen);
}
- if (d->_gradcolor.isValid()){
- QLinearGradient grad(0, 0, 0, height());
- grad.setColorAt(0, d->_color);
- grad.setColorAt(1, d->_gradcolor);
- p.setBrush(grad);
- }else{
- p.setBrush(d->_color);
- }
- if (d->_radius)
+ p.setBrush(*d->gradient->gradient());
+ if (d->_radius > 0.)
p.drawRoundedRect(0, 0, width(), height(), d->_radius, d->_radius);
else
p.drawRect(0, 0, width(), height());
diff --git a/src/declarative/fx/qfxrect.h b/src/declarative/fx/qfxrect.h
index 209a8ef..2c59914 100644
--- a/src/declarative/fx/qfxrect.h
+++ b/src/declarative/fx/qfxrect.h
@@ -79,6 +79,49 @@ private:
};
QML_DECLARE_TYPE(QFxPen)
+class Q_DECLARATIVE_EXPORT QFxGradientStop : public QObject
+{
+ Q_OBJECT
+
+ Q_PROPERTY(qreal position READ position WRITE setPosition)
+ Q_PROPERTY(QColor color READ color WRITE setColor)
+
+public:
+ QFxGradientStop(QObject *parent=0) : QObject(parent) {}
+
+ qreal position() const { return m_position; }
+ void setPosition(qreal position) { m_position = position; }
+
+ QColor color() const { return m_color; }
+ void setColor(const QColor &color) { m_color = color; }
+
+private:
+ qreal m_position;
+ QColor m_color;
+};
+QML_DECLARE_TYPE(QFxGradientStop)
+
+class Q_DECLARATIVE_EXPORT QFxGradient : public QObject
+{
+ Q_OBJECT
+
+ Q_PROPERTY(QList<QFxGradientStop *> *stops READ stops)
+ Q_CLASSINFO("DefaultProperty", "stops")
+
+public:
+ QFxGradient(QObject *parent=0) : QObject(parent), m_gradient(0), m_created(false) {}
+
+ QList<QFxGradientStop *> *stops() { return &m_stops; }
+
+ const QGradient *gradient() const;
+
+private:
+ QList<QFxGradientStop *> m_stops;
+ mutable QGradient *m_gradient;
+ mutable bool m_created;
+};
+QML_DECLARE_TYPE(QFxGradient)
+
class QFxRectPrivate;
class Q_DECLARATIVE_EXPORT QFxRect : public QFxItem
{
@@ -86,7 +129,7 @@ class Q_DECLARATIVE_EXPORT QFxRect : public QFxItem
Q_PROPERTY(QColor color READ color WRITE setColor)
Q_PROPERTY(QColor tintColor READ tintColor WRITE setTintColor)
- Q_PROPERTY(QColor gradientColor READ gradientColor WRITE setGradientColor)
+ Q_PROPERTY(QFxGradient *gradient READ gradient WRITE setGradient)
Q_PROPERTY(QFxPen * pen READ pen)
Q_PROPERTY(qreal radius READ radius WRITE setRadius)
public:
@@ -98,11 +141,11 @@ public:
QColor tintColor() const;
void setTintColor(const QColor &);
- QColor gradientColor() const;
- void setGradientColor(const QColor &);
-
QFxPen *pen();
+ QFxGradient *gradient() const;
+ void setGradient(QFxGradient *gradient);
+
qreal radius() const;
void setRadius(qreal radius);
diff --git a/src/declarative/fx/qfxrect_p.h b/src/declarative/fx/qfxrect_p.h
index 2fd555f..8faaa38 100644
--- a/src/declarative/fx/qfxrect_p.h
+++ b/src/declarative/fx/qfxrect_p.h
@@ -61,13 +61,15 @@
QT_BEGIN_NAMESPACE
+class QFxGradient;
+
class QFxRectPrivate : public QFxItemPrivate
{
Q_DECLARE_PUBLIC(QFxRect)
public:
QFxRectPrivate()
- : _pen(0), _radius(0)
+ : gradient(0), _pen(0), _radius(0)
{
}
@@ -85,7 +87,7 @@ public:
#endif
QColor getColor();
QColor _color;
- QColor _gradcolor;
+ QFxGradient *gradient;
QColor _tintColor;
QFxPen *pen() {
if (!_pen) {