StelScriptMgr.cpp   StelScriptMgr.cpp 
skipping to change at line 49 skipping to change at line 49
#include <QDateTime> #include <QDateTime>
#include <QDebug> #include <QDebug>
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
#include <QRegExp> #include <QRegExp>
#include <QSet> #include <QSet>
#include <QStringList> #include <QStringList>
#include <QTemporaryFile> #include <QTemporaryFile>
#include <QVariant> #include <QVariant>
#include <QtScript>
#include <cmath> #include <cmath>
QScriptValue vec3fToScriptValue(QScriptEngine *engine, const Vec3f& c) QScriptValue vec3fToScriptValue(QScriptEngine *engine, const Vec3f& c)
{ {
QScriptValue obj = engine->newObject(); QScriptValue obj = engine->newObject();
obj.setProperty("r", QScriptValue(engine, c[0])); obj.setProperty("r", QScriptValue(engine, c[0]));
obj.setProperty("g", QScriptValue(engine, c[1])); obj.setProperty("g", QScriptValue(engine, c[1]));
obj.setProperty("b", QScriptValue(engine, c[2])); obj.setProperty("b", QScriptValue(engine, c[2]));
return obj; return obj;
skipping to change at line 77 skipping to change at line 78
QScriptValue createVec3f(QScriptContext* context, QScriptEngine *engine) QScriptValue createVec3f(QScriptContext* context, QScriptEngine *engine)
{ {
Vec3f c; Vec3f c;
c[0] = context->argument(0).toNumber(); c[0] = context->argument(0).toNumber();
c[1] = context->argument(1).toNumber(); c[1] = context->argument(1).toNumber();
c[2] = context->argument(2).toNumber(); c[2] = context->argument(2).toNumber();
return vec3fToScriptValue(engine, c); return vec3fToScriptValue(engine, c);
} }
class StelScriptEngineAgent : public QScriptEngineAgent
{
public:
explicit StelScriptEngineAgent(QScriptEngine *engine);
virtual ~StelScriptEngineAgent() {}
void setPauseScript(bool pause) { isPaused=pause; }
bool getPauseScript() { return isPaused; }
void positionChange(qint64 scriptId, int lineNumber, int columnNumbe
r);
private:
bool isPaused;
};
StelScriptMgr::StelScriptMgr(QObject *parent): QObject(parent) StelScriptMgr::StelScriptMgr(QObject *parent): QObject(parent)
{ {
engine = new QScriptEngine(this);
connect(&StelApp::getInstance(), SIGNAL(aboutToQuit()), this, SLOT(s topScript()), Qt::DirectConnection); connect(&StelApp::getInstance(), SIGNAL(aboutToQuit()), this, SLOT(s topScript()), Qt::DirectConnection);
// Scripting images // Scripting images
ScreenImageMgr* scriptImages = new ScreenImageMgr(); ScreenImageMgr* scriptImages = new ScreenImageMgr();
scriptImages->init(); scriptImages->init();
StelApp::getInstance().getModuleMgr().registerModule(scriptImages); StelApp::getInstance().getModuleMgr().registerModule(scriptImages);
// Allow Vec3f managment in scripts // Allow Vec3f managment in scripts
qScriptRegisterMetaType(&engine, vec3fToScriptValue, vec3fFromScript Value); qScriptRegisterMetaType(engine, vec3fToScriptValue, vec3fFromScriptV alue);
// Constructor // Constructor
QScriptValue ctor = engine.newFunction(createVec3f); QScriptValue ctor = engine->newFunction(createVec3f);
engine.globalObject().setProperty("Vec3f", ctor); engine->globalObject().setProperty("Vec3f", ctor);
// Add the core object to access methods related to core // Add the core object to access methods related to core
mainAPI = new StelMainScriptAPI(this); mainAPI = new StelMainScriptAPI(this);
QScriptValue objectValue = engine.newQObject(mainAPI); QScriptValue objectValue = engine->newQObject(mainAPI);
engine.globalObject().setProperty("core", objectValue); engine->globalObject().setProperty("core", objectValue);
// Add other classes which we want to be directly accessible from sc ripts // Add other classes which we want to be directly accessible from sc ripts
if(StelSkyLayerMgr* smgr = GETSTELMODULE(StelSkyLayerMgr)) if(StelSkyLayerMgr* smgr = GETSTELMODULE(StelSkyLayerMgr))
objectValue = engine.newQObject(smgr); objectValue = engine->newQObject(smgr);
// For accessing star scale, twinkle etc. // For accessing star scale, twinkle etc.
objectValue = engine.newQObject(StelApp::getInstance().getCore()->ge objectValue = engine->newQObject(StelApp::getInstance().getCore()->g
tSkyDrawer()); etSkyDrawer());
engine.globalObject().setProperty("StelSkyDrawer", objectValue); engine->globalObject().setProperty("StelSkyDrawer", objectValue);
setScriptRate(1.0); setScriptRate(1.0);
engine.setProcessEventsInterval(10); engine->setProcessEventsInterval(10);
agent = new StelScriptEngineAgent(&engine); agent = new StelScriptEngineAgent(engine);
engine.setAgent(agent); engine->setAgent(agent);
initActions(); initActions();
} }
void StelScriptMgr::initActions() void StelScriptMgr::initActions()
{ {
StelActionMgr* actionMgr = StelApp::getInstance().getStelActionManag er(); StelActionMgr* actionMgr = StelApp::getInstance().getStelActionManag er();
QSignalMapper* mapper = new QSignalMapper(this); QSignalMapper* mapper = new QSignalMapper(this);
foreach(const QString script, getScriptList()) foreach(const QString script, getScriptList())
{ {
QString shortcut = getShortcut(script); QString shortcut = getShortcut(script);
QString actionId = "actionScript/" + script; QString actionId = "actionScript/" + script;
StelAction* action = actionMgr->addAction( StelAction* action = actionMgr->addAction(
actionId, N_("Scripts"), q_(getName(script).trimmed()), mapper, "map()", shortcut); actionId, N_("Scripts"), q_(getName( script).trimmed()), mapper, "map()", shortcut);
mapper->setMapping(action, script); mapper->setMapping(action, script);
} }
connect(mapper, SIGNAL(mapped(QString)), this, SLOT(runScript(QStrin g))); connect(mapper, SIGNAL(mapped(QString)), this, SLOT(runScript(QStrin g)));
} }
StelScriptMgr::~StelScriptMgr() StelScriptMgr::~StelScriptMgr()
{ {
} }
void StelScriptMgr::addModules() void StelScriptMgr::addModules()
{ {
// Add all the StelModules into the script engine // Add all the StelModules into the script engine
StelModuleMgr* mmgr = &StelApp::getInstance().getModuleMgr(); StelModuleMgr* mmgr = &StelApp::getInstance().getModuleMgr();
foreach (StelModule* m, mmgr->getAllModules()) foreach (StelModule* m, mmgr->getAllModules())
{ {
QScriptValue objectValue = engine.newQObject(m); QScriptValue objectValue = engine->newQObject(m);
engine.globalObject().setProperty(m->objectName(), objectVal engine->globalObject().setProperty(m->objectName(), objectVa
ue); lue);
} }
} }
QStringList StelScriptMgr::getScriptList() QStringList StelScriptMgr::getScriptList()
{ {
QStringList scriptFiles; QStringList scriptFiles;
QSet<QString> files = StelFileMgr::listContents("scripts", StelFileM gr::File, true); QSet<QString> files = StelFileMgr::listContents("scripts", StelFileM gr::File, true);
QRegExp fileRE("^.*\\.ssc$"); QRegExp fileRE("^.*\\.ssc$");
foreach(const QString& f, files) foreach(const QString& f, files)
{ {
if (fileRE.exactMatch(f)) if (fileRE.exactMatch(f))
scriptFiles << f; scriptFiles << f;
} }
return scriptFiles; return scriptFiles;
} }
bool StelScriptMgr::scriptIsRunning() bool StelScriptMgr::scriptIsRunning()
{ {
return engine.isEvaluating(); return engine->isEvaluating();
} }
QString StelScriptMgr::runningScriptId() QString StelScriptMgr::runningScriptId()
{ {
return scriptFileName; return scriptFileName;
} }
QString StelScriptMgr::getHeaderSingleLineCommentText(const QString& s, con st QString& id, const QString& notFoundText) const QString StelScriptMgr::getHeaderSingleLineCommentText(const QString& s, con st QString& id, const QString& notFoundText) const
{ {
QFile file(StelFileMgr::findFile("scripts/" + s, StelFileMgr::File)) ; QFile file(StelFileMgr::findFile("scripts/" + s, StelFileMgr::File)) ;
skipping to change at line 306 skipping to change at line 324
} }
desc += d; desc += d;
} }
} }
file.close(); file.close();
return desc.trimmed(); return desc.trimmed();
} }
bool StelScriptMgr::runPreprocessedScript(const QString &preprocessedScript , const QString& scriptId) bool StelScriptMgr::runPreprocessedScript(const QString &preprocessedScript , const QString& scriptId)
{ {
if (engine.isEvaluating()) if (engine->isEvaluating())
{ {
QString msg = QString("ERROR: there is already a script runn ing, please wait until it's over."); QString msg = QString("ERROR: there is already a script runn ing, please wait until it's over.");
emit(scriptDebug(msg)); emit(scriptDebug(msg));
qWarning() << msg; qWarning() << msg;
return false; return false;
} }
// Make sure that the gui objects have been completely initialized ( there used to be problems with startup scripts). // Make sure that the gui objects have been completely initialized ( there used to be problems with startup scripts).
Q_ASSERT(StelApp::getInstance().getGui()); Q_ASSERT(StelApp::getInstance().getGui());
engine.globalObject().setProperty("scriptRateReadOnly", 1.0); engine->globalObject().setProperty("scriptRateReadOnly", 1.0);
scriptFileName = scriptId; scriptFileName = scriptId;
// Notify that the script starts here // Notify that the script starts here
emit(scriptRunning()); emit(scriptRunning());
emit runningScriptIdChanged(scriptId); emit runningScriptIdChanged(scriptId);
// run that script // run that script
engine.evaluate(preprocessedScript); engine->evaluate(preprocessedScript);
scriptEnded(); scriptEnded();
return true; return true;
} }
// Run the script located at the given location // Run the script located at the given location
bool StelScriptMgr::runScript(const QString& fileName, const QString& inclu dePath) bool StelScriptMgr::runScript(const QString& fileName, const QString& inclu dePath)
{ {
QString preprocessedScript; QString preprocessedScript;
prepareScript(preprocessedScript,fileName,includePath); prepareScript(preprocessedScript,fileName,includePath);
return runPreprocessedScript(preprocessedScript,fileName); return runPreprocessedScript(preprocessedScript,fileName);
skipping to change at line 401 skipping to change at line 419
if (!ok) if (!ok)
{ {
return false; return false;
} }
return true; return true;
} }
void StelScriptMgr::stopScript() void StelScriptMgr::stopScript()
{ {
if (engine.isEvaluating()) if (engine->isEvaluating())
{ {
GETSTELMODULE(LabelMgr)->deleteAllLabels(); GETSTELMODULE(LabelMgr)->deleteAllLabels();
GETSTELMODULE(ScreenImageMgr)->deleteAllImages(); GETSTELMODULE(ScreenImageMgr)->deleteAllImages();
if (agent->getPauseScript()) { if (agent->getPauseScript()) {
agent->setPauseScript(false); agent->setPauseScript(false);
} }
QString msg = QString("INFO: asking running script to exit") ; QString msg = QString("INFO: asking running script to exit") ;
emit(scriptDebug(msg)); emit(scriptDebug(msg));
//qDebug() << msg; //qDebug() << msg;
engine.abortEvaluation(); engine->abortEvaluation();
} }
scriptEnded(); scriptEnded();
} }
void StelScriptMgr::setScriptRate(float r) void StelScriptMgr::setScriptRate(float r)
{ {
//qDebug() << "StelScriptMgr::setScriptRate(" << r << ")"; //qDebug() << "StelScriptMgr::setScriptRate(" << r << ")";
if (!engine.isEvaluating()) if (!engine->isEvaluating())
{ {
engine.globalObject().setProperty("scriptRateReadOnly", r); engine->globalObject().setProperty("scriptRateReadOnly", r);
return; return;
} }
float currentScriptRate = engine.globalObject().property("scriptRate ReadOnly").toNumber(); float currentScriptRate = engine->globalObject().property("scriptRat eReadOnly").toNumber();
// pre-calculate the new time rate in an effort to prevent there bei ng much latency // pre-calculate the new time rate in an effort to prevent there bei ng much latency
// between setting the script rate and the time rate. // between setting the script rate and the time rate.
float factor = r / currentScriptRate; float factor = r / currentScriptRate;
StelCore* core = StelApp::getInstance().getCore(); StelCore* core = StelApp::getInstance().getCore();
core->setTimeRate(core->getTimeRate() * factor); core->setTimeRate(core->getTimeRate() * factor);
GETSTELMODULE(StelMovementMgr)->setMovementSpeedFactor(core->getTime Rate()); GETSTELMODULE(StelMovementMgr)->setMovementSpeedFactor(core->getTime Rate());
engine.globalObject().setProperty("scriptRateReadOnly", r); engine->globalObject().setProperty("scriptRateReadOnly", r);
} }
void StelScriptMgr::pauseScript() { void StelScriptMgr::pauseScript() {
agent->setPauseScript(true); agent->setPauseScript(true);
} }
void StelScriptMgr::resumeScript() { void StelScriptMgr::resumeScript() {
agent->setPauseScript(false); agent->setPauseScript(false);
} }
double StelScriptMgr::getScriptRate() double StelScriptMgr::getScriptRate()
{ {
return engine.globalObject().property("scriptRateReadOnly").toNumber (); return engine->globalObject().property("scriptRateReadOnly").toNumbe r();
} }
void StelScriptMgr::debug(const QString& msg) void StelScriptMgr::debug(const QString& msg)
{ {
emit(scriptDebug(msg)); emit(scriptDebug(msg));
} }
void StelScriptMgr::output(const QString &msg) void StelScriptMgr::output(const QString &msg)
{ {
StelScriptOutput::writeLog(msg); StelScriptOutput::writeLog(msg);
skipping to change at line 476 skipping to change at line 494
emit(scriptOutput("")); emit(scriptOutput(""));
} }
void StelScriptMgr::saveOutputAs(const QString &filename) void StelScriptMgr::saveOutputAs(const QString &filename)
{ {
StelScriptOutput::saveOutputAs(filename); StelScriptOutput::saveOutputAs(filename);
} }
void StelScriptMgr::scriptEnded() void StelScriptMgr::scriptEnded()
{ {
if (engine.hasUncaughtException()) if (engine->hasUncaughtException())
{ {
QString msg = QString("script error: \"%1\" @ line %2").arg( engine.uncaughtException().toString()).arg(engine.uncaughtExceptionLineNumb er()); QString msg = QString("script error: \"%1\" @ line %2").arg( engine->uncaughtException().toString()).arg(engine->uncaughtExceptionLineNu mber());
emit(scriptDebug(msg)); emit(scriptDebug(msg));
qWarning() << msg; qWarning() << msg;
} }
GETSTELMODULE(StelMovementMgr)->setMovementSpeedFactor(1.0); GETSTELMODULE(StelMovementMgr)->setMovementSpeedFactor(1.0);
scriptFileName = QString(); scriptFileName = QString();
emit runningScriptIdChanged(scriptFileName); emit runningScriptIdChanged(scriptFileName);
emit(scriptStopped()); emit(scriptStopped());
} }
 End of changes. 25 change blocks. 
29 lines changed or deleted 48 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/