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#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
41 QtConcurrent::run(&ModelManager::loadLists, this);
42#else
43 QtConcurrent::run(this, &ModelManager::loadLists);
44#endif
45}
46
47ModelManager::~ModelManager()
48{
49 qDeleteAll(m_ModelList);
50 foreach (QList<SkyObjItem *> list, m_ObjectList)
51 qDeleteAll(list);
52 delete tempModel;
53}
54
55void ModelManager::loadLists()
56{
58 return;
59
60 emit loadProgressUpdated(0);
61 KStarsData *data = KStarsData::Instance();
63 listStars.append(data->skyComposite()->objectLists(SkyObject::STAR));
64 for (int i = 0; i < listStars.size(); i++)
65 {
66 QPair<QString, const SkyObject *> pair = listStars.value(i);
67 const StarObject *star = dynamic_cast<const StarObject *>(pair.second);
68 if (star != nullptr && star->hasLatinName())
69 m_ObjectList[Stars].append(new SkyObjItem((SkyObject *)(star)));
70 }
71 QString prevName;
72 for (int i = 0; i < m_ObjectList[Stars].size(); i++)
73 {
74 SkyObjItem *star = m_ObjectList[Stars].at(i);
75 if (prevName == star->getName())
76 {
77 m_ObjectList[Stars].removeAt(i);
78 i--;
79 }
80 prevName = star->getName();
81 }
82
83 KSFileReader fileReader;
84 if (!fileReader.open("Interesting.dat"))
85 return;
86
87 while (fileReader.hasMoreLines())
88 {
90 return;
91
92 QString line = fileReader.readLine();
93
94 if (line.length() == 0 || line[0] == '#')
95 continue;
96
97 SkyObject *o;
98 if ((o = data->skyComposite()->findByName(line)))
99 {
100 //qDebug()<<o->longname()<<o->typeName();
101 switch (o->type())
102 {
103 case SkyObject::OPEN_CLUSTER:
104 case SkyObject::GLOBULAR_CLUSTER:
105 case SkyObject::GALAXY_CLUSTER:
106 favoriteClusters.append(new SkyObjItem(o));
107 break;
108 case SkyObject::PLANETARY_NEBULA:
109 case SkyObject::DARK_NEBULA:
110 case SkyObject::GASEOUS_NEBULA:
111 favoriteNebulas.append(new SkyObjItem(o));
112 break;
113 case SkyObject::GALAXY:
114 favoriteGalaxies.append(new SkyObjItem(o));
115 break;
116 }
117 }
118 }
119
120 emit loadProgressUpdated(0.20);
121 //Asteroids Loading is causing a crash. FIX ME!!
122 //loadObjectList(m_ObjectList[Asteroids], SkyObject::ASTEROID);
123 emit loadProgressUpdated(0.30);
124 loadObjectList(m_ObjectList[Comets], SkyObject::COMET);
125 emit loadProgressUpdated(0.40);
126 loadObjectList(m_ObjectList[Satellites], SkyObject::SATELLITE);
127 loadObjectList(m_ObjectList[Supernovas], SkyObject::SUPERNOVA);
128 emit loadProgressUpdated(0.50);
129 loadObjectList(m_ObjectList[Constellations], SkyObject::CONSTELLATION);
130 emit loadProgressUpdated(0.55);
131 loadObjectList(m_ObjectList[Planets], SkyObject::PLANET);
132 emit loadProgressUpdated(0.60);
133
134 loadObjectList(m_ObjectList[Galaxies], SkyObject::GALAXY);
135 emit loadProgressUpdated(0.70);
136
137 loadObjectList(m_ObjectList[Clusters], SkyObject::OPEN_CLUSTER);
138 loadObjectList(m_ObjectList[Clusters], SkyObject::GLOBULAR_CLUSTER);
139 loadObjectList(m_ObjectList[Clusters], SkyObject::GALAXY_CLUSTER);
140 emit loadProgressUpdated(0.80);
141
142 loadObjectList(m_ObjectList[Nebulas], SkyObject::PLANETARY_NEBULA);
143 loadObjectList(m_ObjectList[Nebulas], SkyObject::SUPERNOVA_REMNANT);
144 loadObjectList(m_ObjectList[Nebulas], SkyObject::GASEOUS_NEBULA);
145 loadObjectList(m_ObjectList[Nebulas], SkyObject::DARK_NEBULA);
146
147 emit loadProgressUpdated(0.90);
148 emit loadProgressUpdated(1);
149}
150
152{
153 m_ObsConditions = obs;
155
156 for (int i = 0; i < NumberOfLists; i++)
157 loadObjectsIntoModel(*m_ModelList[i], m_ObjectList[i]);
158}
159
160void ModelManager::updateModel(ObsConditions *obs, QString modelName)
161{
162 m_ObsConditions = obs;
163 SkyObjListModel *model = returnModel(modelName);
164 const auto modelNumber = getModelNumber(modelName);
165
166 if (modelNumber > -1 && model)
167 {
168 model->resetModel();
169 if (showOnlyFavorites && modelName == "galaxies")
170 loadObjectsIntoModel(*m_ModelList[modelNumber], favoriteGalaxies);
171 else if (showOnlyFavorites && modelName == "nebulas")
172 loadObjectsIntoModel(*m_ModelList[modelNumber], favoriteNebulas);
173 else if (showOnlyFavorites && modelName == "clusters")
174 loadObjectsIntoModel(*m_ModelList[modelNumber], favoriteClusters);
175 else
176 loadObjectsIntoModel(*m_ModelList[modelNumber], m_ObjectList[modelNumber]);
177 emit modelUpdated();
178 }
179}
180
181void ModelManager::loadObjectList(QList<SkyObjItem *> &skyObjectList, int type)
182{
183 if (KStars::Closing)
184 return;
185
186 KStarsData *data = KStarsData::Instance();
187 QVector<QPair<QString, const SkyObject *>> objects = data->skyComposite()->objectLists(type);
188
189 for (int i = 0; i < objects.size(); i++)
190 {
191 if (KStars::Closing)
192 return;
193
194 QPair<QString, const SkyObject *> pair = objects.value(i);
195 const SkyObject *listObject = pair.second;
196 if(listObject)
197 if (listObject->name() != i18n("Sun"))
198 skyObjectList.append(new SkyObjItem(const_cast<SkyObject *>(listObject)));
199 }
200 QString prevName;
201 for (int i = 0; i < skyObjectList.size(); i++)
202 {
203 if (KStars::Closing)
204 return;
205
206 SkyObjItem *obj = skyObjectList.at(i);
207 if (prevName == obj->getName())
208 {
209 skyObjectList.removeAt(i);
210 i--;
211 }
212 prevName = obj->getName();
213 }
214}
215
216void ModelManager::loadObjectsIntoModel(SkyObjListModel &model, QList<SkyObjItem *> &skyObjectList)
217{
218 KStarsData *data = KStarsData::Instance();
219
220 foreach (SkyObjItem *soitem, skyObjectList)
221 {
222 bool isVisible =
223 (showOnlyVisible) ? (m_ObsConditions->isVisible(data->geo(), data->lst(), soitem->getSkyObject())) : true;
224 if (isVisible)
225 model.addSkyObject(soitem);
226 }
227}
228
230{
231 foreach (SkyObjListModel *model, m_ModelList)
232 model->resetModel();
233}
234
235int ModelManager::getModelNumber(QString modelName)
236{
237 if (modelName == "planets")
238 return Planets;
239 if (modelName == "stars")
240 return Stars;
241 if (modelName == "constellations")
242 return Constellations;
243 if (modelName == "galaxies")
244 return Galaxies;
245 if (modelName == "clusters")
246 return Clusters;
247 if (modelName == "nebulas")
248 return Nebulas;
249 if (modelName == "asteroids")
250 return Asteroids;
251 if (modelName == "comets")
252 return Comets;
253 if (modelName == "supernovas")
254 return Supernovas;
255 if (modelName == "satellites")
256 return Satellites;
257 if (modelName == "messier")
258 return Messier;
259 if (modelName == "ngc")
260 return NGC;
261 if (modelName == "ic")
262 return IC;
263 if (modelName == "sharpless")
264 return Sharpless;
265 else
266 return -1;
267}
268
270{
271 int modelNumber = getModelNumber(modelName);
272 if (modelNumber > -1 && modelNumber < NumberOfLists)
273 return m_ModelList[modelNumber];
274 else
275 return tempModel;
276}
277
279{
280 const auto id = getModelNumber(name);
281 if (m_CatalogMap.count(id) > 0)
282 return;
283
284 const std::unordered_map<QString, QString> search_prefixes{
285 { "ngc", "NGC " }, { "ic", "IC " }, { "messier", "M " }, { "sharpless", "Sh2 " }
286 };
287
288 CatalogsDB::DBManager manager{ CatalogsDB::dso_db_path() };
289
290 const auto &prefix = search_prefixes.at(name);
291 const int offset = prefix.size();
292
293 m_CatalogMap[id] = std::get<2>(manager.general_master_query(
294 QString("name LIKE '%1'").arg(prefix + "%"),
295 QString("CAST(SUBSTR(name,%1) AS INT)").arg(offset)));
296
297 auto &lst = m_CatalogSkyObjItems[id];
298
299 for (auto &obj : m_CatalogMap[id])
300 {
301 obj.updateCoordsNow(KStarsData::Instance()->updateNum());
302 lst.emplace_back(&obj);
303 }
304
305 auto &p_lst = m_ObjectList[id];
306 for (auto &obj : lst)
307 p_lst.append(&obj);
308
309 updateModel(m_ObsConditions, name);
310 emit loadProgressUpdated(1);
311};
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...
QString readLine()
increments the line number and returns the next line from the file as a QString.
bool hasMoreLines() const
bool open(const QString &fname)
opens the file fname from the QStandardPaths::AppLocalDataLocation directory and uses that file for t...
KStarsData is the backbone of KStars.
Definition kstarsdata.h:74
CachingDms * lst()
Definition kstarsdata.h:226
GeoLocation * geo()
Definition kstarsdata.h:232
SkyMapComposite * skyComposite()
Definition kstarsdata.h:168
static bool Closing
Set to true when the application is being closed.
Definition kstars.h:857
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
SkyObject * getSkyObject()
Get sky-object associated with the SkyObjItem.
Definition skyobjitem.h:123
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
virtual QString name(void) const
Definition skyobject.h:146
int type(void) const
Definition skyobject.h:189
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
T value(qsizetype i) 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 Sat Dec 21 2024 17:04:47 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.