Stellarium 0.15.0
StelScriptMgr.hpp
1 /*
2  * Stellarium
3  * Copyright (C) 2007 Fabien Chereau, 2009 Matthew Gates
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  */
19 
20 #ifndef _STELSCRIPTMGR_HPP_
21 #define _STELSCRIPTMGR_HPP_
22 
23 #include <QObject>
24 #include <QtScript>
25 #include <QStringList>
26 #include <QFile>
27 #include <QTime>
28 #include <QTimer>
29 #include <QScriptEngineAgent>
30 
31 class StelMainScriptAPI;
33 
34 #ifdef ENABLE_SCRIPT_CONSOLE
35 class ScriptConsole;
36 #endif
37 
39 class StelScriptMgr : public QObject
40 {
41  Q_OBJECT
42 
43 #ifdef ENABLE_SCRIPT_CONSOLE
44 friend class ScriptConsole;
45 #endif
46 
47 public:
48  StelScriptMgr(QObject *parent=0);
49  ~StelScriptMgr();
50 
51  QStringList getScriptList();
52 
55  bool scriptIsRunning();
59  QString runningScriptId();
60 
61  // Pre-processor functions
66  bool preprocessScript(const QString& input, QString& output, const QString& scriptDir);
67  bool preprocessScript(QFile &input, QString& output, const QString& scriptDir);
68 
70  void addModules();
71 public slots:
78  const QString getName(const QString& s);
79 
86  const QString getAuthor(const QString& s);
87 
94  const QString getLicense(const QString& s);
95 
103  const QString getDescription(const QString& s);
104 
111  const QString getShortcut(const QString& s);
112 
116  bool runPreprocessedScript(const QString& preprocessedScript);
117 
126  bool runScript(const QString& fileName, const QString& includePath="");
127 
130  void stopScript();
131 
137  void setScriptRate(float r);
138 
141  double getScriptRate();
142 
145  void debug(const QString& msg);
146 
149  void output(const QString& msg);
150 
152  void resetOutput(void);
153 
156  void saveOutputAs(const QString &filename);
157 
159  void pauseScript();
160 
162  void resumeScript();
163 
164 private slots:
166  void scriptEnded();
167 signals:
169  void scriptRunning();
171  void scriptStopped();
173  void scriptDebug(const QString&);
175  void scriptOutput(const QString&);
176 
177 private:
178  // Utility functions for preprocessor
179  QMap<QString, QString> mappify(const QStringList& args, bool lowerKey=false);
180  bool strToBool(const QString& str);
181 
184  void initActions();
185 
192  const QString getHeaderSingleLineCommentText(const QString& s, const QString& id, const QString& notFoundText="");
193  QScriptEngine engine;
194 
196  StelMainScriptAPI *mainAPI;
197 
198  QString scriptFileName;
199 
200  //Script engine agent
201  StelScriptEngineAgent *agent;
202 
203 };
204 
205 class StelScriptEngineAgent : public QScriptEngineAgent
206 {
207 public:
208  explicit StelScriptEngineAgent(QScriptEngine *engine);
209  virtual ~StelScriptEngineAgent() {}
210 
211  void setPauseScript(bool pause) { isPaused=pause; }
212  bool getPauseScript() { return isPaused; }
213 
214  void positionChange(qint64 scriptId, int lineNumber, int columnNumber);
215 
216 private:
217  bool isPaused;
218 
219 };
220 
221 #endif // _STELSCRIPTMGR_HPP_
const QString getLicense(const QString &s)
Gets the licensing terms for the script.
const QString getAuthor(const QString &s)
Gets the name of the script Author.
bool runScript(const QString &fileName, const QString &includePath="")
Run the script located at the given location.
void debug(const QString &msg)
cause the emission of the scriptDebug signal.
void setScriptRate(float r)
Changes the rate at which the script executes as a multiple of real time.
void scriptRunning()
Notification when a script starts running.
void scriptDebug(const QString &)
Notification of a script event - warnings, current execution line etc.
void stopScript()
Stops any running script.
bool scriptIsRunning()
Find out if a script is running.
void addModules()
Add all the StelModules into the script engine.
bool preprocessScript(const QString &input, QString &output, const QString &scriptDir)
Preprocess script, esp.
Provide script API for Stellarium global functions.
void resumeScript()
Resume a paused script.
void scriptStopped()
Notification when a script has stopped running.
void output(const QString &msg)
cause the emission of the scriptOutput signal.
void pauseScript()
Pause a running script.
Manage scripting in Stellarium.
const QString getDescription(const QString &s)
Gets a description of the script.
const QString getName(const QString &s)
Gets a single line name of the script.
QString runningScriptId()
Get the ID (filename) of the currently running script.
void resetOutput(void)
Reset output file and cause the emission of an (empty) scriptOutput signal.
void saveOutputAs(const QString &filename)
Save output file to new file (in same directory as output.txt).
bool runPreprocessedScript(const QString &preprocessedScript)
Run the preprocessed script.
void scriptOutput(const QString &)
Notification of a script event - output line.
const QString getShortcut(const QString &s)
Gets the default shortcut of the script.
double getScriptRate()
Get the rate at which the script is running as a multiple of the normal execution rate...