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/factorial
|
---|
30 | \title Factorial States Example
|
---|
31 |
|
---|
32 | The Factorial States example shows how to use \l{The State Machine
|
---|
33 | Framework} to calculate the factorial of an integer.
|
---|
34 |
|
---|
35 | The statechart for calculating the factorial looks as follows:
|
---|
36 |
|
---|
37 | \img factorial-example.png
|
---|
38 | \omit
|
---|
39 | \caption This is a caption
|
---|
40 | \endomit
|
---|
41 |
|
---|
42 | In other words, the state machine calculates the factorial of 6 and prints
|
---|
43 | the result.
|
---|
44 |
|
---|
45 | \snippet examples/statemachine/factorial/main.cpp 0
|
---|
46 |
|
---|
47 | The Factorial class is used to hold the data of the computation, \c x and
|
---|
48 | \c fac. It also provides a signal that's emitted whenever the value of \c
|
---|
49 | x changes.
|
---|
50 |
|
---|
51 | \snippet examples/statemachine/factorial/main.cpp 1
|
---|
52 |
|
---|
53 | The FactorialLoopTransition class implements the guard (\c x > 1) and
|
---|
54 | calculations (\c fac = \c x * \c fac; \c x = \c x - 1) of the factorial
|
---|
55 | loop.
|
---|
56 |
|
---|
57 | \snippet examples/statemachine/factorial/main.cpp 2
|
---|
58 |
|
---|
59 | The FactorialDoneTransition class implements the guard (\c x <= 1) that
|
---|
60 | terminates the factorial computation. It also prints the final result to
|
---|
61 | standard output.
|
---|
62 |
|
---|
63 | \snippet examples/statemachine/factorial/main.cpp 3
|
---|
64 |
|
---|
65 | The application's main() function first creates the application object, a
|
---|
66 | Factorial object and a state machine.
|
---|
67 |
|
---|
68 | \snippet examples/statemachine/factorial/main.cpp 4
|
---|
69 |
|
---|
70 | The \c compute state is created, and the initial values of \c x and \c fac
|
---|
71 | are defined. A FactorialLoopTransition object is created and added to the
|
---|
72 | state.
|
---|
73 |
|
---|
74 | \snippet examples/statemachine/factorial/main.cpp 5
|
---|
75 |
|
---|
76 | A final state, \c done, is created, and a FactorialDoneTransition object
|
---|
77 | is created with \c done as its target state. The transition is then added
|
---|
78 | to the \c compute state.
|
---|
79 |
|
---|
80 | \snippet examples/statemachine/factorial/main.cpp 6
|
---|
81 |
|
---|
82 | The machine's initial state is set to be the \c compute state. We connect
|
---|
83 | the QStateMachine::finished() signal to the QCoreApplication::quit() slot,
|
---|
84 | so the application will quit when the state machine's work is
|
---|
85 | done. Finally, the state machine is started, and the application's event
|
---|
86 | loop is entered.
|
---|
87 |
|
---|
88 | */
|
---|