Stellarium 0.90.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  Q_PROPERTY(QString runningScriptId READ runningScriptId NOTIFY runningScriptIdChanged)
44 
45 #ifdef ENABLE_SCRIPT_CONSOLE
46 friend class ScriptConsole;
47 #endif
48 
49 public:
50  StelScriptMgr(QObject *parent=0);
51  ~StelScriptMgr();
52 
53  QStringList getScriptList();
54 
57  bool scriptIsRunning();
61  QString runningScriptId();
62 
63  // Pre-processor functions
68  bool preprocessScript(const QString& input, QString& output, const QString& scriptDir);
69  bool preprocessScript(QFile &input, QString& output, const QString& scriptDir);
70 
72  void addModules();
73 public slots:
78  QString getHtmlDescription(const QString& s, bool generateDocumentTags=true) const;
79 
86  QString getName(const QString& s) const;
87 
94  QString getAuthor(const QString& s) const;
95 
102  QString getLicense(const QString& s) const;
103 
111  QString getDescription(const QString& s) const;
112 
119  QString getShortcut(const QString& s) const;
120 
130  bool runScript(const QString& fileName, const QString& includePath="");
131 
139  bool runScriptDirect(const QString& scriptCode, const QString &includePath = QString());
140 
147  bool runPreprocessedScript(const QString& preprocessedScript, const QString &scriptId);
148 
160  bool prepareScript(QString& script, const QString& fileName, const QString& includePath="");
161 
164  void stopScript();
165 
171  void setScriptRate(float r);
172 
175  double getScriptRate();
176 
179  void debug(const QString& msg);
180 
183  void output(const QString& msg);
184 
186  void resetOutput(void);
187 
190  void saveOutputAs(const QString &filename);
191 
193  void pauseScript();
194 
196  void resumeScript();
197 
198 private slots:
200  void scriptEnded();
201 signals:
203  void runningScriptIdChanged(const QString& id);
205  void scriptRunning();
207  void scriptStopped();
209  void scriptDebug(const QString&) const;
211  void scriptOutput(const QString&) const;
212 
213 private:
214  // Utility functions for preprocessor
215  QMap<QString, QString> mappify(const QStringList& args, bool lowerKey=false);
216  bool strToBool(const QString& str);
217 
220  void initActions();
221 
228  QString getHeaderSingleLineCommentText(const QString& s, const QString& id, const QString& notFoundText="") const;
229  QScriptEngine engine;
230 
232  StelMainScriptAPI *mainAPI;
233 
234  QString scriptFileName;
235 
236  //Script engine agent
237  StelScriptEngineAgent *agent;
238 
239 };
240 
241 class StelScriptEngineAgent : public QScriptEngineAgent
242 {
243 public:
244  explicit StelScriptEngineAgent(QScriptEngine *engine);
245  virtual ~StelScriptEngineAgent() {}
246 
247  void setPauseScript(bool pause) { isPaused=pause; }
248  bool getPauseScript() { return isPaused; }
249 
250  void positionChange(qint64 scriptId, int lineNumber, int columnNumber);
251 
252 private:
253  bool isPaused;
254 
255 };
256 
257 #endif // _STELSCRIPTMGR_HPP_
void scriptOutput(const QString &) const
Notification of a script event - output line.
QString getName(const QString &s) const
Gets a single line name of the script.
bool runScript(const QString &fileName, const QString &includePath="")
Run the script located in the given file.
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 runningScriptIdChanged(const QString &id)
Emitted when the running script id changes (also on start/stop)
void scriptDebug(const QString &) const
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.
QString getHtmlDescription(const QString &s, bool generateDocumentTags=true) const
Returns a HTML description of the specified script.
Provide script API for Stellarium global functions.
void resumeScript()
Resume a paused script.
QString getAuthor(const QString &s) const
Gets the name of the script Author.
bool runPreprocessedScript(const QString &preprocessedScript, const QString &scriptId)
Runs preprocessed script code which has been generated using runPreprocessedScript().
void scriptStopped()
Notification when a script has stopped running.
QString getShortcut(const QString &s) const
Gets the default shortcut of the script.
void output(const QString &msg)
cause the emission of the scriptOutput signal.
void pauseScript()
Pause a running script.
QString getLicense(const QString &s) const
Gets the licensing terms for the script.
Manage scripting in Stellarium.
bool runScriptDirect(const QString &scriptCode, const QString &includePath=QString())
Runs the script code given.
QString runningScriptId()
Get the ID (usually filename) of the currently running script.
QString getDescription(const QString &s) const
Gets a description of the 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 prepareScript(QString &script, const QString &fileName, const QString &includePath="")
Loads a script file and does all preparatory steps except for actually executing the script in the en...
double getScriptRate()
Get the rate at which the script is running as a multiple of the normal execution rate...