1 /*
2  * Copyright (C) 2012 Alexander Wolf
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
17  */
18 
19 #ifndef EXOPLANETS_HPP
20 #define EXOPLANETS_HPP
21 
22 #include "StelObjectModule.hpp"
23 #include "StelObject.hpp"
24 #include "StelFader.hpp"
25 #include "StelTextureTypes.hpp"
26 #include "Exoplanet.hpp"
27 #include <QFont>
28 #include <QVariantMap>
29 #include <QDateTime>
30 #include <QList>
31 #include <QSharedPointer>
32 #include <QNetworkAccessManager>
33 #include <QNetworkReply>
34 
35 class QSettings;
36 class QTimer;
37 class ExoplanetsDialog;
38 class StelPainter;
39 class StelButton;
40 
41 /*! @defgroup exoplanets Exoplanets Plug-in
42 @{
43 The %Exoplanets plugin plots the position of stars with exoplanets. Exoplanets
44 data is derived from [The Extrasolar Planets Encyclopaedia](http://exoplanet.eu/).
45 List of potential habitable exoplanets and data about them were taken from
46 [The Habitable Exoplanets Catalog](http://phl.upr.edu/projects/habitable-exoplanets-catalog)
47 by [Planetary Habitability Laboratory](http://phl.upr.edu/home).
48 
49 <b>Exoplanets Catalog</b>
50 
51 The exoplanets catalog is stored on the disk in [JSON](http://www.json.org/)
52 format, in a file named "exoplanets.json". A default copy is embedded in the
53 plug-in at compile time. A working copy is kept in the user data directory.
54 
55 <b>Configuration</b>
56 
57 The plug-ins' configuration data is stored in Stellarium's main configuration
58 file (section [Exoplanets]).
59 
60 @}
61 */
62 
63 //! @ingroup exoplanets
64 typedef QSharedPointer<Exoplanet> ExoplanetP;
65 
66 //! @class Exoplanets
67 //! Main class of the %Exoplanets plugin.
68 //! @author Alexander Wolf
69 //! @ingroup exoplanets
70 class Exoplanets : public StelObjectModule
71 {
72 	Q_OBJECT
73 	Q_PROPERTY(bool showExoplanets
74 		   READ getFlagShowExoplanets
75 		   WRITE setFlagShowExoplanets
76 		   NOTIFY flagExoplanetsVisibilityChanged
77 		   )
78 	Q_PROPERTY(Vec3f markerColor
79 		   READ getMarkerColor
80 		   WRITE setMarkerColor
81 		   NOTIFY markerColorChanged
82 		   )
83 	Q_PROPERTY(Vec3f habitableColor
84 		   READ getHabitableColor
85 		   WRITE setHabitableColor
86 		   NOTIFY habitableColorChanged
87 		   )
88 public:
89 	//! @enum UpdateState
90 	//! Used for keeping for track of the download/update status
91 	enum UpdateState {
92 		Updating,				//!< Update in progress
93 		CompleteNoUpdates,	//!< Update completed, there we no updates
94 		CompleteUpdates,		//!< Update completed, there were updates
95 		DownloadError,			//!< Error during download phase
96 		OtherError				//!< Other error
97 	};
98 	Q_ENUM(UpdateState)
99 	//! @enum TemperatureScale
100 	//! Available temperature scales
101 	enum TemperatureScale
102 	{
103 		Kelvin		= 0,
104 		Celsius		= 1,
105 		Fahrenheit	= 2
106 	};
107 	Q_ENUM(TemperatureScale)
108 
109 	Exoplanets();
110 	virtual ~Exoplanets() Q_DECL_OVERRIDE;
111 
112 	///////////////////////////////////////////////////////////////////////////
113 	// Methods defined in the StelModule class
114 	virtual void init() Q_DECL_OVERRIDE;
115 	virtual void deinit() Q_DECL_OVERRIDE;
116 	virtual void update(double deltaTime) Q_DECL_OVERRIDE;
117 	virtual void draw(StelCore* core) Q_DECL_OVERRIDE;
118 	virtual void drawPointer(StelCore* core, StelPainter& painter);
119 	virtual double getCallOrder(StelModuleActionName actionName) const Q_DECL_OVERRIDE;
120 
121 	///////////////////////////////////////////////////////////////////////////
122 	// Methods defined in StelObjectModule class
123 	//! Used to get a list of objects which are near to some position.
124 	//! @param v a vector representing the position in th sky around which to search for exoplanets.
125 	//! @param limitFov the field of view around the position v in which to search for exoplanets.
126 	//! @param core the StelCore to use for computations.
127 	//! @return a list containing the exoplanets located inside the limitFov circle around position v.
128 	virtual QList<StelObjectP> searchAround(const Vec3d& v, double limitFov, const StelCore* core) const Q_DECL_OVERRIDE;
129 
130 	//! Return the matching exoplanet system object's pointer if exists or Q_NULLPTR.
131 	//! @param nameI18n The case in-sensitive localized exoplanet system name
132 	virtual StelObjectP searchByNameI18n(const QString& nameI18n) const Q_DECL_OVERRIDE;
133 
134 	//! Return the matching exoplanet system if exists or Q_NULLPTR.
135 	//! @param name The case in-sensitive english exoplanet system name
136 	virtual StelObjectP searchByName(const QString& name) const Q_DECL_OVERRIDE;
137 
138 	//! Return the matching exoplanet system if exists or Q_NULLPTR.
139 	//! @param id The exoplanet system id
140 	virtual StelObjectP searchByID(const QString &id) const Q_DECL_OVERRIDE;
141 
142 	//! Find and return the list of at most maxNbItem objects auto-completing the passed object name.
143 	//! @param objPrefix the case insensitive first letters of the searched object
144 	//! @param maxNbItem the maximum number of returned object names
145 	//! @param useStartOfWords the autofill mode for returned objects names
146 	//! @return a list of matching object name by order of relevance, or an empty list if nothing match
147 	virtual QStringList listMatchingObjects(const QString& objPrefix, int maxNbItem=5, bool useStartOfWords=false) const Q_DECL_OVERRIDE;
148 
149 	virtual QStringList listAllObjects(bool inEnglish) const Q_DECL_OVERRIDE;
150 
getName() const151 	virtual QString getName() const Q_DECL_OVERRIDE { return "Exoplanets"; }
getStelObjectType() const152 	virtual QString getStelObjectType() const Q_DECL_OVERRIDE { return Exoplanet::EXOPLANET_TYPE; }
153 
154 	//! get a exoplanet object by identifier
155 	ExoplanetP getByID(const QString& id) const;
156 
157 	//! Implement this to tell the main Stellarium GUI that there is a GUI element to configure this
158 	//! plugin.
159 	virtual bool configureGui(bool show=true) Q_DECL_OVERRIDE;
160 
161 	//! Set up the plugin with default values.  This means clearing out the Exoplanets section in the
162 	//! main config.ini (if one already exists), and populating it with default values.  It also
163 	//! creates the default exoplanets.json file from the resource embedded in the plugin lib/dll file.
164 	void restoreDefaults(void);
165 
166 	//! Read (or re-read) settings from the main config file.  This will be called from init and also
167 	//! when restoring defaults (i.e. from the configuration dialog / restore defaults button).
168 	void loadConfiguration(void);
169 
170 	//! Save the settings to the main configuration file.
171 	void saveConfiguration(void);
172 
173 	//! get whether or not the plugin will try to update TLE data from the internet
174 	//! @return true if updates are set to be done, false otherwise
getUpdatesEnabled(void) const175 	bool getUpdatesEnabled(void) const {return updatesEnabled;}
176 	//! set whether or not the plugin will try to update TLE data from the internet
177 	//! @param b if true, updates will be enabled, else they will be disabled
setUpdatesEnabled(bool b)178 	void setUpdatesEnabled(bool b) {updatesEnabled=b;}
179 
setEnableAtStartup(bool b)180 	void setEnableAtStartup(bool b) { enableAtStartup=b; }
getEnableAtStartup(void) const181 	bool getEnableAtStartup(void) const { return enableAtStartup; }
182 
183 	//! get the date and time the TLE elements were updated
getLastUpdate(void) const184 	QDateTime getLastUpdate(void) const {return lastUpdate;}
185 
186 	//! get the update frequency in hours
getUpdateFrequencyHours(void) const187 	int getUpdateFrequencyHours(void) const {return updateFrequencyHours;}
setUpdateFrequencyHours(int hours)188 	void setUpdateFrequencyHours(int hours) {updateFrequencyHours = hours;}
189 
190 	//! get the number of seconds till the next update
191 	int getSecondsToUpdate(void);
192 
193 	//! Get the current updateState
getUpdateState(void) const194 	UpdateState getUpdateState(void) const {return updateState;}
195 
getExoplanetsData(int mode) const196 	QList<double> getExoplanetsData(int mode) const
197 	{
198 		switch(mode)
199 		{
200 			case 1:
201 				return EPSemiAxisAll;
202 			case 2:
203 				return EPMassAll;
204 			case 3:
205 				return EPRadiusAll;
206 			case 4:
207 				return EPPeriodAll;
208 			case 5:
209 				return EPAngleDistanceAll;
210 			case 6:
211 				return EPEffectiveTempHostStarAll;
212 			case 7:
213 				return EPYearDiscoveryAll;
214 			case 8:
215 				return EPMetallicityHostStarAll;
216 			case 9:
217 				return EPVMagHostStarAll;
218 			case 10:
219 				return EPRAHostStarAll;
220 			case 11:
221 				return EPDecHostStarAll;
222 			case 12:
223 				return EPDistanceHostStarAll;
224 			case 13:
225 				return EPMassHostStarAll;
226 			case 14:
227 				return EPRadiusHostStarAll;
228 			default:
229 				return EPEccentricityAll;
230 		}
231 	}
232 
233 	//! Get the list of all exoplanetary systems.
getAllExoplanetarySystems() const234 	const QList<ExoplanetP>& getAllExoplanetarySystems() const {return ep;}
235 
236 signals:
237 	//! @param state the new update state.
238 	void updateStateChanged(Exoplanets::UpdateState state);
239 
240 	//! emitted after a JSON update has run.
241 	void jsonUpdateComplete(void);
242 
243 	void flagExoplanetsVisibilityChanged(bool b);
244 	void markerColorChanged(Vec3f);
245 	void habitableColorChanged(Vec3f);
246 
247 public slots:
248 	//! Download JSON from web recources described in the module section of the
249 	//! module.ini file and update the local JSON file.
250 	void updateJSON(void);
251 
252 	//! Enable/disable display of markers of exoplanetary systems
253 	//! @param b boolean flag
254 	void setFlagShowExoplanets(bool b);
255 	//! Get status to display of markers of exoplanetary systems
256 	//! @return true if it's visible
getFlagShowExoplanets(void) const257 	bool getFlagShowExoplanets(void) const { return flagShowExoplanets; }
258 
259 	//! Enable/disable display of designations of exoplanetary systems
260 	//! @param b boolean flag
261 	void setFlagShowExoplanetsDesignations(bool b);
262 	//! Get status to display of designations of exoplanetary systems
263 	//! @return true if it's visible
264 	bool getFlagShowExoplanetsDesignations(void) const;
265 
266 	//! Enable/disable display of number of exoplanets in exoplanetary system
267 	//! @param b boolean flag
268 	void setFlagShowExoplanetsNumbers(bool b);
269 	//! Get status to display of designations of exoplanets in exoplanetary system
270 	//! @return true if it's visible
271 	bool getFlagShowExoplanetsNumbers(void) const;
272 
273 	//! Define whether the button toggling exoplanets should be visible
274 	void setFlagShowExoplanetsButton(bool b);
getFlagShowExoplanetsButton(void) const275 	bool getFlagShowExoplanetsButton(void) const { return flagShowExoplanetsButton; }
276 
277 	//! Get status to display of distribution of exoplanetary systems
278 	//! @return true if distribution of exoplanetary systems is enabled
279 	bool getDisplayMode(void) const;
280 	//! Enable/disable display of distribution of exoplanetary systems
281 	//! @param b
282 	void setDisplayMode(bool b);
283 
284 	//! Get status to display of systems with exoplanets after their discovery
285 	//! @return true if markers of exoplanetary systems are visible after discovery of exoplanets
286 	bool getTimelineMode(void) const;
287 	//! Enable/disable display of systems with exoplanets after their discovery only
288 	//! @param b
289 	void setTimelineMode(bool b);
290 
291 	//! Get status to display of exoplanetary systems with the potentially habitable exoplanets
292 	//! @return true if systems with only potentially habitable exoplanets are visible
293 	bool getHabitableMode(void) const;
294 	//! Enable/disable display of exoplanetary systems with the potentially habitable exoplanets only
295 	//! @param b
296 	void setHabitableMode(bool b);
297 
298 	//! Get color for markers of exoplanetary systems
299 	//! @param h set false if you want get color of markers of potentially habitable exoplanets
300 	//! @return color
301 	Vec3f getMarkerColor() const;
302 	//! Set color for markers of exoplanetary systems
303 	//! @param c color
304 	//! @param h set true if you want set color for potentially habitable exoplanets
305 	//! @code
306 	//! // example of usage in scripts
307 	//! Exoplanets.setMarkerColor(Vec3f(1.0,0.0,0.0), true);
308 	//! @endcode
309 	void setMarkerColor(const Vec3f& c);
310 
311 	//! Get color for markers of habitable exoplanetary systems
312 	//! @param h set false if you want get color of markers of potentially habitable exoplanets
313 	//! @return color
314 	Vec3f getHabitableColor() const;
315 	//! Set color for markers of exoplanetary systems
316 	//! @param c color
317 	//! @param h set true if you want set color for potentially habitable exoplanets
318 	//! @code
319 	//! // example of usage in scripts
320 	//! Exoplanets.setHabitableColor(Vec3f(1.0,0.0,0.0), true);
321 	//! @endcode
322 	void setHabitableColor(const Vec3f& c);
323 
324 	//! Get count of planetary systems from catalog
325 	//! @return count of planetary systems
getCountPlanetarySystems(void) const326 	int getCountPlanetarySystems(void) const
327 	{
328 		return PSCount;
329 	}
330 
331 	//! Get count of exoplanets from catalog
332 	//! @return count of all exoplanets
getCountAllExoplanets(void) const333 	int getCountAllExoplanets(void) const
334 	{
335 		return EPCountAll;
336 	}
337 
338 	//! Get count of potentially habitable exoplanets from catalog
339 	//! @return count of potentially habitable exoplanets
getCountHabitableExoplanets(void) const340 	int getCountHabitableExoplanets(void) const
341 	{
342 		return EPCountPH;
343 	}
344 
345 	//! Set the temperature scale
setCurrentTemperatureScale(TemperatureScale tscale)346 	void setCurrentTemperatureScale(TemperatureScale tscale)
347 	{
348 		Exoplanet::temperatureScaleID = static_cast<int>(tscale);
349 	}
350 	//! Get the current temperature scale
getCurrentTemperatureScale() const351 	TemperatureScale getCurrentTemperatureScale() const
352 	{
353 		return static_cast<TemperatureScale>(Exoplanet::temperatureScaleID);
354 	}
355 	//! Get the key of current temperature scale
356 	QString getCurrentTemperatureScaleKey(void) const;
357 	//! Set the temperature scale from its key
358 	void setCurrentTemperatureScaleKey(QString key);
359 
360 	//! Connect this to StelApp font size.
setFontSize(int s)361 	void setFontSize(int s){font.setPixelSize(s);}
362 private:
363 	// Font used for displaying our text
364 	QFont font;
365 
366 	// if existing, delete Satellites section in main config.ini, then create with default values
367 	void resetConfiguration(void);
368 
369 	// Upgrade config.ini: rename old key settings to new
370 	void upgradeConfigIni(void);
371 
372 	//! replace the json file with the default from the compiled-in resource
373 	void restoreDefaultJsonFile(void);
374 
375 	//! read the json file and create list of exoplanets.
376 	void readJsonFile(void);
377 
378 	//! Creates a backup of the exoplanets.json file called exoplanets.json.old
379 	//! @param deleteOriginal if true, the original file is removed, else not
380 	//! @return true on OK, false on failure
381 	bool backupJsonFile(bool deleteOriginal=false) const;
382 
383 	//! Get the version of catalog format from the "version of the format" value in the exoplanets.json file
384 	//! @return version string, e.g. "1"
385 	int getJsonFileFormatVersion(void) const;
386 
387 	//! Check format of the catalog of exoplanets
388 	//! @return valid boolean, e.g. "true"
389 	bool checkJsonFileFormat(void) const;
390 
391 	//! parse JSON file and load exoplanets to map
392 	QVariantMap loadEPMap(QString path=QString());
393 
394 	//! set items for list of struct from data map
395 	void setEPMap(const QVariantMap& map);
396 
397 	//! A fake method for strings marked for translation.
398 	//! Use it instead of translations.h for N_() strings, except perhaps for
399 	//! keyboard action descriptions. (It's better for them to be in a single
400 	//! place.)
401 	static void translations();
402 
403 	QString jsonCatalogPath;
404 
405 	int PSCount;	// Count of planetary systems
406 	int EPCountAll;	// Count of exoplents
407 	int EPCountPH;	// Count of exoplanets in habitable zone
408 
409 	// Lists of various data about exoplanets for quick plot of graphs
410 	QList<double> EPEccentricityAll, EPSemiAxisAll, EPMassAll, EPRadiusAll, EPPeriodAll, EPAngleDistanceAll,
411 		      EPEffectiveTempHostStarAll, EPYearDiscoveryAll, EPMetallicityHostStarAll, EPVMagHostStarAll,
412 		      EPRAHostStarAll, EPDecHostStarAll, EPDistanceHostStarAll, EPMassHostStarAll, EPRadiusHostStarAll;
413 
414 	StelTextureSP texPointer;
415 	QList<ExoplanetP> ep;
416 
417 	// variables and functions for the updater
418 	UpdateState updateState;
419 	QNetworkAccessManager * networkManager;
420 	QNetworkReply * downloadReply;
421 	QString updateUrl;
422 	QTimer* updateTimer;
423 	bool updatesEnabled;
424 	QDateTime lastUpdate;
425 	int updateFrequencyHours;
426 	bool enableAtStartup;
427 
428 	void startDownload(QString url);
429 	void deleteDownloadProgressBar();
430 
431 	QSettings* conf;
432 
433 	// GUI
434 	ExoplanetsDialog* exoplanetsConfigDialog;
435 	bool flagShowExoplanets;
436 	bool flagShowExoplanetsButton;
437 	StelButton* toolbarButton;
438 	class StelProgressController* progressBar;
439 
440 private slots:
441 	//! check to see if an update is required.  This is called periodically by a timer
442 	//! if the last update was longer than updateFrequencyHours ago then the update is
443 	//! done.
444 	void checkForUpdate(void);
445 
446 	void updateDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
447 	void downloadComplete(QNetworkReply * reply);
448 
449 	//! Display a message. This is used for plugin-specific warnings and such
450 	void displayMessage(const QString& message, const QString hexColor="#999999");
451 
452 	void reloadCatalog(void);
453 	//! Call when button "Save settings" in main GUI are pressed
saveSettings()454 	void 	saveSettings() { saveConfiguration(); }
455 };
456 
457 
458 
459 #include <QObject>
460 #include "StelPluginInterface.hpp"
461 
462 //! This class is used by Qt to manage a plug-in interface
463 class ExoplanetsStelPluginInterface : public QObject, public StelPluginInterface
464 {
465 	Q_OBJECT
466 	Q_PLUGIN_METADATA(IID StelPluginInterface_iid)
467 	Q_INTERFACES(StelPluginInterface)
468 public:
469 	virtual StelModule* getStelModule() const Q_DECL_OVERRIDE;
470 	virtual StelPluginInfo getPluginInfo() const Q_DECL_OVERRIDE;
getExtensionList() const471 	virtual QObjectList getExtensionList() const Q_DECL_OVERRIDE { return QObjectList(); }
472 };
473 
474 #endif /* EXOPLANETS_HPP */
475