0
|
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 documentation 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 |
/*!
|
|
43 |
\example statemachine/pingpong
|
|
44 |
\title Ping Pong States Example
|
|
45 |
|
|
46 |
The Ping Pong States example shows how to use parallel states together
|
|
47 |
with custom events and transitions in \l{The State Machine Framework}.
|
|
48 |
|
|
49 |
This example implements a statechart where two states communicate by
|
|
50 |
posting events to the state machine. The state chart looks as follows:
|
|
51 |
|
|
52 |
\img pingpong-example.png
|
|
53 |
\omit
|
|
54 |
\caption This is a caption
|
|
55 |
\endomit
|
|
56 |
|
|
57 |
The \c pinger and \c ponger states are parallel states, i.e. they are
|
|
58 |
entered simultaneously and will take transitions independently of
|
|
59 |
eachother.
|
|
60 |
|
|
61 |
The \c pinger state will post the first \c ping event upon entry; the \c
|
|
62 |
ponger state will respond by posting a \c pong event; this will cause the
|
|
63 |
\c pinger state to post a new \c ping event; and so on.
|
|
64 |
|
|
65 |
\snippet examples/statemachine/pingpong/main.cpp 0
|
|
66 |
|
|
67 |
Two custom events are defined, \c PingEvent and \c PongEvent.
|
|
68 |
|
|
69 |
\snippet examples/statemachine/pingpong/main.cpp 1
|
|
70 |
|
|
71 |
The \c Pinger class defines a state that posts a \c PingEvent to the state
|
|
72 |
machine when the state is entered.
|
|
73 |
|
|
74 |
\snippet examples/statemachine/pingpong/main.cpp 2
|
|
75 |
|
|
76 |
The \c PingTransition class defines a transition that is triggered by
|
|
77 |
events of type \c PingEvent, and that posts a \c PongEvent (with a delay
|
|
78 |
of 500 milliseconds) to the state machine when the transition is
|
|
79 |
triggered.
|
|
80 |
|
|
81 |
\snippet examples/statemachine/pingpong/main.cpp 3
|
|
82 |
|
|
83 |
The \c PongTransition class defines a transition that is triggered by
|
|
84 |
events of type \c PongEvent, and that posts a \c PingEvent (with a delay
|
|
85 |
of 500 milliseconds) to the state machine when the transition is
|
|
86 |
triggered.
|
|
87 |
|
|
88 |
\snippet examples/statemachine/pingpong/main.cpp 4
|
|
89 |
|
|
90 |
The main() function begins by creating a state machine and a parallel
|
|
91 |
state group.
|
|
92 |
|
|
93 |
\snippet examples/statemachine/pingpong/main.cpp 5
|
|
94 |
|
|
95 |
Next, the \c pinger and \c ponger states are created, with the parallel
|
|
96 |
state group as their parent state. Note that the transitions are \e
|
|
97 |
targetless. When such a transition is triggered, the source state won't be
|
|
98 |
exited and re-entered; only the transition's onTransition() function will
|
|
99 |
be called, and the state machine's configuration will remain the same,
|
|
100 |
which is precisely what we want in this case.
|
|
101 |
|
|
102 |
\snippet examples/statemachine/pingpong/main.cpp 6
|
|
103 |
|
|
104 |
Finally, the group is added to the state machine, the machine is started,
|
|
105 |
and the application event loop is entered.
|
|
106 |
|
|
107 |
*/
|