diff options
Diffstat (limited to 'scc/examples/loginmvc')
-rw-r--r-- | scc/examples/loginmvc/controller.scxml | 105 | ||||
-rw-r--r-- | scc/examples/loginmvc/frame.ui | 123 | ||||
-rw-r--r-- | scc/examples/loginmvc/loginmvc.pro | 8 | ||||
-rw-r--r-- | scc/examples/loginmvc/main.cpp | 192 |
4 files changed, 428 insertions, 0 deletions
diff --git a/scc/examples/loginmvc/controller.scxml b/scc/examples/loginmvc/controller.scxml new file mode 100644 index 0000000..8bffc1a --- /dev/null +++ b/scc/examples/loginmvc/controller.scxml @@ -0,0 +1,105 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<scxml xmlns="http://www.w3.org/2005/07/scxml" + xmlns:Qt="http://www.qtsoftware.com scxml-ext" + initial="root"> + <datamodel> + <data id="username" Qt:type="QString" /> + <data id="password" Qt:type="QString" /> + <data id="loginTimeout" Qt:type="int" expr="10000" /> + <data id="firstTime" Qt:type="bool" expr="true" /> + <data id="loginButton" Qt:type="QObject*" expr="NULL" /> + <data id="logoutButton" Qt:type="QObject*" expr="NULL" /> + <data id="cancelButton" Qt:type="QObject*" expr="NULL" /> + </datamodel> + <parallel id="root"> + <state id="business_logic"> + <initial> + <transition target="idle"> + <if cond="_data.firstTime"> + <send event="notify_welcome()" /> + <if cond="!_data.username.isEmpty()"> + <send event="notify_hello(QString)"> + <param expr="_data.username" /> + </send> + </if> + <assign dataid="firstTime" expr="false" /> + </if> + </transition> + </initial> + <state id="idle"> + <onentry><log level="0" label="Info" expr=""Now in Idle"" /><raise event="in_idle" /></onentry> + <onexit><log level="0" label="Info" expr=""Now not in Idle"" /><raise event="not_in_idle" /></onexit> + <transition target="trying_to_login" event="intent_login()" /> + <Qt:property object="_data.cancelButton" property="enabled" value="false" /> + </state> + <state id="trying_to_login"> + <Qt:property object="_data.cancelButton" property="enabled" value="true" /> + <transition event="cancel_login()" target="idle" /> + <transition event="login_timeout()" target="loginTimeout" /> + <onentry> + <log level="0" label="Info" expr=""Trying to log in"" /> + <send event="login_action(QString,QString)"> + <param expr="_data.username" /> + <param expr="_data.password" /> + </send> + <send id="cancel_login_timeout" event="login_timeout()" delay="_data.loginTimeout" /> + </onentry> + <onexit> + <cancel id="cancel_login_timeout" /> + </onexit> + <transition event="login_complete(bool)" cond="_event.data[0].toBool()" target="loggedIn" /> + <transition event="login_complete(bool)" cond="!_event.data[0].toBool()" target="loginError" /> + </state> + <state id="error" initial="loginError"> + <Qt:property object="_data.cancelButton" property="enabled" value="false" /> + <transition event="intent_continue()" target="idle" /> + <onentry> + <raise event="error.login()" /> + </onentry> + <state id="cancelled"> + <onentry> + <send event="notify_cancel()" /> + </onentry> + </state> + <state id="loginError"> + <onentry> + <log label="Error" level="1" expr="_data.username + ' ' + _data.password" /> + <send event="notify_error()" /> + </onentry> + </state> + <state id="loginTimeout"> + <onentry> + <log label="Error" level="1" expr="_data.username + ' ' + _data.password" /> + <send event="notify_timeout()" /> + </onentry> + </state> + </state> + <state id="loggedIn"> + <onentry> + <send event="notify_loggedIn()" /> + </onentry> + <transition event="intent_continue()" target="active" /> + </state> + <state id="active"> + <transition event="intent_logout()" target="idle" /> + <Qt:property object="_data.cancelButton" property="enabled" value="false" /> + </state> + <final id="exit" /> + </state> + <state id="gui" initial="hist"> + <history id="hist" type="deep"> + <transition target="gui_idle" /> + </history> + <state id="gui_idle"> + <Qt:property object="_data.loginButton" property="enabled" value="true" /> + <Qt:property object="_data.logoutButton" property="enabled" value="false" /> + <transition target="gui_active" cond="!In(idle)" /> + </state> + <state id="gui_active"> + <transition target="gui_idle" cond="In(idle)" /> + <Qt:property object="_data.loginButton" property="enabled" value="false" /> + <Qt:property object="_data.logoutButton" property="enabled" value="true" /> + </state> + </state> + </parallel> +</scxml> diff --git a/scc/examples/loginmvc/frame.ui b/scc/examples/loginmvc/frame.ui new file mode 100644 index 0000000..7f984de --- /dev/null +++ b/scc/examples/loginmvc/frame.ui @@ -0,0 +1,123 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>Frame</class> + <widget class="QFrame" name="Frame"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>337</width> + <height>158</height> + </rect> + </property> + <property name="windowTitle"> + <string>Frame</string> + </property> + <property name="frameShape"> + <enum>QFrame::StyledPanel</enum> + </property> + <property name="frameShadow"> + <enum>QFrame::Raised</enum> + </property> + <layout class="QFormLayout" name="formLayout"> + <property name="fieldGrowthPolicy"> + <enum>QFormLayout::AllNonFixedFieldsGrow</enum> + </property> + <item row="0" column="0"> + <widget class="QLabel" name="label"> + <property name="text"> + <string>Username</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QLineEdit" name="usernameEdit"/> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="label_2"> + <property name="text"> + <string>Password</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QLineEdit" name="passwordEdit"> + <property name="echoMode"> + <enum>QLineEdit::Password</enum> + </property> + </widget> + </item> + <item row="2" column="0"> + <widget class="QLabel" name="label_3"> + <property name="text"> + <string>Timeout</string> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QSlider" name="timeoutSlider"> + <property name="maximum"> + <number>20000</number> + </property> + <property name="singleStep"> + <number>500</number> + </property> + <property name="pageStep"> + <number>2000</number> + </property> + <property name="value"> + <number>10000</number> + </property> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + </widget> + </item> + <item row="4" column="1"> + <widget class="QWidget" name="widget" native="true"> + <layout class="QHBoxLayout" name="horizontalLayout_2"> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QPushButton" name="loginButton"> + <property name="text"> + <string>Log In</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="logoutButton"> + <property name="text"> + <string>Logout</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="cancelButton"> + <property name="text"> + <string>Cancel</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item row="5" column="1"> + <layout class="QHBoxLayout" name="horizontalLayout"/> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/scc/examples/loginmvc/loginmvc.pro b/scc/examples/loginmvc/loginmvc.pro new file mode 100644 index 0000000..9af965a --- /dev/null +++ b/scc/examples/loginmvc/loginmvc.pro @@ -0,0 +1,8 @@ +TEMPLATE = app +CONFIG += console scc +SOURCES = main.cpp +FORMS += frame.ui +QT += network +STATECHARTS += controller.scxml +OTHER_FILES += controller.scxml +CONFIG -= app_bundle diff --git a/scc/examples/loginmvc/main.cpp b/scc/examples/loginmvc/main.cpp new file mode 100644 index 0000000..c01a3be --- /dev/null +++ b/scc/examples/loginmvc/main.cpp @@ -0,0 +1,192 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://www.qtsoftware.com/contact. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include "sc_controller.h" +#include <QLabel> +#include <QMessageBox> +#include "ui_frame.h" +#include <QNetworkAccessManager> +#include <QNetworkReply> +#include <QSslSocket> +class AbstractModel : public QObject +{ + Q_OBJECT + public: + AbstractModel(QObject* o = NULL) : QObject(o) + { + } + public slots: + virtual void login(const QString & u, const QString & p) = 0; + signals: + void loginComplete(bool); +}; + +class DummyModel : public AbstractModel +{ + Q_OBJECT + public: + DummyModel(QObject* o = NULL) : AbstractModel(o) + { + } + public slots: + void test() + { + emit loginComplete(user == "user" && password == "password"); + } + virtual void login(const QString & u, const QString & p) + { + user = u; password = p; + QTimer::singleShot(5000,this,SLOT(test())); + } + signals: + void loginComplete(bool); + private: + QString user,password; + QNetworkAccessManager netAccess; +}; +class GMailModel : public AbstractModel +{ + Q_OBJECT + public: + GMailModel(QObject* o = NULL) : AbstractModel(o) + { + } + public slots: + void loginFinished() + { + QNetworkReply* rep = qobject_cast<QNetworkReply*>(sender()); + rep->deleteLater(); + qDebug() << rep->error() << rep->errorString(); + emit loginComplete(rep->error() == QNetworkReply::NoError); + } + virtual void login(const QString & u, const QString & p) + { + QNetworkRequest req; + QUrl url("https://mail.google.com/mail/feed/atom"); + url.setUserName(u); + url.setPassword(p); + req.setUrl(url); + QNetworkReply* reply = netAccess.get(req); + reply->ignoreSslErrors(); + connect(reply,SIGNAL(finished()),this,SLOT(loginFinished())); + } + private: + QNetworkAccessManager netAccess; +}; + +class MyView : public QFrame, public virtual Ui::Frame +{ + Q_OBJECT + public: + MyView(QWidget* o = NULL) : QFrame(o) + { + setupUi(this); + connect(loginButton,SIGNAL(clicked()),this,SIGNAL(loginIntent())); + connect(logoutButton,SIGNAL(clicked()),this,SIGNAL(logoutIntent())); + connect(cancelButton,SIGNAL(clicked()),this,SIGNAL(cancelIntent())); + } + + public slots: + + void notifyLogin() + { + QMessageBox::information(this,"Logged In","You are now logged in!"); + emit contIntent(); + } + void notifyTimeout() + { + QMessageBox::information(this,"Timeout...","Sorry, login has timed out"); + emit contIntent(); + } + void notifyWelcome() + { + QMessageBox::information(this,"","Welcome!"); + } + void notifyError() + { + qDebug() << "notifyError"; + QMessageBox::warning(this,"Not Logged in","Login has failed..."); + emit contIntent(); + } + + void notifyHello(const QString & name) + { + QMessageBox::information(this,"Welcome",QString("Hello ") + name); + } + + signals: + void loginIntent(); + void logoutIntent(); + void cancelIntent(); + void contIntent(); +}; +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + AbstractModel* model = QSslSocket::supportsSsl () ? (AbstractModel*)new GMailModel() : (AbstractModel*)new DummyModel(); + MyView* view = new MyView(); + SMClass_controller *controller = new SMClass_controller(); + controller->setupStateMachine(); + controller->set_loginButton(view->loginButton); + controller->set_logoutButton(view->logoutButton); + controller->set_cancelButton(view->cancelButton); + QObject::connect(controller,SIGNAL(event_login_action(QString,QString)),model,SLOT(login(QString,QString))); + QObject::connect(controller,SIGNAL(event_notify_loggedIn()),view,SLOT(notifyLogin())); + QObject::connect(controller,SIGNAL(event_notify_error()),view,SLOT(notifyError())); + QObject::connect(controller,SIGNAL(event_notify_hello(QString)),view,SLOT(notifyHello(QString))); + QObject::connect(controller,SIGNAL(event_notify_welcome()),view,SLOT(notifyWelcome())); + QObject::connect(controller,SIGNAL(event_notify_timeout()),view,SLOT(notifyTimeout())); + QObject::connect(model,SIGNAL(loginComplete(bool)),controller,SIGNAL(event_login_complete(bool))); + QObject::connect(view,SIGNAL(loginIntent()),controller,SIGNAL(event_intent_login())); + QObject::connect(view,SIGNAL(cancelIntent()),controller,SIGNAL(event_cancel_login())); + QObject::connect(view,SIGNAL(logoutIntent()),controller,SIGNAL(event_intent_logout())); + QObject::connect(view,SIGNAL(contIntent()),controller,SIGNAL(event_intent_continue())); + QObject::connect(view->usernameEdit,SIGNAL(textChanged(QString)),controller,SLOT(set_username(QString))); + QObject::connect(view->passwordEdit,SIGNAL(textChanged(QString)),controller,SLOT(set_password(QString))); + QObject::connect(view->timeoutSlider,SIGNAL(valueChanged(int)),controller,SLOT(set_loginTimeout(int))); + controller->start(); + view->show(); + QObject::connect(view,SIGNAL(closed()),qApp,SLOT(quit())); + return a.exec(); +} +#include <main.moc> |