Kstars

modelmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2012 Samikshan Bairagya <samikshan@gmail.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "modelmanager.h"
8
9#include "ksfilereader.h"
10#include "kstars.h"
11#include "kstarsdata.h"
12#include "obsconditions.h"
13#include "skymapcomposite.h"
14#include "skyobjitem.h"
15#include "skyobjlistmodel.h"
16#include "starobject.h"
17#include "catalogsdb.h"
18
19#include <QtConcurrent>
20
22{
23 m_ObsConditions = obs;
24
25 tempModel = new SkyObjListModel();
26
27 m_ModelList = QList<SkyObjListModel *>();
28 m_ObjectList = QList<QList<SkyObjItem *>>();
29
30 favoriteClusters = QList<SkyObjItem *>();
31 favoriteNebulas = QList<SkyObjItem *>();
32 favoriteGalaxies = QList<SkyObjItem *>();
33
34 for (int i = 0; i < NumberOfLists; i++)
35 {
36 m_ModelList.append(new SkyObjListModel());
37 m_ObjectList.append(QList<SkyObjItem *>());
38 }
39
40 QtConcurrent::run(this, &ModelManager::loadLists);
41}
42
43ModelManager::~ModelManager()
44{
45 qDeleteAll(m_ModelList);
46 foreach (QList<SkyObjItem *> list, m_ObjectList)
47 qDeleteAll(list);
48 delete tempModel;
49}
50
51void ModelManager::loadLists()
52{
54 return;
55
56 emit loadProgressUpdated(0);
57 KStarsData *data = KStarsData::Instance();
59 listStars.append(data->skyComposite()->objectLists(SkyObject::STAR));
60 for (int i = 0; i < listStars.size(); i++)
61 {
63 const StarObject *star = dynamic_cast<const StarObject *>(pair.second);
64 if (star != nullptr && star->hasLatinName())
65 m_ObjectList[Stars].append(new SkyObjItem((SkyObject *)(star)));
66 }
68 for (int i = 0; i < m_ObjectList[Stars].size(); i++)
69 {
70 SkyObjItem *star = m_ObjectList[Stars].at(i);
71 if (prevName == star->getName())
72 {
73 m_ObjectList[Stars].removeAt(i);
74 i--;
75 }
76 prevName = star->getName();
77 }
78
80 if (!fileReader.open("Interesting.dat"))
81 return;
82
83 while (fileReader.hasMoreLines())
84 {
86 return;
87
88 QString line = fileReader.readLine();
89
90 if (line.length() == 0 || line[0] == '#')
91 continue;
92
93 SkyObject *o;
94 if ((o = data->skyComposite()->findByName(line)))
95 {
96 //qDebug()<<o->longname()<<o->typeName();
97 switch (o->type())
98 {
99 case SkyObject::OPEN_CLUSTER:
100 case SkyObject::GLOBULAR_CLUSTER:
101 case SkyObject::GALAXY_CLUSTER:
102 favoriteClusters.append(new SkyObjItem(o));
103 break;
104 case SkyObject::PLANETARY_NEBULA:
105 case SkyObject::DARK_NEBULA:
106 case SkyObject::GASEOUS_NEBULA:
107 favoriteNebulas.append(new SkyObjItem(o));
108 break;
109 case SkyObject::GALAXY:
110 favoriteGalaxies.append(new SkyObjItem(o));
111 break;
112 }
113 }
114 }
115
116 emit loadProgressUpdated(0.20);
117
118 loadObjectList(m_ObjectList[Asteroids], SkyObject::ASTEROID);
119 emit loadProgressUpdated(0.30);
120 loadObjectList(m_ObjectList[Comets], SkyObject::COMET);
121 emit loadProgressUpdated(0.40);
122 loadObjectList(m_ObjectList[Satellites], SkyObject::SATELLITE);
123 loadObjectList(m_ObjectList[Supernovas], SkyObject::SUPERNOVA);
124 emit loadProgressUpdated(0.50);
125 loadObjectList(m_ObjectList[Constellations], SkyObject::CONSTELLATION);
126 emit loadProgressUpdated(0.55);
127 loadObjectList(m_ObjectList[Planets], SkyObject::PLANET);
128 emit loadProgressUpdated(0.60);
129
130 loadObjectList(m_ObjectList[Galaxies], SkyObject::GALAXY);
131 emit loadProgressUpdated(0.70);
132
133 loadObjectList(m_ObjectList[Clusters], SkyObject::OPEN_CLUSTER);
134 loadObjectList(m_ObjectList[Clusters], SkyObject::GLOBULAR_CLUSTER);
135 loadObjectList(m_ObjectList[Clusters], SkyObject::GALAXY_CLUSTER);
136 emit loadProgressUpdated(0.80);
137
138 loadObjectList(m_ObjectList[Nebulas], SkyObject::PLANETARY_NEBULA);
139 loadObjectList(m_ObjectList[Nebulas], SkyObject::SUPERNOVA_REMNANT);
140 loadObjectList(m_ObjectList[Nebulas], SkyObject::GASEOUS_NEBULA);
141 loadObjectList(m_ObjectList[Nebulas], SkyObject::DARK_NEBULA);
142
143 emit loadProgressUpdated(0.90);
144 emit loadProgressUpdated(1);
145}
146
148{
149 m_ObsConditions = obs;
151
152 for (int i = 0; i < NumberOfLists; i++)
153 loadObjectsIntoModel(*m_ModelList[i], m_ObjectList[i]);
154}
155
156void ModelManager::updateModel(ObsConditions *obs, QString modelName)
157{
158 m_ObsConditions = obs;
160 const auto modelNumber = getModelNumber(modelName);
161
162 if (modelNumber > -1 && model)
163 {
164 model->resetModel();
165 if (showOnlyFavorites && modelName == "galaxies")
166 loadObjectsIntoModel(*m_ModelList[modelNumber], favoriteGalaxies);
167 else if (showOnlyFavorites && modelName == "nebulas")
168 loadObjectsIntoModel(*m_ModelList[modelNumber], favoriteNebulas);
169 else if (showOnlyFavorites && modelName == "clusters")
170 loadObjectsIntoModel(*m_ModelList[modelNumber], favoriteClusters);
171 else
172 loadObjectsIntoModel(*m_ModelList[modelNumber], m_ObjectList[modelNumber]);
173 emit modelUpdated();
174 }
175}
176
177void ModelManager::loadObjectList(QList<SkyObjItem *> &skyObjectList, int type)
178{
179 if (KStars::Closing)
180 return;
181
182 KStarsData *data = KStarsData::Instance();
183 QVector<QPair<QString, const SkyObject *>> objects = data->skyComposite()->objectLists(type);
184
185 for (int i = 0; i < objects.size(); i++)
186 {
187 if (KStars::Closing)
188 return;
189
190 QPair<QString, const SkyObject *> pair = objects.value(i);
191 const SkyObject *listObject = pair.second;
192 if(listObject)
193 if (listObject->name() != i18n("Sun"))
194 skyObjectList.append(new SkyObjItem(const_cast<SkyObject *>(listObject)));
195 }
197 for (int i = 0; i < skyObjectList.size(); i++)
198 {
199 if (KStars::Closing)
200 return;
201
202 SkyObjItem *obj = skyObjectList.at(i);
203 if (prevName == obj->getName())
204 {
205 skyObjectList.removeAt(i);
206 i--;
207 }
208 prevName = obj->getName();
209 }
210}
211
212void ModelManager::loadObjectsIntoModel(SkyObjListModel &model, QList<SkyObjItem *> &skyObjectList)
213{
214 KStarsData *data = KStarsData::Instance();
215
217 {
218 bool isVisible =
219 (showOnlyVisible) ? (m_ObsConditions->isVisible(data->geo(), data->lst(), soitem->getSkyObject())) : true;
220 if (isVisible)
221 model.addSkyObject(soitem);
222 }
223}
224
226{
227 foreach (SkyObjListModel *model, m_ModelList)
228 model->resetModel();
229}
230
231int ModelManager::getModelNumber(QString modelName)
232{
233 if (modelName == "planets")
234 return Planets;
235 if (modelName == "stars")
236 return Stars;
237 if (modelName == "constellations")
238 return Constellations;
239 if (modelName == "galaxies")
240 return Galaxies;
241 if (modelName == "clusters")
242 return Clusters;
243 if (modelName == "nebulas")
244 return Nebulas;
245 if (modelName == "asteroids")
246 return Asteroids;
247 if (modelName == "comets")
248 return Comets;
249 if (modelName == "supernovas")
250 return Supernovas;
251 if (modelName == "satellites")
252 return Satellites;
253 if (modelName == "messier")
254 return Messier;
255 if (modelName == "ngc")
256 return NGC;
257 if (modelName == "ic")
258 return IC;
259 if (modelName == "sharpless")
260 return Sharpless;
261 else
262 return -1;
263}
264
266{
267 int modelNumber = getModelNumber(modelName);
268 if (modelNumber > -1 && modelNumber < NumberOfLists)
269 return m_ModelList[modelNumber];
270 else
271 return tempModel;
272}
273
275{
276 const auto id = getModelNumber(name);
277 if (m_CatalogMap.count(id) > 0)
278 return;
279
280 const std::unordered_map<QString, QString> search_prefixes{
281 { "ngc", "NGC " }, { "ic", "IC " }, { "messier", "M " }, { "sharpless", "Sh2 " }
282 };
283
284 CatalogsDB::DBManager manager{ CatalogsDB::dso_db_path() };
285
286 const auto &prefix = search_prefixes.at(name);
287 const int offset = prefix.size();
288
289 m_CatalogMap[id] = std::get<2>(manager.general_master_query(
290 QString("name LIKE '%1'").arg(prefix + "%"),
291 QString("CAST(SUBSTR(name,%1) AS INT)").arg(offset)));
292
293 auto &lst = m_CatalogSkyObjItems[id];
294
295 for (auto &obj : m_CatalogMap[id])
296 {
297 obj.updateCoordsNow(KStarsData::Instance()->updateNum());
298 lst.emplace_back(&obj);
299 }
300
301 auto &p_lst = m_ObjectList[id];
302 for (auto &obj : lst)
303 p_lst.append(&obj);
304
305 updateModel(m_ObsConditions, name);
306 emit loadProgressUpdated(1);
307};
Manages the catalog database and provides an interface to provide an interface to query and modify th...
Definition catalogsdb.h:183
I totally rewrote this because the earlier scheme of reading all the lines of a file into a buffer be...
KStarsData is the backbone of KStars.
Definition kstarsdata.h:72
CachingDms * lst()
Definition kstarsdata.h:224
GeoLocation * geo()
Definition kstarsdata.h:230
SkyMapComposite * skyComposite()
Definition kstarsdata.h:166
static bool Closing
Set to true when the application is being closed.
Definition kstars.h:859
void resetAllModels()
Clears all sky-objects list models.
void loadCatalog(const QString &name)
Load objects from the dso db for the catalog with name can be used to retreive the object lists later...
ModelManager(ObsConditions *obs)
Constructor - Creates models for different sky-object types.
SkyObjListModel * returnModel(QString modelName)
Returns model of given type.
void updateAllModels(ObsConditions *obs)
Updates sky-object list models.
This class deals with the observing conditions of the night sky.
bool isVisible(GeoLocation *geo, dms *lst, SkyObject *so)
Evaluate visibility of sky-object based on current observing conditions.
SkyObject * findByName(const QString &name, bool exact=true) override
Search the children of this SkyMapComposite for a SkyObject whose name matches the argument.
Represents an item in the list of interesting sky-objects.
Definition skyobjitem.h:21
QString getName() const
Get name of sky-object associated with the SkyObjItem.
Definition skyobjitem.h:69
Represents a model for the list of interesting sky-objects to be displayed in the QML interface.
void resetModel()
Erase all data in model.
void addSkyObject(SkyObjItem *sobj)
Add a sky-object to the model.
Provides all necessary information about an object in the sky: its coordinates, name(s),...
Definition skyobject.h:42
int type(void) const
Definition skyobject.h:188
This is a subclass of SkyObject.
Definition starobject.h:33
bool hasLatinName() const
Definition starobject.h:123
QString i18n(const char *text, const TYPE &arg...)
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
void removeAt(qsizetype i)
qsizetype size() const const
qsizetype length() const const
QFuture< T > run(Function function,...)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:48:27 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.