Stellarium  0.16.1
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=Q_NULLPTR);
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 
109  QString getVersion(const QString& s) const;
110 
118  QString getDescription(const QString& s) const;
119 
126  QString getShortcut(const QString& s) const;
127 
137  bool runScript(const QString& fileName, const QString& includePath="");
138 
146  bool runScriptDirect(const QString& scriptCode, const QString &includePath = QString());
147 
154  bool runPreprocessedScript(const QString& preprocessedScript, const QString &scriptId);
155 
167  bool prepareScript(QString& script, const QString& fileName, const QString& includePath="");
168 
171  void stopScript();
172 
178  void setScriptRate(float r);
179 
182  double getScriptRate();
183 
186  void debug(const QString& msg);
187 
190  void output(const QString& msg);
191 
193  void resetOutput(void);
194 
197  void saveOutputAs(const QString &filename);
198 
200  void pauseScript();
201 
203  void resumeScript();
204 
205 private slots:
207  void scriptEnded();
208 signals:
210  void runningScriptIdChanged(const QString& id);
212  void scriptRunning();
214  void scriptStopped();
216  void scriptDebug(const QString&) const;
218  void scriptOutput(const QString&) const;
219 
220 private:
221  // Utility functions for preprocessor
222  QMap<QString, QString> mappify(const QStringList& args, bool lowerKey=false);
223  bool strToBool(const QString& str);
224 
227  void initActions();
228 
235  QString getHeaderSingleLineCommentText(const QString& s, const QString& id, const QString& notFoundText="") const;
236  QScriptEngine* engine;
237 
239  StelMainScriptAPI *mainAPI;
240 
241  QString scriptFileName;
242 
243  //Script engine agent
244  StelScriptEngineAgent *agent;
245 };
246 
247 #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.
QString getVersion(const QString &s) const
Gets the version 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...