diff options
author | Martin Jones <martin.jones@nokia.com> | 2009-05-20 04:51:29 (GMT) |
---|---|---|
committer | Martin Jones <martin.jones@nokia.com> | 2009-05-20 04:51:29 (GMT) |
commit | e484ee27f929aca738a269947237d834dab89b39 (patch) | |
tree | 216f95598411eb4c72141308973695d5e0d8b372 /examples/statemachine | |
parent | ded29009f766a8373193d94bcb8309270f66a266 (diff) | |
parent | d1f1f2c91f63609ca1ffad32efdd6590f9f0ce6a (diff) | |
download | Qt-e484ee27f929aca738a269947237d834dab89b39.zip Qt-e484ee27f929aca738a269947237d834dab89b39.tar.gz Qt-e484ee27f929aca738a269947237d834dab89b39.tar.bz2 |
Merge branch 'kinetic-declarativeui' of git@scm.dev.nokia.troll.no:qt/kinetic into kinetic-declarativeui
Diffstat (limited to 'examples/statemachine')
-rw-r--r-- | examples/statemachine/clockticking/clockticking.pro | 10 | ||||
-rw-r--r-- | examples/statemachine/clockticking/main.cpp | 123 | ||||
-rw-r--r-- | examples/statemachine/composition/composition.pro | 7 | ||||
-rw-r--r-- | examples/statemachine/composition/main.cpp | 104 | ||||
-rw-r--r-- | examples/statemachine/errorstateplugins/errorstateplugins.pro | 11 | ||||
-rw-r--r-- | examples/statemachine/eventtransitions/main.cpp | 16 | ||||
-rw-r--r-- | examples/statemachine/factorial/main.cpp | 64 | ||||
-rw-r--r-- | examples/statemachine/helloworld/helloworld.pro | 10 | ||||
-rw-r--r-- | examples/statemachine/helloworld/main.cpp | 78 | ||||
-rw-r--r-- | examples/statemachine/pauseandresume/main.cpp | 102 | ||||
-rw-r--r-- | examples/statemachine/pauseandresume/pauseandresume.pro | 7 | ||||
-rw-r--r-- | examples/statemachine/pingpong/main.cpp | 18 | ||||
-rw-r--r-- | examples/statemachine/statemachine.pro | 8 | ||||
-rw-r--r-- | examples/statemachine/tankgame/gameitem.cpp (renamed from examples/statemachine/errorstate/gameitem.cpp) | 0 | ||||
-rw-r--r-- | examples/statemachine/tankgame/gameitem.h (renamed from examples/statemachine/errorstate/gameitem.h) | 0 | ||||
-rw-r--r-- | examples/statemachine/tankgame/gameovertransition.cpp | 39 | ||||
-rw-r--r-- | examples/statemachine/tankgame/gameovertransition.h | 22 | ||||
-rw-r--r-- | examples/statemachine/tankgame/main.cpp (renamed from examples/statemachine/errorstate/main.cpp) | 0 | ||||
-rw-r--r-- | examples/statemachine/tankgame/mainwindow.cpp (renamed from examples/statemachine/errorstate/mainwindow.cpp) | 138 | ||||
-rw-r--r-- | examples/statemachine/tankgame/mainwindow.h (renamed from examples/statemachine/errorstate/mainwindow.h) | 3 | ||||
-rw-r--r-- | examples/statemachine/tankgame/plugin.h (renamed from examples/statemachine/errorstate/plugin.h) | 0 | ||||
-rw-r--r-- | examples/statemachine/tankgame/rocketitem.cpp (renamed from examples/statemachine/errorstate/rocketitem.cpp) | 0 | ||||
-rw-r--r-- | examples/statemachine/tankgame/rocketitem.h (renamed from examples/statemachine/errorstate/rocketitem.h) | 0 | ||||
-rw-r--r-- | examples/statemachine/tankgame/tankgame.pro (renamed from examples/statemachine/errorstate/errorstate.pro) | 6 | ||||
-rw-r--r-- | examples/statemachine/tankgame/tankitem.cpp (renamed from examples/statemachine/errorstate/tankitem.cpp) | 1 | ||||
-rw-r--r-- | examples/statemachine/tankgame/tankitem.h (renamed from examples/statemachine/errorstate/tankitem.h) | 3 | ||||
-rw-r--r-- | examples/statemachine/tankgameplugins/random_ai/random_ai.pro (renamed from examples/statemachine/errorstateplugins/random_ai/random_ai.pro) | 6 | ||||
-rw-r--r-- | examples/statemachine/tankgameplugins/random_ai/random_ai_plugin.cpp (renamed from examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.cpp) | 0 | ||||
-rw-r--r-- | examples/statemachine/tankgameplugins/random_ai/random_ai_plugin.h (renamed from examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.h) | 4 | ||||
-rw-r--r-- | examples/statemachine/tankgameplugins/seek_ai/seek_ai.cpp (renamed from examples/statemachine/errorstateplugins/seek_ai/seek_ai.cpp) | 0 | ||||
-rw-r--r-- | examples/statemachine/tankgameplugins/seek_ai/seek_ai.h (renamed from examples/statemachine/errorstateplugins/seek_ai/seek_ai.h) | 16 | ||||
-rw-r--r-- | examples/statemachine/tankgameplugins/seek_ai/seek_ai.pro (renamed from examples/statemachine/errorstateplugins/seek_ai/seek_ai.pro) | 6 | ||||
-rw-r--r-- | examples/statemachine/tankgameplugins/spin_ai/spin_ai.cpp (renamed from examples/statemachine/errorstateplugins/spin_ai/spin_ai.cpp) | 0 | ||||
-rw-r--r-- | examples/statemachine/tankgameplugins/spin_ai/spin_ai.h (renamed from examples/statemachine/errorstateplugins/spin_ai/spin_ai.h) | 11 | ||||
-rw-r--r-- | examples/statemachine/tankgameplugins/spin_ai/spin_ai.pro (renamed from examples/statemachine/errorstateplugins/spin_ai/spin_ai.pro) | 6 | ||||
-rw-r--r-- | examples/statemachine/tankgameplugins/spin_ai_with_error/spin_ai_with_error.cpp (renamed from examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.cpp) | 0 | ||||
-rw-r--r-- | examples/statemachine/tankgameplugins/spin_ai_with_error/spin_ai_with_error.h (renamed from examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.h) | 11 | ||||
-rw-r--r-- | examples/statemachine/tankgameplugins/spin_ai_with_error/spin_ai_with_error.pro (renamed from examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.pro) | 6 | ||||
-rw-r--r-- | examples/statemachine/tankgameplugins/tankgameplugins.pro | 11 | ||||
-rw-r--r-- | examples/statemachine/trafficlight/main.cpp | 47 | ||||
-rw-r--r-- | examples/statemachine/twowaybutton/main.cpp | 22 |
41 files changed, 346 insertions, 570 deletions
diff --git a/examples/statemachine/clockticking/clockticking.pro b/examples/statemachine/clockticking/clockticking.pro deleted file mode 100644 index bff9cb8..0000000 --- a/examples/statemachine/clockticking/clockticking.pro +++ /dev/null @@ -1,10 +0,0 @@ -QT = core -TEMPLATE = app -TARGET = -win32: CONFIG += console -mac:CONFIG -= app_bundle -DEPENDPATH += . -INCLUDEPATH += . - -# Input -SOURCES += main.cpp diff --git a/examples/statemachine/clockticking/main.cpp b/examples/statemachine/clockticking/main.cpp deleted file mode 100644 index ea8e692..0000000 --- a/examples/statemachine/clockticking/main.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** Contact: Qt Software Information (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 qt-sales@nokia.com. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QtCore> -#include <stdio.h> -#ifdef QT_STATEMACHINE_SOLUTION -#include <qstatemachine.h> -#include <qstate.h> -#include <qabstracttransition.h> -#endif - -class ClockEvent : public QEvent -{ -public: - ClockEvent() : QEvent(QEvent::Type(QEvent::User+2)) - {} -}; - -class ClockState : public QState -{ -public: - ClockState(QState *parent) - : QState(parent) {} - -protected: - virtual void onEntry(QEvent *) - { - fprintf(stdout, "ClockState entered; posting the initial tick\n"); - machine()->postEvent(new ClockEvent()); - } -}; - -class ClockTransition : public QAbstractTransition -{ -public: - ClockTransition() {} - -protected: - virtual bool eventTest(QEvent *e) const { - return (e->type() == QEvent::User+2); - } - virtual void onTransition(QEvent *) - { - fprintf(stdout, "ClockTransition triggered; posting another tick with a delay of 1 second\n"); - machine()->postEvent(new ClockEvent(), 1000); - } -}; - -class ClockListener : public QAbstractTransition -{ -public: - ClockListener() {} - -protected: - virtual bool eventTest(QEvent *e) const { - return (e->type() == QEvent::User+2); - } - virtual void onTransition(QEvent *) - { - fprintf(stdout, "ClockListener heard a tick!\n"); - } -}; - -int main(int argc, char **argv) -{ - QCoreApplication app(argc, argv); - - QStateMachine machine; - QState *group = new QState(QState::ParallelStates); - group->setObjectName("group"); - - ClockState *clock = new ClockState(group); - clock->setObjectName("clock"); - clock->addTransition(new ClockTransition()); - - QState *listener = new QState(group); - listener->setObjectName("listener"); - listener->addTransition(new ClockListener()); - - machine.addState(group); - machine.setInitialState(group); - machine.start(); - - return app.exec(); -} diff --git a/examples/statemachine/composition/composition.pro b/examples/statemachine/composition/composition.pro deleted file mode 100644 index 6a976cb..0000000 --- a/examples/statemachine/composition/composition.pro +++ /dev/null @@ -1,7 +0,0 @@ -TEMPLATE = app -TARGET = -DEPENDPATH += . -INCLUDEPATH += . - -# Input -SOURCES += main.cpp diff --git a/examples/statemachine/composition/main.cpp b/examples/statemachine/composition/main.cpp deleted file mode 100644 index 0afff66..0000000 --- a/examples/statemachine/composition/main.cpp +++ /dev/null @@ -1,104 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** Contact: Qt Software Information (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 qt-sales@nokia.com. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QtGui> -#ifdef QT_STATEMACHINE_SOLUTION -#include <qstatemachine.h> -#include <qstate.h> -#include <qfinalstate.h> -#endif - -int main(int argc, char **argv) -{ - QApplication app(argc, argv); - QLabel label; - label.setAlignment(Qt::AlignCenter); - - QStateMachine machine; - - QState *s1 = new QState(); - s1->setObjectName("s1"); - s1->assignProperty(&label, "text", "In S1, hang on..."); - s1->assignProperty(&label, "geometry", QRect(100, 100, 200, 100)); - - QState *s1_timer = new QState(s1); - s1_timer->setObjectName("s1_timer"); - QTimer t1; - t1.setInterval(2000); - QObject::connect(s1_timer, SIGNAL(entered()), &t1, SLOT(start())); - QFinalState *s1_done = new QFinalState(s1); - s1_done->setObjectName("s1_done"); - s1_timer->addTransition(&t1, SIGNAL(timeout()), s1_done); - s1->setInitialState(s1_timer); - - QState *s2 = new QState(); - s2->setObjectName("s2"); - s2->assignProperty(&label, "text", "In S2, I'm gonna quit on you..."); - s2->assignProperty(&label, "geometry", QRect(300, 300, 300, 100)); -// s2->invokeMethodOnEntry(&label, "setNum", QList<QVariant>() << 123); -// s2->invokeMethodOnEntry(&label, "showMaximized"); - - QState *s2_timer = new QState(s2); - s2_timer->setObjectName("s2_timer"); - QTimer t2; - t2.setInterval(2000); - QObject::connect(s2_timer, SIGNAL(entered()), &t2, SLOT(start())); - QFinalState *s2_done = new QFinalState(s2); - s2_done->setObjectName("s2_done"); - s2_timer->addTransition(&t2, SIGNAL(timeout()), s2_done); - s2->setInitialState(s2_timer); - - s1->addTransition(s1, SIGNAL(finished()), s2); - - QFinalState *s3 = new QFinalState(); - s3->setObjectName("s3"); - s2->addTransition(s2, SIGNAL(finished()), s3); - - machine.addState(s1); - machine.addState(s2); - machine.addState(s3); - machine.setInitialState(s1); - QObject::connect(&machine, SIGNAL(finished()), &app, SLOT(quit())); - machine.start(); - - label.show(); - return app.exec(); -} diff --git a/examples/statemachine/errorstateplugins/errorstateplugins.pro b/examples/statemachine/errorstateplugins/errorstateplugins.pro deleted file mode 100644 index 5b6b758..0000000 --- a/examples/statemachine/errorstateplugins/errorstateplugins.pro +++ /dev/null @@ -1,11 +0,0 @@ -TEMPLATE = subdirs -SUBDIRS = random_ai \ - spin_ai_with_error \ - spin_ai \ - seek_ai - -# install -target.path = $$[QT_INSTALL_EXAMPLES]/statemachine/errorstateplugins -sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS errorstateplugins.pro -sources.path = $$[QT_INSTALL_EXAMPLES]/statemachine/errorstateplugins -INSTALLS += target sources diff --git a/examples/statemachine/eventtransitions/main.cpp b/examples/statemachine/eventtransitions/main.cpp index f564b7e..aba0c73 100644 --- a/examples/statemachine/eventtransitions/main.cpp +++ b/examples/statemachine/eventtransitions/main.cpp @@ -46,6 +46,7 @@ #include <qeventtransition.h> #endif +//! [0] class Window : public QWidget { public: @@ -54,7 +55,9 @@ public: { QPushButton *button = new QPushButton(this); button->setGeometry(QRect(100, 100, 100, 100)); +//! [0] +//! [1] QStateMachine *machine = new QStateMachine(this); QState *s1 = new QState(); @@ -62,15 +65,21 @@ public: QState *s2 = new QState(); s2->assignProperty(button, "text", "Inside"); +//! [1] +//! [2] QEventTransition *enterTransition = new QEventTransition(button, QEvent::Enter); enterTransition->setTargetState(s2); s1->addTransition(enterTransition); +//! [2] +//! [3] QEventTransition *leaveTransition = new QEventTransition(button, QEvent::Leave); leaveTransition->setTargetState(s1); s2->addTransition(leaveTransition); +//! [3] +//! [4] QState *s3 = new QState(); s3->assignProperty(button, "text", "Pressing..."); @@ -81,16 +90,20 @@ public: QEventTransition *releaseTransition = new QEventTransition(button, QEvent::MouseButtonRelease); releaseTransition->setTargetState(s2); s3->addTransition(releaseTransition); +//! [4] +//! [5] machine->addState(s1); machine->addState(s2); machine->addState(s3); + machine->setInitialState(s1); - QObject::connect(machine, SIGNAL(finished()), qApp, SLOT(quit())); machine->start(); } }; +//! [5] +//! [6] int main(int argc, char **argv) { QApplication app(argc, argv); @@ -100,3 +113,4 @@ int main(int argc, char **argv) return app.exec(); } +//! [6] diff --git a/examples/statemachine/factorial/main.cpp b/examples/statemachine/factorial/main.cpp index 2b63690..1065eb8 100644 --- a/examples/statemachine/factorial/main.cpp +++ b/examples/statemachine/factorial/main.cpp @@ -48,6 +48,7 @@ #include <qfinalstate.h> #endif +//! [0] class Factorial : public QObject { Q_OBJECT @@ -55,10 +56,8 @@ class Factorial : public QObject Q_PROPERTY(int fac READ fac WRITE setFac) public: Factorial(QObject *parent = 0) - : QObject(parent) + : QObject(parent), m_x(-1), m_fac(1) { - m_fac = 1; - m_x = -1; } int x() const @@ -71,7 +70,7 @@ public: if (x == m_x) return; m_x = x; - emit xChanged(); + emit xChanged(x); } int fac() const @@ -85,28 +84,34 @@ public: } Q_SIGNALS: - void xChanged(); + void xChanged(int value); private: int m_x; int m_fac; }; +//! [0] +//! [1] class FactorialLoopTransition : public QSignalTransition { public: FactorialLoopTransition(Factorial *fact) - : QSignalTransition(fact, SIGNAL(xChanged())), m_fact(fact) + : QSignalTransition(fact, SIGNAL(xChanged(int))), m_fact(fact) {} - virtual bool eventTest(QEvent *) const + virtual bool eventTest(QEvent *e) { - return m_fact->property("x").toInt() > 1; + if (!QSignalTransition::eventTest(e)) + return false; + QSignalEvent *se = static_cast<QSignalEvent*>(e); + return se->arguments().at(0).toInt() > 1; } - virtual void onTransition(QEvent *) + virtual void onTransition(QEvent *e) { - int x = m_fact->property("x").toInt(); + QSignalEvent *se = static_cast<QSignalEvent*>(e); + int x = se->arguments().at(0).toInt(); int fac = m_fact->property("fac").toInt(); m_fact->setProperty("fac", x * fac); m_fact->setProperty("x", x - 1); @@ -115,17 +120,22 @@ public: private: Factorial *m_fact; }; +//! [1] +//! [2] class FactorialDoneTransition : public QSignalTransition { public: FactorialDoneTransition(Factorial *fact) - : QSignalTransition(fact, SIGNAL(xChanged())), m_fact(fact) + : QSignalTransition(fact, SIGNAL(xChanged(int))), m_fact(fact) {} - virtual bool eventTest(QEvent *) const + virtual bool eventTest(QEvent *e) { - return m_fact->property("x").toInt() <= 1; + if (!QSignalTransition::eventTest(e)) + return false; + QSignalEvent *se = static_cast<QSignalEvent*>(e); + return se->arguments().at(0).toInt() <= 1; } virtual void onTransition(QEvent *) @@ -136,35 +146,37 @@ public: private: Factorial *m_fact; }; +//! [2] +//! [3] int main(int argc, char **argv) { QCoreApplication app(argc, argv); - Factorial factorial; - QStateMachine machine; +//! [3] - QState *computing = new QState(machine.rootState()); - computing->addTransition(new FactorialLoopTransition(&factorial)); +//! [4] + QState *compute = new QState(machine.rootState()); + compute->assignProperty(&factorial, "fac", 1); + compute->assignProperty(&factorial, "x", 6); + compute->addTransition(new FactorialLoopTransition(&factorial)); +//! [4] +//! [5] QFinalState *done = new QFinalState(machine.rootState()); FactorialDoneTransition *doneTransition = new FactorialDoneTransition(&factorial); doneTransition->setTargetState(done); - computing->addTransition(doneTransition); - - QState *initialize = new QState(machine.rootState()); - initialize->assignProperty(&factorial, "x", 6); - FactorialLoopTransition *enterLoopTransition = new FactorialLoopTransition(&factorial); - enterLoopTransition->setTargetState(computing); - initialize->addTransition(enterLoopTransition); + compute->addTransition(doneTransition); +//! [5] +//! [6] + machine.setInitialState(compute); QObject::connect(&machine, SIGNAL(finished()), &app, SLOT(quit())); - - machine.setInitialState(initialize); machine.start(); return app.exec(); } +//! [6] #include "main.moc" diff --git a/examples/statemachine/helloworld/helloworld.pro b/examples/statemachine/helloworld/helloworld.pro deleted file mode 100644 index ac79117..0000000 --- a/examples/statemachine/helloworld/helloworld.pro +++ /dev/null @@ -1,10 +0,0 @@ -TEMPLATE = app -TARGET = -QT = core -win32: CONFIG += console -mac:CONFIG -= app_bundle -DEPENDPATH += . -INCLUDEPATH += . - -# Input -SOURCES += main.cpp diff --git a/examples/statemachine/helloworld/main.cpp b/examples/statemachine/helloworld/main.cpp deleted file mode 100644 index fbe34b5..0000000 --- a/examples/statemachine/helloworld/main.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** Contact: Qt Software Information (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 qt-sales@nokia.com. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QtCore> -#ifdef QT_STATEMACHINE_SOLUTION -#include <qstatemachine.h> -#include <qstate.h> -#include <qfinalstate.h> -#endif - -class S0 : public QState -{ -public: - S0(QState *parent = 0) - : QState(parent) {} - - virtual void onEntry(QEvent *) - { - fprintf(stdout, "Hello world!\n"); - } -}; - -int main(int argc, char **argv) -{ - QCoreApplication app(argc, argv); - - QStateMachine machine; - QState *s0 = new S0(); - QFinalState *s1 = new QFinalState(); - s0->addTransition(s1); - - machine.addState(s0); - machine.addState(s1); - machine.setInitialState(s0); - - QObject::connect(&machine, SIGNAL(finished()), QCoreApplication::instance(), SLOT(quit())); - machine.start(); - - return app.exec(); -} diff --git a/examples/statemachine/pauseandresume/main.cpp b/examples/statemachine/pauseandresume/main.cpp deleted file mode 100644 index 5bacb41..0000000 --- a/examples/statemachine/pauseandresume/main.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** Contact: Qt Software Information (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 qt-sales@nokia.com. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QtGui> -#ifdef QT_STATEMACHINE_SOLUTION -#include <qstatemachine.h> -#include <qstate.h> -#include <qfinalstate.h> -#include <qhistorystate.h> -#endif - -class Window : public QWidget -{ -public: - Window(QWidget *parent = 0) - : QWidget(parent) - { - QPushButton *pb = new QPushButton("Go"); - QPushButton *pauseButton = new QPushButton("Pause"); - QPushButton *quitButton = new QPushButton("Quit"); - QVBoxLayout *vbox = new QVBoxLayout(this); - vbox->addWidget(pb); - vbox->addWidget(pauseButton); - vbox->addWidget(quitButton); - - QStateMachine *machine = new QStateMachine(this); - - QState *process = new QState(machine->rootState()); - process->setObjectName("process"); - - QState *s1 = new QState(process); - s1->setObjectName("s1"); - QState *s2 = new QState(process); - s2->setObjectName("s2"); - s1->addTransition(pb, SIGNAL(clicked()), s2); - s2->addTransition(pb, SIGNAL(clicked()), s1); - - QHistoryState *h = new QHistoryState(process); - h->setDefaultState(s1); - - QState *interrupted = new QState(machine->rootState()); - interrupted->setObjectName("interrupted"); - QFinalState *terminated = new QFinalState(machine->rootState()); - terminated->setObjectName("terminated"); - interrupted->addTransition(pauseButton, SIGNAL(clicked()), h); - interrupted->addTransition(quitButton, SIGNAL(clicked()), terminated); - - process->addTransition(pauseButton, SIGNAL(clicked()), interrupted); - process->addTransition(quitButton, SIGNAL(clicked()), terminated); - - process->setInitialState(s1); - machine->setInitialState(process); - QObject::connect(machine, SIGNAL(finished()), QApplication::instance(), SLOT(quit())); - machine->start(); - } -}; - -int main(int argc, char **argv) -{ - QApplication app(argc, argv); - Window win; - win.show(); - return app.exec(); -} diff --git a/examples/statemachine/pauseandresume/pauseandresume.pro b/examples/statemachine/pauseandresume/pauseandresume.pro deleted file mode 100644 index 6a976cb..0000000 --- a/examples/statemachine/pauseandresume/pauseandresume.pro +++ /dev/null @@ -1,7 +0,0 @@ -TEMPLATE = app -TARGET = -DEPENDPATH += . -INCLUDEPATH += . - -# Input -SOURCES += main.cpp diff --git a/examples/statemachine/pingpong/main.cpp b/examples/statemachine/pingpong/main.cpp index eb8fd5d..331627e 100644 --- a/examples/statemachine/pingpong/main.cpp +++ b/examples/statemachine/pingpong/main.cpp @@ -47,6 +47,7 @@ #include <qabstracttransition.h> #endif +//! [0] class PingEvent : public QEvent { public: @@ -60,7 +61,9 @@ public: PongEvent() : QEvent(QEvent::Type(QEvent::User+3)) {} }; +//! [0] +//! [1] class Pinger : public QState { public: @@ -74,14 +77,16 @@ protected: fprintf(stdout, "ping?\n"); } }; +//! [1] +//! [3] class PongTransition : public QAbstractTransition { public: PongTransition() {} protected: - virtual bool eventTest(QEvent *e) const { + virtual bool eventTest(QEvent *e) { return (e->type() == QEvent::User+3); } virtual void onTransition(QEvent *) @@ -90,14 +95,16 @@ protected: fprintf(stdout, "ping?\n"); } }; +//! [3] +//! [2] class PingTransition : public QAbstractTransition { public: PingTransition() {} protected: - virtual bool eventTest(QEvent *e) const { + virtual bool eventTest(QEvent *e) { return (e->type() == QEvent::User+2); } virtual void onTransition(QEvent *) @@ -106,7 +113,9 @@ protected: fprintf(stdout, "pong!\n"); } }; +//! [2] +//! [4] int main(int argc, char **argv) { QCoreApplication app(argc, argv); @@ -114,7 +123,9 @@ int main(int argc, char **argv) QStateMachine machine; QState *group = new QState(QState::ParallelStates); group->setObjectName("group"); +//! [4] +//! [5] Pinger *pinger = new Pinger(group); pinger->setObjectName("pinger"); pinger->addTransition(new PongTransition()); @@ -122,10 +133,13 @@ int main(int argc, char **argv) QState *ponger = new QState(group); ponger->setObjectName("ponger"); ponger->addTransition(new PingTransition()); +//! [5] +//! [6] machine.addState(group); machine.setInitialState(group); machine.start(); return app.exec(); } +//! [6] diff --git a/examples/statemachine/statemachine.pro b/examples/statemachine/statemachine.pro index ba32c12..5074a3c 100644 --- a/examples/statemachine/statemachine.pro +++ b/examples/statemachine/statemachine.pro @@ -1,14 +1,12 @@ TEMPLATE = subdirs SUBDIRS = \ - clockticking \ - composition \ eventtransitions \ factorial \ - helloworld \ - pauseandresume \ pingpong \ trafficlight \ - twowaybutton + twowaybutton \ + tankgame \ + tankgameplugins # install target.path = $$[QT_INSTALL_EXAMPLES]/statemachine diff --git a/examples/statemachine/errorstate/gameitem.cpp b/examples/statemachine/tankgame/gameitem.cpp index 1a2af71..1a2af71 100644 --- a/examples/statemachine/errorstate/gameitem.cpp +++ b/examples/statemachine/tankgame/gameitem.cpp diff --git a/examples/statemachine/errorstate/gameitem.h b/examples/statemachine/tankgame/gameitem.h index 43b8785..43b8785 100644 --- a/examples/statemachine/errorstate/gameitem.h +++ b/examples/statemachine/tankgame/gameitem.h diff --git a/examples/statemachine/tankgame/gameovertransition.cpp b/examples/statemachine/tankgame/gameovertransition.cpp new file mode 100644 index 0000000..cec786e --- /dev/null +++ b/examples/statemachine/tankgame/gameovertransition.cpp @@ -0,0 +1,39 @@ +#include "gameovertransition.h" +#include "tankitem.h" + +#include <QSignalEvent> +#include <QSignalMapper> + +GameOverTransition::GameOverTransition(QAbstractState *targetState) + : QSignalTransition(new QSignalMapper(), SIGNAL(mapped(QObject*))) +{ + setTargetState(targetState); + + QSignalMapper *mapper = qobject_cast<QSignalMapper *>(senderObject()); + mapper->setParent(this); +} + +void GameOverTransition::addTankItem(TankItem *tankItem) +{ + m_tankItems.append(tankItem); + + QSignalMapper *mapper = qobject_cast<QSignalMapper *>(senderObject()); + mapper->setMapping(tankItem, tankItem); + connect(tankItem, SIGNAL(aboutToBeDestroyed()), mapper, SLOT(map())); +} + +bool GameOverTransition::eventTest(QEvent *e) +{ + bool ret = QSignalTransition::eventTest(e); + + if (ret) { + QSignalEvent *signalEvent = static_cast<QSignalEvent *>(e); + QObject *sender = qvariant_cast<QObject *>(signalEvent->arguments().at(0)); + TankItem *tankItem = qobject_cast<TankItem *>(sender); + m_tankItems.removeAll(tankItem); + + return m_tankItems.size() <= 1; + } else { + return false; + } +}
\ No newline at end of file diff --git a/examples/statemachine/tankgame/gameovertransition.h b/examples/statemachine/tankgame/gameovertransition.h new file mode 100644 index 0000000..9a86b83 --- /dev/null +++ b/examples/statemachine/tankgame/gameovertransition.h @@ -0,0 +1,22 @@ +#ifndef GAMEOVERTRANSITION_H +#define GAMEOVERTRANSITION_H + +#include <QSignalTransition> + +class TankItem; +class GameOverTransition: public QSignalTransition +{ + Q_OBJECT +public: + GameOverTransition(QAbstractState *targetState); + + void addTankItem(TankItem *tankItem); + +protected: + bool eventTest(QEvent *event); + +private: + QList<TankItem *> m_tankItems; +}; + +#endif diff --git a/examples/statemachine/errorstate/main.cpp b/examples/statemachine/tankgame/main.cpp index 26fc1bb..26fc1bb 100644 --- a/examples/statemachine/errorstate/main.cpp +++ b/examples/statemachine/tankgame/main.cpp diff --git a/examples/statemachine/errorstate/mainwindow.cpp b/examples/statemachine/tankgame/mainwindow.cpp index 39b8663..fcc0325 100644 --- a/examples/statemachine/errorstate/mainwindow.cpp +++ b/examples/statemachine/tankgame/mainwindow.cpp @@ -2,6 +2,7 @@ #include "tankitem.h" #include "rocketitem.h" #include "plugin.h" +#include "gameovertransition.h" #include <QStateMachine> #include <QGraphicsView> @@ -12,6 +13,9 @@ #include <QTimer> #include <QFileDialog> #include <QPluginLoader> +#include <QApplication> +#include <QInputDialog> +#include <QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), m_scene(0), m_machine(0), m_runningState(0), m_started(false) @@ -50,7 +54,7 @@ void MainWindow::init() { TankItem *item = new TankItem(this); - item->setPos(m_scene->sceneRect().topLeft() + QPointF(15.0, 15.0)); + item->setPos(m_scene->sceneRect().topLeft() + QPointF(30.0, 30.0)); item->setDirection(45.0); item->setColor(Qt::red); @@ -60,7 +64,7 @@ void MainWindow::init() { TankItem *item = new TankItem(this); - item->setPos(m_scene->sceneRect().topRight() + QPointF(-15.0, 15.0)); + item->setPos(m_scene->sceneRect().topRight() + QPointF(-30.0, 30.0)); item->setDirection(135.0); item->setColor(Qt::green); @@ -70,7 +74,7 @@ void MainWindow::init() { TankItem *item = new TankItem(this); - item->setPos(m_scene->sceneRect().bottomRight() + QPointF(-15.0, -15.0)); + item->setPos(m_scene->sceneRect().bottomRight() + QPointF(-30.0, -30.0)); item->setDirection(225.0); item->setColor(Qt::blue); @@ -80,7 +84,7 @@ void MainWindow::init() { TankItem *item = new TankItem(this); - item->setPos(m_scene->sceneRect().bottomLeft() + QPointF(15.0, -15.0)); + item->setPos(m_scene->sceneRect().bottomLeft() + QPointF(30.0, -30.0)); item->setDirection(315.0); item->setColor(Qt::yellow); @@ -122,30 +126,45 @@ void MainWindow::init() stoppedState->assignProperty(this, "started", false); m_machine->setInitialState(stoppedState); - QState *spawnsAvailable = new QState(stoppedState); - spawnsAvailable->setObjectName("spawnsAvailable"); +//! [5] + QState *spawnsAvailable = new QState(stoppedState); spawnsAvailable->assignProperty(addTankAction, "enabled", true); - QState *noSpawnsAvailable = new QState(stoppedState); - noSpawnsAvailable->setObjectName("noSpawnsAvailable"); + QState *noSpawnsAvailable = new QState(stoppedState); noSpawnsAvailable->assignProperty(addTankAction, "enabled", false); +//! [5] + spawnsAvailable->setObjectName("spawnsAvailable"); + noSpawnsAvailable->setObjectName("noSpawnsAvailable"); spawnsAvailable->addTransition(this, SIGNAL(mapFull()), noSpawnsAvailable); - QHistoryState *hs = new QHistoryState(stoppedState); +//! [3] + QHistoryState *hs = new QHistoryState(stoppedState); hs->setDefaultState(spawnsAvailable); +//! [3] + hs->setObjectName("hs"); stoppedState->setInitialState(hs); +//! [0] m_runningState = new QState(QState::ParallelStates, m_machine->rootState()); +//! [0] m_runningState->setObjectName("runningState"); m_runningState->assignProperty(addTankAction, "enabled", false); m_runningState->assignProperty(runGameAction, "enabled", false); m_runningState->assignProperty(stopGameAction, "enabled", true); + + QState *gameOverState = new QState(m_machine->rootState()); + gameOverState->setObjectName("gameOverState"); + gameOverState->assignProperty(stopGameAction, "enabled", false); + connect(gameOverState, SIGNAL(entered()), this, SLOT(gameOver())); stoppedState->addTransition(runGameAction, SIGNAL(triggered()), m_runningState); m_runningState->addTransition(stopGameAction, SIGNAL(triggered()), stoppedState); + m_gameOverTransition = new GameOverTransition(gameOverState); + m_runningState->addTransition(m_gameOverTransition); + QTimer *timer = new QTimer(this); timer->setInterval(100); connect(timer, SIGNAL(timeout()), this, SLOT(runStep())); @@ -175,6 +194,22 @@ void MainWindow::runStep() } } +void MainWindow::gameOver() +{ + QList<QGraphicsItem *> items = m_scene->items(); + + TankItem *lastTankStanding = 0; + foreach (QGraphicsItem *item, items) { + if (GameItem *gameItem = qgraphicsitem_cast<GameItem *>(item)) { + if (lastTankStanding = qobject_cast<TankItem *>(gameItem)) + break; + } + } + + QMessageBox::information(this, "Game over!", + QString::fromLatin1("The tank played by '%1' has won!").arg(lastTankStanding->objectName())); +} + void MainWindow::addRocket() { TankItem *tankItem = qobject_cast<TankItem *>(sender()); @@ -193,25 +228,74 @@ void MainWindow::addTank() { Q_ASSERT(!m_spawns.isEmpty()); - QString fileName = QFileDialog::getOpenFileName(this, "Select plugin file", "plugins/", "*.dll"); - QPluginLoader loader(fileName); + QDir pluginsDir(qApp->applicationDirPath()); +#if defined(Q_OS_WIN) + if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release") + pluginsDir.cdUp(); +#elif defined(Q_OS_MAC) + if (pluginsDir.dirName() == "MacOS") { + pluginsDir.cdUp(); + pluginsDir.cdUp(); + pluginsDir.cdUp(); + } +#endif + + pluginsDir.cd("plugins"); + + QStringList itemNames; + QList<Plugin *> items; + foreach (QString fileName, pluginsDir.entryList(QDir::Files)) { + QPluginLoader loader(pluginsDir.absoluteFilePath(fileName)); + QObject *possiblePlugin = loader.instance(); + if (Plugin *plugin = qobject_cast<Plugin *>(possiblePlugin)) { + QString objectName = possiblePlugin->objectName(); + if (objectName.isEmpty()) + objectName = fileName; + + itemNames.append(objectName); + items.append(plugin); + } + } + + if (items.isEmpty()) { + QMessageBox::information(this, "No tank types found", "Please build the errorstateplugins directory"); + return; + } + + bool ok; +//! [1] + QString selectedName = QInputDialog::getItem(this, "Select a tank type", "Tank types", + itemNames, 0, false, &ok); +//! [1] - Plugin *plugin = qobject_cast<Plugin *>(loader.instance()); - if (plugin != 0) { - TankItem *tankItem = m_spawns.takeLast(); - m_scene->addItem(tankItem); - connect(tankItem, SIGNAL(cannonFired()), this, SLOT(addRocket())); - if (m_spawns.isEmpty()) - emit mapFull(); - - QState *region = new QState(m_runningState); - QState *pluginState = plugin->create(region, tankItem); - region->setInitialState(pluginState); - - // If the plugin has an error it is disabled - QState *errorState = new QState(region); - errorState->assignProperty(tankItem, "enabled", false); - pluginState->setErrorState(errorState); + if (ok && !selectedName.isEmpty()) { + int idx = itemNames.indexOf(selectedName); + if (Plugin *plugin = idx >= 0 ? items.at(idx) : 0) { + TankItem *tankItem = m_spawns.takeLast(); + tankItem->setObjectName(selectedName); + tankItem->setToolTip(selectedName); + m_scene->addItem(tankItem); + connect(tankItem, SIGNAL(cannonFired()), this, SLOT(addRocket())); + if (m_spawns.isEmpty()) + emit mapFull(); + + m_gameOverTransition->addTankItem(tankItem); + + QState *region = new QState(m_runningState); + region->setObjectName(QString::fromLatin1("region%1").arg(m_spawns.size())); +//! [2] + QState *pluginState = plugin->create(region, tankItem); +//! [2] + region->setInitialState(pluginState); + + // If the plugin has an error it is disabled +//! [4] + QState *errorState = new QState(region); + errorState->setObjectName(QString::fromLatin1("errorState%1").arg(m_spawns.size())); + errorState->assignProperty(tankItem, "enabled", false); + pluginState->setErrorState(errorState); +//! [4] + } } } diff --git a/examples/statemachine/errorstate/mainwindow.h b/examples/statemachine/tankgame/mainwindow.h index 622dabe..40e1595 100644 --- a/examples/statemachine/errorstate/mainwindow.h +++ b/examples/statemachine/tankgame/mainwindow.h @@ -7,6 +7,7 @@ class QGraphicsScene; class QStateMachine; class QState; +class GameOverTransition; class TankItem; class MainWindow: public QMainWindow { @@ -23,6 +24,7 @@ public slots: void addTank(); void addRocket(); void runStep(); + void gameOver(); signals: void mapFull(); @@ -35,6 +37,7 @@ private: QStateMachine *m_machine; QState *m_runningState; + GameOverTransition *m_gameOverTransition; QList<TankItem *> m_spawns; QTime m_time; diff --git a/examples/statemachine/errorstate/plugin.h b/examples/statemachine/tankgame/plugin.h index 2b48d43..2b48d43 100644 --- a/examples/statemachine/errorstate/plugin.h +++ b/examples/statemachine/tankgame/plugin.h diff --git a/examples/statemachine/errorstate/rocketitem.cpp b/examples/statemachine/tankgame/rocketitem.cpp index c324980..c324980 100644 --- a/examples/statemachine/errorstate/rocketitem.cpp +++ b/examples/statemachine/tankgame/rocketitem.cpp diff --git a/examples/statemachine/errorstate/rocketitem.h b/examples/statemachine/tankgame/rocketitem.h index 189a1dd..189a1dd 100644 --- a/examples/statemachine/errorstate/rocketitem.h +++ b/examples/statemachine/tankgame/rocketitem.h diff --git a/examples/statemachine/errorstate/errorstate.pro b/examples/statemachine/tankgame/tankgame.pro index b93a691..46cfe2e 100644 --- a/examples/statemachine/errorstate/errorstate.pro +++ b/examples/statemachine/tankgame/tankgame.pro @@ -5,9 +5,9 @@ TEMPLATE = app TARGET = DEPENDPATH += . -INCLUDEPATH += C:/dev/kinetic/examples/statemachine/errorstate/. . +INCLUDEPATH += C:/dev/kinetic/examples/statemachine/tankgame/. . # Input -HEADERS += mainwindow.h plugin.h tankitem.h rocketitem.h gameitem.h -SOURCES += main.cpp mainwindow.cpp tankitem.cpp rocketitem.cpp gameitem.cpp +HEADERS += mainwindow.h plugin.h tankitem.h rocketitem.h gameitem.h gameovertransition.h +SOURCES += main.cpp mainwindow.cpp tankitem.cpp rocketitem.cpp gameitem.cpp gameovertransition.cpp CONFIG += console diff --git a/examples/statemachine/errorstate/tankitem.cpp b/examples/statemachine/tankgame/tankitem.cpp index 5506a7e..c322d21 100644 --- a/examples/statemachine/errorstate/tankitem.cpp +++ b/examples/statemachine/tankgame/tankitem.cpp @@ -113,6 +113,7 @@ void TankItem::idle(qreal elapsed) void TankItem::hitByRocket() { + emit aboutToBeDestroyed(); deleteLater(); } diff --git a/examples/statemachine/errorstate/tankitem.h b/examples/statemachine/tankgame/tankitem.h index cefed69..9475397 100644 --- a/examples/statemachine/errorstate/tankitem.h +++ b/examples/statemachine/tankgame/tankitem.h @@ -35,11 +35,13 @@ public: qreal distanceToObstacle() const; qreal distanceToObstacle(QGraphicsItem **item) const; +//! [0] signals: void tankSpotted(qreal direction, qreal distance); void collision(const QLineF &collidedLine); void actionCompleted(); void cannonFired(); + void aboutToBeDestroyed(); public slots: void moveForwards(qreal length = 10.0); @@ -48,6 +50,7 @@ public slots: void turnTo(qreal degrees = 0.0); void stop(); void fireCannon(); +//! [0] protected: virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); diff --git a/examples/statemachine/errorstateplugins/random_ai/random_ai.pro b/examples/statemachine/tankgameplugins/random_ai/random_ai.pro index f290250..5bc0b26 100644 --- a/examples/statemachine/errorstateplugins/random_ai/random_ai.pro +++ b/examples/statemachine/tankgameplugins/random_ai/random_ai.pro @@ -4,10 +4,10 @@ INCLUDEPATH += ../.. HEADERS = random_ai_plugin.h SOURCES = random_ai_plugin.cpp TARGET = $$qtLibraryTarget(random_ai) -DESTDIR = ../../errorstate/plugins +DESTDIR = ../../tankgame/plugins #! [0] # install -target.path = $$[QT_INSTALL_EXAMPLES]/statemachine/errorstate/plugins +target.path = $$[QT_INSTALL_EXAMPLES]/statemachine/tankgame/plugins sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS random_ai.pro -sources.path = $$[QT_INSTALL_EXAMPLES]/statemachine/errorstateplugins/random_ai
\ No newline at end of file +sources.path = $$[QT_INSTALL_EXAMPLES]/statemachine/tankgameplugins/random_ai
\ No newline at end of file diff --git a/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.cpp b/examples/statemachine/tankgameplugins/random_ai/random_ai_plugin.cpp index c196247..c196247 100644 --- a/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.cpp +++ b/examples/statemachine/tankgameplugins/random_ai/random_ai_plugin.cpp diff --git a/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.h b/examples/statemachine/tankgameplugins/random_ai/random_ai_plugin.h index 3db464b..f5e3b6f 100644 --- a/examples/statemachine/errorstateplugins/random_ai/random_ai_plugin.h +++ b/examples/statemachine/tankgameplugins/random_ai/random_ai_plugin.h @@ -4,7 +4,7 @@ #include <QObject> #include <QState> -#include <errorstate/plugin.h> +#include <tankgame/plugin.h> class SelectActionState: public QState { @@ -56,6 +56,8 @@ class RandomAiPlugin: public QObject, public Plugin Q_OBJECT Q_INTERFACES(Plugin) public: + RandomAiPlugin() { setObjectName("Random"); } + virtual QState *create(QState *parentState, QObject *tank); }; diff --git a/examples/statemachine/errorstateplugins/seek_ai/seek_ai.cpp b/examples/statemachine/tankgameplugins/seek_ai/seek_ai.cpp index 2fb05d4..2fb05d4 100644 --- a/examples/statemachine/errorstateplugins/seek_ai/seek_ai.cpp +++ b/examples/statemachine/tankgameplugins/seek_ai/seek_ai.cpp diff --git a/examples/statemachine/errorstateplugins/seek_ai/seek_ai.h b/examples/statemachine/tankgameplugins/seek_ai/seek_ai.h index 34d203e..9d4aabc 100644 --- a/examples/statemachine/errorstateplugins/seek_ai/seek_ai.h +++ b/examples/statemachine/tankgameplugins/seek_ai/seek_ai.h @@ -1,7 +1,7 @@ #ifndef SEEK_AI_H #define SEEK_AI_H -#include <errorstate/plugin.h> +#include <tankgame/plugin.h> #include <QState> #include <QFinalState> @@ -81,7 +81,7 @@ public: } protected: - bool eventTest(QEvent *event) const + bool eventTest(QEvent *event) { bool b = QSignalTransition::eventTest(event); if (b) { @@ -105,7 +105,7 @@ protected: } private: - mutable QLineF m_lastLine; + QLineF m_lastLine; QObject *m_tank; QState *m_turnTo; }; @@ -137,16 +137,20 @@ public: ChaseState(QObject *tank, QState *parentState = 0) : QState(parentState), m_tank(tank) { QState *fireCannon = new QState(this); + fireCannon->setObjectName("fireCannon"); connect(fireCannon, SIGNAL(entered()), tank, SLOT(fireCannon())); setInitialState(fireCannon); - m_goToLocation = new GoToLocationState(this); + m_goToLocation = new GoToLocationState(tank, this); + m_goToLocation->setObjectName("goToLocation"); fireCannon->addTransition(tank, SIGNAL(actionCompleted()), m_goToLocation); m_turnToDirection = new QState(this); + m_turnToDirection->setObjectName("turnToDirection"); m_goToLocation->addTransition(tank, SIGNAL(actionCompleted()), m_turnToDirection); QFinalState *finalState = new QFinalState(this); + finalState->setObjectName("finalState"); m_turnToDirection->addTransition(tank, SIGNAL(actionCompleted()), finalState); } @@ -176,7 +180,7 @@ public: } protected: - bool eventTest(QEvent *event) const + bool eventTest(QEvent *event) { bool b = QSignalTransition::eventTest(event); if (b) { @@ -196,6 +200,8 @@ class SeekAi: public QObject, public Plugin Q_OBJECT Q_INTERFACES(Plugin) public: + SeekAi() { setObjectName("Seek and destroy"); } + virtual QState *create(QState *parentState, QObject *tank); }; diff --git a/examples/statemachine/errorstateplugins/seek_ai/seek_ai.pro b/examples/statemachine/tankgameplugins/seek_ai/seek_ai.pro index 11bd242..0d8bf2e 100644 --- a/examples/statemachine/errorstateplugins/seek_ai/seek_ai.pro +++ b/examples/statemachine/tankgameplugins/seek_ai/seek_ai.pro @@ -4,10 +4,10 @@ INCLUDEPATH += ../.. HEADERS = seek_ai.h SOURCES = seek_ai.cpp TARGET = $$qtLibraryTarget(seek_ai) -DESTDIR = ../../errorstate/plugins +DESTDIR = ../../tankgame/plugins #! [0] # install -target.path = $$[QT_INSTALL_EXAMPLES]/statemachine/errorstate/plugins +target.path = $$[QT_INSTALL_EXAMPLES]/statemachine/tankgame/plugins sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS seek_ai.pro -sources.path = $$[QT_INSTALL_EXAMPLES]/statemachine/errorstateplugins/seek_ai
\ No newline at end of file +sources.path = $$[QT_INSTALL_EXAMPLES]/statemachine/tankgameplugins/seek_ai
\ No newline at end of file diff --git a/examples/statemachine/errorstateplugins/spin_ai/spin_ai.cpp b/examples/statemachine/tankgameplugins/spin_ai/spin_ai.cpp index de95f41..de95f41 100644 --- a/examples/statemachine/errorstateplugins/spin_ai/spin_ai.cpp +++ b/examples/statemachine/tankgameplugins/spin_ai/spin_ai.cpp diff --git a/examples/statemachine/errorstateplugins/spin_ai/spin_ai.h b/examples/statemachine/tankgameplugins/spin_ai/spin_ai.h index 4b4629c..d8d3d73 100644 --- a/examples/statemachine/errorstateplugins/spin_ai/spin_ai.h +++ b/examples/statemachine/tankgameplugins/spin_ai/spin_ai.h @@ -1,7 +1,7 @@ #ifndef SPIN_AI_H #define SPIN_AI_H -#include <errorstate/plugin.h> +#include <tankgame/plugin.h> #include <QObject> #include <QState> @@ -18,7 +18,7 @@ public: public slots: void spin() { - m_tank->setProperty("direction", 90.0); + m_tank->setProperty("direction", m_tank->property("direction").toDouble() + 90.0); } protected: @@ -28,6 +28,11 @@ protected: spin(); } + void onExit(QEvent *) + { + disconnect(m_tank, SIGNAL(actionCompleted()), this, SLOT(spin())); + } + private: QObject *m_tank; @@ -38,6 +43,8 @@ class SpinAi: public QObject, public Plugin Q_OBJECT Q_INTERFACES(Plugin) public: + SpinAi() { setObjectName("Spin and destroy"); } + virtual QState *create(QState *parentState, QObject *tank); }; diff --git a/examples/statemachine/errorstateplugins/spin_ai/spin_ai.pro b/examples/statemachine/tankgameplugins/spin_ai/spin_ai.pro index c2fd937..8ab4da0 100644 --- a/examples/statemachine/errorstateplugins/spin_ai/spin_ai.pro +++ b/examples/statemachine/tankgameplugins/spin_ai/spin_ai.pro @@ -4,10 +4,10 @@ INCLUDEPATH += ../.. HEADERS = spin_ai.h SOURCES = spin_ai.cpp TARGET = $$qtLibraryTarget(spin_ai) -DESTDIR = ../../errorstate/plugins +DESTDIR = ../../tankgame/plugins #! [0] # install -target.path = $$[QT_INSTALL_EXAMPLES]/statemachine/errorstate/plugins +target.path = $$[QT_INSTALL_EXAMPLES]/statemachine/tankgame/plugins sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS spin_ai.pro -sources.path = $$[QT_INSTALL_EXAMPLES]/statemachine/errorstateplugins/spin_ai
\ No newline at end of file +sources.path = $$[QT_INSTALL_EXAMPLES]/statemachine/tankgameplugins/spin_ai
\ No newline at end of file diff --git a/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.cpp b/examples/statemachine/tankgameplugins/spin_ai_with_error/spin_ai_with_error.cpp index 5499ba3..5499ba3 100644 --- a/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.cpp +++ b/examples/statemachine/tankgameplugins/spin_ai_with_error/spin_ai_with_error.cpp diff --git a/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.h b/examples/statemachine/tankgameplugins/spin_ai_with_error/spin_ai_with_error.h index 9a96a8b..456ba01 100644 --- a/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.h +++ b/examples/statemachine/tankgameplugins/spin_ai_with_error/spin_ai_with_error.h @@ -1,7 +1,7 @@ #ifndef SPIN_AI_WITH_ERROR_H #define SPIN_AI_WITH_ERROR_H -#include <errorstate/plugin.h> +#include <tankgame/plugin.h> #include <QObject> #include <QState> @@ -18,7 +18,7 @@ public: public slots: void spin() { - m_tank->setProperty("direction", 90.0); + m_tank->setProperty("direction", m_tank->property("direction").toDouble() + 90.0); } protected: @@ -28,6 +28,11 @@ protected: spin(); } + void onExit(QEvent *) + { + disconnect(m_tank, SIGNAL(actionCompleted()), this, SLOT(spin())); + } + private: QObject *m_tank; @@ -38,6 +43,8 @@ class SpinAiWithError: public QObject, public Plugin Q_OBJECT Q_INTERFACES(Plugin) public: + SpinAiWithError() { setObjectName("Spin and destroy with runtime error in state machine"); } + virtual QState *create(QState *parentState, QObject *tank); }; diff --git a/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.pro b/examples/statemachine/tankgameplugins/spin_ai_with_error/spin_ai_with_error.pro index 31f4c7f..124cf98 100644 --- a/examples/statemachine/errorstateplugins/spin_ai_with_error/spin_ai_with_error.pro +++ b/examples/statemachine/tankgameplugins/spin_ai_with_error/spin_ai_with_error.pro @@ -4,10 +4,10 @@ INCLUDEPATH += ../.. HEADERS = spin_ai_with_error.h SOURCES = spin_ai_with_error.cpp TARGET = $$qtLibraryTarget(spin_ai_with_error) -DESTDIR = ../../errorstate/plugins +DESTDIR = ../../tankgame/plugins #! [0] # install -target.path = $$[QT_INSTALL_EXAMPLES]/statemachine/errorstate/plugins +target.path = $$[QT_INSTALL_EXAMPLES]/statemachine/tankgame/plugins sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS spin_ai_with_error.pro -sources.path = $$[QT_INSTALL_EXAMPLES]/statemachine/errorstateplugins/spin_ai_with_error
\ No newline at end of file +sources.path = $$[QT_INSTALL_EXAMPLES]/statemachine/tankgameplugins/spin_ai_with_error
\ No newline at end of file diff --git a/examples/statemachine/tankgameplugins/tankgameplugins.pro b/examples/statemachine/tankgameplugins/tankgameplugins.pro new file mode 100644 index 0000000..a098e03 --- /dev/null +++ b/examples/statemachine/tankgameplugins/tankgameplugins.pro @@ -0,0 +1,11 @@ +TEMPLATE = subdirs +SUBDIRS = random_ai \ + spin_ai_with_error \ + spin_ai \ + seek_ai + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/statemachine/tankgameplugins +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS tankgameplugins.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/statemachine/tankgameplugins +INSTALLS += target sources diff --git a/examples/statemachine/trafficlight/main.cpp b/examples/statemachine/trafficlight/main.cpp index 3c47a51..8a46fff 100644 --- a/examples/statemachine/trafficlight/main.cpp +++ b/examples/statemachine/trafficlight/main.cpp @@ -87,27 +87,6 @@ private: //! [0] //! [1] -class LightState : public QState -{ -public: - LightState(LightWidget *light, int duration, QState *parent = 0) - : QState(parent) - { - QTimer *timer = new QTimer(this); - timer->setInterval(duration); - timer->setSingleShot(true); - QState *timing = new QState(this); - QObject::connect(timing, SIGNAL(entered()), light, SLOT(turnOn())); - QObject::connect(timing, SIGNAL(entered()), timer, SLOT(start())); - QObject::connect(timing, SIGNAL(exited()), light, SLOT(turnOff())); - QFinalState *done = new QFinalState(this); - timing->addTransition(timer, SIGNAL(timeout()), done); - setInitialState(timing); - } -}; -//! [1] - -//! [2] class TrafficLightWidget : public QWidget { public: @@ -139,6 +118,24 @@ private: LightWidget *m_yellow; LightWidget *m_green; }; +//! [1] + +//! [2] +QState *createLightState(LightWidget *light, int duration, QState *parent = 0) +{ + QState *lightState = new QState(parent); + QTimer *timer = new QTimer(lightState); + timer->setInterval(duration); + timer->setSingleShot(true); + QState *timing = new QState(lightState); + QObject::connect(timing, SIGNAL(entered()), light, SLOT(turnOn())); + QObject::connect(timing, SIGNAL(entered()), timer, SLOT(start())); + QObject::connect(timing, SIGNAL(exited()), light, SLOT(turnOff())); + QFinalState *done = new QFinalState(lightState); + timing->addTransition(timer, SIGNAL(timeout()), done); + lightState->setInitialState(timing); + return lightState; +} //! [2] //! [3] @@ -154,15 +151,15 @@ public: vbox->setMargin(0); QStateMachine *machine = new QStateMachine(this); - LightState *redGoingYellow = new LightState(widget->redLight(), 3000); + QState *redGoingYellow = createLightState(widget->redLight(), 3000); redGoingYellow->setObjectName("redGoingYellow"); - LightState *yellowGoingGreen = new LightState(widget->yellowLight(), 1000); + QState *yellowGoingGreen = createLightState(widget->yellowLight(), 1000); yellowGoingGreen->setObjectName("yellowGoingGreen"); redGoingYellow->addTransition(redGoingYellow, SIGNAL(finished()), yellowGoingGreen); - LightState *greenGoingYellow = new LightState(widget->greenLight(), 3000); + QState *greenGoingYellow = createLightState(widget->greenLight(), 3000); greenGoingYellow->setObjectName("greenGoingYellow"); yellowGoingGreen->addTransition(yellowGoingGreen, SIGNAL(finished()), greenGoingYellow); - LightState *yellowGoingRed = new LightState(widget->yellowLight(), 1000); + QState *yellowGoingRed = createLightState(widget->yellowLight(), 1000); yellowGoingRed->setObjectName("yellowGoingRed"); greenGoingYellow->addTransition(greenGoingYellow, SIGNAL(finished()), yellowGoingRed); yellowGoingRed->addTransition(yellowGoingRed, SIGNAL(finished()), redGoingYellow); diff --git a/examples/statemachine/twowaybutton/main.cpp b/examples/statemachine/twowaybutton/main.cpp index 61a0f32..a2c6e45 100644 --- a/examples/statemachine/twowaybutton/main.cpp +++ b/examples/statemachine/twowaybutton/main.cpp @@ -45,34 +45,42 @@ #include <qstatemachine.h> #endif +//! [0] int main(int argc, char **argv) { QApplication app(argc, argv); - QPushButton button; - QStateMachine machine; - QState *first = new QState(); - first->setObjectName("first"); +//! [0] +//! [1] QState *off = new QState(); off->assignProperty(&button, "text", "Off"); off->setObjectName("off"); - first->addTransition(off); QState *on = new QState(); on->setObjectName("on"); on->assignProperty(&button, "text", "On"); +//! [1] + +//! [2] off->addTransition(&button, SIGNAL(clicked()), on); on->addTransition(&button, SIGNAL(clicked()), off); +//! [2] - machine.addState(first); +//! [3] machine.addState(off); machine.addState(on); - machine.setInitialState(first); +//! [3] + +//! [4] + machine.setInitialState(off); machine.start(); +//! [4] +//! [5] button.resize(100, 50); button.show(); return app.exec(); } +//! [5] |