diff options
author | Jarek Kobus <jaroslaw.kobus@theqtcompany.com> | 2015-08-25 16:04:14 +0200 |
---|---|---|
committer | Erik Verbruggen <erik.verbruggen@theqtcompany.com> | 2015-10-30 13:08:05 +0000 |
commit | 9b4a5ada50ab01527dc72ef612735da19a7214ef (patch) | |
tree | 29411a83914eb9b6345628d3efa836b2c0f6e3e6 /examples | |
parent | 91d6194d28843f477326084f3624a8e0ff570ca9 (diff) |
Add a calculator example, taken from w3c document.
The example got slightly modified.
Not working perfectly (due to internal logic of the example).
Change-Id: I4698688dd724497518aadbe5155a280329074615
Reviewed-by: Erik Verbruggen <erik.verbruggen@theqtcompany.com>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/calculator-common/calculator.scxml | 164 | ||||
-rw-r--r-- | examples/calculator-common/mainwindow.cpp | 121 | ||||
-rw-r--r-- | examples/calculator-common/mainwindow.h | 70 | ||||
-rw-r--r-- | examples/calculator-common/mainwindow.ui | 180 | ||||
-rw-r--r-- | examples/calculator-widgets-static/calculator-widgets-static.cpp | 60 | ||||
-rw-r--r-- | examples/calculator-widgets-static/calculator-widgets-static.pro | 21 | ||||
-rw-r--r-- | examples/examples.pro | 1 |
7 files changed, 617 insertions, 0 deletions
diff --git a/examples/calculator-common/calculator.scxml b/examples/calculator-common/calculator.scxml new file mode 100644 index 0000000..2b1e926 --- /dev/null +++ b/examples/calculator-common/calculator.scxml @@ -0,0 +1,164 @@ +<?xml version="1.0" ?> +<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" + initial="wrapper" datamodel="ecmascript" name="Calculator"> + <datamodel> + <data id="long_expr" /> + <data id="short_expr" /> + <data id="res" /> + </datamodel> + <state id="wrapper" initial="on"> + <state id="on" initial="ready"> + <onentry> + <send event="DISPLAY.UPDATE" /> + </onentry> + <state id="ready" initial="begin"> + <state id="begin"> + <transition event="OPER.MINUS" target="negated1" /> + <onentry> + <assign location="long_expr" expr="''" /> + <assign location="short_expr" expr="0" /> + <assign location="res" expr="0" /> + <send event="DISPLAY.UPDATE" /> + </onentry> + </state> + <state id="result"> + </state> + <transition event="OPER" target="opEntered" /> + <transition event="DIGIT.0" target="zero1"> + <assign location="short_expr" expr="''" /> + </transition> + <transition event="DIGIT" target="int1"> + <assign location="short_expr" expr="''" /> + </transition> + <transition event="POINT" target="frac1"> + <assign location="short_expr" expr="''" /> + </transition> + </state> + <state id="negated1"> + <onentry> + <assign location="short_expr" expr="'-'" /> + <send event="DISPLAY.UPDATE" /> + </onentry> + <transition event="DIGIT.0" target="zero1" /> + <transition event="DIGIT" target="int1" /> + <transition event="POINT" target="frac1" /> + </state> + <state id="operand1"> + <state id="zero1"> + <transition event="DIGIT" cond="_event.name != 'DIGIT.0'" target="int1" /> + <transition event="POINT" target="frac1" /> + </state> + <state id="int1"> + <transition event="POINT" target="frac1" /> + <transition event="DIGIT"> + <assign location="short_expr" expr="short_expr+_event.name.substr(_event.name.lastIndexOf('.')+1)" /> + <send event="DISPLAY.UPDATE" /> + </transition> + <onentry> + <assign location="short_expr" expr="short_expr+_event.name.substr(_event.name.lastIndexOf('.')+1)" /> + <send event="DISPLAY.UPDATE" /> + </onentry> + </state> + <state id="frac1"> + <onentry> + <assign location="short_expr" expr="short_expr+'.'" /> + <send event="DISPLAY.UPDATE" /> + </onentry> + <transition event="DIGIT"> + <assign location="short_expr" expr="short_expr+_event.name.substr(_event.name.lastIndexOf('.')+1)" /> + <send event="DISPLAY.UPDATE" /> + </transition> + </state> + <transition event="OPER" target="opEntered" /> + </state> + <state id="opEntered"> + <transition event="OPER.MINUS" target="negated2" /> + <transition event="POINT" target="frac2" /> + <transition event="DIGIT.0" target="zero2" /> + <transition event="DIGIT" target="int2" /> + <onentry> + <raise event="CALC.SUB" /> + <send target="#_internal" event="OP.INSERT"> + <param name="operator" expr="_event.name" /> + </send> + </onentry> + </state> + <state id="negated2"> + <onentry> + <assign location="short_expr" expr="'-'" /> + <send event="DISPLAY.UPDATE" /> + </onentry> + <transition event="DIGIT.0" target="zero2" /> + <transition event="DIGIT" target="int2" /> + <transition event="POINT" target="frac2" /> + </state> + <state id="operand2"> + <state id="zero2"> + <transition event="DIGIT" cond="_event.name != 'DIGIT.0'" target="int2" /> + <transition event="POINT" target="frac2" /> + </state> + <state id="int2"> + <transition event="DIGIT"> + <assign location="short_expr" expr="short_expr+_event.name.substr(_event.name.lastIndexOf('.')+1)" /> + <send event="DISPLAY.UPDATE" /> + </transition> + <onentry> + <assign location="short_expr" expr="short_expr+_event.name.substr(_event.name.lastIndexOf('.')+1)" /> + <send event="DISPLAY.UPDATE" /> + </onentry> + <transition event="POINT" target="frac2" /> + </state> + <state id="frac2"> + <onentry> + <assign location="short_expr" expr="short_expr +'.'" /> + <send event="DISPLAY.UPDATE" /> + </onentry> + <transition event="DIGIT"> + <assign location="short_expr" expr="short_expr +_event.name.substr(_event.name.lastIndexOf('.')+1)" /> + <send event="DISPLAY.UPDATE" /> + </transition> + </state> + <transition event="OPER" target="opEntered"> + <raise event="CALC.SUB" /> + <raise event="OP.INSERT" /> + </transition> + <transition event="EQUALS" target="result"> + <raise event="CALC.SUB" /> + <raise event="CALC.DO" /> + </transition> + </state> + <transition event="C" target="on" /> + </state> + <transition event="CALC.DO"> + <assign location="short_expr" expr="''+ res" /> + <assign location="long_expr" expr="''" /> + <assign location="res" expr="0" /> + </transition> + <transition event="CALC.SUB"> + <if cond="short_expr!=''"> + <assign location="long_expr" expr="long_expr+'('+short_expr+')'" /> + </if> + <assign location="res" expr="eval(long_expr)" /> + <assign location="short_expr" expr="''" /> + <send event="DISPLAY.UPDATE" /> + </transition> + <transition event="DISPLAY.UPDATE"> + <log label="'result'" expr="short_expr==''?res:short_expr" /> + <send type="qt:signal" event="updateDisplay"> + <param name="display" expr="short_expr==''?res:short_expr"/> + </send> + </transition> + <transition event="OP.INSERT"> + <log expr="_event.data.operator" /> + <if cond="_event.data.operator == 'OPER.PLUS'"> + <assign location="long_expr" expr="long_expr+'+'" /> + <elseif cond="_event.data.operator=='OPER.MINUS'" /> + <assign location="long_expr" expr="long_expr+'-'" /> + <elseif cond="_event.data.operator=='OPER.STAR'" /> + <assign location="long_expr" expr="long_expr+'*'" /> + <elseif cond="_event.data.operator=='OPER.DIV'" /> + <assign location="long_expr" expr="long_expr+'/'" /> + </if> + </transition> + </state> +</scxml> diff --git a/examples/calculator-common/mainwindow.cpp b/examples/calculator-common/mainwindow.cpp new file mode 100644 index 0000000..0502d41 --- /dev/null +++ b/examples/calculator-common/mainwindow.cpp @@ -0,0 +1,121 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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$ +** +****************************************************************************/ + +#include "mainwindow.h" +#include "ui_mainwindow.h" + +#include <QStringListModel> +#include <QScxmlStateMachine> + +#include <QDebug> + +QT_USE_NAMESPACE + +MainWindow::MainWindow(QScxmlStateMachine *machine, QWidget *parent) : + QWidget(parent), + ui(new Ui::MainWindow), + m_machine(machine) +{ + ui->setupUi(this); + + connect(ui->digit0, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("DIGIT.0"); + }); + connect(ui->digit1, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("DIGIT.1"); + }); + connect(ui->digit2, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("DIGIT.2"); + }); + connect(ui->digit3, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("DIGIT.3"); + }); + connect(ui->digit4, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("DIGIT.4"); + }); + connect(ui->digit5, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("DIGIT.5"); + }); + connect(ui->digit6, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("DIGIT.6"); + }); + connect(ui->digit7, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("DIGIT.7"); + }); + connect(ui->digit8, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("DIGIT.8"); + }); + connect(ui->digit9, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("DIGIT.9"); + }); + connect(ui->point, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("POINT"); + }); + connect(ui->operPlus, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("OPER.PLUS"); + }); + connect(ui->operMinus, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("OPER.MINUS"); + }); + connect(ui->operStar, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("OPER.STAR"); + }); + connect(ui->operDiv, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("OPER.DIV"); + }); + connect(ui->equals, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("EQUALS"); + }); + connect(ui->c, &QAbstractButton::clicked, [this] { + m_machine->submitEvent("C"); + }); + +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::updateDisplay(const QVariant &data) +{ + QString display = data.toMap().value("display").toString(); + ui->display->setText(display); +} diff --git a/examples/calculator-common/mainwindow.h b/examples/calculator-common/mainwindow.h new file mode 100644 index 0000000..c5c1a33 --- /dev/null +++ b/examples/calculator-common/mainwindow.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QWidget> + +QT_BEGIN_NAMESPACE +namespace Ui { +class MainWindow; +} +QT_END_NAMESPACE + +class QScxmlStateMachine; + +class MainWindow : public QWidget +{ + Q_OBJECT + +public: + explicit MainWindow(QScxmlStateMachine *machine, QWidget *parent = 0); + ~MainWindow(); + +public slots: + void updateDisplay(const QVariant &data); + +private: + QT_PREPEND_NAMESPACE(Ui::MainWindow) *ui; + QScxmlStateMachine *m_machine; +}; + +#endif // MAINWINDOW_H diff --git a/examples/calculator-common/mainwindow.ui b/examples/calculator-common/mainwindow.ui new file mode 100644 index 0000000..8001ff1 --- /dev/null +++ b/examples/calculator-common/mainwindow.ui @@ -0,0 +1,180 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>MainWindow</class> + <widget class="QWidget" name="MainWindow"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>364</width> + <height>182</height> + </rect> + </property> + <property name="windowTitle"> + <string>Form</string> + </property> + <layout class="QGridLayout" name="gridLayout_2"> + <item row="0" column="0" colspan="2"> + <widget class="QLabel" name="display"> + <property name="frameShape"> + <enum>QFrame::StyledPanel</enum> + </property> + <property name="alignment"> + <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> + </property> + </widget> + </item> + <item row="1" column="0"> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item row="1" column="1"> + <layout class="QGridLayout" name="gridLayout"> + <item row="0" column="0"> + <widget class="QPushButton" name="digit7"> + <property name="text"> + <string>7</string> + </property> + </widget> + </item> + <item row="3" column="1"> + <widget class="QPushButton" name="point"> + <property name="text"> + <string>.</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QPushButton" name="digit5"> + <property name="text"> + <string>5</string> + </property> + </widget> + </item> + <item row="0" column="2"> + <widget class="QPushButton" name="digit9"> + <property name="text"> + <string>9</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QPushButton" name="digit8"> + <property name="text"> + <string>8</string> + </property> + </widget> + </item> + <item row="4" column="2"> + <widget class="QPushButton" name="c"> + <property name="text"> + <string>C</string> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QPushButton" name="digit4"> + <property name="text"> + <string>4</string> + </property> + </widget> + </item> + <item row="2" column="3"> + <widget class="QPushButton" name="operStar"> + <property name="text"> + <string>*</string> + </property> + </widget> + </item> + <item row="2" column="2"> + <widget class="QPushButton" name="digit3"> + <property name="text"> + <string>3</string> + </property> + </widget> + </item> + <item row="4" column="3"> + <widget class="QPushButton" name="equals"> + <property name="text"> + <string>=</string> + </property> + </widget> + </item> + <item row="1" column="3"> + <widget class="QPushButton" name="operMinus"> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="2" column="0"> + <widget class="QPushButton" name="digit1"> + <property name="text"> + <string>1</string> + </property> + </widget> + </item> + <item row="3" column="0"> + <widget class="QPushButton" name="digit0"> + <property name="text"> + <string>0</string> + </property> + </widget> + </item> + <item row="3" column="3"> + <widget class="QPushButton" name="operDiv"> + <property name="text"> + <string>/</string> + </property> + </widget> + </item> + <item row="0" column="3"> + <widget class="QPushButton" name="operPlus"> + <property name="text"> + <string>+</string> + </property> + </widget> + </item> + <item row="1" column="2"> + <widget class="QPushButton" name="digit6"> + <property name="text"> + <string>6</string> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QPushButton" name="digit2"> + <property name="text"> + <string>2</string> + </property> + </widget> + </item> + </layout> + </item> + <item row="2" column="1"> + <spacer name="verticalSpacer"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>10</height> + </size> + </property> + </spacer> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/examples/calculator-widgets-static/calculator-widgets-static.cpp b/examples/calculator-widgets-static/calculator-widgets-static.cpp new file mode 100644 index 0000000..ee4567b --- /dev/null +++ b/examples/calculator-widgets-static/calculator-widgets-static.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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$ +** +****************************************************************************/ + +#include "calculator.h" +#include "../calculator-common/mainwindow.h" + +#include <QApplication> + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + Calculator machine; + machine.init(); + MainWindow mainWindow(&machine); + + QObject::connect(&machine, &Calculator::event_updateDisplay, + &mainWindow, &MainWindow::updateDisplay); + + machine.start(); + mainWindow.show(); + return app.exec(); +} diff --git a/examples/calculator-widgets-static/calculator-widgets-static.pro b/examples/calculator-widgets-static/calculator-widgets-static.pro new file mode 100644 index 0000000..5bf4c27 --- /dev/null +++ b/examples/calculator-widgets-static/calculator-widgets-static.pro @@ -0,0 +1,21 @@ +QT += widgets scxml + +CONFIG += c++11 + +STATECHARTS = ../calculator-common/calculator.scxml + +SOURCES += \ + calculator-widgets-static.cpp \ + ../calculator-common/mainwindow.cpp + +FORMS += \ + ../calculator-common/mainwindow.ui + +HEADERS += \ + ../calculator-common/mainwindow.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/scxml/calculator-widgets +INSTALLS += target + +load(qscxmlc) diff --git a/examples/examples.pro b/examples/examples.pro index b7d4253..7ea8f7d 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -7,6 +7,7 @@ qtHaveModule(widgets) { SUBDIRS += mediaplayer-widgets-dynamic SUBDIRS += pinball-widgets-static SUBDIRS += pinball-ecmascript-widgets-static + SUBDIRS += calculator-widgets-static } qtHaveModule(qml) { |