summaryrefslogtreecommitdiffstats
path: root/examples/statemachine/trafficlight/main.cpp
diff options
context:
space:
mode:
authorAaron Kennedy <aaron.kennedy@nokia.com>2009-05-18 06:40:15 (GMT)
committerAaron Kennedy <aaron.kennedy@nokia.com>2009-05-18 06:40:15 (GMT)
commit0add74a9339a9d7dda302aa4f31cbb50bdb69908 (patch)
tree9d2c767bee200cb79f81aed24551bcb1989eefb7 /examples/statemachine/trafficlight/main.cpp
parentc22504ae79b45268d603c2b2bc66bd2a59c331c7 (diff)
parent3769beacd9126b567f6a42e6c357a7eda004a602 (diff)
downloadQt-0add74a9339a9d7dda302aa4f31cbb50bdb69908.zip
Qt-0add74a9339a9d7dda302aa4f31cbb50bdb69908.tar.gz
Qt-0add74a9339a9d7dda302aa4f31cbb50bdb69908.tar.bz2
Merge branch 'kinetic-declarativeui' of git@scm.dev.nokia.troll.no:qt/kinetic into kinetic-declarativeui
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);