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 STELMODULE_HPP
21 #define STELMODULE_HPP
22 
23 #include <QString>
24 #include <QObject>
25 #include <functional>
26 
27 // Predeclaration
28 class StelCore;
29 class QSettings;
30 
31 //! This is the common base class for all the main components of stellarium.
32 //! Standard StelModules are the one displaying the stars, the constellations, the planets etc..
33 //! Every new module derived class should implement the methods defined in StelModule.hpp.
34 //! Once a module is registered into the StelModuleMgr, it is automatically managed by the program.
35 //! It can be called using the GETSTELMODULE macro, passing as argument its name, which is also the QObject name
36 //! Because StelModules are very generic components, it is also possible to load them dynamically,
37 //! thus enabling creation of external plug-ins for stellarium.
38 //! @sa StelObjectModule for StelModule managing collections of StelObject.
39 //! @sa @ref plugins for documentation on how to develop external plugins.
40 //!
41 //! There are several signals that StelApp emits that subclasses may be interested in:
42 //! laguageChanged()
43 //!	Update i18n strings from english names according to current global sky and application language.
44 //!	This method also reload the proper fonts depending on the language.
45 //!	The translation shall be done using the StelTranslator provided by the StelApp singleton instance.
46 //! skyCultureChanged(const QString&)
47 //!	Update sky culture, i.e. load data if necessary and translate them to current sky language if needed.
48 //! colorSchemeChanged(const QString&)
49 //!	Load the given color style
50 class StelModule : public QObject
51 {
52 	Q_OBJECT
53 	// Do not add Q_OBJECT here!!
54 	// This make this class compiled by the Qt moc compiler and for some unknown reasons makes it impossible to dynamically
55 	// load plugins on windows.
56 
57 public:
58 	StelModule();
59 
~StelModule()60 	virtual ~StelModule() Q_DECL_OVERRIDE {;}
61 
62 	//! Initialize itself.
63 	//! If the initialization takes significant time, the progress should be displayed on the loading bar.
64 	virtual void init() = 0;
65 
66 	//! Called before the module will be delete, and before the openGL context is suppressed.
67 	//! Deinitialize all openGL texture in this method.
deinit()68 	virtual void deinit() {;}
69 
70 	//! Return module-specific settings. This can be useful mostly by plugins which may want to keep their settings to their own files.
71 	//! The default implementation returns a null pointer!
getSettings()72 	virtual QSettings *getSettings() {return Q_NULLPTR;}
73 
74 	//! Execute all the drawing functions for this module.
75 	//! @param core the core to use for the drawing
draw(StelCore * core)76 	virtual void draw(StelCore* core) {Q_UNUSED(core);}
77 
78 	//! Update the module with respect to the time.
79 	//! @param deltaTime the time increment in second since last call.
80 	virtual void update(double deltaTime) = 0;
81 
82 	//! Get the version of the module, default is stellarium main version
83 	virtual QString getModuleVersion() const;
84 
85 	//! Get the name of the module author
getAuthorName() const86 	virtual QString getAuthorName() const {return "Stellarium's Team";}
87 
88 	//! Get the email adress of the module author
getAuthorEmail() const89 	virtual QString getAuthorEmail() const {return "stellarium@googlegroups.com";}
90 
91 	//! Handle mouse clicks. Please note that most of the interactions will be done through the GUI module.
92 	//! @return set the event as accepted if it was intercepted
handleMouseClicks(class QMouseEvent *)93 	virtual void handleMouseClicks(class QMouseEvent*) {;}
94 
95 	//! Handle mouse wheel. Please note that most of the interactions will be done through the GUI module.
96 	//! @return set the event as accepted if it was intercepted
handleMouseWheel(class QWheelEvent *)97 	virtual void handleMouseWheel(class QWheelEvent*) {;}
98 
99 	//! Handle mouse moves. Please note that most of the interactions will be done through the GUI module.
100 	//! @return true if the event was intercepted
handleMouseMoves(int x,int y,Qt::MouseButtons b)101 	virtual bool handleMouseMoves(int x, int y, Qt::MouseButtons b) {Q_UNUSED(x); Q_UNUSED(y); Q_UNUSED(b); return false;}
102 
103 	//! Handle key events. Please note that most of the interactions will be done through the GUI module.
104 	//! @param e the Key event
105 	//! @return set the event as accepted if it was intercepted
handleKeys(class QKeyEvent * e)106 	virtual void handleKeys(class QKeyEvent* e) {Q_UNUSED(e);}
107 
108 	//! Handle pinch gesture events.
109 	//! @param scale the value of the pinch gesture.
110 	//! @param started define whether the pinch has just started.
111 	//! @return true if the event was intercepted.
handlePinch(qreal scale,bool started)112 	virtual bool handlePinch(qreal scale, bool started) {Q_UNUSED(scale); Q_UNUSED(started); return false;}
113 
114 	//! Enum used when selecting objects to define whether to add to, replace, or remove from the existing selection list.
115 	enum StelModuleSelectAction
116 	{
117 		AddToSelection,		//!< Add the StelObject to the current list of selected ones.
118 		ReplaceSelection,	//!< Set the StelObject as the new list of selected ones.
119 		RemoveFromSelection	//!< Subtract the StelObject from the current list of selected ones.
120 	};
121 	Q_ENUM(StelModuleSelectAction)
122 	//! Define the possible action for which an order is defined
123 	enum StelModuleActionName
124 	{
125 		ActionDraw,              //!< Action associated to the draw() method
126 		ActionUpdate,            //!< Action associated to the update() method
127 		ActionHandleMouseClicks, //!< Action associated to the handleMouseClicks() method
128 		ActionHandleMouseMoves,  //!< Action associated to the handleMouseMoves() method
129 		ActionHandleKeys         //!< Action associated to the handleKeys() method
130 	};
Q_ENUM(StelModuleActionName)131 	Q_ENUM(StelModuleActionName)
132 	//! Return the value defining the order of call for the given action
133 	//! For example if stars.callOrder[ActionDraw] == 10 and constellation.callOrder[ActionDraw] == 11,
134 	//! the stars module will be drawn before the constellations
135 	//! @param actionName the name of the action for which we want the call order
136 	//! @return the value defining the order. The closer to 0 the earlier the module's action will be called
137 	virtual double getCallOrder(StelModuleActionName actionName) const {Q_UNUSED(actionName); return 0;}
138 
139 	//! Detect or show the configuration GUI elements for the module.  This is to be used with
140 	//! plugins to display a configuration dialog from the plugin list window.
141 	//! @param show if true, make the configuration GUI visible.  If false, hide the config GUI if there is one.
142 	//! @return true if the module has a configuration GUI, else false.
configureGui(bool show=true)143 	virtual bool configureGui(bool show=true) {Q_UNUSED(show); return false;}
144 
145 protected:
146 	//! convenience methods to add an action (call to slot) to the StelActionMgr object.
147 	//! @param id unique identifier. Should be called actionMy_Action. (i.e., start with "action" and then "Capitalize_Underscore" style.)
148 	//! @param groupId string to be used in the Help menu. The action will be listed in this group.
149 	//! @param text short translatable description what the action does.
150 	//! @param target recipient of the call
151 	//! @param slot name of slot in target recipient
152 	//! @param shortcut default shortcut. Can be reconfigured.
153 	//! @param altShortcut default alternative shortcut. Can be reconfigured.
154 	class StelAction* addAction(const QString& id, const QString& groupId, const QString& text,
155 	                            QObject* target, const char* slot,
156 	                            const QString& shortcut="", const QString& altShortcut="");
157 
158 	//! convenience methods to add an action (call to own slot) to the StelActionMgr object.
159 	//! @param id unique identifier. Should be called actionMy_Action. (i.e., start with "action" and then "Capitalize_Underscore" style.)
160 	//! @param groupId string to be used in the Help menu. The action will be listed in this group.
161 	//! @param text short translatable description what the action does.
162 	//! @param slot name of slot in target recipient
163 	//! @param shortcut default shortcut. Can be reconfigured.
164 	//! @param altShortcut default alternative shortcut. Can be reconfigured.
addAction(const QString & id,const QString & groupId,const QString & text,const char * slot,const QString & shortcut="",const QString & altShortcut="")165 	class StelAction* addAction(const QString& id, const QString& groupId, const QString& text,
166 	                            const char* slot,
167 	                            const QString& shortcut="", const QString& altShortcut="") {
168 		return addAction(id, groupId, text, this, slot, shortcut, altShortcut);
169 	}
170 
171 	//! convenience methods to add an action (call to Lambda functor) to the StelActionMgr object.
172 	//! @param id unique identifier. Should be called actionMy_Action. (i.e., start with "action" and then "Capitalize_Underscore" style.)
173 	//! @param groupId string to be used in the Help menu. The action will be listed in this group.
174 	//! @param text short translatable description what the action does.
175 	//! @param contextObject The lambda will only be called if this object exists. Use "this" in most cases.
176 	//! @param lambda a C++11 Lambda function.
177 	//! @param shortcut default shortcut. Can be reconfigured.
178 	//! @param altShortcut default alternative shortcut. Can be reconfigured.
179 	StelAction* addAction(const QString& id, const QString& groupId, const QString& text,
180 						QObject* contextObject, std::function<void()> lambda,
181 						const QString& shortcut="", const QString& altShortcut="");
182 };
183 
184 Q_DECLARE_METATYPE(StelModule::StelModuleSelectAction)
185 
186 #endif // STELMODULE_HPP
187