examples/animation/states/main.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtCore module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 class Pixmap : public QGraphicsObject
       
    45 {
       
    46     Q_OBJECT
       
    47 public:
       
    48     Pixmap(const QPixmap &pix) : QGraphicsObject(), p(pix)
       
    49     {
       
    50     }
       
    51 
       
    52     void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
       
    53     {
       
    54         painter->drawPixmap(QPointF(), p);
       
    55     }
       
    56 
       
    57     QRectF boundingRect() const
       
    58     {
       
    59         return QRectF( QPointF(0, 0), p.size());
       
    60     }
       
    61 
       
    62 private:
       
    63     QPixmap p;
       
    64 };
       
    65 
       
    66 int main(int argc, char *argv[])
       
    67 {
       
    68     Q_INIT_RESOURCE(states);
       
    69 
       
    70     QApplication app(argc, argv);
       
    71 
       
    72     // Text edit and button
       
    73     QTextEdit *edit = new QTextEdit;
       
    74     edit->setText("asdf lkjha yuoiqwe asd iuaysd u iasyd uiy "
       
    75                   "asdf lkjha yuoiqwe asd iuaysd u iasyd uiy "
       
    76                   "asdf lkjha yuoiqwe asd iuaysd u iasyd uiy "
       
    77                   "asdf lkjha yuoiqwe asd iuaysd u iasyd uiy!");
       
    78 
       
    79     QPushButton *button = new QPushButton;
       
    80     QGraphicsProxyWidget *buttonProxy = new QGraphicsProxyWidget;
       
    81     buttonProxy->setWidget(button);
       
    82     QGraphicsProxyWidget *editProxy = new QGraphicsProxyWidget;
       
    83     editProxy->setWidget(edit);
       
    84 
       
    85     QGroupBox *box = new QGroupBox;
       
    86     box->setFlat(true);
       
    87     box->setTitle("Options");
       
    88 
       
    89     QVBoxLayout *layout2 = new QVBoxLayout;
       
    90     box->setLayout(layout2);
       
    91     layout2->addWidget(new QRadioButton("Herring"));
       
    92     layout2->addWidget(new QRadioButton("Blue Parrot"));
       
    93     layout2->addWidget(new QRadioButton("Petunias"));
       
    94     layout2->addStretch();
       
    95 
       
    96     QGraphicsProxyWidget *boxProxy = new QGraphicsProxyWidget;
       
    97     boxProxy->setWidget(box);
       
    98 
       
    99     // Parent widget
       
   100     QGraphicsWidget *widget = new QGraphicsWidget;
       
   101     QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical, widget);
       
   102     layout->addItem(editProxy);
       
   103     layout->addItem(buttonProxy);
       
   104     widget->setLayout(layout);
       
   105 
       
   106     Pixmap *p1 = new Pixmap(QPixmap(":/digikam.png"));
       
   107     Pixmap *p2 = new Pixmap(QPixmap(":/akregator.png"));
       
   108     Pixmap *p3 = new Pixmap(QPixmap(":/accessories-dictionary.png"));
       
   109     Pixmap *p4 = new Pixmap(QPixmap(":/k3b.png"));
       
   110     Pixmap *p5 = new Pixmap(QPixmap(":/help-browser.png"));
       
   111     Pixmap *p6 = new Pixmap(QPixmap(":/kchart.png"));
       
   112 
       
   113     QGraphicsScene scene(0, 0, 400, 300);
       
   114     scene.setBackgroundBrush(scene.palette().window());
       
   115     scene.addItem(widget);
       
   116     scene.addItem(boxProxy);
       
   117     scene.addItem(p1);
       
   118     scene.addItem(p2);
       
   119     scene.addItem(p3);
       
   120     scene.addItem(p4);
       
   121     scene.addItem(p5);
       
   122     scene.addItem(p6);
       
   123 
       
   124     QStateMachine machine;
       
   125     QState *state1 = new QState(&machine);
       
   126     QState *state2 = new QState(&machine);
       
   127     QState *state3 = new QState(&machine);
       
   128     machine.setInitialState(state1);
       
   129 
       
   130     // State 1
       
   131     state1->assignProperty(button, "text", "Switch to state 2");
       
   132     state1->assignProperty(widget, "geometry", QRectF(0, 0, 400, 150));
       
   133     state1->assignProperty(box, "geometry", QRect(-200, 150, 200, 150));
       
   134     state1->assignProperty(p1, "pos", QPointF(68, 185));
       
   135     state1->assignProperty(p2, "pos", QPointF(168, 185));
       
   136     state1->assignProperty(p3, "pos", QPointF(268, 185));
       
   137     state1->assignProperty(p4, "pos", QPointF(68-150, 48-150));
       
   138     state1->assignProperty(p5, "pos", QPointF(168, 48-150));
       
   139     state1->assignProperty(p6, "pos", QPointF(268+150, 48-150));
       
   140     state1->assignProperty(p1, "rotation", qreal(0));
       
   141     state1->assignProperty(p2, "rotation", qreal(0));
       
   142     state1->assignProperty(p3, "rotation", qreal(0));
       
   143     state1->assignProperty(p4, "rotation", qreal(-270));
       
   144     state1->assignProperty(p5, "rotation", qreal(-90));
       
   145     state1->assignProperty(p6, "rotation", qreal(270));
       
   146     state1->assignProperty(boxProxy, "opacity", qreal(0));
       
   147     state1->assignProperty(p1, "opacity", qreal(1));
       
   148     state1->assignProperty(p2, "opacity", qreal(1));
       
   149     state1->assignProperty(p3, "opacity", qreal(1));
       
   150     state1->assignProperty(p4, "opacity", qreal(0));
       
   151     state1->assignProperty(p5, "opacity", qreal(0));
       
   152     state1->assignProperty(p6, "opacity", qreal(0));
       
   153 
       
   154     // State 2
       
   155     state2->assignProperty(button, "text", "Switch to state 3");
       
   156     state2->assignProperty(widget, "geometry", QRectF(200, 150, 200, 150));
       
   157     state2->assignProperty(box, "geometry", QRect(9, 150, 190, 150));
       
   158     state2->assignProperty(p1, "pos", QPointF(68-150, 185+150));
       
   159     state2->assignProperty(p2, "pos", QPointF(168, 185+150));
       
   160     state2->assignProperty(p3, "pos", QPointF(268+150, 185+150));
       
   161     state2->assignProperty(p4, "pos", QPointF(64, 48));
       
   162     state2->assignProperty(p5, "pos", QPointF(168, 48));
       
   163     state2->assignProperty(p6, "pos", QPointF(268, 48));
       
   164     state2->assignProperty(p1, "rotation", qreal(-270));
       
   165     state2->assignProperty(p2, "rotation", qreal(90));
       
   166     state2->assignProperty(p3, "rotation", qreal(270));
       
   167     state2->assignProperty(p4, "rotation", qreal(0));
       
   168     state2->assignProperty(p5, "rotation", qreal(0));
       
   169     state2->assignProperty(p6, "rotation", qreal(0));
       
   170     state2->assignProperty(boxProxy, "opacity", qreal(1));
       
   171     state2->assignProperty(p1, "opacity", qreal(0));
       
   172     state2->assignProperty(p2, "opacity", qreal(0));
       
   173     state2->assignProperty(p3, "opacity", qreal(0));
       
   174     state2->assignProperty(p4, "opacity", qreal(1));
       
   175     state2->assignProperty(p5, "opacity", qreal(1));
       
   176     state2->assignProperty(p6, "opacity", qreal(1));
       
   177 
       
   178     // State 3
       
   179     state3->assignProperty(button, "text", "Switch to state 1");
       
   180     state3->assignProperty(p1, "pos", QPointF(0, 5));
       
   181     state3->assignProperty(p2, "pos", QPointF(0, 5 + 64 + 5));
       
   182     state3->assignProperty(p3, "pos", QPointF(5, 5 + (64 + 5) + 64));
       
   183     state3->assignProperty(p4, "pos", QPointF(5 + 64 + 5, 5));
       
   184     state3->assignProperty(p5, "pos", QPointF(5 + 64 + 5, 5 + 64 + 5));
       
   185     state3->assignProperty(p6, "pos", QPointF(5 + 64 + 5, 5 + (64 + 5) + 64));
       
   186     state3->assignProperty(widget, "geometry", QRectF(138, 5, 400 - 138, 200));
       
   187     state3->assignProperty(box, "geometry", QRect(5, 205, 400, 90));
       
   188     state3->assignProperty(p1, "opacity", qreal(1));
       
   189     state3->assignProperty(p2, "opacity", qreal(1));
       
   190     state3->assignProperty(p3, "opacity", qreal(1));
       
   191     state3->assignProperty(p4, "opacity", qreal(1));
       
   192     state3->assignProperty(p5, "opacity", qreal(1));
       
   193     state3->assignProperty(p6, "opacity", qreal(1));
       
   194 
       
   195     QAbstractTransition *t1 = state1->addTransition(button, SIGNAL(clicked()), state2);
       
   196     QSequentialAnimationGroup *animation1SubGroup = new QSequentialAnimationGroup;
       
   197     animation1SubGroup->addPause(250);
       
   198     animation1SubGroup->addAnimation(new QPropertyAnimation(box, "geometry"));
       
   199     t1->addAnimation(animation1SubGroup);
       
   200     t1->addAnimation(new QPropertyAnimation(widget, "geometry"));
       
   201     t1->addAnimation(new QPropertyAnimation(p1, "pos"));
       
   202     t1->addAnimation(new QPropertyAnimation(p2, "pos"));
       
   203     t1->addAnimation(new QPropertyAnimation(p3, "pos"));
       
   204     t1->addAnimation(new QPropertyAnimation(p4, "pos"));
       
   205     t1->addAnimation(new QPropertyAnimation(p5, "pos"));
       
   206     t1->addAnimation(new QPropertyAnimation(p6, "pos"));
       
   207     t1->addAnimation(new QPropertyAnimation(p1, "rotation"));
       
   208     t1->addAnimation(new QPropertyAnimation(p2, "rotation"));
       
   209     t1->addAnimation(new QPropertyAnimation(p3, "rotation"));
       
   210     t1->addAnimation(new QPropertyAnimation(p4, "rotation"));
       
   211     t1->addAnimation(new QPropertyAnimation(p5, "rotation"));
       
   212     t1->addAnimation(new QPropertyAnimation(p6, "rotation"));
       
   213     t1->addAnimation(new QPropertyAnimation(p1, "opacity"));
       
   214     t1->addAnimation(new QPropertyAnimation(p2, "opacity"));
       
   215     t1->addAnimation(new QPropertyAnimation(p3, "opacity"));
       
   216     t1->addAnimation(new QPropertyAnimation(p4, "opacity"));
       
   217     t1->addAnimation(new QPropertyAnimation(p5, "opacity"));
       
   218     t1->addAnimation(new QPropertyAnimation(p6, "opacity"));
       
   219 
       
   220     QAbstractTransition *t2 = state2->addTransition(button, SIGNAL(clicked()), state3);
       
   221     t2->addAnimation(new QPropertyAnimation(box, "geometry"));
       
   222     t2->addAnimation(new QPropertyAnimation(widget, "geometry"));
       
   223     t2->addAnimation(new QPropertyAnimation(p1, "pos"));
       
   224     t2->addAnimation(new QPropertyAnimation(p2, "pos"));
       
   225     t2->addAnimation(new QPropertyAnimation(p3, "pos"));
       
   226     t2->addAnimation(new QPropertyAnimation(p4, "pos"));
       
   227     t2->addAnimation(new QPropertyAnimation(p5, "pos"));
       
   228     t2->addAnimation(new QPropertyAnimation(p6, "pos"));
       
   229     t2->addAnimation(new QPropertyAnimation(p1, "rotation"));
       
   230     t2->addAnimation(new QPropertyAnimation(p2, "rotation"));
       
   231     t2->addAnimation(new QPropertyAnimation(p3, "rotation"));
       
   232     t2->addAnimation(new QPropertyAnimation(p4, "rotation"));
       
   233     t2->addAnimation(new QPropertyAnimation(p5, "rotation"));
       
   234     t2->addAnimation(new QPropertyAnimation(p6, "rotation"));
       
   235     t2->addAnimation(new QPropertyAnimation(p1, "opacity"));
       
   236     t2->addAnimation(new QPropertyAnimation(p2, "opacity"));
       
   237     t2->addAnimation(new QPropertyAnimation(p3, "opacity"));
       
   238     t2->addAnimation(new QPropertyAnimation(p4, "opacity"));
       
   239     t2->addAnimation(new QPropertyAnimation(p5, "opacity"));
       
   240     t2->addAnimation(new QPropertyAnimation(p6, "opacity"));
       
   241 
       
   242     QAbstractTransition *t3 = state3->addTransition(button, SIGNAL(clicked()), state1);
       
   243     t3->addAnimation(new QPropertyAnimation(box, "geometry"));
       
   244     t3->addAnimation(new QPropertyAnimation(widget, "geometry"));
       
   245     t3->addAnimation(new QPropertyAnimation(p1, "pos"));
       
   246     t3->addAnimation(new QPropertyAnimation(p2, "pos"));
       
   247     t3->addAnimation(new QPropertyAnimation(p3, "pos"));
       
   248     t3->addAnimation(new QPropertyAnimation(p4, "pos"));
       
   249     t3->addAnimation(new QPropertyAnimation(p5, "pos"));
       
   250     t3->addAnimation(new QPropertyAnimation(p6, "pos"));
       
   251     t3->addAnimation(new QPropertyAnimation(p1, "rotation"));
       
   252     t3->addAnimation(new QPropertyAnimation(p2, "rotation"));
       
   253     t3->addAnimation(new QPropertyAnimation(p3, "rotation"));
       
   254     t3->addAnimation(new QPropertyAnimation(p4, "rotation"));
       
   255     t3->addAnimation(new QPropertyAnimation(p5, "rotation"));
       
   256     t3->addAnimation(new QPropertyAnimation(p6, "rotation"));
       
   257     t3->addAnimation(new QPropertyAnimation(p1, "opacity"));
       
   258     t3->addAnimation(new QPropertyAnimation(p2, "opacity"));
       
   259     t3->addAnimation(new QPropertyAnimation(p3, "opacity"));
       
   260     t3->addAnimation(new QPropertyAnimation(p4, "opacity"));
       
   261     t3->addAnimation(new QPropertyAnimation(p5, "opacity"));
       
   262     t3->addAnimation(new QPropertyAnimation(p6, "opacity"));
       
   263 
       
   264     machine.start();
       
   265 
       
   266     QGraphicsView view(&scene);
       
   267     view.show();
       
   268 
       
   269     return app.exec();
       
   270 }
       
   271 
       
   272 #include "main.moc"