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