diff options
author | Ulf Hermann <ulf.hermann@theqtcompany.com> | 2016-03-18 12:45:51 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@theqtcompany.com> | 2016-03-22 15:00:53 +0000 |
commit | c070eb7e43a3111b2ca1cfc2084394d3a11a75a5 (patch) | |
tree | 5273b4e6e33d2af7e6225d0af1854d07ac601094 /examples/scxml | |
parent | 4e4f0f1cc2a96c7b27ee368d1f045e95a210a742 (diff) |
Polish traffic light example
Change-Id: I85f56f87b985ff966b975c571a399fa8f1f6e86f
Reviewed-by: Diana de Sousa <diana.desousa@theqtcompany.com>
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@theqtcompany.com>
Reviewed-by: Erik Verbruggen <erik.verbruggen@theqtcompany.com>
Diffstat (limited to 'examples/scxml')
22 files changed, 145 insertions, 151 deletions
diff --git a/examples/scxml/trafficlight-common/Button.qml b/examples/scxml/trafficlight-common/Button.qml index 36bec5f..24512ce 100644 --- a/examples/scxml/trafficlight-common/Button.qml +++ b/examples/scxml/trafficlight-common/Button.qml @@ -50,31 +50,13 @@ import QtQuick 2.5 -Item { +Image { id: button signal clicked - property string text: "hello" - Rectangle { - x: 5 - y: 5 - width: parent.width - 10 - height: parent.height - 10 - radius: 5 - color: "blue" - - Text { - anchors.fill: parent - color: "white" - text: button.text - horizontalAlignment: Text.AlignHCenter - verticalAlignment: Text.AlignVCenter - } - - MouseArea { - id: mouse - anchors.fill: parent - onClicked: button.clicked() - } + MouseArea { + id: mouse + anchors.fill: parent + onClicked: button.clicked() } } diff --git a/examples/scxml/trafficlight-common/Light.qml b/examples/scxml/trafficlight-common/Light.qml deleted file mode 100644 index f444a9c..0000000 --- a/examples/scxml/trafficlight-common/Light.qml +++ /dev/null @@ -1,68 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtScxml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -import QtQuick 2.5 -import QtQuick.Window 2.2 - -Item { - property string color: "blue" - width: Math.min(parent.width, parent.height / 3) - height: width - - Rectangle { - x: 3 - y: 3 - width: parent.width - 2 * x - height: parent.height - 2 * y - color: parent.color - radius: width - } -} - diff --git a/examples/scxml/trafficlight-common/TrafficLight.qml b/examples/scxml/trafficlight-common/TrafficLight.qml index 4d8cb15..03eac30 100644 --- a/examples/scxml/trafficlight-common/TrafficLight.qml +++ b/examples/scxml/trafficlight-common/TrafficLight.qml @@ -56,14 +56,13 @@ Window { property QtObject stateMachine visible: true - width: 100 - height: 350 color: "black" + width: lights.width + height: lights.height - Item { + Image { id: lights - width: parent.width - height: 300 + source: "qrc:///background.png" MouseArea { anchors.fill: parent @@ -72,40 +71,38 @@ Window { } } - Light { + Image { id: redLight - anchors.top: parent.top - color: "red" + x: (lights.width - width) / 2 + y: 40 + source: "qrc:///red.png" visible: stateMachine.red || stateMachine.redGoingGreen } - Light { + Image { id: yellowLight - anchors.top: redLight.bottom - color: "yellow" + x: (lights.width - width) / 2 + y: 135 + source: "qrc:///yellow.png" visible: stateMachine.yellow || stateMachine.blinking } - Light { + Image { id: greenLight - anchors.top: yellowLight.bottom - color: "green" + x: (lights.width - width) / 2 + y: 230 + source: "qrc:///green.png" visible: stateMachine.green } } Button { id: button - width: parent.width - anchors.top: lights.bottom + anchors.right: parent.right anchors.bottom: parent.bottom + anchors.margins: 20 - text: { - if (stateMachine.working) - "Pause" - else - "Unpause" - } + source: stateMachine.working ? "qrc:///pause.png" : "qrc:///play.png" onClicked: { if (stateMachine.working) diff --git a/examples/scxml/trafficlight-common/background.png b/examples/scxml/trafficlight-common/background.png Binary files differnew file mode 100644 index 0000000..8e5eb66 --- /dev/null +++ b/examples/scxml/trafficlight-common/background.png diff --git a/examples/scxml/trafficlight-common/doc/images/trafficlight.png b/examples/scxml/trafficlight-common/doc/images/trafficlight.png Binary files differindex e3ce3be..e4bc681 100644 --- a/examples/scxml/trafficlight-common/doc/images/trafficlight.png +++ b/examples/scxml/trafficlight-common/doc/images/trafficlight.png diff --git a/examples/scxml/trafficlight-common/doc/src/trafficlight-state-machine.qdocinc b/examples/scxml/trafficlight-common/doc/src/trafficlight-state-machine.qdocinc index 14a0619..5dcdb64 100644 --- a/examples/scxml/trafficlight-common/doc/src/trafficlight-state-machine.qdocinc +++ b/examples/scxml/trafficlight-common/doc/src/trafficlight-state-machine.qdocinc @@ -7,5 +7,6 @@ to the state specified by the \c target attribute: \quotefromfile trafficlight-common/statemachine.scxml - \skipto enable-qt-mode - \printuntil /^ {4}\<\// + \skipto <state id="red"> + \printuntil <state id="green"> + \printuntil </state> diff --git a/examples/scxml/trafficlight-common/green.png b/examples/scxml/trafficlight-common/green.png Binary files differnew file mode 100644 index 0000000..fd50b2c --- /dev/null +++ b/examples/scxml/trafficlight-common/green.png diff --git a/examples/scxml/trafficlight-common/pause.png b/examples/scxml/trafficlight-common/pause.png Binary files differnew file mode 100644 index 0000000..cc87637 --- /dev/null +++ b/examples/scxml/trafficlight-common/pause.png diff --git a/examples/scxml/trafficlight-common/play.png b/examples/scxml/trafficlight-common/play.png Binary files differnew file mode 100644 index 0000000..c984a88 --- /dev/null +++ b/examples/scxml/trafficlight-common/play.png diff --git a/examples/scxml/trafficlight-common/red.png b/examples/scxml/trafficlight-common/red.png Binary files differnew file mode 100644 index 0000000..98d86d9 --- /dev/null +++ b/examples/scxml/trafficlight-common/red.png diff --git a/examples/scxml/trafficlight-common/trafficlight.cpp b/examples/scxml/trafficlight-common/trafficlight.cpp index 7341c96..6cbed1b 100644 --- a/examples/scxml/trafficlight-common/trafficlight.cpp +++ b/examples/scxml/trafficlight-common/trafficlight.cpp @@ -56,19 +56,17 @@ class TrafficLightWidget : public QWidget { public: TrafficLightWidget(QWidget *parent = 0) - : QWidget(parent) + : QWidget(parent), m_background(QLatin1String(":/background.png")) { QVBoxLayout *vbox = new QVBoxLayout(this); - m_red = new LightWidget(Qt::red); - vbox->addWidget(m_red); - m_yellow = new LightWidget(Qt::yellow); - vbox->addWidget(m_yellow); - m_green = new LightWidget(Qt::green); - vbox->addWidget(m_green); - QPalette pal = palette(); - pal.setColor(QPalette::Background, Qt::black); - setPalette(pal); - setAutoFillBackground(true); + vbox->setContentsMargins(0, 40, 0, 80); + m_red = new LightWidget(QLatin1String(":/red.png")); + vbox->addWidget(m_red, 0, Qt::AlignHCenter); + m_yellow = new LightWidget(QLatin1String(":/yellow.png")); + vbox->addWidget(m_yellow, 0, Qt::AlignHCenter); + m_green = new LightWidget(QLatin1String(":/green.png")); + vbox->addWidget(m_green, 0, Qt::AlignHCenter); + setLayout(vbox); } LightWidget *redLight() const @@ -78,7 +76,20 @@ public: LightWidget *greenLight() const { return m_green; } + virtual void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE + { + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); + painter.drawImage(0, 0, m_background); + } + + virtual QSize sizeHint() const Q_DECL_OVERRIDE + { + return m_background.size(); + } + private: + QImage m_background; LightWidget *m_red; LightWidget *m_yellow; LightWidget *m_green; @@ -88,16 +99,8 @@ TrafficLight::TrafficLight(QScxmlStateMachine *machine, QWidget *parent) : QWidget(parent) , m_machine(machine) { - QVBoxLayout *vbox = new QVBoxLayout(this); - TrafficLightWidget *widget = new TrafficLightWidget(); - vbox->addWidget(widget); - vbox->setMargin(0); - - QPushButton *button = new QPushButton; - vbox->addWidget(button); - button->setCheckable(true); - button->setText(QStringLiteral("Pause")); - connect(button, SIGNAL(toggled(bool)), this, SLOT(toggleWorking(bool))); + TrafficLightWidget *widget = new TrafficLightWidget(this); + setFixedSize(widget->sizeHint()); Q_ASSERT(machine->init()); @@ -111,6 +114,17 @@ TrafficLight::TrafficLight(QScxmlStateMachine *machine, QWidget *parent) widget->yellowLight(), SLOT(switchLight(bool))); machine->connectToState(QStringLiteral("green"), widget->greenLight(), SLOT(switchLight(bool))); + + QAbstractButton *button = new ButtonWidget(this); + auto setButtonGeometry = [this, button](){ + QSize buttonSize = button->sizeHint(); + button->setGeometry(width() - buttonSize.width() - 20, height() - buttonSize.height() - 20, + buttonSize.width(), buttonSize.height()); + }; + connect(button, &QAbstractButton::toggled, this, setButtonGeometry); + setButtonGeometry(); + + connect(button, &QAbstractButton::toggled, this, &TrafficLight::toggleWorking); } void TrafficLight::toggleWorking(bool pause) @@ -118,9 +132,9 @@ void TrafficLight::toggleWorking(bool pause) m_machine->submitEvent(pause ? "smash" : "repair"); } -LightWidget::LightWidget(const QColor &color, QWidget *parent) +LightWidget::LightWidget(const QString &image, QWidget *parent) : QWidget(parent) - , m_color(color) + , m_image(image) , m_on(false) {} @@ -144,6 +158,29 @@ void LightWidget::paintEvent(QPaintEvent *) return; QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); - painter.setBrush(m_color); - painter.drawEllipse(0, 0, width(), height()); + painter.drawImage(0, 0, m_image); +} + +QSize LightWidget::sizeHint() const +{ + return m_image.size(); +} + +ButtonWidget::ButtonWidget(QWidget *parent) : + QAbstractButton(parent), m_playIcon(QLatin1String(":/play.png")), + m_pauseIcon(QLatin1String(":/pause.png")) +{ + setCheckable(true); +} + +void ButtonWidget::paintEvent(QPaintEvent *) +{ + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); + painter.drawImage(0, 0, isChecked() ? m_playIcon : m_pauseIcon); +} + +QSize ButtonWidget::sizeHint() const +{ + return isChecked() ? m_playIcon.size() : m_pauseIcon.size(); } diff --git a/examples/scxml/trafficlight-common/trafficlight.h b/examples/scxml/trafficlight-common/trafficlight.h index 0623249..cf9e22f 100644 --- a/examples/scxml/trafficlight-common/trafficlight.h +++ b/examples/scxml/trafficlight-common/trafficlight.h @@ -54,6 +54,7 @@ #include <QScxmlStateMachine> #include <QWidget> +#include <QAbstractButton> class TrafficLight : public QWidget { @@ -75,7 +76,7 @@ class LightWidget: public QWidget Q_PROPERTY(bool on READ isOn WRITE setOn) public: - LightWidget(const QColor &color, QWidget *parent = 0); + LightWidget(const QString &image, QWidget *parent = 0); bool isOn() const; void setOn(bool on); @@ -85,10 +86,26 @@ public slots: protected: virtual void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE; + virtual QSize sizeHint() const Q_DECL_OVERRIDE; private: - QColor m_color; + QImage m_image; bool m_on; }; +class ButtonWidget : public QAbstractButton +{ + Q_OBJECT +public: + ButtonWidget(QWidget *parent = 0); + +protected: + virtual void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE; + virtual QSize sizeHint() const Q_DECL_OVERRIDE; + +private: + QImage m_playIcon; + QImage m_pauseIcon; +}; + #endif // TRAFFICLIGHT_H diff --git a/examples/scxml/trafficlight-common/yellow.png b/examples/scxml/trafficlight-common/yellow.png Binary files differnew file mode 100644 index 0000000..a1e0876 --- /dev/null +++ b/examples/scxml/trafficlight-common/yellow.png diff --git a/examples/scxml/trafficlight-qml-dynamic/doc/src/trafficlight-qml-dynamic.qdoc b/examples/scxml/trafficlight-qml-dynamic/doc/src/trafficlight-qml-dynamic.qdoc index 67ec5c8..c2846d9 100644 --- a/examples/scxml/trafficlight-qml-dynamic/doc/src/trafficlight-qml-dynamic.qdoc +++ b/examples/scxml/trafficlight-qml-dynamic/doc/src/trafficlight-qml-dynamic.qdoc @@ -55,14 +55,14 @@ \quotefromfile trafficlight-qml-dynamic/trafficlight-qml-dynamic.qml \skipto import Scxml - \printuntil } + \printuntil /^\}/ \include trafficlight-state-machine.qdocinc We connect to the states as follows: \quotefromfile trafficlight-common/TrafficLight.qml - \skipto Light + \skipto /^ {8}Image/ \printuntil greenLight - \printuntil /^ {4}\}/ + \printuntil /^ {8}\}/ */ diff --git a/examples/scxml/trafficlight-qml-dynamic/trafficlight-qml-dynamic.qrc b/examples/scxml/trafficlight-qml-dynamic/trafficlight-qml-dynamic.qrc index fa110f2..475a7be 100644 --- a/examples/scxml/trafficlight-qml-dynamic/trafficlight-qml-dynamic.qrc +++ b/examples/scxml/trafficlight-qml-dynamic/trafficlight-qml-dynamic.qrc @@ -1,9 +1,14 @@ <RCC> <qresource prefix="/"> <file alias="TrafficLight.qml">../trafficlight-common/TrafficLight.qml</file> - <file alias="Light.qml">../trafficlight-common/Light.qml</file> <file alias="Button.qml">../trafficlight-common/Button.qml</file> <file>trafficlight-qml-dynamic.qml</file> <file alias="statemachine.scxml">../trafficlight-common/statemachine.scxml</file> + <file alias="green.png">../trafficlight-common/green.png</file> + <file alias="yellow.png">../trafficlight-common/yellow.png</file> + <file alias="red.png">../trafficlight-common/red.png</file> + <file alias="pause.png">../trafficlight-common/pause.png</file> + <file alias="play.png">../trafficlight-common/play.png</file> + <file alias="background.png">../trafficlight-common/background.png</file> </qresource> </RCC> diff --git a/examples/scxml/trafficlight-qml-static/doc/src/trafficlight-qml-static.qdoc b/examples/scxml/trafficlight-qml-static/doc/src/trafficlight-qml-static.qdoc index 77e7aa7..a83de5f 100644 --- a/examples/scxml/trafficlight-qml-static/doc/src/trafficlight-qml-static.qdoc +++ b/examples/scxml/trafficlight-qml-static/doc/src/trafficlight-qml-static.qdoc @@ -58,7 +58,7 @@ We connect to the states as follows: \quotefromfile trafficlight-common/TrafficLight.qml - \skipto Light + \skipto /^ {8}Image/ \printuntil greenLight - \printuntil /^ {4}\}/ + \printuntil /^ {8}\}/ */ diff --git a/examples/scxml/trafficlight-qml-static/trafficlight-qml-static.qrc b/examples/scxml/trafficlight-qml-static/trafficlight-qml-static.qrc index d75853c..46c408f 100644 --- a/examples/scxml/trafficlight-qml-static/trafficlight-qml-static.qrc +++ b/examples/scxml/trafficlight-qml-static/trafficlight-qml-static.qrc @@ -2,7 +2,12 @@ <qresource prefix="/"> <file>trafficlight-qml-static.qml</file> <file alias="TrafficLight.qml">../trafficlight-common/TrafficLight.qml</file> - <file alias="Light.qml">../trafficlight-common/Light.qml</file> <file alias="Button.qml">../trafficlight-common/Button.qml</file> + <file alias="green.png">../trafficlight-common/green.png</file> + <file alias="yellow.png">../trafficlight-common/yellow.png</file> + <file alias="red.png">../trafficlight-common/red.png</file> + <file alias="pause.png">../trafficlight-common/pause.png</file> + <file alias="play.png">../trafficlight-common/play.png</file> + <file alias="background.png">../trafficlight-common/background.png</file> </qresource> </RCC> diff --git a/examples/scxml/trafficlight-widgets-dynamic/trafficlight-widgets-dynamic.cpp b/examples/scxml/trafficlight-widgets-dynamic/trafficlight-widgets-dynamic.cpp index 223da17..cc0044d 100644 --- a/examples/scxml/trafficlight-widgets-dynamic/trafficlight-widgets-dynamic.cpp +++ b/examples/scxml/trafficlight-widgets-dynamic/trafficlight-widgets-dynamic.cpp @@ -69,7 +69,6 @@ int main(int argc, char **argv) } TrafficLight widget(machine); - widget.resize(110, 300); widget.show(); machine->setParent(&widget); machine->start(); diff --git a/examples/scxml/trafficlight-widgets-dynamic/trafficlight-widgets-dynamic.qrc b/examples/scxml/trafficlight-widgets-dynamic/trafficlight-widgets-dynamic.qrc index 897d792..5de9c00 100644 --- a/examples/scxml/trafficlight-widgets-dynamic/trafficlight-widgets-dynamic.qrc +++ b/examples/scxml/trafficlight-widgets-dynamic/trafficlight-widgets-dynamic.qrc @@ -1,5 +1,11 @@ <RCC> <qresource prefix="/"> <file alias="statemachine.scxml">../trafficlight-common/statemachine.scxml</file> + <file alias="green.png">../trafficlight-common/green.png</file> + <file alias="yellow.png">../trafficlight-common/yellow.png</file> + <file alias="red.png">../trafficlight-common/red.png</file> + <file alias="pause.png">../trafficlight-common/pause.png</file> + <file alias="play.png">../trafficlight-common/play.png</file> + <file alias="background.png">../trafficlight-common/background.png</file> </qresource> </RCC> diff --git a/examples/scxml/trafficlight-widgets-static/trafficlight-widgets-static.cpp b/examples/scxml/trafficlight-widgets-static/trafficlight-widgets-static.cpp index e0d59e3..67b5018 100644 --- a/examples/scxml/trafficlight-widgets-static/trafficlight-widgets-static.cpp +++ b/examples/scxml/trafficlight-widgets-static/trafficlight-widgets-static.cpp @@ -59,7 +59,6 @@ int main(int argc, char **argv) TrafficLightStateMachine machine; TrafficLight widget(&machine); - widget.resize(110, 300); widget.show(); machine.start(); diff --git a/examples/scxml/trafficlight-widgets-static/trafficlight-widgets-static.pro b/examples/scxml/trafficlight-widgets-static/trafficlight-widgets-static.pro index b40a692..c98fa98 100644 --- a/examples/scxml/trafficlight-widgets-static/trafficlight-widgets-static.pro +++ b/examples/scxml/trafficlight-widgets-static/trafficlight-widgets-static.pro @@ -12,3 +12,6 @@ SOURCES += trafficlight-widgets-static.cpp # install target.path = $$[QT_INSTALL_EXAMPLES]/scxml/trafficlight-widgets-static INSTALLS += target + +RESOURCES += \ + trafficlight-widgets-static.qrc diff --git a/examples/scxml/trafficlight-widgets-static/trafficlight-widgets-static.qrc b/examples/scxml/trafficlight-widgets-static/trafficlight-widgets-static.qrc new file mode 100644 index 0000000..af19380 --- /dev/null +++ b/examples/scxml/trafficlight-widgets-static/trafficlight-widgets-static.qrc @@ -0,0 +1,11 @@ +<RCC> + <qresource prefix="/"> + <file alias="green.png">../trafficlight-common/green.png</file> + <file alias="yellow.png">../trafficlight-common/yellow.png</file> + <file alias="red.png">../trafficlight-common/red.png</file> + <file alias="pause.png">../trafficlight-common/pause.png</file> + <file alias="play.png">../trafficlight-common/play.png</file> + <file alias="background.png">../trafficlight-common/background.png</file> + </qresource> +</RCC> + |