KTutorial  0.5.1
ScriptedStep.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2009 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00003  *   Copyright (C) 2010 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00004  *   Copyright (C) 2012 by Daniel Calviño Sánchez <danxuliu@gmail.com>     *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  *   This program is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU General Public License for more details.                          *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU General Public License     *
00017  *   along with this program; If not, see <http://www.gnu.org/licenses/>.  *
00018  ***************************************************************************/
00019 
00020 #include "ScriptedStep.h"
00021 
00022 #include <kross/core/action.h>
00023 
00024 #include "../WaitFor.h"
00025 
00026 namespace ktutorial {
00027 namespace scripting {
00028 
00029 ScriptedStep::ScriptedStep(const QString& id): Step(id),
00030     mScriptAction(0) {
00031 }
00032 
00033 //protected:
00034 
00035 void ScriptedStep::setup() {
00036     emit setup(this);
00037 }
00038 
00039 void ScriptedStep::tearDown() {
00040     emit tearDown(this);
00041 }
00042 
00043 void ScriptedStep::connectWaitFor(WaitFor* waitFor, QObject* receiver,
00044                           const QString& slot) {
00045     if (!qobject_cast<Kross::Action*>(receiver)) {
00046         Step::connectWaitFor(waitFor, receiver, slot);
00047         return;
00048     }
00049 
00050     Kross::Action* scriptAction = static_cast<Kross::Action*>(receiver);
00051     if (mScriptAction == 0) {
00052         mScriptAction = scriptAction;
00053     }
00054     //Check that the previously set Kross::Action is the same as the current one
00055     Q_ASSERT(mScriptAction == scriptAction);
00056 
00057     QString functionName = slot;
00058     int index = functionName.indexOf('(');
00059     if (index > -1) {
00060         functionName = functionName.left(index);
00061     }
00062 
00063     mFunctions.insert(waitFor, functionName);
00064 
00065     connect(waitFor, SIGNAL(waitEnded(WaitFor*)),
00066             this, SLOT(executeScriptFunctionFor(WaitFor*)));
00067 }
00068 
00069 void ScriptedStep::disconnectWaitFor(WaitFor* waitFor) {
00070     if (!mFunctions.contains(waitFor)) {
00071         Step::disconnectWaitFor(waitFor);
00072         return;
00073     }
00074 
00075     mFunctions.remove(waitFor);
00076 
00077     disconnect(waitFor, SIGNAL(waitEnded(WaitFor*)));
00078 }
00079 
00080 //private:
00081 
00082 void ScriptedStep::executeScriptFunctionFor(WaitFor* waitFor) {
00083     mScriptAction->callFunction(mFunctions.value(waitFor).toLatin1());
00084 }
00085 
00086 }
00087 }