examples/animation/appchooser/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 <QtCore>
       
    43 #include <QtGui>
       
    44 
       
    45 
       
    46 class Pixmap : public QGraphicsWidget
       
    47 {
       
    48     Q_OBJECT
       
    49 
       
    50 public:
       
    51     Pixmap(const QPixmap &pix, QGraphicsItem *parent = 0)
       
    52         : QGraphicsWidget(parent), orig(pix), p(pix)
       
    53     {
       
    54     }
       
    55 
       
    56     void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
       
    57     {
       
    58         painter->drawPixmap(QPointF(), p);
       
    59     }
       
    60 
       
    61     virtual void mousePressEvent(QGraphicsSceneMouseEvent * )
       
    62     {
       
    63         emit clicked();
       
    64     }
       
    65 
       
    66     virtual void setGeometry(const QRectF &rect)
       
    67     {
       
    68         QGraphicsWidget::setGeometry(rect);
       
    69 
       
    70         if (rect.size().width() > orig.size().width())
       
    71             p = orig.scaled(rect.size().toSize());
       
    72         else
       
    73             p = orig;
       
    74     }
       
    75 
       
    76 Q_SIGNALS:
       
    77     void clicked();
       
    78 
       
    79 private:
       
    80     QPixmap orig;
       
    81     QPixmap p;
       
    82 };
       
    83 
       
    84 void createStates(const QObjectList &objects,
       
    85                   const QRect &selectedRect, QState *parent)
       
    86 {
       
    87     for (int i = 0; i < objects.size(); ++i) {
       
    88         QState *state = new QState(parent);
       
    89         state->assignProperty(objects.at(i), "geometry", selectedRect);
       
    90         parent->addTransition(objects.at(i), SIGNAL(clicked()), state);
       
    91     }
       
    92 }
       
    93 
       
    94 void createAnimations(const QObjectList &objects, QStateMachine *machine)
       
    95 {
       
    96     for (int i=0; i<objects.size(); ++i)
       
    97         machine->addDefaultAnimation(new QPropertyAnimation(objects.at(i), "geometry"));    
       
    98 }
       
    99 
       
   100 int main(int argc, char **argv)
       
   101 {
       
   102     Q_INIT_RESOURCE(appchooser);
       
   103 
       
   104     QApplication app(argc, argv);
       
   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 
       
   111     p1->setObjectName("p1");
       
   112     p2->setObjectName("p2");
       
   113     p3->setObjectName("p3");
       
   114     p4->setObjectName("p4");
       
   115 
       
   116     p1->setGeometry(QRectF(0.0, 0.0, 64.0, 64.0));
       
   117     p2->setGeometry(QRectF(236.0, 0.0, 64.0, 64.0));
       
   118     p3->setGeometry(QRectF(236.0, 236.0, 64.0, 64.0));
       
   119     p4->setGeometry(QRectF(0.0, 236.0, 64.0, 64.0));
       
   120 
       
   121     QGraphicsScene scene(0, 0, 300, 300);
       
   122     scene.setBackgroundBrush(Qt::white);
       
   123     scene.addItem(p1);
       
   124     scene.addItem(p2);
       
   125     scene.addItem(p3);
       
   126     scene.addItem(p4);
       
   127 
       
   128     QGraphicsView window(&scene);
       
   129     window.setFrameStyle(0);
       
   130     window.setAlignment(Qt::AlignLeft | Qt::AlignTop);
       
   131     window.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
       
   132     window.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
       
   133 
       
   134     QStateMachine machine;
       
   135     machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties);
       
   136 
       
   137     QState *group = new QState(&machine);
       
   138     group->setObjectName("group");
       
   139     QRect selectedRect(86, 86, 128, 128);
       
   140 
       
   141     QState *idleState = new QState(group);
       
   142     group->setInitialState(idleState);
       
   143 
       
   144     QObjectList objects; 
       
   145     objects << p1 << p2 << p3 << p4;
       
   146     createStates(objects, selectedRect, group);
       
   147     createAnimations(objects, &machine);
       
   148 
       
   149     machine.setInitialState(group);
       
   150     machine.start();
       
   151 
       
   152     window.resize(300, 300);
       
   153     window.show();
       
   154 
       
   155     return app.exec();
       
   156 }
       
   157 
       
   158 #include "main.moc"