summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMichael Brasser <michael.brasser@nokia.com>2009-04-29 05:09:07 (GMT)
committerMichael Brasser <michael.brasser@nokia.com>2009-04-29 05:09:07 (GMT)
commit012bf05b8c5e37bb102406d79023cb5d875deaac (patch)
tree344813cafa06bb2bf6293a2fe167d57c5a2503c3 /src
parent3acc614737ab2ac5d15c4d4f38066c417065a843 (diff)
downloadQt-012bf05b8c5e37bb102406d79023cb5d875deaac.zip
Qt-012bf05b8c5e37bb102406d79023cb5d875deaac.tar.gz
Qt-012bf05b8c5e37bb102406d79023cb5d875deaac.tar.bz2
API improvements for Flipable.
You can now specify an axis and rotation, rather than using transform. The tranform behavior has been preserved, however, to allow for cases where you want a more complex transform when rotating.
Diffstat (limited to 'src')
-rw-r--r--src/declarative/fx/qfxflipable.cpp133
-rw-r--r--src/declarative/fx/qfxflipable.h10
-rw-r--r--src/declarative/fx/qfxtransform.cpp25
3 files changed, 160 insertions, 8 deletions
diff --git a/src/declarative/fx/qfxflipable.cpp b/src/declarative/fx/qfxflipable.cpp
index 9db0b57..1d15827 100644
--- a/src/declarative/fx/qfxflipable.cpp
+++ b/src/declarative/fx/qfxflipable.cpp
@@ -49,11 +49,17 @@ QML_DEFINE_TYPE(QFxFlipable,Flipable);
class QFxFlipablePrivate : public QFxItemPrivate
{
public:
- QFxFlipablePrivate() : current(QFxFlipable::Front), front(0), back(0) {}
+ QFxFlipablePrivate() : current(QFxFlipable::Front), front(0), back(0), axis(0), rotation(0) {}
+
+ void setBackTransform();
+ void _q_updateAxis();
QFxFlipable::Side current;
QFxItem *front;
QFxItem *back;
+ QFxAxis *axis;
+ QFxRotation axisRotation;
+ qreal rotation;
};
/*!
@@ -65,18 +71,18 @@ public:
\code
<Flipable id="flipable" width="40" height="40">
- <transform>
- <Axis id="axis" xStart="20" xEnd="20" yStart="20" yEnd="0" />
- </transform>
+ <axis>
+ <Axis startX="20" startY="0" endX="20" endY="40" />
+ </axis>
<front>
- <Image file="front.png"/>
+ <Image src="front.png"/>
</front>
<back>
- <Image file="back.png"/>
+ <Image src="back.png"/>
</back>
<states>
<State name="back">
- <SetProperty target="{axis}" property="rotation" value="180" />
+ <SetProperty target="{flipable}" property="rotation" value="180" />
</State>
</states>
<transitions>
@@ -153,6 +159,114 @@ void QFxFlipable::setBack(QFxItem *back)
children()->append(d->back);
if (Front == d->current)
d->back->setOpacity(0.);
+ d->setBackTransform();
+}
+
+/*!
+ \qmlproperty Axis Flipable::axis
+
+ The axis to flip around. See the \l Axis documentation for more
+ information on specifying an axis.
+*/
+
+QFxAxis *QFxFlipable::axis()
+{
+ Q_D(QFxFlipable);
+ return d->axis;
+}
+
+void QFxFlipable::setAxis(QFxAxis *axis)
+{
+ Q_D(QFxFlipable);
+ //### disconnect if we are already connected?
+ if (d->axis)
+ disconnect(d->axis, SIGNAL(updated()), this, SLOT(_q_updateAxis()));
+ d->axis = axis;
+ connect(d->axis, SIGNAL(updated()), this, SLOT(_q_updateAxis()));
+ d->_q_updateAxis();
+}
+
+void QFxFlipablePrivate::_q_updateAxis()
+{
+ axisRotation.axis()->setStartX(axis->startX());
+ axisRotation.axis()->setStartY(axis->startY());
+ axisRotation.axis()->setEndX(axis->endX());
+ axisRotation.axis()->setEndY(axis->endY());
+ axisRotation.axis()->setEndZ(axis->endZ());
+
+ setBackTransform();
+}
+
+void QFxFlipablePrivate::setBackTransform()
+{
+ if (!back)
+ return;
+
+ QPointF p1(0, 0);
+ QPointF p2(1, 0);
+ QPointF p3(1, 1);
+
+ axisRotation.setAngle(180);
+ p1 = axisRotation.transform().map(p1);
+ p2 = axisRotation.transform().map(p2);
+ p3 = axisRotation.transform().map(p3);
+ axisRotation.setAngle(rotation);
+
+ QSimpleCanvas::Matrix mat;
+#ifdef QFX_RENDER_OPENGL
+ mat.translate(back->width()/2,back->height()/2, 0);
+ if (back->width() && p1.x() >= p2.x())
+ mat.rotate(180, 0, 1, 0);
+ if (back->height() && p2.y() >= p3.y())
+ mat.rotate(180, 1, 0, 0);
+ mat.translate(-back->width()/2,-back->height()/2, 0);
+#else
+ mat.translate(back->width()/2,back->height()/2);
+ if (back->width() && p1.x() >= p2.x())
+ mat.rotate(180, Qt::YAxis);
+ if (back->height() && p2.y() >= p3.y())
+ mat.rotate(180, Qt::XAxis);
+ mat.translate(-back->width()/2,-back->height()/2);
+#endif
+ back->setTransform(mat);
+}
+
+/*!
+ \qmlproperty real Flipable::rotation
+ The angle to rotate the flipable. For example, to show the back side of the flipable
+ you can set the rotation to 180.
+*/
+qreal QFxFlipable::rotation() const
+{
+ Q_D(const QFxFlipable);
+ return d->rotation;
+}
+
+void QFxFlipable::setRotation(qreal angle)
+{
+ Q_D(QFxFlipable);
+ d->rotation = angle;
+ d->axisRotation.setAngle(angle);
+ setTransform(d->axisRotation.transform());
+
+
+ int simpleAngle = int(angle) % 360;
+
+ Side newSide;
+ if (simpleAngle < 91 || simpleAngle > 270) {
+ newSide = Front;
+ } else {
+ newSide = Back;
+ }
+
+ if (newSide != d->current) {
+ d->current = newSide;
+ if (d->front)
+ d->front->setOpacity((d->current==Front)?1.:0.);
+ if (d->back)
+ d->back->setOpacity((d->current==Back)?1.:0.);
+ emit sideChanged();
+ }
}
/*!
@@ -167,6 +281,9 @@ QFxFlipable::Side QFxFlipable::side() const
return d->current;
}
+//in some cases the user may want to specify a more complex transformation.
+//in that case, we still allow the generic use of transform.
+//(the logic here should be kept in sync with setBackTransform and setRotation)
void QFxFlipable::transformChanged(const QSimpleCanvas::Matrix &trans)
{
Q_D(QFxFlipable);
@@ -218,3 +335,5 @@ void QFxFlipable::transformChanged(const QSimpleCanvas::Matrix &trans)
}
QT_END_NAMESPACE
+
+#include "moc_qfxflipable.cpp"
diff --git a/src/declarative/fx/qfxflipable.h b/src/declarative/fx/qfxflipable.h
index 2c6c849..ef1832e 100644
--- a/src/declarative/fx/qfxflipable.h
+++ b/src/declarative/fx/qfxflipable.h
@@ -55,6 +55,7 @@ QT_BEGIN_NAMESPACE
QT_MODULE(Declarative)
+class QFxAxis;
class QFxFlipablePrivate;
class Q_DECLARATIVE_EXPORT QFxFlipable : public QFxItem
{
@@ -63,6 +64,8 @@ class Q_DECLARATIVE_EXPORT QFxFlipable : public QFxItem
Q_ENUMS(Side);
Q_PROPERTY(QFxItem *front READ front WRITE setFront)
Q_PROPERTY(QFxItem *back READ back WRITE setBack)
+ Q_PROPERTY(QFxAxis *axis READ axis WRITE setAxis)
+ Q_PROPERTY(qreal rotation READ rotation WRITE setRotation)
Q_PROPERTY(Side side READ side NOTIFY sideChanged)
public:
QFxFlipable(QFxItem *parent=0);
@@ -74,6 +77,12 @@ public:
QFxItem *back();
void setBack(QFxItem *);
+ QFxAxis *axis();
+ void setAxis(QFxAxis *axis);
+
+ qreal rotation() const;
+ void setRotation(qreal angle);
+
enum Side { Front, Back };
Side side() const;
@@ -84,6 +93,7 @@ Q_SIGNALS:
void sideChanged();
private:
+ Q_PRIVATE_SLOT(d_func(), void _q_updateAxis())
Q_DISABLE_COPY(QFxFlipable)
Q_DECLARE_PRIVATE(QFxFlipable)
};
diff --git a/src/declarative/fx/qfxtransform.cpp b/src/declarative/fx/qfxtransform.cpp
index 9f18413..c355158 100644
--- a/src/declarative/fx/qfxtransform.cpp
+++ b/src/declarative/fx/qfxtransform.cpp
@@ -85,7 +85,16 @@ void QFxTransform::update()
/*!
\qmlclass Axis
- \brief An axis that can be used for rotation or translation.
+ \brief The Axis element defines an axis that can be used for rotation or translation.
+
+ An axis is specified by 2 points in 3D space: a start point and
+ an end point. While technically the axis is the line running through these two points
+ (and thus many different sets of two points could define the same axis), the distance
+ between the points does matter for translation along an axis.
+
+ \code
+ <Axis startX="0" startY="0" endX="20" endY="30"/>
+ \endcode
*/
QML_DEFINE_TYPE(QFxAxis, Axis);
@@ -99,6 +108,13 @@ QFxAxis::~QFxAxis()
{
}
+/*!
+ \qmlproperty real Axis::startX
+ \qmlproperty real Axis::startY
+
+ The start point of the axis. The z-position of the start point is assumed to be 0, and cannot
+ be changed.
+*/
qreal QFxAxis::startX() const
{
return _startX;
@@ -121,6 +137,13 @@ void QFxAxis::setStartY(qreal y)
emit updated();
}
+/*!
+ \qmlproperty real Axis::endX
+ \qmlproperty real Axis::endY
+ \qmlproperty real Axis::endZ
+
+ The end point of the axis.
+*/
qreal QFxAxis::endX() const
{
return _endX;