diff --git a/src/Core/QtScriptBackend.cpp b/src/Core/QtScriptBackend.cpp index 3dcb140d..8fdfef89 100644 --- a/src/Core/QtScriptBackend.cpp +++ b/src/Core/QtScriptBackend.cpp @@ -1,165 +1,166 @@ /* This file is part of Rocs. Copyright 2004-2011 Tomaz Canabrava This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "QtScriptBackend.h" #include "DataStructure.h" #include "Data.h" #include #include #include "Document.h" #include #include // usleep #include static QtScriptBackend *self; static QScriptValue debug_script(QScriptContext* context, QScriptEngine* /*engine*/) { self->debug(QString("%1").arg(context->argument(0).toString())); return QScriptValue(); } static QScriptValue output_script(QScriptContext *context, QScriptEngine* /*engine*/){ self->output(QString("%1").arg(context->argument(0).toString())); return QScriptValue(); } static QScriptValue include_script(QScriptContext *context, QScriptEngine* /*engine*/){ self->includeFile(QString("%1").arg(context->argument(0).toString())); return QScriptValue(); } void QtScriptBackend::stop(){ if (!_engine) return; if (_engine->isEvaluating()){ _engine->abortEvaluation(); } // _engine->deleteLater(); // _engine = 0; emit finished(); } void QtScriptBackend::start() { stop(); if (!_engine){ _engine = new QScriptEngine(); emit engineCreated(_engine); } _engine->globalObject().setProperty("debug", engine()->newFunction(debug_script)); _engine->globalObject().setProperty("output", engine()->newFunction(output_script)); _engine->globalObject().setProperty("include", engine()->newFunction(include_script)); int size = _document->dataStructures().size(); for (int i = 0; i < size; i++) { _document->dataStructures().at(i)->setEngine(_engine); } createGraphList(); _engine->setProcessEventsInterval(100); //! TODO: Make that changable. QString error = _engine->evaluate(_script, i18n("Rocs Console script")).toString(); if (_engine && _engine->hasUncaughtException()) { emit scriptError(); emit sendDebug(""+error+""); } emit finished(); } bool QtScriptBackend::isRunning(){ if ((_engine) && (_engine->isEvaluating())){ return true; } return _runningTool; } QtScriptBackend::QtScriptBackend(QObject* parent): QObject(parent){ self = this; _engine = 0; _runningTool = false; } void QtScriptBackend::runTool(ToolsPluginInterface * plugin, Document *graphs){ _runningTool = true; _document = graphs; _script = plugin->run(graphs); if ( !_script.isEmpty()){ start(); } _runningTool = false; } void QtScriptBackend::setScript(const QString& s,Document *graphs ) { _script = s; _document = graphs; kDebug() << "script Set" << _script; } void QtScriptBackend::createGraphList() { QScriptValue graphList = _engine->newArray(); _engine->globalObject().setProperty("graphs", graphList); // Add all the graphs on the array as an array, and if it has a name, // also add it for direct acess with it's name. int size = _document->dataStructures().size(); for (int i = 0; i < size; i++) { graphList.property("push").call(graphList, QScriptValueList() << _document->dataStructures().at(i)->scriptValue()); } } void QtScriptBackend::loadFile(const QString& file) { _script.clear(); QFile f(file); if ( !f.open(QIODevice::ReadOnly | QIODevice::Text ) ) { kDebug() << "File not found"; return; } while ( ! f.atEnd() ) { QByteArray line = f.readLine(); _script += line; } _script += '\n'; } void QtScriptBackend::debug(const QString& str){ emit sendDebug(str); } void QtScriptBackend::output(const QString& str){ emit sendOutput(str); emit sendDebug(""+str+""); } void QtScriptBackend::includeFile(const QString & includedFile){ QString fileName = m_includeManager.seekFile(includedFile); if (m_includeManager.checkIfWasIncluded(fileName)) return; QFile f(fileName); if (f.open(QFile::ReadOnly)){ QString script = m_includeManager.include(f.readAll(), fileName.section('/',0,-2), fileName.section('/',-1)); + _engine->currentContext()->setActivationObject( _engine->currentContext()->parentContext()->activationObject()); QString error = _engine->evaluate(script, includedFile).toString(); if (_engine && _engine->hasUncaughtException()) { emit scriptError(); emit sendDebug(i18n(" Error in include file %1").arg(includedFile)); emit sendDebug(""+error+""); } } }