8
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
1 |
/****************************************************************************
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
2 |
**
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
4 |
** All rights reserved.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
6 |
**
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
7 |
** This file is part of the documentation of the Qt Toolkit.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
8 |
**
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
10 |
** No Commercial Usage
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
11 |
** This file contains pre-release code and may not be distributed.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
12 |
** You may use this file in accordance with the terms and conditions
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
14 |
** this package.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
15 |
**
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
16 |
** GNU Lesser General Public License Usage
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
18 |
** General Public License version 2.1 as published by the Free Software
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
20 |
** packaging of this file. Please review the following information to
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
23 |
**
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
27 |
**
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
28 |
** If you have questions regarding the use of this file, please contact
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
29 |
** Nokia at qt-info@nokia.com.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
30 |
**
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
31 |
**
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
32 |
**
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
33 |
**
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
34 |
**
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
35 |
**
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
36 |
**
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
37 |
**
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
38 |
** $QT_END_LICENSE$
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
39 |
**
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
40 |
****************************************************************************/
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
41 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
42 |
/*!
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
43 |
\example statemachine/pingpong
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
44 |
\title Ping Pong States Example
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
45 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
46 |
The Ping Pong States example shows how to use parallel states together
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
47 |
with custom events and transitions in \l{The State Machine Framework}.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
48 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
49 |
This example implements a statechart where two states communicate by
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
50 |
posting events to the state machine. The state chart looks as follows:
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
51 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
52 |
\img pingpong-example.png
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
53 |
\omit
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
54 |
\caption This is a caption
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
55 |
\endomit
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
56 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
57 |
The \c pinger and \c ponger states are parallel states, i.e. they are
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
58 |
entered simultaneously and will take transitions independently of
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
59 |
eachother.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
60 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
61 |
The \c pinger state will post the first \c ping event upon entry; the \c
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
62 |
ponger state will respond by posting a \c pong event; this will cause the
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
63 |
\c pinger state to post a new \c ping event; and so on.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
64 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
65 |
\snippet examples/statemachine/pingpong/main.cpp 0
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
66 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
67 |
Two custom events are defined, \c PingEvent and \c PongEvent.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
68 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
69 |
\snippet examples/statemachine/pingpong/main.cpp 1
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
70 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
71 |
The \c Pinger class defines a state that posts a \c PingEvent to the state
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
72 |
machine when the state is entered.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
73 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
74 |
\snippet examples/statemachine/pingpong/main.cpp 2
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
75 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
76 |
The \c PingTransition class defines a transition that is triggered by
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
77 |
events of type \c PingEvent, and that posts a \c PongEvent (with a delay
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
78 |
of 500 milliseconds) to the state machine when the transition is
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
79 |
triggered.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
80 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
81 |
\snippet examples/statemachine/pingpong/main.cpp 3
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
82 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
83 |
The \c PongTransition class defines a transition that is triggered by
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
84 |
events of type \c PongEvent, and that posts a \c PingEvent (with a delay
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
85 |
of 500 milliseconds) to the state machine when the transition is
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
86 |
triggered.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
87 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
88 |
\snippet examples/statemachine/pingpong/main.cpp 4
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
89 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
90 |
The main() function begins by creating a state machine and a parallel
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
91 |
state group.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
92 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
93 |
\snippet examples/statemachine/pingpong/main.cpp 5
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
94 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
95 |
Next, the \c pinger and \c ponger states are created, with the parallel
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
96 |
state group as their parent state. Note that the transitions are \e
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
97 |
targetless. When such a transition is triggered, the source state won't be
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
98 |
exited and re-entered; only the transition's onTransition() function will
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
99 |
be called, and the state machine's configuration will remain the same,
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
100 |
which is precisely what we want in this case.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
101 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
102 |
\snippet examples/statemachine/pingpong/main.cpp 6
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
103 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
104 |
Finally, the group is added to the state machine, the machine is started,
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
105 |
and the application event loop is entered.
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
106 |
|
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
diff
changeset
|
107 |
*/
|