examples/statemachine/factorial/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 <stdio.h>
       
    44 
       
    45 //! [0]
       
    46 class Factorial : public QObject
       
    47 {
       
    48     Q_OBJECT
       
    49     Q_PROPERTY(int x READ x WRITE setX)
       
    50     Q_PROPERTY(int fac READ fac WRITE setFac)
       
    51 public:
       
    52     Factorial(QObject *parent = 0)
       
    53         : QObject(parent), m_x(-1), m_fac(1)
       
    54     {
       
    55     }
       
    56 
       
    57     int x() const
       
    58     {
       
    59         return m_x;
       
    60     }
       
    61 
       
    62     void setX(int x)
       
    63     {
       
    64         if (x == m_x)
       
    65             return;
       
    66         m_x = x;
       
    67         emit xChanged(x);
       
    68     }
       
    69 
       
    70     int fac() const
       
    71     {
       
    72         return m_fac;
       
    73     }
       
    74 
       
    75     void setFac(int fac)
       
    76     {
       
    77         m_fac = fac;
       
    78     }
       
    79 
       
    80 Q_SIGNALS:
       
    81     void xChanged(int value);
       
    82     
       
    83 private:
       
    84     int m_x;
       
    85     int m_fac;
       
    86 };
       
    87 //! [0]
       
    88 
       
    89 //! [1]
       
    90 class FactorialLoopTransition : public QSignalTransition
       
    91 {
       
    92 public:
       
    93     FactorialLoopTransition(Factorial *fact)
       
    94         : QSignalTransition(fact, SIGNAL(xChanged(int))), m_fact(fact)
       
    95     {}
       
    96 
       
    97     virtual bool eventTest(QEvent *e)
       
    98     {
       
    99         if (!QSignalTransition::eventTest(e))
       
   100             return false;
       
   101         QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e);
       
   102         return se->arguments().at(0).toInt() > 1;
       
   103     }
       
   104 
       
   105     virtual void onTransition(QEvent *e)
       
   106     {
       
   107         QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e);
       
   108         int x = se->arguments().at(0).toInt();
       
   109         int fac = m_fact->property("fac").toInt();
       
   110         m_fact->setProperty("fac",  x * fac);
       
   111         m_fact->setProperty("x",  x - 1);
       
   112     }
       
   113 
       
   114 private:
       
   115     Factorial *m_fact;
       
   116 };
       
   117 //! [1]
       
   118 
       
   119 //! [2]
       
   120 class FactorialDoneTransition : public QSignalTransition
       
   121 {
       
   122 public:
       
   123     FactorialDoneTransition(Factorial *fact)
       
   124         : QSignalTransition(fact, SIGNAL(xChanged(int))), m_fact(fact)
       
   125     {}
       
   126 
       
   127     virtual bool eventTest(QEvent *e)
       
   128     {
       
   129         if (!QSignalTransition::eventTest(e))
       
   130             return false;
       
   131         QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e);
       
   132         return se->arguments().at(0).toInt() <= 1;
       
   133     }
       
   134 
       
   135     virtual void onTransition(QEvent *)
       
   136     {
       
   137         fprintf(stdout, "%d\n", m_fact->property("fac").toInt());
       
   138     }
       
   139 
       
   140 private:
       
   141     Factorial *m_fact;
       
   142 };
       
   143 //! [2]
       
   144 
       
   145 //! [3]
       
   146 int main(int argc, char **argv)
       
   147 {
       
   148     QCoreApplication app(argc, argv);
       
   149     Factorial factorial;
       
   150     QStateMachine machine;
       
   151 //! [3]
       
   152 
       
   153 //! [4]
       
   154     QState *compute = new QState(&machine);
       
   155     compute->assignProperty(&factorial, "fac", 1);
       
   156     compute->assignProperty(&factorial, "x", 6);
       
   157     compute->addTransition(new FactorialLoopTransition(&factorial));
       
   158 //! [4]
       
   159 
       
   160 //! [5]
       
   161     QFinalState *done = new QFinalState(&machine);
       
   162     FactorialDoneTransition *doneTransition = new FactorialDoneTransition(&factorial);
       
   163     doneTransition->setTargetState(done);
       
   164     compute->addTransition(doneTransition);
       
   165 //! [5]
       
   166 
       
   167 //! [6]
       
   168     machine.setInitialState(compute);
       
   169     QObject::connect(&machine, SIGNAL(finished()), &app, SLOT(quit()));
       
   170     machine.start();
       
   171 
       
   172     return app.exec();
       
   173 }
       
   174 //! [6]
       
   175 
       
   176 #include "main.moc"