summaryrefslogtreecommitdiffstats
path: root/examples/statemachine/trafficlight/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/statemachine/trafficlight/main.cpp')
-rw-r--r--examples/statemachine/trafficlight/main.cpp47
1 files changed, 22 insertions, 25 deletions
diff --git a/examples/statemachine/trafficlight/main.cpp b/examples/statemachine/trafficlight/main.cpp
index 3c47a51..8a46fff 100644
--- a/examples/statemachine/trafficlight/main.cpp
+++ b/examples/statemachine/trafficlight/main.cpp
@@ -87,27 +87,6 @@ private:
//! [0]
//! [1]
-class LightState : public QState
-{
-public:
- LightState(LightWidget *light, int duration, QState *parent = 0)
- : QState(parent)
- {
- QTimer *timer = new QTimer(this);
- timer->setInterval(duration);
- timer->setSingleShot(true);
- QState *timing = new QState(this);
- QObject::connect(timing, SIGNAL(entered()), light, SLOT(turnOn()));
- QObject::connect(timing, SIGNAL(entered()), timer, SLOT(start()));
- QObject::connect(timing, SIGNAL(exited()), light, SLOT(turnOff()));
- QFinalState *done = new QFinalState(this);
- timing->addTransition(timer, SIGNAL(timeout()), done);
- setInitialState(timing);
- }
-};
-//! [1]
-
-//! [2]
class TrafficLightWidget : public QWidget
{
public:
@@ -139,6 +118,24 @@ private:
LightWidget *m_yellow;
LightWidget *m_green;
};
+//! [1]
+
+//! [2]
+QState *createLightState(LightWidget *light, int duration, QState *parent = 0)
+{
+ QState *lightState = new QState(parent);
+ QTimer *timer = new QTimer(lightState);
+ timer->setInterval(duration);
+ timer->setSingleShot(true);
+ QState *timing = new QState(lightState);
+ QObject::connect(timing, SIGNAL(entered()), light, SLOT(turnOn()));
+ QObject::connect(timing, SIGNAL(entered()), timer, SLOT(start()));
+ QObject::connect(timing, SIGNAL(exited()), light, SLOT(turnOff()));
+ QFinalState *done = new QFinalState(lightState);
+ timing->addTransition(timer, SIGNAL(timeout()), done);
+ lightState->setInitialState(timing);
+ return lightState;
+}
//! [2]
//! [3]
@@ -154,15 +151,15 @@ public:
vbox->setMargin(0);
QStateMachine *machine = new QStateMachine(this);
- LightState *redGoingYellow = new LightState(widget->redLight(), 3000);
+ QState *redGoingYellow = createLightState(widget->redLight(), 3000);
redGoingYellow->setObjectName("redGoingYellow");
- LightState *yellowGoingGreen = new LightState(widget->yellowLight(), 1000);
+ QState *yellowGoingGreen = createLightState(widget->yellowLight(), 1000);
yellowGoingGreen->setObjectName("yellowGoingGreen");
redGoingYellow->addTransition(redGoingYellow, SIGNAL(finished()), yellowGoingGreen);
- LightState *greenGoingYellow = new LightState(widget->greenLight(), 3000);
+ QState *greenGoingYellow = createLightState(widget->greenLight(), 3000);
greenGoingYellow->setObjectName("greenGoingYellow");
yellowGoingGreen->addTransition(yellowGoingGreen, SIGNAL(finished()), greenGoingYellow);
- LightState *yellowGoingRed = new LightState(widget->yellowLight(), 1000);
+ QState *yellowGoingRed = createLightState(widget->yellowLight(), 1000);
yellowGoingRed->setObjectName("yellowGoingRed");
greenGoingYellow->addTransition(greenGoingYellow, SIGNAL(finished()), yellowGoingRed);
yellowGoingRed->addTransition(yellowGoingRed, SIGNAL(finished()), redGoingYellow);