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 QtScxml 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 "trafficlight.h"
52
53#include <QPainter>
54#include <QVBoxLayout>
55
56class TrafficLightWidget : public QWidget
57{
58public:
59 TrafficLightWidget(QWidget *parent = nullptr)
60 : QWidget(parent), m_background(QLatin1String(":/background.png"))
61 {
62 QVBoxLayout *vbox = new QVBoxLayout(this);
63 vbox->setContentsMargins(left: 0, top: 40, right: 0, bottom: 80);
64 m_red = new LightWidget(QLatin1String(":/red.png"));
65 vbox->addWidget(m_red, stretch: 0, alignment: Qt::AlignHCenter);
66 m_yellow = new LightWidget(QLatin1String(":/yellow.png"));
67 vbox->addWidget(m_yellow, stretch: 0, alignment: Qt::AlignHCenter);
68 m_green = new LightWidget(QLatin1String(":/green.png"));
69 vbox->addWidget(m_green, stretch: 0, alignment: Qt::AlignHCenter);
70 setLayout(vbox);
71 }
72
73 LightWidget *redLight() const
74 { return m_red; }
75 LightWidget *yellowLight() const
76 { return m_yellow; }
77 LightWidget *greenLight() const
78 { return m_green; }
79
80 void paintEvent(QPaintEvent *) override
81 {
82 QPainter painter(this);
83 painter.setRenderHint(hint: QPainter::Antialiasing);
84 painter.drawImage(x: 0, y: 0, image: m_background);
85 }
86
87 QSize sizeHint() const override
88 {
89 return m_background.size();
90 }
91
92private:
93 QImage m_background;
94 LightWidget *m_red;
95 LightWidget *m_yellow;
96 LightWidget *m_green;
97};
98
99TrafficLight::TrafficLight(QScxmlStateMachine *machine, QWidget *parent)
100 : QWidget(parent)
101 , m_machine(machine)
102{
103 TrafficLightWidget *widget = new TrafficLightWidget(this);
104 setFixedSize(widget->sizeHint());
105
106 machine->connectToState(QStringLiteral("red"),
107 receiver: widget->redLight(), method: &LightWidget::switchLight);
108 machine->connectToState(QStringLiteral("redGoingGreen"),
109 receiver: widget->redLight(), method: &LightWidget::switchLight);
110 machine->connectToState(QStringLiteral("yellow"),
111 receiver: widget->yellowLight(), method: &LightWidget::switchLight);
112 machine->connectToState(QStringLiteral("blinking"),
113 receiver: widget->yellowLight(), method: &LightWidget::switchLight);
114 machine->connectToState(QStringLiteral("green"),
115 receiver: widget->greenLight(), method: &LightWidget::switchLight);
116
117 QAbstractButton *button = new ButtonWidget(this);
118 auto setButtonGeometry = [this, button](){
119 QSize buttonSize = button->sizeHint();
120 button->setGeometry(ax: width() - buttonSize.width() - 20,
121 ay: height() - buttonSize.height() - 20,
122 aw: buttonSize.width(), ah: buttonSize.height());
123 };
124 connect(sender: button, signal: &QAbstractButton::toggled, context: this, slot: setButtonGeometry);
125 setButtonGeometry();
126
127 connect(sender: button, signal: &QAbstractButton::toggled,
128 receiver: this, slot: &TrafficLight::toggleWorking);
129}
130
131void TrafficLight::toggleWorking(bool pause)
132{
133 m_machine->submitEvent(eventName: pause ? "smash" : "repair");
134}
135
136LightWidget::LightWidget(const QString &image, QWidget *parent)
137 : QWidget(parent)
138 , m_image(image)
139{}
140
141bool LightWidget::isOn() const
142{ return m_on; }
143
144void LightWidget::setOn(bool on)
145{
146 if (on == m_on)
147 return;
148 m_on = on;
149 update();
150}
151
152void LightWidget::switchLight(bool onoff)
153{ setOn(onoff); }
154
155void LightWidget::paintEvent(QPaintEvent *)
156{
157 if (!m_on)
158 return;
159 QPainter painter(this);
160 painter.setRenderHint(hint: QPainter::Antialiasing);
161 painter.drawImage(x: 0, y: 0, image: m_image);
162}
163
164QSize LightWidget::sizeHint() const
165{
166 return m_image.size();
167}
168
169ButtonWidget::ButtonWidget(QWidget *parent) :
170 QAbstractButton(parent), m_playIcon(QLatin1String(":/play.png")),
171 m_pauseIcon(QLatin1String(":/pause.png"))
172{
173 setCheckable(true);
174}
175
176void ButtonWidget::paintEvent(QPaintEvent *)
177{
178 QPainter painter(this);
179 painter.setRenderHint(hint: QPainter::Antialiasing);
180 painter.drawImage(x: 0, y: 0, image: isChecked() ? m_playIcon : m_pauseIcon);
181}
182
183QSize ButtonWidget::sizeHint() const
184{
185 return isChecked() ? m_playIcon.size() : m_pauseIcon.size();
186}
187

source code of qtscxml/examples/scxml/trafficlight-common/trafficlight.cpp