1 /*
2  * Stellarium
3  * Copyright (C) 2006 Fabien Chereau
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 STELAPP_HPP
21 #define STELAPP_HPP
22 
23 #include <QString>
24 #include <QObject>
25 #include "StelModule.hpp"
26 #include "VecMath.hpp"
27 
28 // Predeclaration of some classes
29 class StelCore;
30 class StelTextureMgr;
31 class StelObjectMgr;
32 class StelLocaleMgr;
33 class StelModuleMgr;
34 class StelMainView;
35 class StelSkyCultureMgr;
36 class StelViewportEffect;
37 class QOpenGLFramebufferObject;
38 class QOpenGLFunctions;
39 class QSettings;
40 class QNetworkAccessManager;
41 class QNetworkReply;
42 class QTimer;
43 class StelLocationMgr;
44 class StelSkyLayerMgr;
45 class StelAudioMgr;
46 class StelVideoMgr;
47 class StelGuiBase;
48 class StelMainScriptAPIProxy;
49 class StelScriptMgr;
50 class StelActionMgr;
51 class StelPropertyMgr;
52 class StelProgressController;
53 
54 #ifdef 	ENABLE_SPOUT
55 class SpoutSender;
56 #endif
57 
58 //! @class StelApp
59 //! Singleton main Stellarium application class.
60 //! This is the central class of Stellarium.  Only one singleton instance of
61 //! this class is created and can be accessed from anywhere else.  This class
62 //! is the access point to several "Manager" class which provide application-wide
63 //! services for managment of font, textures, localization, sky culture, and in
64 //! theory all other services used by the other part of the program.
65 //!
66 //! The StelApp class is also the one managing the StelModule in a generic manner
67 //! by calling their update, drawing and other methods when needed.
68 //! @author Fabien Chereau
69 class StelApp : public QObject
70 {
71 	Q_OBJECT
72 	Q_PROPERTY(bool nightMode READ getVisionModeNight WRITE setVisionModeNight NOTIFY visionNightModeChanged)
73 	Q_PROPERTY(bool flagShowDecimalDegrees  READ getFlagShowDecimalDegrees  WRITE setFlagShowDecimalDegrees  NOTIFY flagShowDecimalDegreesChanged)
74 	Q_PROPERTY(bool flagUseAzimuthFromSouth READ getFlagSouthAzimuthUsage   WRITE setFlagSouthAzimuthUsage   NOTIFY flagUseAzimuthFromSouthChanged)
75 	Q_PROPERTY(bool flagUseCCSDesignation   READ getFlagUseCCSDesignation   WRITE setFlagUseCCSDesignation   NOTIFY flagUseCCSDesignationChanged)
76 	Q_PROPERTY(bool flagUseFormattingOutput READ getFlagUseFormattingOutput WRITE setFlagUseFormattingOutput NOTIFY flagUseFormattingOutputChanged)
77 	Q_PROPERTY(bool flagOverwriteInfoColor  READ getFlagOverwriteInfoColor  WRITE setFlagOverwriteInfoColor  NOTIFY flagOverwriteInfoColorChanged)
78 	Q_PROPERTY(Vec3f overwriteInfoColor	READ getOverwriteInfoColor	WRITE setOverwriteInfoColor	 NOTIFY overwriteInfoColorChanged)
79 	Q_PROPERTY(Vec3f daylightInfoColor	READ getDaylightInfoColor	WRITE setDaylightInfoColor	 NOTIFY daylightInfoColorChanged)
80 	Q_PROPERTY(int  screenFontSize          READ getScreenFontSize          WRITE setScreenFontSize          NOTIFY screenFontSizeChanged)
81 	Q_PROPERTY(int  guiFontSize             READ getGuiFontSize             WRITE setGuiFontSize             NOTIFY guiFontSizeChanged)
82 
83 	Q_PROPERTY(QString version READ getVersion)
84 
85 public:
86 	friend class StelAppGraphicsWidget;
87 	friend class StelRootItem;
88 
89 	//! Create and initialize the main Stellarium application.
90 	//! @param parent the QObject parent
91 	//! The configFile will be searched for in the search path by the StelFileMgr,
92 	//! it is therefore possible to specify either just a file name or path within the
93 	//! search path, or use a full path or even a relative path to an existing file
94 	StelApp(StelMainView* parent);
95 
96 	//! Deinitialize and destroy the main Stellarium application.
97 	virtual ~StelApp();
98 
99 	//! Initialize core and all the modules.
100 	void init(QSettings* conf);
101 	//! Deinitialize core and all the modules.
102 	void deinit();
103 
104 	//! Load and initialize external modules (plugins)
105 	void initPlugIns();
106 
107 	//! Registers all loaded StelModules with the ScriptMgr, and queues starting of the startup script.
108 	void initScriptMgr();
109 
110 	//! Returns all arguments passed on the command line, together with the contents of the STEL_OPTS environment variable.
111 	//! You can use the CLIProcessor class to help parse it.
112 	//! @return the arguments passed to Stellarium on the command line concatenated with the STEL_OPTS environment variable
113 	static QStringList getCommandlineArguments();
114 
115 	//! Get the StelApp singleton instance.
116 	//! @return the StelApp singleton instance
getInstance()117 	static StelApp& getInstance() {Q_ASSERT(singleton); return *singleton;}
118 
119 	//! Get the module manager to use for accessing any module loaded in the application.
120 	//! @return the module manager.
getModuleMgr() const121 	StelModuleMgr& getModuleMgr() const {return *moduleMgr;}
122 
123 	//! Get the corresponding module or Q_NULLPTR if can't find it.
124 	//! This is a shortcut for getModleMgr().getModule().
125 	//! @return the module pointer.
126 	StelModule* getModule(const QString& moduleID) const;
127 
128 	//! Get the locale manager to use for i18n & date/time localization.
129 	//! @return the font manager to use for loading fonts.
getLocaleMgr() const130 	StelLocaleMgr& getLocaleMgr() const {return *localeMgr;}
131 
132 	//! Get the sky cultures manager.
133 	//! @return the sky cultures manager
getSkyCultureMgr() const134 	StelSkyCultureMgr& getSkyCultureMgr() const {return *skyCultureMgr;}
135 
136 	//! Get the texture manager to use for loading textures.
137 	//! @return the texture manager to use for loading textures.
getTextureManager() const138 	StelTextureMgr& getTextureManager() const {return *textureMgr;}
139 
140 	//! Get the Location manager to use for managing stored locations
141 	//! @return the Location manager to use for managing stored locations
getLocationMgr() const142 	StelLocationMgr& getLocationMgr() const {return *planetLocationMgr;}
143 
144 	//! Get the StelObject manager to use for querying from all stellarium objects.
145 	//! @return the StelObject manager to use for querying from all stellarium objects 	.
getStelObjectMgr() const146 	StelObjectMgr& getStelObjectMgr() const {return *stelObjectMgr;}
147 
getSkyImageMgr() const148 	StelSkyLayerMgr& getSkyImageMgr() const {return *skyImageMgr;}
149 
150 	//! Get the audio manager
getStelAudioMgr() const151 	StelAudioMgr* getStelAudioMgr() const {return audioMgr;}
152 
153 	//! Get the actions manager to use for managing and editing actions
getStelActionManager() const154 	StelActionMgr* getStelActionManager() const {return actionMgr;}
155 
156 	//! Return the property manager
getStelPropertyManager() const157 	StelPropertyMgr* getStelPropertyManager() const {return propMgr;}
158 
159 	//! Get the video manager
getStelVideoMgr() const160 	StelVideoMgr* getStelVideoMgr() const {return videoMgr;}
161 
162 	//! Get the core of the program.
163 	//! It is the one which provide the projection, navigation and tone converter.
164 	//! @return the StelCore instance of the program
getCore() const165 	StelCore* getCore() const {return core;}
166 
167 	//! Get the common instance of QNetworkAccessManager used in stellarium
getNetworkAccessManager() const168 	QNetworkAccessManager* getNetworkAccessManager() const {return networkAccessManager;}
169 
170 	//! Update translations, font for GUI and sky everywhere in the program.
171 	void updateI18n();
172 
173 	//! Return the main configuration options
getSettings() const174 	QSettings* getSettings() const {return confSettings;}
175 
176 	//! Return the currently used style
getCurrentStelStyle() const177 	const QString getCurrentStelStyle() const {return "color";}
178 
179 	//! Update all object according to the deltaTime in seconds.
180 	void update(double deltaTime);
181 
182 	//! Draw all registered StelModule in the order defined by the order lists.
183 	// 2014-11: OLD COMMENT? What does a void return?
184 	// @return the max squared distance in pixels that any object has travelled since the last update.
185 	void draw();
186 
187 	//! Get the ratio between real device pixel and "Device Independent Pixel".
188 	//! Usually this value is 1, but for a mac with retina screen this will be value 2.
getDevicePixelsPerPixel() const189 	qreal getDevicePixelsPerPixel() const {return devicePixelsPerPixel;}
190 	void setDevicePixelsPerPixel(qreal dppp);
191 
192 	//! Get the scaling ratio to apply on all display elements, like GUI, text etc..
193 	//! When this ratio is 1, all pixel sizes used in Stellarium will look OK on a regular
194 	//! computer screen with 96 pixel per inch (reference for tuning sizes).
getGlobalScalingRatio() const195 	float getGlobalScalingRatio() const {return globalScalingRatio;}
setGlobalScalingRatio(float r)196 	void setGlobalScalingRatio(float r) {globalScalingRatio=r;}
197 
198 	//! Get the fontsize used for screen text.
getScreenFontSize() const199 	int getScreenFontSize() const { return screenFontSize; }
200 	//! Change screen font size.
201 	void setScreenFontSize(int s);
202 	//! Get the principal font size used for GUI panels.
203 	int getGuiFontSize() const;
204 	//! change GUI font size.
205 	void setGuiFontSize(int s);
206 
207 	//! Get the GUI instance implementing the abstract GUI interface.
getGui() const208 	StelGuiBase* getGui() const {return stelGui;}
209 	//! Tell the StelApp instance which GUI is currently being used.
210 	//! The caller is responsible for destroying the GUI.
setGui(StelGuiBase * b)211 	void setGui(StelGuiBase* b) {stelGui=b;}
212 
213 #ifdef ENABLE_SCRIPTING
214 	//! Get the script API proxy (for signal handling)
getMainScriptAPIProxy() const215 	StelMainScriptAPIProxy* getMainScriptAPIProxy() const {return scriptAPIProxy;}
216 	//! Get the script manager
getScriptMgr() const217 	StelScriptMgr& getScriptMgr() const {return *scriptMgr;}
218 #endif
219 
220 	static void initStatic();
221 	static void deinitStatic();
222 
223 	//! Add a progression indicator to the GUI (if applicable).
224 	//! @return a controller which can be used to indicate the current status.
225 	//! The StelApp instance remains the owner of the controller.
226 	StelProgressController* addProgressBar();
227 	void removeProgressBar(StelProgressController* p);
228 
229 	//! Define the type of viewport effect to use
230 	//! @param effectName must be one of 'none', 'framebufferOnly', 'sphericMirrorDistorter'
231 	void setViewportEffect(const QString& effectName);
232 	//! Get the type of viewport effect currently used
233 	QString getViewportEffect() const;
234 
235 	//! Dump diagnostics about action call priorities
236 	void dumpModuleActionPriorities(StelModule::StelModuleActionName actionName) const;
237 
238 	///////////////////////////////////////////////////////////////////////////
239 	// Scriptable methods
240 public slots:
241 	//! Call this when the size of the GL window has changed.
242 	void glWindowHasBeenResized(const QRectF &rect);
243 
244 	//! Set flag for activating night vision mode.
245 	void setVisionModeNight(bool);
246 	//! Get flag for activating night vision mode.
getVisionModeNight() const247 	bool getVisionModeNight() const {return flagNightVision;}
248 
249 	//! Set flag for activating overwrite mode for text color in info panel.
250 	void setFlagOverwriteInfoColor(bool);
251 	//! Get flag for activating overwrite mode for text color in info panel.
getFlagOverwriteInfoColor() const252 	bool getFlagOverwriteInfoColor() const {return flagOverwriteInfoColor; }
253 
254 	//! Set flag for showing decimal degree in various places.
255 	void setFlagShowDecimalDegrees(bool b);
256 	//! Get flag for showing decimal degree in various places.
getFlagShowDecimalDegrees() const257 	bool getFlagShowDecimalDegrees() const {return flagShowDecimalDegrees;}
258 
259 	//! Set flag for using calculation of azimuth from south towards west (instead north towards east)
getFlagSouthAzimuthUsage() const260 	bool getFlagSouthAzimuthUsage() const { return flagUseAzimuthFromSouth; }
261 	//! Get flag for using calculation of azimuth from south towards west (instead north towards east)
setFlagSouthAzimuthUsage(bool use)262 	void setFlagSouthAzimuthUsage(bool use) { flagUseAzimuthFromSouth=use; emit flagUseAzimuthFromSouthChanged(use);}
263 
264 	//! Set flag for using of formatting output for coordinates
265 	void setFlagUseFormattingOutput(bool b);
266 	//! Get flag for using of formatting output for coordinates
getFlagUseFormattingOutput() const267 	bool getFlagUseFormattingOutput() const {return flagUseFormattingOutput;}
268 
269 	//! Set flag for using designations for celestial coordinate systems
270 	void setFlagUseCCSDesignation(bool b);
271 	//! Get flag for using designations for celestial coordinate systems
getFlagUseCCSDesignation() const272 	bool getFlagUseCCSDesignation() const {return flagUseCCSDesignation;}
273 
274 	//! Define info text color for overwrites
275 	void setOverwriteInfoColor(const Vec3f& color);
276 	//! Get info text color
277 	Vec3f getOverwriteInfoColor() const;
278 
279 	//! Define info text color for daylight mode
280 	void setDaylightInfoColor(const Vec3f& color);
281 	//! Get info text color
282 	Vec3f getDaylightInfoColor() const;
283 
284 	//! Get the current number of frame per second.
285 	//! @return the FPS averaged on the last second
getFps() const286 	float getFps() const {return fps;}
287 
288 	//! Set global application font.
289 	//! To retrieve, you can use QGuiApplication::font().
290 	//! emits fontChanged(font)
291 	void setAppFont(QFont font);
292 
293 	//! Returns the default FBO handle, to be used when StelModule instances want to release their own FBOs.
294 	//! Note that this is usually not the same as QOpenGLContext::defaultFramebufferObject(),
295 	//! so use this call instead of the Qt version!
296 	//! Valid through a StelModule::draw() call, do not use elsewhere.
getDefaultFBO() const297 	quint32 getDefaultFBO() const { return currentFbo; }
298 
299 	//! Makes sure the correct GL context used for main drawing is made current.
300 	//! This is always the case during init() and draw() calls, but if OpenGL access is required elsewhere,
301 	//! this MUST be called before using any GL functions.
302 	void ensureGLContextCurrent();
303 
304 	//! Return the time since when stellarium is running in second.
305 	static double getTotalRunTime();
306 
307 	//! Return the scaled time for animated objects
308 	static double getAnimationTime();
309 
310 	//! Report that a download occured. This is used for statistics purposes.
311 	//! Connect this slot to QNetworkAccessManager::finished() slot to obtain statistics at the end of the program.
312 	void reportFileDownloadFinished(QNetworkReply* reply);
313 
314 	//! do some cleanup and call QCoreApplication::exit(0)
315 	void quit();
316 signals:
317 	void visionNightModeChanged(bool);
318 	void flagShowDecimalDegreesChanged(bool);
319 	void flagUseAzimuthFromSouthChanged(bool);
320 	void flagUseCCSDesignationChanged(bool);
321 	void flagUseFormattingOutputChanged(bool);
322 	void flagOverwriteInfoColorChanged(bool);
323 	void colorSchemeChanged(const QString&);
324 	void languageChanged();
325 	void screenFontSizeChanged(int);
326 	void guiFontSizeChanged(int);
327 	void fontChanged(QFont);
328 	void overwriteInfoColorChanged(const Vec3f & color);
329 	void daylightInfoColorChanged(const Vec3f & color);
330 
331 	//! Called just after a progress bar is added.
332 	void progressBarAdded(const StelProgressController*);
333 	//! Called just before a progress bar is removed.
334 	void progressBarRemoved(const StelProgressController*);
335 	//! Called just before we exit Qt mainloop.
336 	void aboutToQuit();
337 
338 private:
339 	//! Handle mouse clics.
340 	void handleClick(class QMouseEvent* event);
341 	//! Handle mouse wheel.
342 	void handleWheel(class QWheelEvent* event);
343 	//! Handle mouse move.
344 	bool handleMove(qreal x, qreal y, Qt::MouseButtons b);
345 	//! Handle key press and release.
346 	void handleKeys(class QKeyEvent* event);
347 	//! Handle pinch on multi touch devices.
348 	void handlePinch(qreal scale, bool started);
349 
350 	//! Used internally to set the viewport effects.
351 	void prepareRenderBuffer();
352 	//! Used internally to set the viewport effects.
353 	//! @param drawFbo the OpenGL fbo we need to render into.
354 	void applyRenderBuffer(quint32 drawFbo=0);
355 
356 	QString getVersion() const;
357 
358 	// The StelApp singleton
359 	static StelApp* singleton;
360 
361 	//! The main window which is the parent of this object
362 	StelMainView* mainWin;
363 
364 	// The associated StelCore instance
365 	StelCore* core;
366 
367 	// Module manager for the application
368 	StelModuleMgr* moduleMgr;
369 
370 	// Locale manager for the application
371 	StelLocaleMgr* localeMgr;
372 
373 	// Sky cultures manager for the application
374 	StelSkyCultureMgr* skyCultureMgr;
375 
376 	//Actions manager fot the application.  Will replace shortcutMgr.
377 	StelActionMgr* actionMgr;
378 
379 	//Property manager for the application
380 	StelPropertyMgr* propMgr;
381 
382 	// Textures manager for the application
383 	StelTextureMgr* textureMgr;
384 
385 	// Manager for all the StelObjects of the program
386 	StelObjectMgr* stelObjectMgr;
387 
388 	// Manager for the list of observer locations on planets
389 	StelLocationMgr* planetLocationMgr;
390 
391 	// Main network manager used for the program
392 	QNetworkAccessManager* networkAccessManager;
393 
394 	//! Get proxy settings from config file... if not set use http_proxy env var
395 	void setupNetworkProxy();
396 
397 	// The audio manager.  Must execute in the main thread.
398 	StelAudioMgr* audioMgr;
399 
400 	// The video manager.  Must execute in the main thread.
401 	StelVideoMgr* videoMgr;
402 
403 	StelSkyLayerMgr* skyImageMgr;
404 
405 #ifdef ENABLE_SCRIPTING
406 	// The script API proxy object (for bridging threads)
407 	StelMainScriptAPIProxy* scriptAPIProxy;
408 
409 	// The script manager based on Qt script engine
410 	StelScriptMgr* scriptMgr;
411 #endif
412 
413 	StelGuiBase* stelGui;
414 
415 	// Store the ratio between real device pixel in "Device Independent Pixel"
416 	// Usually this value is 1, but for a mac with retina screen this will be value 2.
417 	qreal devicePixelsPerPixel;
418 
419 	// The scaling ratio to apply on all display elements, like GUI, text etc..
420 	float globalScalingRatio;
421 
422 	float fps;
423 	int frame;
424 	double frameTimeAccum;		// Used for fps counter
425 
426 	//! Define whether we are in night vision mode
427 	bool flagNightVision;
428 
429 	QSettings* confSettings;
430 
431 	// Define whether the StelApp instance has completed initialization
432 	bool initialized;
433 
434 	static qint64 startMSecs;
435 	static double animationScale;
436 
437 	// Temporary variables used to store the last gl window resize
438 	// if the core was not yet initialized
439 	qreal saveProjW;
440 	qreal saveProjH;
441 
442 	//! Store the number of downloaded files for statistics.
443 	int nbDownloadedFiles;
444 	//! Store the summed size of all downloaded files in bytes.
445 	qint64 totalDownloadedSize;
446 
447 	//! Store the number of downloaded files read from the cache for statistics.
448 	int nbUsedCache;
449 	//! Store the summed size of all downloaded files read from the cache in bytes.
450 	qint64 totalUsedCacheSize;
451 
452 	QList<StelProgressController*> progressControllers;
453 
454 	int screenFontSize;
455 
456 	// Framebuffer object used for viewport effects.
457 	QOpenGLFramebufferObject* renderBuffer;
458 	StelViewportEffect* viewportEffect;
459 	QOpenGLFunctions* gl;
460 
461 	bool flagShowDecimalDegrees;  // Format infotext with decimal degrees, not minutes/seconds
462 	bool flagUseAzimuthFromSouth; // Display calculate azimuth from south towards west (as in some astronomical literature)
463 	bool flagUseFormattingOutput; // Use tabular coordinate format for infotext
464 	bool flagUseCCSDesignation;   // Use symbols like alpha (RA), delta (declination) for coordinate system labels
465 	bool flagOverwriteInfoColor; // Overwrite and use color for text in info panel
466 	Vec3f overwriteInfoColor;
467 	Vec3f daylightInfoColor;
468 #ifdef 	ENABLE_SPOUT
469 	SpoutSender* spoutSender;
470 #endif
471 
472 	// The current main FBO/render target handle, without requiring GL queries. Valid through a draw() call
473 	quint32 currentFbo;
474 };
475 
476 #endif // STELAPP_HPP
477