| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtCore module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:BSD$
|
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
|---|
| 11 | **
|
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | ** modification, are permitted provided that the following conditions are
|
|---|
| 14 | ** met:
|
|---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
|---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
|---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
|---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
|---|
| 19 | ** the documentation and/or other materials provided with the
|
|---|
| 20 | ** distribution.
|
|---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
|---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
|---|
| 23 | ** products derived from this software without specific prior written
|
|---|
| 24 | ** permission.
|
|---|
| 25 | **
|
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|---|
| 37 | ** $QT_END_LICENSE$
|
|---|
| 38 | **
|
|---|
| 39 | ****************************************************************************/
|
|---|
| 40 |
|
|---|
| 41 | #include <QtGui>
|
|---|
| 42 |
|
|---|
| 43 | //! [0]
|
|---|
| 44 | class LightWidget : public QWidget
|
|---|
| 45 | {
|
|---|
| 46 | Q_OBJECT
|
|---|
| 47 | Q_PROPERTY(bool on READ isOn WRITE setOn)
|
|---|
| 48 | public:
|
|---|
| 49 | LightWidget(const QColor &color, QWidget *parent = 0)
|
|---|
| 50 | : QWidget(parent), m_color(color), m_on(false) {}
|
|---|
| 51 |
|
|---|
| 52 | bool isOn() const
|
|---|
| 53 | { return m_on; }
|
|---|
| 54 | void setOn(bool on)
|
|---|
| 55 | {
|
|---|
| 56 | if (on == m_on)
|
|---|
| 57 | return;
|
|---|
| 58 | m_on = on;
|
|---|
| 59 | update();
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | public slots:
|
|---|
| 63 | void turnOff() { setOn(false); }
|
|---|
| 64 | void turnOn() { setOn(true); }
|
|---|
| 65 |
|
|---|
| 66 | protected:
|
|---|
| 67 | virtual void paintEvent(QPaintEvent *)
|
|---|
| 68 | {
|
|---|
| 69 | if (!m_on)
|
|---|
| 70 | return;
|
|---|
| 71 | QPainter painter(this);
|
|---|
| 72 | painter.setRenderHint(QPainter::Antialiasing);
|
|---|
| 73 | painter.setBrush(m_color);
|
|---|
| 74 | painter.drawEllipse(0, 0, width(), height());
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | private:
|
|---|
| 78 | QColor m_color;
|
|---|
| 79 | bool m_on;
|
|---|
| 80 | };
|
|---|
| 81 | //! [0]
|
|---|
| 82 |
|
|---|
| 83 | //! [1]
|
|---|
| 84 | class TrafficLightWidget : public QWidget
|
|---|
| 85 | {
|
|---|
| 86 | public:
|
|---|
| 87 | TrafficLightWidget(QWidget *parent = 0)
|
|---|
| 88 | : QWidget(parent)
|
|---|
| 89 | {
|
|---|
| 90 | QVBoxLayout *vbox = new QVBoxLayout(this);
|
|---|
| 91 | m_red = new LightWidget(Qt::red);
|
|---|
| 92 | vbox->addWidget(m_red);
|
|---|
| 93 | m_yellow = new LightWidget(Qt::yellow);
|
|---|
| 94 | vbox->addWidget(m_yellow);
|
|---|
| 95 | m_green = new LightWidget(Qt::green);
|
|---|
| 96 | vbox->addWidget(m_green);
|
|---|
| 97 | QPalette pal = palette();
|
|---|
| 98 | pal.setColor(QPalette::Background, Qt::black);
|
|---|
| 99 | setPalette(pal);
|
|---|
| 100 | setAutoFillBackground(true);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | LightWidget *redLight() const
|
|---|
| 104 | { return m_red; }
|
|---|
| 105 | LightWidget *yellowLight() const
|
|---|
| 106 | { return m_yellow; }
|
|---|
| 107 | LightWidget *greenLight() const
|
|---|
| 108 | { return m_green; }
|
|---|
| 109 |
|
|---|
| 110 | private:
|
|---|
| 111 | LightWidget *m_red;
|
|---|
| 112 | LightWidget *m_yellow;
|
|---|
| 113 | LightWidget *m_green;
|
|---|
| 114 | };
|
|---|
| 115 | //! [1]
|
|---|
| 116 |
|
|---|
| 117 | //! [2]
|
|---|
| 118 | QState *createLightState(LightWidget *light, int duration, QState *parent = 0)
|
|---|
| 119 | {
|
|---|
| 120 | QState *lightState = new QState(parent);
|
|---|
| 121 | QTimer *timer = new QTimer(lightState);
|
|---|
| 122 | timer->setInterval(duration);
|
|---|
| 123 | timer->setSingleShot(true);
|
|---|
| 124 | QState *timing = new QState(lightState);
|
|---|
| 125 | QObject::connect(timing, SIGNAL(entered()), light, SLOT(turnOn()));
|
|---|
| 126 | QObject::connect(timing, SIGNAL(entered()), timer, SLOT(start()));
|
|---|
| 127 | QObject::connect(timing, SIGNAL(exited()), light, SLOT(turnOff()));
|
|---|
| 128 | QFinalState *done = new QFinalState(lightState);
|
|---|
| 129 | timing->addTransition(timer, SIGNAL(timeout()), done);
|
|---|
| 130 | lightState->setInitialState(timing);
|
|---|
| 131 | return lightState;
|
|---|
| 132 | }
|
|---|
| 133 | //! [2]
|
|---|
| 134 |
|
|---|
| 135 | //! [3]
|
|---|
| 136 | class TrafficLight : public QWidget
|
|---|
| 137 | {
|
|---|
| 138 | public:
|
|---|
| 139 | TrafficLight(QWidget *parent = 0)
|
|---|
| 140 | : QWidget(parent)
|
|---|
| 141 | {
|
|---|
| 142 | QVBoxLayout *vbox = new QVBoxLayout(this);
|
|---|
| 143 | TrafficLightWidget *widget = new TrafficLightWidget();
|
|---|
| 144 | vbox->addWidget(widget);
|
|---|
| 145 | vbox->setMargin(0);
|
|---|
| 146 |
|
|---|
| 147 | QStateMachine *machine = new QStateMachine(this);
|
|---|
| 148 | QState *redGoingYellow = createLightState(widget->redLight(), 3000);
|
|---|
| 149 | redGoingYellow->setObjectName("redGoingYellow");
|
|---|
| 150 | QState *yellowGoingGreen = createLightState(widget->yellowLight(), 1000);
|
|---|
| 151 | yellowGoingGreen->setObjectName("yellowGoingGreen");
|
|---|
| 152 | redGoingYellow->addTransition(redGoingYellow, SIGNAL(finished()), yellowGoingGreen);
|
|---|
| 153 | QState *greenGoingYellow = createLightState(widget->greenLight(), 3000);
|
|---|
| 154 | greenGoingYellow->setObjectName("greenGoingYellow");
|
|---|
| 155 | yellowGoingGreen->addTransition(yellowGoingGreen, SIGNAL(finished()), greenGoingYellow);
|
|---|
| 156 | QState *yellowGoingRed = createLightState(widget->yellowLight(), 1000);
|
|---|
| 157 | yellowGoingRed->setObjectName("yellowGoingRed");
|
|---|
| 158 | greenGoingYellow->addTransition(greenGoingYellow, SIGNAL(finished()), yellowGoingRed);
|
|---|
| 159 | yellowGoingRed->addTransition(yellowGoingRed, SIGNAL(finished()), redGoingYellow);
|
|---|
| 160 |
|
|---|
| 161 | machine->addState(redGoingYellow);
|
|---|
| 162 | machine->addState(yellowGoingGreen);
|
|---|
| 163 | machine->addState(greenGoingYellow);
|
|---|
| 164 | machine->addState(yellowGoingRed);
|
|---|
| 165 | machine->setInitialState(redGoingYellow);
|
|---|
| 166 | machine->start();
|
|---|
| 167 | }
|
|---|
| 168 | };
|
|---|
| 169 | //! [3]
|
|---|
| 170 |
|
|---|
| 171 | //! [4]
|
|---|
| 172 | int main(int argc, char **argv)
|
|---|
| 173 | {
|
|---|
| 174 | QApplication app(argc, argv);
|
|---|
| 175 |
|
|---|
| 176 | TrafficLight widget;
|
|---|
| 177 | widget.resize(110, 300);
|
|---|
| 178 | widget.show();
|
|---|
| 179 |
|
|---|
| 180 | return app.exec();
|
|---|
| 181 | }
|
|---|
| 182 | //! [4]
|
|---|
| 183 |
|
|---|
| 184 | #include "main.moc"
|
|---|