~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/statemachine-factorial-main-cpp.html

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<head>
 
6
  <title>Qt 4.6: main.cpp Example File (statemachine/factorial/main.cpp)</title>
 
7
  <link href="classic.css" rel="stylesheet" type="text/css" />
 
8
</head>
 
9
<body>
 
10
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
11
<tr>
 
12
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
 
13
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td><td class="searchBar" align="right" valign="center"><form action="http://www.google.com/cse" id="cse-search-box"><div><input type="hidden" name="cx" value="000136343326384750312:dhbxnqlakyu" /><input type="hidden" name="ie" value="UTF-8" /><input type="text" name="q" size="31" /><input type="submit" name="sa" value="Search" /></div></form></td>
 
14
</tr></table><h1 class="title">main.cpp Example File<br /><span class="small-subtitle">statemachine/factorial/main.cpp</span>
 
15
</h1>
 
16
<pre><span class="comment"> /****************************************************************************
 
17
 **
 
18
 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 
19
 ** All rights reserved.
 
20
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 
21
 **
 
22
 ** This file is part of the QtCore module of the Qt Toolkit.
 
23
 **
 
24
 ** $QT_BEGIN_LICENSE:LGPL$
 
25
 ** No Commercial Usage
 
26
 ** This file contains pre-release code and may not be distributed.
 
27
 ** You may use this file in accordance with the terms and conditions
 
28
 ** contained in the Technology Preview License Agreement accompanying
 
29
 ** this package.
 
30
 **
 
31
 ** GNU Lesser General Public License Usage
 
32
 ** Alternatively, this file may be used under the terms of the GNU Lesser
 
33
 ** General Public License version 2.1 as published by the Free Software
 
34
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 
35
 ** packaging of this file.  Please review the following information to
 
36
 ** ensure the GNU Lesser General Public License version 2.1 requirements
 
37
 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
38
 **
 
39
 ** In addition, as a special exception, Nokia gives you certain additional
 
40
 ** rights.  These rights are described in the Nokia Qt LGPL Exception
 
41
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
42
 **
 
43
 ** If you have questions regarding the use of this file, please contact
 
44
 ** Nokia at qt-info@nokia.com.
 
45
 **
 
46
 **
 
47
 **
 
48
 **
 
49
 **
 
50
 **
 
51
 **
 
52
 **
 
53
 ** $QT_END_LICENSE$
 
54
 **
 
55
 ****************************************************************************/</span>
 
56
 
 
57
 #include &lt;QtCore&gt;
 
58
 #include &lt;stdio.h&gt;
 
59
 
 
60
 class Factorial : public QObject
 
61
 {
 
62
     Q_OBJECT
 
63
     Q_PROPERTY(int x READ x WRITE setX)
 
64
     Q_PROPERTY(int fac READ fac WRITE setFac)
 
65
 public:
 
66
     Factorial(QObject *parent = 0)
 
67
         : QObject(parent), m_x(-1), m_fac(1)
 
68
     {
 
69
     }
 
70
 
 
71
     int x() const
 
72
     {
 
73
         return m_x;
 
74
     }
 
75
 
 
76
     void setX(int x)
 
77
     {
 
78
         if (x == m_x)
 
79
             return;
 
80
         m_x = x;
 
81
         emit xChanged(x);
 
82
     }
 
83
 
 
84
     int fac() const
 
85
     {
 
86
         return m_fac;
 
87
     }
 
88
 
 
89
     void setFac(int fac)
 
90
     {
 
91
         m_fac = fac;
 
92
     }
 
93
 
 
94
 Q_SIGNALS:
 
95
     void xChanged(int value);
 
96
 
 
97
 private:
 
98
     int m_x;
 
99
     int m_fac;
 
100
 };
 
101
 
 
102
 class FactorialLoopTransition : public QSignalTransition
 
