1 /*
2     SPDX-FileCopyrightText: 2012 Samikshan Bairagya <samikshan@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include "catalogobject.h"
10 #include "skyobjitem.h"
11 #include "catalogsdb.h"
12 #include <QList>
13 #include <QObject>
14 
15 #include "polyfills/qstring_hash.h"
16 #include <unordered_map>
17 
18 class ObsConditions;
19 class SkyObjListModel;
20 
21 /**
22  * @class ModelManager
23  * @brief Manages models for QML listviews of different types of sky-objects.
24  *
25  * @author Samikshan Bairagya
26  */
27 class ModelManager : public QObject
28 {
29     Q_OBJECT
30   public:
31     /**
32      * @enum ObjectList
33      * @brief Model type for different types of sky-objects.
34      */
35     enum ObjectList
36     {
37         Planets,
38         Stars,
39         Constellations,
40         Galaxies,
41         Clusters,
42         Nebulas,
43         Satellites,
44         Asteroids,
45         Comets,
46         Supernovas,
47         Messier,
48         NGC,
49         IC,
50         Sharpless,
51         NumberOfLists
52     };
53 
54     /**
55      * @brief Constructor - Creates models for different sky-object types.
56      * @param obs   Pointer to an ObsConditions object.
57      */
58     explicit ModelManager(ObsConditions *obs);
59     ~ModelManager() override;
60 
61     /** Updates sky-object list models. */
62     void updateAllModels(ObsConditions *obs);
63 
64     void updateModel(ObsConditions *obs, QString modelName);
65 
66     /** Clears all sky-objects list models. */
67     void resetAllModels();
68 
setShowOnlyVisibleObjects(bool show)69     void setShowOnlyVisibleObjects(bool show) { showOnlyVisible = show; }
70 
showOnlyVisibleObjects()71     bool showOnlyVisibleObjects() { return showOnlyVisible; }
72 
setShowOnlyFavoriteObjects(bool show)73     void setShowOnlyFavoriteObjects(bool show) { showOnlyFavorites = show; }
74 
showOnlyFavoriteObjects()75     bool showOnlyFavoriteObjects() { return showOnlyFavorites; }
76 
77     /**
78      * Load objects from the dso db for the catalog with \p name can
79      * be used to retreive the object lists later.
80      *
81      * This is implemented by searching the dso database for objects
82      * whichs name starts with a prefix to capture subsets of a catalog.
83      */
84     void loadCatalog(const QString &name);
85 
86     /**
87      * @brief Returns model of given type.
88      * @return Pointer to SkyObjListModel of given type.
89      * @param modelName Name of sky-object model to be returned.
90      */
91     SkyObjListModel *returnModel(QString modelName);
92 
93     int getModelNumber(QString modelName);
94 
getTempModel()95     SkyObjListModel *getTempModel() { return tempModel; }
96 
97   signals:
98     void loadProgressUpdated(double progress);
99     void modelUpdated();
100 
101   private:
102     void loadLists();
103     void loadObjectList(QList<SkyObjItem *> &skyObjectList, int type);
104     void loadNamedStarList();
105     void loadObjectsIntoModel(SkyObjListModel &model, QList<SkyObjItem *> &skyObjectList);
106 
107     ObsConditions *m_ObsConditions{ nullptr };
108     QList<QList<SkyObjItem *>> m_ObjectList;
109     QList<SkyObjListModel *> m_ModelList;
110     bool showOnlyVisible{ true };
111     bool showOnlyFavorites{ true };
112     QList<SkyObjItem *> favoriteGalaxies;
113     QList<SkyObjItem *> favoriteNebulas;
114     QList<SkyObjItem *> favoriteClusters;
115     SkyObjListModel *tempModel{ nullptr };
116     std::unordered_map<int, CatalogsDB::CatalogObjectList> m_CatalogMap;
117     std::unordered_map<int, std::list<SkyObjItem>> m_CatalogSkyObjItems;
118 };
119