/**************************************************************************** ** ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). ** Contact: Qt Software Information (qt-info@nokia.com) ** ** This file is part of the QtDeclarative module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** No Commercial Usage ** This file contains pre-release code and may not be distributed. ** You may use this file in accordance with the terms and conditions ** contained in the either Technology Preview License Agreement or the ** Beta Release License Agreement. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Nokia gives you certain ** additional rights. These rights are described in the Nokia Qt LGPL ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this ** package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** If you are unsure which license is appropriate for your use, please ** contact the sales department at qt-sales@nokia.com. ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "qfxflipable.h" #include "private/qfxitem_p.h" #include "QtGui/qgraphicstransform.h" #include QT_BEGIN_NAMESPACE QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,Flipable,QFxFlipable) class QFxFlipablePrivate : public QFxItemPrivate { Q_DECLARE_PUBLIC(QFxFlipable) public: QFxFlipablePrivate() : current(QFxFlipable::Front), front(0), back(0) {} void updateSceneTransformFromParent(); QFxFlipable::Side current; QFxItem *front; QFxItem *back; }; /*! \qmlclass Flipable QFxFlipable \brief The Flipable item provides a surface that can be flipped. \inherits Item Flipable allows you to specify a front and a back and then flip between those sides. \qml Flipable { id: flipable width: 40 height: 40 axis: Axis { startX: 20 startY: 0 endX: 20 endY: 40 } front: Image { source: "front.png" } back: Image { source: "back.png" } states: [ State { name: "back" SetProperty { target: flipable property: "rotation" value: 180 } } ] transitions: [ Transition { NumberAnimation { easing: "easeInOutQuad" properties: "rotation" } } ] } \endqml \image flipable.gif */ /*! \internal \class QFxFlipable \brief The QFxFlipable class provides a flipable surface. \ingroup group_widgets QFxFlipable allows you to specify a front and a back, as well as an axis for the flip. */ QFxFlipable::QFxFlipable(QFxItem *parent) : QFxItem(*(new QFxFlipablePrivate), parent) { } QFxFlipable::~QFxFlipable() { } /*! \qmlproperty Item Flipable::front \qmlproperty Item Flipable::back The front and back sides of the flipable. */ QFxItem *QFxFlipable::front() { Q_D(const QFxFlipable); return d->front; } void QFxFlipable::setFront(QFxItem *front) { Q_D(QFxFlipable); if (d->front) { qmlInfo(tr("front is a write-once property"),this); return; } d->front = front; fxChildren()->append(d->front); if (Back == d->current) d->front->setOpacity(0.); } QFxItem *QFxFlipable::back() { Q_D(const QFxFlipable); return d->back; } void QFxFlipable::setBack(QFxItem *back) { Q_D(QFxFlipable); if (d->back) { qmlInfo(tr("back is a write-once property"),this); return; } d->back = back; fxChildren()->append(d->back); if (Front == d->current) d->back->setOpacity(0.); } /*! \qmlproperty enumeration Flipable::side The side of the Flippable currently visible. Possible values are \c Front and \c Back. */ QFxFlipable::Side QFxFlipable::side() const { Q_D(const QFxFlipable); if (d->dirtySceneTransform) const_cast(d)->updateSceneTransformFromParent(); return d->current; } // determination on the currently visible side of the flipable // has to be done on the complete scene transform to give // correct results. void QFxFlipablePrivate::updateSceneTransformFromParent() { Q_Q(QFxFlipable); QFxItemPrivate::updateSceneTransformFromParent(); QPointF p1(0, 0); QPointF p2(1, 0); QPointF p3(1, 1); p1 = sceneTransform.map(p1); p2 = sceneTransform.map(p2); p3 = sceneTransform.map(p3); qreal cross = (p1.x() - p2.x()) * (p3.y() - p2.y()) - (p1.y() - p2.y()) * (p3.x() - p2.x()); QFxFlipable::Side newSide; if (cross > 0) { newSide = QFxFlipable::Back; } else { newSide = QFxFlipable::Front; } if (newSide != current) { current = newSide; if (current == QFxFlipable::Back) { QTransform mat; 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); back->setTransform(mat); } if (front) front->setOpacity((current==QFxFlipable::Front)?1.:0.); if (back) back->setOpacity((current==QFxFlipable::Back)?1.:0.); emit q->sideChanged(); } } QT_END_NAMESPACE #include "moc_qfxflipable.cpp"