103
 {
 
104
 public:
 
105
     FactorialLoopTransition(Factorial *fact)
 
106
         : QSignalTransition(fact, SIGNAL(xChanged(int))), m_fact(fact)
 
107
     {}
 
108
 
 
109
     virtual bool eventTest(QEvent *e)
 
110
     {
 
111
         if (!QSignalTransition::eventTest(e))
 
112
             return false;
 
113
         QStateMachine::SignalEvent *se = static_cast&lt;QStateMachine::SignalEvent*&gt;(e);
 
114
         return se-&gt;arguments().at(0).toInt() &gt; 1;
 
115
     }
 
116
 
 
117
     virtual void onTransition(QEvent *e)
 
118
     {
 
119
         QStateMachine::SignalEvent *se = static_cast&lt;QStateMachine::SignalEvent*&gt;(e);
 
120
         int x = se-&gt;arguments().at(0).toInt();
 
121
         int fac = m_fact-&gt;property(&quot;fac&quot;).toInt();
 
122
         m_fact-&gt;setProperty(&quot;fac&quot;,  x * fac);
 
123
         m_fact-&gt;setProperty(&quot;x&quot;,  x - 1);
 
124
     }
 
125
 
 
126
 private:
 
127
     Factorial *m_fact;
 
128
 };
 
129
 
 
130
 class FactorialDoneTransition : public QSignalTransition
 
131
 {
 
132
 public:
 
133
     FactorialDoneTransition(Factorial *fact)
 
134
         : QSignalTransition(fact, SIGNAL(xChanged(int))), m_fact(fact)
 
135
     {}
 
136
 
 
137
     virtual bool eventTest(QEvent *e)
 
138
     {
 
139
         if (!QSignalTransition::eventTest(e))
 
140
             return false;
 
141
         QStateMachine::SignalEvent *se = static_cast&lt;QStateMachine::SignalEvent*&gt;(e);
 
142
         return se-&gt;arguments().at(0).toInt() &lt;= 1;
 
143
     }
 
144
 
 
145
     virtual void onTransition(QEvent *)
 
146
     {
 
147
         fprintf(stdout, &quot;%d\n&quot;, m_fact-&gt;property(&quot;fac&quot;).toInt());
 
148
     }
 
149
 
 
150
 private:
 
151
     Factorial *m_fact;
 
152
 };
 
153
 
 
154
 int main(int argc, char **argv)
 
155
 {
 
156
     QCoreApplication app(argc, argv);
 
157
     Factorial factorial;
 
158
     QStateMachine machine;
 
159
 
 
160
     QState *compute = new QState(&amp;machine);
 
161
     compute-&gt;assignProperty(&amp;factorial, &quot;fac&quot;, 1);
 
162
     compute-&gt;assignProperty(&amp;factorial, &quot;x&quot;, 6);
 
163
     compute-&gt;addTransition(new FactorialLoopTransition(&amp;factorial));
 
164
 
 
165
     QFinalState *done = new QFinalState(&amp;machine);
 
166
     FactorialDoneTransition *doneTransition = new FactorialDoneTransition(&amp;factorial);
 
167
     doneTransition-&gt;setTargetState(done);
 
168
     compute-&gt;addTransition(doneTransition);
 
169
 
 
170
     machine.setInitialState(compute);
 
171
     QObject::connect(&amp;machine, SIGNAL(finished()), &amp;app, SLOT(quit()));
 
172
     machine.start();
 
173
 
 
174
     return app.exec();
 
175
 }
 
176
 
 
177
 #include &quot;main.moc&quot;</pre>
 
178
<p /><address><hr /><div align="center">
 
179
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
180
<td width="40%" align="left">Copyright &copy; 2009 Nokia Corporation and/or its subsidiary(-ies)</td>
 
181
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
182
<td width="40%" align="right"><div align="right">Qt 4.6.0</div></td>
 
183
<script type="text/javascript" src="http://www.google.com/jsapi"></script><script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script><script type="text/javascript" src="http://www.google.com/coop/cse/t13n?form=cse-search-box&t13n_langs=en"></script><script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script></tr></table></div></address></body>
 
184
</html>