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 documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example statemachine/pingpong
|
---|
30 | \title Ping Pong States Example
|
---|
31 |
|
---|
32 | The Ping Pong States example shows how to use parallel states together
|
---|
33 | with custom events and transitions in \l{The State Machine Framework}.
|
---|
34 |
|
---|
35 | This example implements a statechart where two states communicate by
|
---|
36 | posting events to the state machine. The state chart looks as follows:
|
---|
37 |
|
---|
38 | \img pingpong-example.png
|
---|
39 | \omit
|
---|
40 | \caption This is a caption
|
---|
41 | \endomit
|
---|
42 |
|
---|
43 | The \c pinger and \c ponger states are parallel states, i.e. they are
|
---|
44 | entered simultaneously and will take transitions independently of
|
---|
45 | eachother.
|
---|
46 |
|
---|
47 | The \c pinger state will post the first \c ping event upon entry; the \c
|
---|
48 | ponger state will respond by posting a \c pong event; this will cause the
|
---|
49 | \c pinger state to post a new \c ping event; and so on.
|
---|
50 |
|
---|
51 | \snippet examples/statemachine/pingpong/main.cpp 0
|
---|
52 |
|
---|
53 | Two custom events are defined, \c PingEvent and \c PongEvent.
|
---|
54 |
|
---|
55 | \snippet examples/statemachine/pingpong/main.cpp 1
|
---|
56 |
|
---|
57 | The \c Pinger class defines a state that posts a \c PingEvent to the state
|
---|
58 | machine when the state is entered.
|
---|
59 |
|
---|
60 | \snippet examples/statemachine/pingpong/main.cpp 2
|
---|
61 |
|
---|
62 | The \c PingTransition class defines a transition that is triggered by
|
---|
63 | events of type \c PingEvent, and that posts a \c PongEvent (with a delay
|
---|
64 | of 500 milliseconds) to the state machine when the transition is
|
---|
65 | triggered.
|
---|
66 |
|
---|
67 | \snippet examples/statemachine/pingpong/main.cpp 3
|
---|
68 |
|
---|
69 | The \c PongTransition class defines a transition that is triggered by
|
---|
70 | events of type \c PongEvent, and that posts a \c PingEvent (with a delay
|
---|
71 | of 500 milliseconds) to the state machine when the transition is
|
---|
72 | triggered.
|
---|
73 |
|
---|
74 | \snippet examples/statemachine/pingpong/main.cpp 4
|
---|
75 |
|
---|
76 | The main() function begins by creating a state machine and a parallel
|
---|
77 | state group.
|
---|
78 |
|
---|
79 | \snippet examples/statemachine/pingpong/main.cpp 5
|
---|
80 |
|
---|
81 | Next, the \c pinger and \c ponger states are created, with the parallel
|
---|
82 | state group as their parent state. Note that the transitions are \e
|
---|
83 | targetless. When such a transition is triggered, the source state won't be
|
---|
84 | exited and re-entered; only the transition's onTransition() function will
|
---|
85 | be called, and the state machine's configuration will remain the same,
|
---|
86 | which is precisely what we want in this case.
|
---|
87 |
|
---|
88 | \snippet examples/statemachine/pingpong/main.cpp 6
|
---|
89 |
|
---|
90 | Finally, the group is added to the state machine, the machine is started,
|
---|
91 | and the application event loop is entered.
|
---|
92 |
|
---|
93 | */
|
---|