[Ktutorial-commits] SF.net SVN: ktutorial:[107] trunk/ktutorial
Status: Alpha
Brought to you by:
danxuliu
From: <dan...@us...> - 2010-02-25 06:02:52
|
Revision: 107 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=107&view=rev Author: danxuliu Date: 2010-02-25 06:02:45 +0000 (Thu, 25 Feb 2010) Log Message: ----------- Added WaitForEvent class to wait for events of some type sent to an object. It is used now in UsingKTutorial and TutorialMoveText tutorials. Modified Paths: -------------- trunk/ktutorial/ktutorial-library/src/CMakeLists.txt trunk/ktutorial/ktutorial-library/src/scripting/ScriptingModule.cpp trunk/ktutorial/ktutorial-library/src/tutorials/UsingKTutorial.cpp trunk/ktutorial/ktutorial-library/test/CMakeLists.txt trunk/ktutorial/ktutorial-library/test/scripting/ScriptingModuleTest.cpp trunk/ktutorial/ktutorial-library/test/scripting/ScriptingTest.cpp trunk/ktutorial/ktutorial-test-app/TutorialMoveText.cpp trunk/ktutorial/ktutorial-test-app/TutorialMoveText.h Added Paths: ----------- trunk/ktutorial/ktutorial-library/src/WaitForEvent.cpp trunk/ktutorial/ktutorial-library/src/WaitForEvent.h trunk/ktutorial/ktutorial-library/test/WaitForEventTest.cpp Modified: trunk/ktutorial/ktutorial-library/src/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-library/src/CMakeLists.txt 2010-02-25 05:40:00 UTC (rev 106) +++ trunk/ktutorial/ktutorial-library/src/CMakeLists.txt 2010-02-25 06:02:45 UTC (rev 107) @@ -18,6 +18,7 @@ WaitFor.cpp WaitForAnd.cpp WaitForComposed.cpp + WaitForEvent.cpp WaitForNot.cpp WaitForOr.cpp WaitForSignal.cpp @@ -42,6 +43,7 @@ WaitFor.h WaitForAnd.h WaitForComposed.h + WaitForEvent.h WaitForNot.h WaitForOr.h WaitForSignal.h Added: trunk/ktutorial/ktutorial-library/src/WaitForEvent.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/src/WaitForEvent.cpp (rev 0) +++ trunk/ktutorial/ktutorial-library/src/WaitForEvent.cpp 2010-02-25 06:02:45 UTC (rev 107) @@ -0,0 +1,90 @@ +/*************************************************************************** + * Copyright (C) 2010 by Daniel Calviño Sánchez * + * dan...@gm... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; If not, see <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#include "WaitForEvent.h" + +#include <QMetaEnum> + +#include <KDebug> + +//public: + +WaitForEvent::WaitForEvent(): + mObject(0), + mEventType(QEvent::None), + mConditionMet(false) { +} + +WaitForEvent::WaitForEvent(QObject* object, QEvent::Type type): + mObject(object), + mEventType(type), + mConditionMet(false) { + + object->installEventFilter(this); +} + +void WaitForEvent::setEvent(QObject* object, const QString& typeName) { + int index = QEvent::staticMetaObject.indexOfEnumerator("Type"); + QMetaEnum eventTypeEnumerator = QEvent::staticMetaObject.enumerator(index); + + int eventTypeValue = eventTypeEnumerator.keyToValue(qPrintable(typeName)); + if (eventTypeValue == -1) { + kWarning() << "QEvent::Type named \"" << typeName << "\" is unknown"; + return; + } + + mObject = object; + mEventType = static_cast<QEvent::Type>(eventTypeValue); + + mObject->installEventFilter(this); +} + +bool WaitForEvent::eventFilter(QObject* object, QEvent* event) { + if (!isActive()) { + return false; + } + + if (object == mObject && event->type() == mEventType) { + handleEvent(event); + } + + return false; +} + +bool WaitForEvent::conditionMet() const { + return mConditionMet; +} + +void WaitForEvent::setActive(bool active) { + WaitFor::setActive(active); + + if (active) { + mConditionMet = false; + } +} + +//protected: + +void WaitForEvent::handleEvent(QEvent* event) { + Q_UNUSED(event); + + mConditionMet = true; + emit waitEnded(this); +} + +#include "WaitForEvent.moc" Property changes on: trunk/ktutorial/ktutorial-library/src/WaitForEvent.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-library/src/WaitForEvent.h =================================================================== --- trunk/ktutorial/ktutorial-library/src/WaitForEvent.h (rev 0) +++ trunk/ktutorial/ktutorial-library/src/WaitForEvent.h 2010-02-25 06:02:45 UTC (rev 107) @@ -0,0 +1,154 @@ +/*************************************************************************** + * Copyright (C) 2010 by Daniel Calviño Sánchez * + * dan...@gm... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; If not, see <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#ifndef WAITFOREVENT_H +#define WAITFOREVENT_H + +#include <QEvent> + +#include "ktutorial_export.h" + +#include "WaitFor.h" + +/** + * Waits for an event of some specific type to be sent to an object. + * When an event of the expected type is sent and the WaitForEvent is active, + * the wait ends. + * + * Note that if the event is sent while the WaitFor isn't active, it won't + * be registered and the condition won't be met. In order to met the condition, + * the event must be sent while the WaitForEvent is active. + * + * In some cases, just waiting for an event of some type isn't enough. For + * example, you may want to end the waiting if a QEvent::MouseButtonPress is + * sent, but only if the button pressed is the left. To do this, you can create + * a subclass of WaitForEvent and redefine handleEvent(QEvent*) method. + * \code + * void WaitForEventSubclass::handleEvent(QEvent* event) { + * QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event); + * + * if (mouseEvent->button() == Qt::LeftButton) { + * WaitForEvent::handleEvent(event); + * } + * } + * \endcode + */ +class KTUTORIAL_EXPORT WaitForEvent: public WaitFor { +Q_OBJECT +public: + + /** + * Creates a new WaitForEvent. + * This constructor is needed to dynamically create WaitForEvent objects in + * scripts using ScriptingModule::newWaitFor(const QString&). Method + * setEvent(QObject*, const QString&) must be called to finish setting up + * the object. For C++ tutorials, use + * WaitForEvent(QObject*, QEvent::Type) constructor instead of this one. + */ + Q_INVOKABLE WaitForEvent(); + + /** + * Creates a new WaitForEvent. + * + * @param object The object to watch. + * @param typeName The name of the event type to wait for. + */ + WaitForEvent(QObject* object, QEvent::Type type); + + /** + * Sets the event to wait for. + * Note that the QEvent::Type has to be passed as a string. For example, to + * wait for a QEvent::Close you have to pass "Close" as the second + * parameter. + * This method can be invoked from a script. + * + * In fact, you should only invoke this method from a script, and only once, + * to set up the object. For C++ tutorials, use + * WaitForEvent(QObject*, QEvent::Type) constructor when creating this + * WaitForEvent. + * + * The type is passed as a string with its name instead of using a pure + * QEvent::Type due to a current limitation of Kross (enumeration in QObject + * classes seem supported. However, QEvent is not a QObject but a Q_GADGET). + * + * @param object The object to watch. + * @param typeName The name of the event type to wait for. + */ + Q_INVOKABLE void setEvent(QObject* object, const QString& typeName); + + /** + * Inspects the events sent to the watched object. + * If the event has the type expected by this WaitFor it is handled by + * handleEvent(QEvent*) which, by default, ends the waiting. + * + * @param object The object that the event was sent to. + * @param event The event sent. + * @return False, so the event can be handled further. + */ + virtual bool eventFilter(QObject* object, QEvent* event); + + /** + * Returns true if the event was received while active, false otherwise. + * + * @return True if the event was received while active, false otherwise. + */ + virtual bool conditionMet() const; + + /** + * Sets this WaitForEvent active or inactive. + * Activating it resets its condition. + * + * @param active True to set it active, false otherwise. + */ + virtual void setActive(bool active); + +protected: + + /** + * Handles the event ending the wait (if this WaitForEvent is active). + * WaitForEvent subclasses can redefine this method if just receiving an + * event of the type expected isn't enough to end the waiting and some + * special check has to be done. + * + * The event is assured to have the type expected by this WaitForEvent and + * to be sent to the watched object. + * + * @param event The event. + */ + virtual void handleEvent(QEvent* event); + +private: + + /** + * The watched object. + */ + QObject* mObject; + + /** + * The type of event expected. + */ + QEvent::Type mEventType; + + /** + * Whether the event was received when active or not. + */ + bool mConditionMet; + +}; + +#endif \ No newline at end of file Property changes on: trunk/ktutorial/ktutorial-library/src/WaitForEvent.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-library/src/scripting/ScriptingModule.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/src/scripting/ScriptingModule.cpp 2010-02-25 05:40:00 UTC (rev 106) +++ trunk/ktutorial/ktutorial-library/src/scripting/ScriptingModule.cpp 2010-02-25 06:02:45 UTC (rev 107) @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2009 by Daniel Calviño Sánchez * + * Copyright (C) 2009-2010 by Daniel Calviño Sánchez * * dan...@gm... * * * * This program is free software; you can redistribute it and/or modify * @@ -24,6 +24,7 @@ #include "../Option.h" #include "../WaitFor.h" #include "../WaitForAnd.h" +#include "../WaitForEvent.h" #include "../WaitForNot.h" #include "../WaitForOr.h" #include "../WaitForSignal.h" @@ -34,6 +35,7 @@ if (sSelf == 0) { sSelf = new ScriptingModule(); sSelf->registerWaitForMetaObject(WaitForAnd::staticMetaObject); + sSelf->registerWaitForMetaObject(WaitForEvent::staticMetaObject); sSelf->registerWaitForMetaObject(WaitForNot::staticMetaObject); sSelf->registerWaitForMetaObject(WaitForOr::staticMetaObject); sSelf->registerWaitForMetaObject(WaitForSignal::staticMetaObject); Modified: trunk/ktutorial/ktutorial-library/src/tutorials/UsingKTutorial.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/src/tutorials/UsingKTutorial.cpp 2010-02-25 05:40:00 UTC (rev 106) +++ trunk/ktutorial/ktutorial-library/src/tutorials/UsingKTutorial.cpp 2010-02-25 06:02:45 UTC (rev 107) @@ -27,9 +27,29 @@ #include "../Option.h" #include "../Step.h" #include "../TutorialInformation.h" +#include "../WaitForEvent.h" #include "../WaitForSignal.h" #include "../view/StepWidget.h" +class WaitForLeftMouseButtonPressed: public WaitForEvent { +Q_OBJECT +public: + + WaitForLeftMouseButtonPressed(QObject* object): + WaitForEvent(object, QEvent::MouseButtonPress) { + } + +protected: + + virtual void handleEvent(QEvent* event) { + QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event); + if (mouseEvent->button() == Qt::LeftButton) { + WaitForEvent::handleEvent(event); + } + } + +}; + class ClearTextStep: public Step { public: @@ -69,27 +89,11 @@ virtual void setup() { QTextEdit* textEdit = KTutorial::self()->findObject<QTextEdit*>( "usingKTutorialTextEdit"); - //The filter is removed when the text edit is deleted, that is, when it - //is closed. - textEdit->installEventFilter(this); - addWaitFor(new WaitForSignal(this, SIGNAL(textEditClosed())), + addWaitFor(new WaitForEvent(textEdit, QEvent::Close), mUsingKTutorial, SLOT(closeTextEditDone())); } - bool eventFilter(QObject* object, QEvent* event) { - Q_UNUSED(object); - if (event->type() == QEvent::Close) { - emit textEditClosed(); - } - - return false; - } - -Q_SIGNALS: - - void textEditClosed(); - private: UsingKTutorial* mUsingKTutorial; @@ -108,30 +112,11 @@ virtual void setup() { QWidget* mStepWidget = KTutorial::self()-> findObject<view::StepWidget*>("ktutorial_StepWidget"); - //The filter is removed when the widget is deleted, that is, when the - //tutorial is closed. - mStepWidget->installEventFilter(this); - addWaitFor(new WaitForSignal(this, SIGNAL(mousePressedOnWidget())), + addWaitFor(new WaitForLeftMouseButtonPressed(mStepWidget), mUsingKTutorial, SLOT(moveWidgetPressDone())); } - bool eventFilter(QObject* object, QEvent* event) { - Q_UNUSED(object); - if (event->type() == QEvent::MouseButtonPress) { - QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event); - if (mouseEvent->button() == Qt::LeftButton) { - emit mousePressedOnWidget(); - } - } - - return false; - } - -Q_SIGNALS: - - void mousePressedOnWidget(); - private: UsingKTutorial* mUsingKTutorial; @@ -150,27 +135,11 @@ virtual void setup() { QWidget* mStepWidget = KTutorial::self()-> findObject<view::StepWidget*>("ktutorial_StepWidget"); - //The filter is removed when the widget is deleted, that is, when the - //tutorial is closed. - mStepWidget->installEventFilter(this); - addWaitFor(new WaitForSignal(this, SIGNAL(mouseReleasedOnWidget())), + addWaitFor(new WaitForEvent(mStepWidget, QEvent::MouseButtonRelease), mUsingKTutorial, SLOT(moveWidgetReleaseDone())); } - bool eventFilter(QObject* object, QEvent* event) { - Q_UNUSED(object); - if (event->type() == QEvent::MouseButtonRelease) { - emit mouseReleasedOnWidget(); - } - - return false; - } - -Q_SIGNALS: - - void mouseReleasedOnWidget(); - private: UsingKTutorial* mUsingKTutorial; Modified: trunk/ktutorial/ktutorial-library/test/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-library/test/CMakeLists.txt 2010-02-25 05:40:00 UTC (rev 106) +++ trunk/ktutorial/ktutorial-library/test/CMakeLists.txt 2010-02-25 06:02:45 UTC (rev 107) @@ -14,7 +14,7 @@ ENDFOREACH(_className) ENDMACRO(UNIT_TESTS) -unit_tests(Option Step Tutorial TutorialInformation TutorialManager WaitFor WaitForAnd WaitForComposed WaitForNot WaitForOr WaitForSignal) +unit_tests(Option Step Tutorial TutorialInformation TutorialManager WaitFor WaitForAnd WaitForComposed WaitForEvent WaitForNot WaitForOr WaitForSignal) MACRO(MEM_TESTS) FOREACH(_testname ${ARGN}) @@ -22,4 +22,4 @@ ENDFOREACH(_testname) ENDMACRO(MEM_TESTS) -mem_tests(Option Step Tutorial TutorialInformation TutorialManager WaitFor WaitForAnd WaitForComposed WaitForNot WaitForOr WaitForSignal) +mem_tests(Option Step Tutorial TutorialInformation TutorialManager WaitFor WaitForAnd WaitForComposed WaitForEvent WaitForNot WaitForOr WaitForSignal) Added: trunk/ktutorial/ktutorial-library/test/WaitForEventTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/test/WaitForEventTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-library/test/WaitForEventTest.cpp 2010-02-25 06:02:45 UTC (rev 107) @@ -0,0 +1,221 @@ +/*************************************************************************** + * Copyright (C) 2010 by Daniel Calviño Sánchez * + * dan...@gm... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; If not, see <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#include <QtTest> + +#define protected public +#define private public +#include "WaitForEvent.h" +#undef private +#undef protected + +class WaitForEventTest: public QObject { +Q_OBJECT +private slots: + + void testConstructor(); + void testConstructorDefault(); + + void testSetActive(); + + void testWaitEnded(); + void testWaitEndedWithDefaultConstructor(); + void testWaitEndedNotActive(); + + void testHandleEvent(); + void testHandleEventNotActive(); + void testHandleEventEventTypeNotExpected(); + + void testEventSentAfterWaitForDestruction(); + +}; + +class WaitForEventWithCustomHandling: public WaitForEvent { +public: + + QEvent::Type mType; + int mHandledEventsCount; + + WaitForEventWithCustomHandling(QObject* object, QEvent::Type type): + WaitForEvent(object, type), + mType(type), + mHandledEventsCount(0) { + } + +protected: + + virtual void handleEvent(QEvent* event) { + QCOMPARE(event->type(), mType); + + mHandledEventsCount++; + } + +}; + +void WaitForEventTest::testConstructor() { + WaitForEvent waitForEvent(this, QEvent::ChildAdded); + + QVERIFY(!waitForEvent.isActive()); + QVERIFY(!waitForEvent.conditionMet()); +} + +void WaitForEventTest::testConstructorDefault() { + WaitForEvent waitForEvent; + waitForEvent.setEvent(this, "ChildAdded"); + + QVERIFY(!waitForEvent.isActive()); + QVERIFY(!waitForEvent.conditionMet()); +} + +void WaitForEventTest::testSetActive() { + WaitForEvent waitForEvent(this, QEvent::ChildAdded); + waitForEvent.mConditionMet = true; + + waitForEvent.setActive(true); + + QVERIFY(waitForEvent.isActive()); + QVERIFY(!waitForEvent.conditionMet()); +} + +//WaitFor* must be declared as a metatype to be used in qvariant_cast +Q_DECLARE_METATYPE(WaitFor*); + +void WaitForEventTest::testWaitEnded() { + QObject parentObject; + + WaitForEvent waitForEvent(&parentObject, QEvent::ChildAdded); + waitForEvent.setActive(true); + + //WaitFor* must be registered in order to be used with QSignalSpy + int waitForStarType = qRegisterMetaType<WaitFor*>("WaitFor*"); + QSignalSpy waitEndedSpy(&waitForEvent, SIGNAL(waitEnded(WaitFor*))); + + QObject* childObject = new QObject(); + childObject->setParent(&parentObject); + + QVERIFY(waitForEvent.conditionMet()); + QCOMPARE(waitEndedSpy.count(), 1); + QVariant argument = waitEndedSpy.at(0).at(0); + QCOMPARE(argument.userType(), waitForStarType); + QCOMPARE(qvariant_cast<WaitFor*>(argument), &waitForEvent); +} + +void WaitForEventTest::testWaitEndedWithDefaultConstructor() { + QObject parentObject; + + WaitForEvent waitForEvent; + waitForEvent.setEvent(&parentObject, "ChildAdded"); + waitForEvent.setActive(true); + + //WaitFor* must be registered in order to be used with QSignalSpy + int waitForStarType = qRegisterMetaType<WaitFor*>("WaitFor*"); + QSignalSpy waitEndedSpy(&waitForEvent, SIGNAL(waitEnded(WaitFor*))); + + QObject* childObject = new QObject(); + childObject->setParent(&parentObject); + + QVERIFY(waitForEvent.conditionMet()); + QCOMPARE(waitEndedSpy.count(), 1); + QVariant argument = waitEndedSpy.at(0).at(0); + QCOMPARE(argument.userType(), waitForStarType); + QCOMPARE(qvariant_cast<WaitFor*>(argument), &waitForEvent); +} + +void WaitForEventTest::testWaitEndedNotActive() { + QObject parentObject; + + WaitForEvent waitForEvent(&parentObject, QEvent::ChildAdded); + + qRegisterMetaType<WaitFor*>("WaitFor*"); + QSignalSpy waitEndedSpy(&waitForEvent, SIGNAL(waitEnded(WaitFor*))); + + QObject* childObject = new QObject(); + childObject->setParent(&parentObject); + + QVERIFY(!waitForEvent.conditionMet()); + QCOMPARE(waitEndedSpy.count(), 0); +} + +void WaitForEventTest::testHandleEvent() { + QObject parentObject; + + WaitForEventWithCustomHandling waitForEvent(&parentObject, + QEvent::ChildAdded); + waitForEvent.setActive(true); + + QObject* childObject = new QObject(); + childObject->setParent(&parentObject); + + QCOMPARE(waitForEvent.mHandledEventsCount, 1); +} + +void WaitForEventTest::testHandleEventNotActive() { + QObject parentObject; + + WaitForEventWithCustomHandling waitForEvent(&parentObject, + QEvent::ChildAdded); + + QObject* childObject = new QObject(); + childObject->setParent(&parentObject); + + QCOMPARE(waitForEvent.mHandledEventsCount, 0); +} + +void WaitForEventTest::testHandleEventEventTypeNotExpected() { + QObject parentObject; + + WaitForEventWithCustomHandling waitForEvent(&parentObject, + QEvent::ChildRemoved); + waitForEvent.setActive(true); + + QObject* childObject = new QObject(); + childObject->setParent(&parentObject); + + QCOMPARE(waitForEvent.mHandledEventsCount, 0); + + childObject->setParent(0); + + QCOMPARE(waitForEvent.mHandledEventsCount, 1); + + childObject->setParent(&parentObject); + + QCOMPARE(waitForEvent.mHandledEventsCount, 1); +} + +void WaitForEventTest::testEventSentAfterWaitForDestruction() { + QObject parentObject; + + WaitForEvent* waitForEvent = new WaitForEvent(&parentObject, + QEvent::ChildAdded); + waitForEvent->setActive(true); + + QObject* childObject = new QObject(); + childObject->setParent(&parentObject); + + delete waitForEvent; + + QObject* anotherChildObject = new QObject(); + anotherChildObject->setParent(&parentObject); + + //Nothing is checked explicitly. Implicitly, it is checked if deleting the + //event filter object and then sending another event crashes the application +} + +QTEST_MAIN(WaitForEventTest) + +#include "WaitForEventTest.moc" Property changes on: trunk/ktutorial/ktutorial-library/test/WaitForEventTest.cpp ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-library/test/scripting/ScriptingModuleTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/test/scripting/ScriptingModuleTest.cpp 2010-02-25 05:40:00 UTC (rev 106) +++ trunk/ktutorial/ktutorial-library/test/scripting/ScriptingModuleTest.cpp 2010-02-25 06:02:45 UTC (rev 107) @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2009 by Daniel Calviño Sánchez * + * Copyright (C) 2009-2010 by Daniel Calviño Sánchez * * dan...@gm... * * * * This program is free software; you can redistribute it and/or modify * @@ -30,6 +30,7 @@ #include "../Option.h" #include "../WaitFor.h" #include "../WaitForAnd.h" +#include "../WaitForEvent.h" #include "../WaitForNot.h" #include "../WaitForOr.h" #include "../WaitForSignal.h" @@ -155,6 +156,10 @@ QMetaObject& type = metaObject(scriptingModule, "WaitForAnd"); QCOMPARE(type.className(), WaitForAnd::staticMetaObject.className()); + QVERIFY(containsMetaObject(scriptingModule, "WaitForEvent")); + type = metaObject(scriptingModule, "WaitForEvent"); + QCOMPARE(type.className(), WaitForEvent::staticMetaObject.className()); + QVERIFY(containsMetaObject(scriptingModule, "WaitForNot")); type = metaObject(scriptingModule, "WaitForNot"); QCOMPARE(type.className(), WaitForNot::staticMetaObject.className()); Modified: trunk/ktutorial/ktutorial-library/test/scripting/ScriptingTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-library/test/scripting/ScriptingTest.cpp 2010-02-25 05:40:00 UTC (rev 106) +++ trunk/ktutorial/ktutorial-library/test/scripting/ScriptingTest.cpp 2010-02-25 06:02:45 UTC (rev 107) @@ -303,6 +303,8 @@ out << "tutorial.addStep(firstStep);\n"; out << "secondStep = ktutorial.newStep(\"second\");\n"; out << "tutorial.addStep(secondStep);\n"; + out << "thirdStep = ktutorial.newStep(\"third\");\n"; + out << "tutorial.addStep(thirdStep);\n"; out << "endStep = ktutorial.newStep(\"end\");\n"; out << "endStep.setText(\"The tutorial has ended.\");\n"; out << "tutorial.addStep(endStep);\n"; @@ -330,13 +332,22 @@ out << "waitForAnd.add(waitForAnotherDummy);\n"; out << "waitForAnd.add(waitForOr);\n"; - out << "function checkEnded() {\n"; + out << "function checkThird() {\n"; out << " if (testObject.booleanValue()) {\n"; - out << " tutorial.nextStep(\"end\");\n"; + out << " tutorial.nextStep(\"third\");\n"; out << " }\n"; out << "};\n"; - out << "secondStep.addWaitFor(waitForAnd, self, \"checkEnded\");\n"; + out << "secondStep.addWaitFor(waitForAnd, self, \"checkThird\");\n"; + out << "waitForChildAddedEvent = ktutorial.newWaitFor(\"WaitForEvent\");\n"; + out << "waitForChildAddedEvent.setEvent(testObject, \"ChildAdded\");\n"; + + out << "function thirdDone() {\n"; + out << " tutorial.nextStep(\"end\");\n"; + out << "};\n"; + out << "thirdStep.addWaitFor(waitForChildAddedEvent, \ +self, \"thirdDone\");\n"; + out.flush(); ScriptedTutorial scriptedTutorial(mTemporaryFile->fileName()); @@ -376,6 +387,11 @@ emit anotherDummySignal(); + QCOMPARE(scriptedTutorial.mCurrentStep->id(), QString("third")); + + QObject* childObject = new QObject(); + childObject->setParent(this); + QCOMPARE(scriptedTutorial.mCurrentStep->id(), QString("end")); QCOMPARE(scriptedTutorial.mCurrentStep->text(), QString("The tutorial has ended.")); Modified: trunk/ktutorial/ktutorial-test-app/TutorialMoveText.cpp =================================================================== --- trunk/ktutorial/ktutorial-test-app/TutorialMoveText.cpp 2010-02-25 05:40:00 UTC (rev 106) +++ trunk/ktutorial/ktutorial-test-app/TutorialMoveText.cpp 2010-02-25 06:02:45 UTC (rev 107) @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2008 by Daniel Calviño Sánchez * + * Copyright (C) 2008-2010 by Daniel Calviño Sánchez * * dan...@gm... * * * * This program is free software; you can redistribute it and/or modify * @@ -29,9 +29,28 @@ #include <ktutorial/Step.h> #include <ktutorial/TutorialInformation.h> #include <ktutorial/WaitForAnd.h> +#include <ktutorial/WaitForEvent.h> #include <ktutorial/WaitForNot.h> #include <ktutorial/WaitForSignal.h> +class WaitForLeftMouseButtonPressed: public WaitForEvent { +public: + + WaitForLeftMouseButtonPressed(QObject* object): + WaitForEvent(object, QEvent::MouseButtonPress) { + } + +protected: + + virtual void handleEvent(QEvent* event) { + QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event); + if (mouseEvent->button() == Qt::LeftButton) { + WaitForEvent::handleEvent(event); + } + } + +}; + //public: TutorialMoveText::TutorialMoveText(): Tutorial(0) { @@ -40,7 +59,6 @@ mTutorialInformation->setDescription(i18n("This tutorial shows how to move text in the text area")); mTextArea = KTutorial::self()->findObject<KTextEdit*>("textArea"); - mTextArea->viewport()->installEventFilter(this); mSecondPath = false; @@ -104,7 +122,7 @@ Step* mousePressStep = new Step("mousePress"); mousePressStep->setText(i18nc("@info", "Press with the left button of the mouse on the selected text. You must press on it, or the selection will change, and you will have to select it again. Don't release the button yet.")); - mousePressStep->addWaitFor(new WaitForSignal(this, SIGNAL(mousePressed())), + mousePressStep->addWaitFor(new WaitForLeftMouseButtonPressed(mTextArea->viewport()), this, SLOT(mousePress())); addStep(mousePressStep); @@ -113,7 +131,7 @@ Step* mouseReleaseStep = new Step("mouseRelease"); mouseReleaseStep->setText(i18nc("@info", "Without releasing the mouse button, move the cursor to the desired position, for example the beginning of the text.<nl/>Once the cursor is there, you can release the mouse button, and the text will be moved.")); - mouseReleaseStep->addWaitFor(new WaitForSignal(this, SIGNAL(mouseReleased())), + mouseReleaseStep->addWaitFor(new WaitForEvent(mTextArea->viewport(), QEvent::Drop), this, SLOT(mouseRelease())); addStep(mouseReleaseStep); @@ -134,20 +152,6 @@ addStep(endStep); } -bool TutorialMoveText::eventFilter(QObject* object, QEvent* event) { - if (event->type() == QEvent::MouseButtonPress) { - QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event); - if (mouseEvent->button() == Qt::LeftButton) { - emit mousePressed(); - } - } else if (event->type() == QEvent::Drop) { - QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event); - emit mouseReleased(); - } - - return false; -} - //public slots: void TutorialMoveText::startKeyboardMove() { Modified: trunk/ktutorial/ktutorial-test-app/TutorialMoveText.h =================================================================== --- trunk/ktutorial/ktutorial-test-app/TutorialMoveText.h 2010-02-25 05:40:00 UTC (rev 106) +++ trunk/ktutorial/ktutorial-test-app/TutorialMoveText.h 2010-02-25 06:02:45 UTC (rev 107) @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2008 by Daniel Calviño Sánchez * + * Copyright (C) 2008-2010 by Daniel Calviño Sánchez * * dan...@gm... * * * * This program is free software; you can redistribute it and/or modify * @@ -29,8 +29,6 @@ TutorialMoveText(); - bool eventFilter(QObject* object, QEvent* event); - public slots: void startKeyboardMove(); @@ -55,12 +53,6 @@ void end(); -signals: - - void mousePressed(); - - void mouseReleased(); - protected: void setup(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |