1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtCore module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtCore>
|
---|
42 | #include <stdio.h>
|
---|
43 |
|
---|
44 | //! [0]
|
---|
45 | class Factorial : public QObject
|
---|
46 | {
|
---|
47 | Q_OBJECT
|
---|
48 | Q_PROPERTY(int x READ x WRITE setX)
|
---|
49 | Q_PROPERTY(int fac READ fac WRITE setFac)
|
---|
50 | public:
|
---|
51 | Factorial(QObject *parent = 0)
|
---|
52 | : QObject(parent), m_x(-1), m_fac(1)
|
---|
53 | {
|
---|
54 | }
|
---|
55 |
|
---|
56 | int x() const
|
---|
57 | {
|
---|
58 | return m_x;
|
---|
59 | }
|
---|
60 |
|
---|
61 | void setX(int x)
|
---|
62 | {
|
---|
63 | if (x == m_x)
|
---|
64 | return;
|
---|
65 | m_x = x;
|
---|
66 | emit xChanged(x);
|
---|
67 | }
|
---|
68 |
|
---|
69 | int fac() const
|
---|
70 | {
|
---|
71 | return m_fac;
|
---|
72 | }
|
---|
73 |
|
---|
74 | void setFac(int fac)
|
---|
75 | {
|
---|
76 | m_fac = fac;
|
---|
77 | }
|
---|
78 |
|
---|
79 | Q_SIGNALS:
|
---|
80 | void xChanged(int value);
|
---|
81 |
|
---|
82 | private:
|
---|
83 | int m_x;
|
---|
84 | int m_fac;
|
---|
85 | };
|
---|
86 | //! [0]
|
---|
87 |
|
---|
88 | //! [1]
|
---|
89 | class FactorialLoopTransition : public QSignalTransition
|
---|
90 | {
|
---|
91 | public:
|
---|
92 | FactorialLoopTransition(Factorial *fact)
|
---|
93 | : QSignalTransition(fact, SIGNAL(xChanged(int))), m_fact(fact)
|
---|
94 | {}
|
---|
95 |
|
---|
96 | virtual bool eventTest(QEvent *e)
|
---|
97 | {
|
---|
98 | if (!QSignalTransition::eventTest(e))
|
---|
99 | return false;
|
---|
100 | QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e);
|
---|
101 | return se->arguments().at(0).toInt() > 1;
|
---|
102 | }
|
---|
103 |
|
---|
104 | virtual void onTransition(QEvent *e)
|
---|
105 | {
|
---|
106 | QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e);
|
---|
107 | int x = se->arguments().at(0).toInt();
|
---|
108 | int fac = m_fact->property("fac").toInt();
|
---|
109 | m_fact->setProperty("fac", x * fac);
|
---|
110 | m_fact->setProperty("x", x - 1);
|
---|
111 | }
|
---|
112 |
|
---|
113 | private:
|
---|
114 | Factorial *m_fact;
|
---|
115 | };
|
---|
116 | //! [1]
|
---|
117 |
|
---|
118 | //! [2]
|
---|
119 | class FactorialDoneTransition : public QSignalTransition
|
---|
120 | {
|
---|
121 | public:
|
---|
122 | FactorialDoneTransition(Factorial *fact)
|
---|
123 | : QSignalTransition(fact, SIGNAL(xChanged(int))), m_fact(fact)
|
---|
124 | {}
|
---|
125 |
|
---|
126 | virtual bool eventTest(QEvent *e)
|
---|
127 | {
|
---|
128 | if (!QSignalTransition::eventTest(e))
|
---|
129 | return false;
|
---|
130 | QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e);
|
---|
131 | return se->arguments().at(0).toInt() <= 1;
|
---|
132 | }
|
---|
133 |
|
---|
134 | virtual void onTransition(QEvent *)
|
---|
135 | {
|
---|
136 | fprintf(stdout, "%d\n", m_fact->property("fac").toInt());
|
---|
137 | }
|
---|
138 |
|
---|
139 | private:
|
---|
140 | Factorial *m_fact;
|
---|
141 | };
|
---|
142 | //! [2]
|
---|
143 |
|
---|
144 | //! [3]
|
---|
145 | int main(int argc, char **argv)
|
---|
146 | {
|
---|
147 | QCoreApplication app(argc, argv);
|
---|
148 | Factorial factorial;
|
---|
149 | QStateMachine machine;
|
---|
150 | //! [3]
|
---|
151 |
|
---|
152 | //! [4]
|
---|
153 | QState *compute = new QState(&machine);
|
---|
154 | compute->assignProperty(&factorial, "fac", 1);
|
---|
155 | compute->assignProperty(&factorial, "x", 6);
|
---|
156 | compute->addTransition(new FactorialLoopTransition(&factorial));
|
---|
157 | //! [4]
|
---|
158 |
|
---|
159 | //! [5]
|
---|
160 | QFinalState *done = new QFinalState(&machine);
|
---|
161 | FactorialDoneTransition *doneTransition = new FactorialDoneTransition(&factorial);
|
---|
162 | doneTransition->setTargetState(done);
|
---|
163 | compute->addTransition(doneTransition);
|
---|
164 | //! [5]
|
---|
165 |
|
---|
166 | //! [6]
|
---|
167 | machine.setInitialState(compute);
|
---|
168 | QObject::connect(&machine, SIGNAL(finished()), &app, SLOT(quit()));
|
---|
169 | machine.start();
|
---|
170 |
|
---|
171 | return app.exec();
|
---|
172 | }
|
---|
173 | //! [6]
|
---|
174 |
|
---|
175 | #include "main.moc"
|
---|