[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
[556] | 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
|
---|
[846] | 13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
| 14 | ** written agreement between you and Nokia.
|
---|
[556] | 15 | **
|
---|
[846] | 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.
|
---|
[556] | 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/trafficlight
|
---|
| 30 | \title Traffic Light Example
|
---|
| 31 |
|
---|
| 32 | The Traffic Light example shows how to use \l{The State Machine Framework}
|
---|
| 33 | to implement the control flow of a traffic light.
|
---|
| 34 |
|
---|
| 35 | \image trafficlight-example.png
|
---|
| 36 |
|
---|
| 37 | In this example we write a TrafficLightWidget class. The traffic light has
|
---|
| 38 | three lights: Red, yellow and green. The traffic light transitions from
|
---|
| 39 | one light to another (red to yellow to green to yellow to red again) at
|
---|
| 40 | certain intervals.
|
---|
| 41 |
|
---|
| 42 | \snippet examples/statemachine/trafficlight/main.cpp 0
|
---|
| 43 |
|
---|
| 44 | The LightWidget class represents a single light of the traffic light. It
|
---|
| 45 | provides an \c on property and two slots, turnOn() and turnOff(), to turn
|
---|
| 46 | the light on and off, respectively. The widget paints itself in the color
|
---|
| 47 | that's passed to the constructor.
|
---|
| 48 |
|
---|
| 49 | \snippet examples/statemachine/trafficlight/main.cpp 1
|
---|
| 50 |
|
---|
| 51 | The TrafficLightWidget class represents the visual part of the traffic
|
---|
| 52 | light; it's a widget that contains three lights arranged vertically, and
|
---|
| 53 | provides accessor functions for these.
|
---|
| 54 |
|
---|
| 55 | \snippet examples/statemachine/trafficlight/main.cpp 2
|
---|
| 56 |
|
---|
| 57 | The createLightState() function creates a state that turns a light on when
|
---|
| 58 | the state is entered, and off when the state is exited. The state uses a
|
---|
| 59 | timer, and as we shall see the timeout is used to transition from one
|
---|
| 60 | LightState to another. Here is the statechart for the light state:
|
---|
| 61 |
|
---|
| 62 | \img trafficlight-example1.png
|
---|
| 63 | \omit
|
---|
| 64 | \caption This is a caption
|
---|
| 65 | \endomit
|
---|
| 66 |
|
---|
| 67 | \snippet examples/statemachine/trafficlight/main.cpp 3
|
---|
| 68 |
|
---|
| 69 | The TrafficLight class combines the TrafficLightWidget with a state
|
---|
| 70 | machine. The state graph has four states: red-to-yellow, yellow-to-green,
|
---|
| 71 | green-to-yellow and yellow-to-red. The initial state is red-to-yellow;
|
---|
| 72 | when the state's timer times out, the state machine transitions to
|
---|
| 73 | yellow-to-green. The same process repeats through the other states.
|
---|
| 74 | This is what the statechart looks like:
|
---|
| 75 |
|
---|
| 76 | \img trafficlight-example2.png
|
---|
| 77 | \omit
|
---|
| 78 | \caption This is a caption
|
---|
| 79 | \endomit
|
---|
| 80 |
|
---|
| 81 | \snippet examples/statemachine/trafficlight/main.cpp 4
|
---|
| 82 |
|
---|
| 83 | The main() function constructs a TrafficLight and shows it.
|
---|
| 84 |
|
---|
| 85 | */
|
---|