diff options
author | Noam Rosenthal <nrosenth@nokia.com> | 2009-06-08 12:27:03 -0700 |
---|---|---|
committer | Noam Rosenthal <nrosenth@nokia.com> | 2009-06-08 12:27:03 -0700 |
commit | d0441f605434a89b53735427e4e81182c65debbd (patch) | |
tree | b96d25dc89cdb523c007a22bc0deed3a5aa5dd56 /examples/calc | |
parent | a6553f68f17c28adca049857686496a69b4c1e7a (diff) |
scxml for 4.6
Diffstat (limited to 'examples/calc')
-rw-r--r-- | examples/calc/calc.cpp | 35 | ||||
-rw-r--r-- | examples/calc/calc.h | 19 | ||||
-rw-r--r-- | examples/calc/calc.pro | 16 | ||||
-rw-r--r-- | examples/calc/calc.qrc | 5 | ||||
-rw-r--r-- | examples/calc/calc.scxml | 213 | ||||
-rw-r--r-- | examples/calc/calc.ui | 187 | ||||
-rw-r--r-- | examples/calc/main.cpp | 15 |
7 files changed, 490 insertions, 0 deletions
diff --git a/examples/calc/calc.cpp b/examples/calc/calc.cpp new file mode 100644 index 0000000..4b61027 --- /dev/null +++ b/examples/calc/calc.cpp @@ -0,0 +1,35 @@ +#include "calc.h" +#include "ui_calc.h" +#include <QSignalMapper> +CalcWidget::CalcWidget(QWidget *parent) + : QWidget(parent) +{ + setupUi(this); + QSignalMapper* mapper = new QSignalMapper(this); + connect (mapper, SIGNAL(mapped(QString)), this, SIGNAL(command(QString))); + QList<QAbstractButton*> buttons = findChildren<QAbstractButton*>(); + foreach (QAbstractButton* b, buttons) { + connect (b, SIGNAL(clicked()), mapper, SLOT(map())); + } + mapper->setMapping(button0,"DIGIT.0"); + mapper->setMapping(button1,"DIGIT.1"); + mapper->setMapping(button2,"DIGIT.2"); + mapper->setMapping(button3,"DIGIT.3"); + mapper->setMapping(button4,"DIGIT.4"); + mapper->setMapping(button5,"DIGIT.5"); + mapper->setMapping(button6,"DIGIT.6"); + mapper->setMapping(button7,"DIGIT.7"); + mapper->setMapping(button8,"DIGIT.8"); + mapper->setMapping(button9,"DIGIT.9"); + mapper->setMapping(buttonEq,"EQUALS"); + mapper->setMapping(buttonCE,"CE"); + mapper->setMapping(buttonC,"C"); + mapper->setMapping(buttonPoint,"POINT"); + mapper->setMapping(buttonPlus,"OPER.PLUS"); + mapper->setMapping(buttonStar,"OPER.STAR"); + mapper->setMapping(buttonMinus,"OPER.MINUS"); + mapper->setMapping(buttonDiv,"OPER.DIV"); +} +CalcWidget::~CalcWidget() +{ +} diff --git a/examples/calc/calc.h b/examples/calc/calc.h new file mode 100644 index 0000000..6c7cadc --- /dev/null +++ b/examples/calc/calc.h @@ -0,0 +1,19 @@ +#ifndef CALC_H +#define CALC_H + +#include <QtGui/QWidget> +#include "ui_calc.h" + +class CalcWidget : public QWidget, public Ui::CalcClass +{ + Q_OBJECT + +public: + CalcWidget(QWidget *parent = 0); + ~CalcWidget(); + +Q_SIGNALS: + void command(const QString &); +}; + +#endif // CALC_H diff --git a/examples/calc/calc.pro b/examples/calc/calc.pro new file mode 100644 index 0000000..ff274c2 --- /dev/null +++ b/examples/calc/calc.pro @@ -0,0 +1,16 @@ +# ------------------------------------------------- +# Project created by QtCreator 2008-12-25T19:50:44 +# ------------------------------------------------- + +TARGET = calc +TEMPLATE = app +win32: CONFIG += console +mac:CONFIG -= app_bundle +QT = core gui script +include($$PWD/../../src/qtstatemachine.pri) + +# Input +SOURCES += main.cpp calc.cpp +HEADERS += calc.h +FORMS += calc.ui +RESOURCES += calc.qrc diff --git a/examples/calc/calc.qrc b/examples/calc/calc.qrc new file mode 100644 index 0000000..04b1be3 --- /dev/null +++ b/examples/calc/calc.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/" > + <file>calc.scxml</file> + </qresource> +</RCC> diff --git a/examples/calc/calc.scxml b/examples/calc/calc.scxml new file mode 100644 index 0000000..35a9b9e --- /dev/null +++ b/examples/calc/calc.scxml @@ -0,0 +1,213 @@ +<!-- http://www.state-machine.com/devzone/Recipe_DesigningHSM.pdf --> +<!-- events: OPER.PLUS OPER.MINUS OPER.MULTIPLY OPER.DIVIDE DIGIT.0 DIGIT.1_9 EQUALS CE C POINT - +--> +<scxml + initial="on" profile="ecma" name="calc"> + <script> + function insertDigit () + { + insert (_event.name.charAt(_event.name.lastIndexOf('.')+1)); + } + function insert(c) + { + short_expr += c; + updateDisplay(); + } + function negate () + { + short_expr = "-"; + updateDisplay (); + } + + function updateDisplay () + { + if (short_expr == "") + dispLbl.text = _data.res; + else + dispLbl.text = short_expr; + } + function subcalc () + { + if (short_expr != "") + _data.long_expr += "(" + short_expr + ")"; + _data.res = eval(_data.long_expr); + short_expr = ""; + updateDisplay (); + return true; + } + + function insertOp () + { + var sc = subcalc (); + var op = ''; + if (_event.name == "OPER.PLUS") + op = '+'; + else if (_event.name == "OPER.MINUS") + op = '-'; + else if (_event.name == "OPER.STAR") + op = '*'; + else if (_event.name == "OPER.DIV") + op = '/'; + _data.long_expr += op; + return sc; + } + function reset () + { + short_expr = ""; + } + function calc () + { + if (subcalc ()) { + short_expr = "" + _data.res; + _data.long_expr = ""; + _data.res = 0; + return true; + } else + return false; + } + </script> + <state id="on" initial="ready"> + <datamodel> + <data id="long_expr" /> + <data id="res" >0</data> + </datamodel> + <onentry> + <script> + var short_expr = 0; + _data.res = 0; + _data.long_expr = ""; + updateDisplay(); + </script> + </onentry> + <state id="ready" initial="begin"> + <state id="begin"> + <transition event="OPER.MINUS" target="negated1" /> + <onentry> + <script> + updateDisplay (); + </script> + </onentry> + </state> + <state id="result"> + </state> + <transition event="OPER" target="opEntered" /> + <transition event="DIGIT.0" target="zero1"> + <script> + reset (); + </script> + </transition> + <transition event="DIGIT" target="int1"> + <script> + reset (); + </script> + </transition> + <transition event="POINT" target="frac1"> + <script> + reset (); + </script> + </transition> + </state> + <state id="negated1"> + <onentry> + <script> + negate (); + </script> + </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"> + <script> + insertDigit (); + </script> + </transition> + <onentry> + <script> + insertDigit (); + </script> + </onentry> + </state> + <state id="frac1"> + <onentry> + <script> + insert ('.'); + </script> + </onentry> + <transition event="DIGIT"> + <script> + insertDigit (); + </script> + </transition> + </state> + <transition event="CE" target="ready" /> + <transition event="OPER" target="opEntered" /> + </state> + <state id="error" /> + <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> + <script> + insertOp (); + </script> + </onentry> + </state> + <state id="negated2"> + <onentry> + <script> + negate (); + </script> + </onentry> + <transition event="CE" target="opEntered" /> + <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"> + <script> + insertDigit (); + </script> + </transition> + <onentry> + <script> + insertDigit (); + </script> + </onentry> + <transition event="POINT" target="frac2" /> + </state> + <state id="frac2"> + <onentry> + <script> + insert ('.'); + </script> + </onentry> + <transition event="DIGIT"> + <script> + insertDigit (); + </script> + </transition> + </state> + <transition event="OPER" cond="!insertOp()" target="error" /> + <transition event="OPER" target="opEntered" /> + <transition event="EQUALS" cond="!calc()" target="error" /> + <transition event="EQUALS" target="result" /> + </state> + <transition event="C" target="on" /> + </state> +</scxml> diff --git a/examples/calc/calc.ui b/examples/calc/calc.ui new file mode 100644 index 0000000..0e2a651 --- /dev/null +++ b/examples/calc/calc.ui @@ -0,0 +1,187 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>CalcClass</class> + <widget class="QWidget" name="CalcClass"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>419</width> + <height>156</height> + </rect> + </property> + <property name="maximumSize"> + <size> + <width>419</width> + <height>400</height> + </size> + </property> + <property name="windowTitle"> + <string>SCXML Calculator</string> + </property> + <property name="styleSheet"> + <string notr="true">* {background-color:black;color:white} +button {border-width:3; }</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QLabel" name="dispLbl"> + <property name="styleSheet"> + <string notr="true"> +background-color: #CCCCCC; +color: black; +font: 12pt "Courier New";</string> + </property> + <property name="text"> + <string/> + </property> + <property name="alignment"> + <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> + </property> + </widget> + </item> + <item> + <layout class="QGridLayout" name="gridLayout"> + <property name="horizontalSpacing"> + <number>6</number> + </property> + <item row="3" column="0"> + <widget class="QPushButton" name="button4"> + <property name="text"> + <string>4</string> + </property> + </widget> + </item> + <item row="4" column="0"> + <widget class="QPushButton" name="button1"> + <property name="text"> + <string>1</string> + </property> + </widget> + </item> + <item row="1" column="4"> + <widget class="QPushButton" name="buttonCE"> + <property name="styleSheet"> + <string notr="true">color:red;font-weight:bold</string> + </property> + <property name="text"> + <string>CE</string> + </property> + </widget> + </item> + <item row="1" column="2"> + <widget class="QPushButton" name="button9"> + <property name="text"> + <string>9</string> + </property> + </widget> + </item> + <item row="3" column="1"> + <widget class="QPushButton" name="button5"> + <property name="text"> + <string>5</string> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QPushButton" name="button7"> + <property name="text"> + <string>7</string> + </property> + </widget> + </item> + <item row="4" column="1"> + <widget class="QPushButton" name="button2"> + <property name="text"> + <string>2</string> + </property> + </widget> + </item> + <item row="5" column="0" colspan="2"> + <widget class="QPushButton" name="button0"> + <property name="text"> + <string>0</string> + </property> + </widget> + </item> + <item row="3" column="2"> + <widget class="QPushButton" name="button6"> + <property name="text"> + <string>6</string> + </property> + </widget> + </item> + <item row="4" column="2"> + <widget class="QPushButton" name="button3"> + <property name="text"> + <string>3</string> + </property> + </widget> + </item> + <item row="5" column="2"> + <widget class="QPushButton" name="buttonPoint"> + <property name="text"> + <string>.</string> + </property> + </widget> + </item> + <item row="3" column="3"> + <widget class="QPushButton" name="buttonPlus"> + <property name="text"> + <string>+</string> + </property> + </widget> + </item> + <item row="3" column="4"> + <widget class="QPushButton" name="buttonMinus"> + <property name="text"> + <string>-</string> + </property> + </widget> + </item> + <item row="4" column="3"> + <widget class="QPushButton" name="buttonStar"> + <property name="text"> + <string>*</string> + </property> + </widget> + </item> + <item row="4" column="4"> + <widget class="QPushButton" name="buttonDiv"> + <property name="text"> + <string>/</string> + </property> + </widget> + </item> + <item row="5" column="3" colspan="2"> + <widget class="QPushButton" name="buttonEq"> + <property name="text"> + <string>=</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QPushButton" name="button8"> + <property name="text"> + <string>8</string> + </property> + </widget> + </item> + <item row="1" column="3"> + <widget class="QPushButton" name="buttonC"> + <property name="styleSheet"> + <string notr="true">color: red; font-weight:bold</string> + </property> + <property name="text"> + <string>C</string> + </property> + </widget> + </item> + </layout> + </item> + </layout> + </widget> + <layoutdefault spacing="6" margin="11"/> + <resources/> + <connections/> +</ui> diff --git a/examples/calc/main.cpp b/examples/calc/main.cpp new file mode 100644 index 0000000..9d30c6b --- /dev/null +++ b/examples/calc/main.cpp @@ -0,0 +1,15 @@ + +#include <QtGui/QApplication> +#include "calc.h" +#include "qscriptedstatemachine.h" +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + QtScriptedStateMachine *sm = QtScriptedStateMachine::load(":/calc.scxml"); + CalcWidget w; + sm->registerObject(&w,"",true); + QObject::connect (&w, SIGNAL(command(QString)), sm, SLOT(postNamedEvent(QString))); + w.show(); + sm->start(); + return a.exec(); +} |