Stellarium 0.15.2
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 <QStringList>
25 #include <QFile>
26 #include <QTime>
27 #include <QTimer>
28 
29 class StelMainScriptAPI;
30 class StelScriptEngineAgent;
31 class QScriptEngine;
32 
33 #ifdef ENABLE_SCRIPT_CONSOLE
34 class ScriptConsole;
35 #endif
36 
38 class StelScriptMgr : public QObject
39 {
40  Q_OBJECT
41 
42  Q_PROPERTY(QString runningScriptId READ runningScriptId NOTIFY runningScriptIdChanged)
43 
44 #ifdef ENABLE_SCRIPT_CONSOLE
45 friend class ScriptConsole;
46 #endif
47 
48 public:
49  StelScriptMgr(QObject *parent=0);
50  ~StelScriptMgr();
51 
52  QStringList getScriptList();
53 
56  bool scriptIsRunning();
60  QString runningScriptId();
61 
62  // Pre-processor functions
67  bool preprocessScript(const QString& input, QString& output, const QString& scriptDir);
68  bool preprocessScript(QFile &input, QString& output, const QString& scriptDir);
69 
71  void addModules();
72 public slots:
77  QString getHtmlDescription(const QString& s, bool generateDocumentTags=true) const;
78 
85  QString getName(const QString& s) const;
86 
93  QString getAuthor(const QString& s) const;
94 
101  QString getLicense(const QString& s) const;
102 
110  QString getDescription(const QString& s) const;
111 
118  QString getShortcut(const QString& s) const;
119 
129  bool runScript(const QString& fileName, const QString& includePath="");
130 
138  bool runScriptDirect(const QString& scriptCode, const QString &includePath = QString());
139 
146  bool runPreprocessedScript(const QString& preprocessedScript, const QString &scriptId);
147 
159  bool prepareScript(QString& script, const QString& fileName, const QString& includePath="");
160 
163  void stopScript();
164 
170  void setScriptRate(float r);
171 
174  double getScriptRate();
175 
178  void debug(const QString& msg);
179 
182  void output(const QString& msg);
183 
185  void resetOutput(void);
186 
189  void saveOutputAs(const QString &filename);
190 
192  void pauseScript();
193 
195  void resumeScript();
196 
197 private slots:
199  void scriptEnded();
200 signals:
202  void runningScriptIdChanged(const QString& id);
204  void scriptRunning();
206  void scriptStopped();
208  void scriptDebug(const QString&) const;
210  void scriptOutput(const QString&) const;
211 
212 private:
213  // Utility functions for preprocessor
214  QMap<QString, QString> mappify(const QStringList& args, bool lowerKey=false);
215  bool strToBool(const QString& str);
216 
219  void initActions();
220 
227  QString getHeaderSingleLineCommentText(const QString& s, const QString& id, const QString& notFoundText="") const;
228  QScriptEngine* engine;
229 
231  StelMainScriptAPI *mainAPI;
232 
233  QString scriptFileName;
234 
235  //Script engine agent
236  StelScriptEngineAgent *agent;
237 };
238 
239 #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...