1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include <QtCore>
52
53//! [0]
54class Factorial : public QObject
55{
56 Q_OBJECT
57 Q_PROPERTY(int x READ x WRITE setX)
58 Q_PROPERTY(int fac READ fac WRITE setFac)
59public:
60 using QObject::QObject;
61
62 int x() const
63 {
64 return m_x;
65 }
66
67 void setX(int x)
68 {
69 if (x == m_x)
70 return;
71 m_x = x;
72 emit xChanged(value: x);
73 }
74
75 int fac() const
76 {
77 return m_fac;
78 }
79
80 void setFac(int fac)
81 {
82 m_fac = fac;
83 }
84
85Q_SIGNALS:
86 void xChanged(int value);
87
88private:
89 int m_x = -1;
90 int m_fac = 1;
91};
92//! [0]
93
94//! [1]
95class FactorialLoopTransition : public QSignalTransition
96{
97public:
98 FactorialLoopTransition(Factorial *fact)
99 : QSignalTransition(fact, &Factorial::xChanged), m_fact(fact)
100 {}
101
102 bool eventTest(QEvent *e) override
103 {
104 if (!QSignalTransition::eventTest(event: e))
105 return false;
106 QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e);
107 return se->arguments().at(i: 0).toInt() > 1;
108 }
109
110 void onTransition(QEvent *e) override
111 {
112 QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e);
113 int x = se->arguments().at(i: 0).toInt();
114 int fac = m_fact->property(name: "fac").toInt();
115 m_fact->setProperty(name: "fac", value: x * fac);
116 m_fact->setProperty(name: "x", value: x - 1);
117 }
118
119private:
120 Factorial *m_fact;
121};
122//! [1]
123
124//! [2]
125class FactorialDoneTransition : public QSignalTransition
126{
127public:
128 FactorialDoneTransition(Factorial *fact)
129 : QSignalTransition(fact, &Factorial::xChanged), m_fact(fact)
130 {}
131
132 bool eventTest(QEvent *e) override
133 {
134 if (!QSignalTransition::eventTest(event: e))
135 return false;
136 QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e);
137 return se->arguments().at(i: 0).toInt() <= 1;
138 }
139
140 void onTransition(QEvent *) override
141 {
142 qInfo() << m_fact->property(name: "fac").toInt();
143 }
144
145private:
146 Factorial *m_fact;
147};
148//! [2]
149
150//! [3]
151int main(int argc, char **argv)
152{
153 QCoreApplication app(argc, argv);
154 Factorial factorial;
155 QStateMachine machine;
156//! [3]
157
158//! [4]
159 QState *compute = new QState(&machine);
160 compute->assignProperty(object: &factorial, name: "fac", value: 1);
161 compute->assignProperty(object: &factorial, name: "x", value: 6);
162 compute->addTransition(transition: new FactorialLoopTransition(&factorial));
163//! [4]
164
165//! [5]
166 QFinalState *done = new QFinalState(&machine);
167 FactorialDoneTransition *doneTransition = new FactorialDoneTransition(&factorial);
168 doneTransition->setTargetState(done);
169 compute->addTransition(transition: doneTransition);
170//! [5]
171
172//! [6]
173 machine.setInitialState(compute);
174 QObject::connect(sender: &machine, signal: &QStateMachine::finished, context: &app, slot: QCoreApplication::quit);
175 machine.start();
176
177 return app.exec();
178}
179//! [6]
180
181#include "main.moc"
182

source code of qtbase/examples/widgets/statemachine/factorial/main.cpp