Stellarium 0.12.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 <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
62  bool preprocessScript(const QString& input, QString& output, const QString& scriptDir);
63  bool preprocessScript(QFile &input, QString& output, const QString& scriptDir);
64 
66  void addModules();
67 public slots:
74  const QString getName(const QString& s);
75 
82  const QString getAuthor(const QString& s);
83 
90  const QString getLicense(const QString& s);
91 
99  const QString getDescription(const QString& s);
100 
104  bool runPreprocessedScript(const QString& preprocessedScript);
105 
114  bool runScript(const QString& fileName, const QString& includePath="");
115 
118  void stopScript();
119 
125  void setScriptRate(float r);
126 
129  double getScriptRate();
130 
133  void debug(const QString& msg);
134 
136  void pauseScript();
137 
139  void resumeScript();
140 
141 private slots:
143  void scriptEnded();
144 signals:
146  void scriptRunning();
148  void scriptStopped();
150  void scriptDebug(const QString&);
151 
152 private:
153  // Utility functions for preprocessor
154  QMap<QString, QString> mappify(const QStringList& args, bool lowerKey=false);
155  bool strToBool(const QString& str);
156 
157 #ifdef ENABLE_STRATOSCRIPT_COMPAT
158  bool preprocessStratoScript(QFile& input, QString& output, const QString& scriptDir);
159 #endif
160 
167  const QString getHeaderSingleLineCommentText(const QString& s, const QString& id, const QString& notFoundText="");
168  QScriptEngine engine;
169 
171  StelMainScriptAPI *mainAPI;
172 
173  QString scriptFileName;
174 
175  //Script engine agent
176  StelScriptEngineAgent *agent;
177 
178 };
179 
180 class StelScriptEngineAgent : public QScriptEngineAgent
181 {
182 public:
183  explicit StelScriptEngineAgent(QScriptEngine *engine);
184  virtual ~StelScriptEngineAgent() {}
185 
186  void setPauseScript(bool pause) { isPaused=pause; }
187  bool getPauseScript() { return isPaused; }
188 
189  void positionChange(qint64 scriptId, int lineNumber, int columnNumber);
190 
191 private:
192  bool isPaused;
193 
194 };
195 
196 #endif // _STELSCRIPTMGR_HPP_