| 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 | ** 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
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 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 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at qt-info@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | /*!
|
|---|
| 43 | \example statemachine/trafficlight
|
|---|
| 44 | \title Traffic Light Example
|
|---|
| 45 |
|
|---|
| 46 | The Traffic Light example shows how to use \l{The State Machine Framework}
|
|---|
| 47 | to implement the control flow of a traffic light.
|
|---|
| 48 |
|
|---|
| 49 | \image trafficlight-example.png
|
|---|
| 50 |
|
|---|
| 51 | In this example we write a TrafficLightWidget class. The traffic light has
|
|---|
| 52 | three lights: Red, yellow and green. The traffic light transitions from
|
|---|
| 53 | one light to another (red to yellow to green to yellow to red again) at
|
|---|
| 54 | certain intervals.
|
|---|
| 55 |
|
|---|
| 56 | \snippet examples/statemachine/trafficlight/main.cpp 0
|
|---|
| 57 |
|
|---|
| 58 | The LightWidget class represents a single light of the traffic light. It
|
|---|
| 59 | provides an \c on property and two slots, turnOn() and turnOff(), to turn
|
|---|
| 60 | the light on and off, respectively. The widget paints itself in the color
|
|---|
| 61 | that's passed to the constructor.
|
|---|
| 62 |
|
|---|
| 63 | \snippet examples/statemachine/trafficlight/main.cpp 1
|
|---|
| 64 |
|
|---|
| 65 | The TrafficLightWidget class represents the visual part of the traffic
|
|---|
| 66 | light; it's a widget that contains three lights arranged vertically, and
|
|---|
| 67 | provides accessor functions for these.
|
|---|
| 68 |
|
|---|
| 69 | \snippet examples/statemachine/trafficlight/main.cpp 2
|
|---|
| 70 |
|
|---|
| 71 | The createLightState() function creates a state that turns a light on when
|
|---|
| 72 | the state is entered, and off when the state is exited. The state uses a
|
|---|
| 73 | timer, and as we shall see the timeout is used to transition from one
|
|---|
| 74 | LightState to another. Here is the statechart for the light state:
|
|---|
| 75 |
|
|---|
| 76 | \img trafficlight-example1.png
|
|---|
| 77 | \omit
|
|---|
| 78 | \caption This is a caption
|
|---|
| 79 | \endomit
|
|---|
| 80 |
|
|---|
| 81 | \snippet examples/statemachine/trafficlight/main.cpp 3
|
|---|
| 82 |
|
|---|
| 83 | The TrafficLight class combines the TrafficLightWidget with a state
|
|---|
| 84 | machine. The state graph has four states: red-to-yellow, yellow-to-green,
|
|---|
| 85 | green-to-yellow and yellow-to-red. The initial state is red-to-yellow;
|
|---|
| 86 | when the state's timer times out, the state machine transitions to
|
|---|
| 87 | yellow-to-green. The same process repeats through the other states.
|
|---|
| 88 | This is what the statechart looks like:
|
|---|
| 89 |
|
|---|
| 90 | \img trafficlight-example2.png
|
|---|
| 91 | \omit
|
|---|
| 92 | \caption This is a caption
|
|---|
| 93 | \endomit
|
|---|
| 94 |
|
|---|
| 95 | \snippet examples/statemachine/trafficlight/main.cpp 4
|
|---|
| 96 |
|
|---|
| 97 | The main() function constructs a TrafficLight and shows it.
|
|---|
| 98 |
|
|---|
| 99 | */
|
|---|