1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 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 QtCore module 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 [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtGui>
|
---|
43 |
|
---|
44 | //! [0]
|
---|
45 | class LightWidget : public QWidget
|
---|
46 | {
|
---|
47 | Q_OBJECT
|
---|
48 | Q_PROPERTY(bool on READ isOn WRITE setOn)
|
---|
49 | public:
|
---|
50 | LightWidget(const QColor &color, QWidget *parent = 0)
|
---|
51 | : QWidget(parent), m_color(color), m_on(false) {}
|
---|
52 |
|
---|
53 | bool isOn() const
|
---|
54 | { return m_on; }
|
---|
55 | void setOn(bool on)
|
---|
56 | {
|
---|
57 | if (on == m_on)
|
---|
58 | return;
|
---|
59 | m_on = on;
|
---|
60 | update();
|
---|
61 | }
|
---|
62 |
|
---|
63 | public slots:
|
---|
64 | void turnOff() { setOn(false); }
|
---|
65 | void turnOn() { setOn(true); }
|
---|
66 |
|
---|
67 | protected:
|
---|
68 | virtual void paintEvent(QPaintEvent *)
|
---|
69 | {
|
---|
70 | if (!m_on)
|
---|
71 | return;
|
---|
72 | QPainter painter(this);
|
---|
73 | painter.setRenderHint(QPainter::Antialiasing);
|
---|
74 | painter.setBrush(m_color);
|
---|
75 | painter.drawEllipse(0, 0, width(), height());
|
---|
76 | }
|
---|
77 |
|
---|
78 | private:
|
---|
79 | QColor m_color;
|
---|
80 | bool m_on;
|
---|
81 | };
|
---|
82 | //! [0]
|
---|
83 |
|
---|
84 | //! [1]
|
---|
85 | class TrafficLightWidget : public QWidget
|
---|
86 | {
|
---|
87 | public:
|
---|
88 | TrafficLightWidget(QWidget *parent = 0)
|
---|
89 | : QWidget(parent)
|
---|
90 | {
|
---|
91 | QVBoxLayout *vbox = new QVBoxLayout(this);
|
---|
92 | m_red = new LightWidget(Qt::red);
|
---|
93 | vbox->addWidget(m_red);
|
---|
94 | m_yellow = new LightWidget(Qt::yellow);
|
---|
95 | vbox->addWidget(m_yellow);
|
---|
96 | m_green = new LightWidget(Qt::green);
|
---|
97 | vbox->addWidget(m_green);
|
---|
98 | QPalette pal = palette();
|
---|
99 | pal.setColor(QPalette::Background, Qt::black);
|
---|
100 | setPalette(pal);
|
---|
101 | setAutoFillBackground(true);
|
---|
102 | }
|
---|
103 |
|
---|
104 | LightWidget *redLight() const
|
---|
105 | { return m_red; }
|
---|
106 | LightWidget *yellowLight() const
|
---|
107 | { return m_yellow; }
|
---|
108 | LightWidget *greenLight() const
|
---|
109 | { return m_green; }
|
---|
110 |
|
---|
111 | private:
|
---|
112 | LightWidget *m_red;
|
---|
113 | LightWidget *m_yellow;
|
---|
114 | LightWidget *m_green;
|
---|
115 | };
|
---|
116 | //! [1]
|
---|
117 |
|
---|
118 | //! [2]
|
---|
119 | QState *createLightState(LightWidget *light, int duration, QState *parent = 0)
|
---|
120 | {
|
---|
121 | QState *lightState = new QState(parent);
|
---|
122 | QTimer *timer = new QTimer(lightState);
|
---|
123 | timer->setInterval(duration);
|
---|
124 | timer->setSingleShot(true);
|
---|
125 | QState *timing = new QState(lightState);
|
---|
126 | QObject::connect(timing, SIGNAL(entered()), light, SLOT(turnOn()));
|
---|
127 | QObject::connect(timing, SIGNAL(entered()), timer, SLOT(start()));
|
---|
128 | QObject::connect(timing, SIGNAL(exited()), light, SLOT(turnOff()));
|
---|
129 | QFinalState *done = new QFinalState(lightState);
|
---|
130 | timing->addTransition(timer, SIGNAL(timeout()), done);
|
---|
131 | lightState->setInitialState(timing);
|
---|
132 | return lightState;
|
---|
133 | }
|
---|
134 | //! [2]
|
---|
135 |
|
---|
136 | //! [3]
|
---|
137 | class TrafficLight : public QWidget
|
---|
138 | {
|
---|
139 | public:
|
---|
140 | TrafficLight(QWidget *parent = 0)
|
---|
141 | : QWidget(parent)
|
---|
142 | {
|
---|
143 | QVBoxLayout *vbox = new QVBoxLayout(this);
|
---|
144 | TrafficLightWidget *widget = new TrafficLightWidget();
|
---|
145 | vbox->addWidget(widget);
|
---|
146 | vbox->setMargin(0);
|
---|
147 |
|
---|
148 | QStateMachine *machine = new QStateMachine(this);
|
---|
149 | QState *redGoingYellow = createLightState(widget->redLight(), 3000);
|
---|
150 | redGoingYellow->setObjectName("redGoingYellow");
|
---|
151 | QState *yellowGoingGreen = createLightState(widget->yellowLight(), 1000);
|
---|
152 | yellowGoingGreen->setObjectName("yellowGoingGreen");
|
---|
153 | redGoingYellow->addTransition(redGoingYellow, SIGNAL(finished()), yellowGoingGreen);
|
---|
154 | QState *greenGoingYellow = createLightState(widget->greenLight(), 3000);
|
---|
155 | greenGoingYellow->setObjectName("greenGoingYellow");
|
---|
156 | yellowGoingGreen->addTransition(yellowGoingGreen, SIGNAL(finished()), greenGoingYellow);
|
---|
157 | QState *yellowGoingRed = createLightState(widget->yellowLight(), 1000);
|
---|
158 | yellowGoingRed->setObjectName("yellowGoingRed");
|
---|
159 | greenGoingYellow->addTransition(greenGoingYellow, SIGNAL(finished()), yellowGoingRed);
|
---|
160 | yellowGoingRed->addTransition(yellowGoingRed, SIGNAL(finished()), redGoingYellow);
|
---|
161 |
|
---|
162 | machine->addState(redGoingYellow);
|
---|
163 | machine->addState(yellowGoingGreen);
|
---|
164 | machine->addState(greenGoingYellow);
|
---|
165 | machine->addState(yellowGoingRed);
|
---|
166 | machine->setInitialState(redGoingYellow);
|
---|
167 | machine->start();
|
---|
168 | }
|
---|
169 | };
|
---|
170 | //! [3]
|
---|
171 |
|
---|
172 | //! [4]
|
---|
173 | int main(int argc, char **argv)
|
---|
174 | {
|
---|
175 | QApplication app(argc, argv);
|
---|
176 |
|
---|
177 | TrafficLight widget;
|
---|
178 | widget.resize(110, 300);
|
---|
179 | widget.show();
|
---|
180 |
|
---|
181 | return app.exec();
|
---|
182 | }
|
---|
183 | //! [4]
|
---|
184 |
|
---|
185 | #include "main.moc"
|
---|