From 5bb4fe79e92a4882977f64c03654e707721c59a9 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Wed, 1 Jul 2009 13:24:24 +1000 Subject: Add a Scale transform object. --- doc/src/declarative/elements.qdoc | 1 + src/declarative/fx/qfxtransform.cpp | 133 ++++++++++++++++++++++++++++++++++++ src/declarative/fx/qfxtransform.h | 43 ++++++++++++ 3 files changed, 177 insertions(+) diff --git a/doc/src/declarative/elements.qdoc b/doc/src/declarative/elements.qdoc index 63566f6..eda9079 100644 --- a/doc/src/declarative/elements.qdoc +++ b/doc/src/declarative/elements.qdoc @@ -139,6 +139,7 @@ The following table lists the Qml elements provided by the Qt Declarative module \o \list +\o \l Scale \o \l Rotation \o \l Squish \o \l Rotation3D diff --git a/src/declarative/fx/qfxtransform.cpp b/src/declarative/fx/qfxtransform.cpp index 2c4f842..0f0ce80 100644 --- a/src/declarative/fx/qfxtransform.cpp +++ b/src/declarative/fx/qfxtransform.cpp @@ -84,6 +84,139 @@ void QFxTransform::update() } /*! + \qmlclass Scale + \brief A Scale object provides a way to scale an Item. + + The scale object gives more control over scaling than using Item's scale property. Specifically, + it allows a different scale for the x and y axes, and allows the scale to be relative to an + arbitrary point. + + The following example scales the X axis of the Rect, relative to its interior point 25, 25: + \qml + Rect { + width: 100; height: 100 + color: "blue" + transform: Scale { originX: 25; originY: 25; xScale: 3} + } + \endqml +*/ + +QFxScale::QFxScale(QObject *parent) +: QFxTransform(parent), _originX(0), _originY(0), _xScale(1), _yScale(1), _dirty(true) +{ +} + +QFxScale::~QFxScale() +{ +} + +/*! + \qmlproperty real Scale::originX + \qmlproperty real Scale::originY + + The origin point for the scale. The scale will be relative to this point. +*/ +qreal QFxScale::originX() const +{ + return _originX; +} + +void QFxScale::setOriginX(qreal ox) +{ + _originX = ox; + update(); +} + +qreal QFxScale::originY() const +{ + return _originY; +} + +void QFxScale::setOriginY(qreal oy) +{ + _originY = oy; + update(); +} + +/*! + \qmlproperty real Scale::xScale + + The scaling factor for the X axis. +*/ +qreal QFxScale::xScale() const +{ + return _xScale; +} + +void QFxScale::setXScale(qreal scale) +{ + if (_xScale == scale) + return; + _xScale = scale; + update(); + emit scaleChanged(); +} + +/*! + \qmlproperty real Scale::yScale + + The scaling factor for the Y axis. +*/ +qreal QFxScale::yScale() const +{ + return _yScale; +} + +void QFxScale::setYScale(qreal scale) +{ + if (_yScale == scale) + return; + _yScale = scale; + update(); + emit scaleChanged(); +} + +bool QFxScale::isIdentity() const +{ + return (_xScale == 1. && _yScale == 1.); +} + +#if defined(QFX_RENDER_QPAINTER) +QTransform QFxScale::transform() const +{ + if (_dirty) { + _transform = QTransform(); + _dirty = false; + _transform.translate(_originX, _originY); + _transform.scale(_xScale, _yScale); + _transform.translate(-_originX, -_originY); + } + return _transform; +} +#elif defined(QFX_RENDER_OPENGL) +QMatrix4x4 QFxScale::transform() const +{ + if (_dirty) { + _transform = QMatrix4x4(); + _dirty = false; + _transform.translate(_originX, _originY); + _transform.scale(_xScale, _yScale); + _transform.translate(-_originX, -_originY); + } + return _transform; +} +#endif + +void QFxScale::update() +{ + _dirty = true; + QFxTransform::update(); +} + +QML_DEFINE_TYPE(QFxScale, Scale) + + +/*! \qmlclass Axis \brief A Axis object defines an axis that can be used for rotation or translation. diff --git a/src/declarative/fx/qfxtransform.h b/src/declarative/fx/qfxtransform.h index 8693e7b..e0bd276 100644 --- a/src/declarative/fx/qfxtransform.h +++ b/src/declarative/fx/qfxtransform.h @@ -68,6 +68,48 @@ public: virtual QSimpleCanvas::Matrix transform() const; }; +class Q_DECLARATIVE_EXPORT QFxScale : public QFxTransform +{ + Q_OBJECT + + Q_PROPERTY(qreal originX READ originX WRITE setOriginX) + Q_PROPERTY(qreal originY READ originY WRITE setOriginY) + Q_PROPERTY(qreal xScale READ xScale WRITE setXScale NOTIFY scaleChanged()) + Q_PROPERTY(qreal yScale READ yScale WRITE setYScale NOTIFY scaleChanged()) +public: + QFxScale(QObject *parent=0); + ~QFxScale(); + + qreal originX() const; + void setOriginX(qreal); + + qreal originY() const; + void setOriginY(qreal); + + qreal xScale() const; + void setXScale(qreal); + + qreal yScale() const; + void setYScale(qreal); + + virtual bool isIdentity() const; + virtual QSimpleCanvas::Matrix transform() const; + +Q_SIGNALS: + void scaleChanged(); + +private Q_SLOTS: + void update(); +private: + qreal _originX; + qreal _originY; + qreal _xScale; + qreal _yScale; + + mutable bool _dirty; + mutable QSimpleCanvas::Matrix _transform; +}; + class Q_DECLARATIVE_EXPORT QFxAxis : public QObject { Q_OBJECT @@ -308,6 +350,7 @@ private: QT_END_NAMESPACE QML_DECLARE_TYPE(QFxTransform) +QML_DECLARE_TYPE(QFxScale) QML_DECLARE_TYPE(QFxAxis) QML_DECLARE_TYPE(QFxRotation) QML_DECLARE_TYPE(QFxRotation3D) -- cgit v0.12