Kstars

skymapview.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Akarsh Simha <akarsh@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "skymapview.h"
8#include "nan.h"
9#include "kstarsdata.h"
10#include "ksuserdb.h"
11#include <kstars_debug.h>
12
13SkyMapView::SkyMapView(const QString &name_, const QJsonObject &jsonData)
14 : name(name_)
15{
16 // Check version
17 if (jsonData["version"].toString() != "1.0.0")
18 {
19 qCCritical(KSTARS) << "Unhandled SkyMapView JSON schema version " << jsonData["version"].toString();
20 return;
21 }
22 useAltAz = jsonData["useAltAz"].toBool();
23 viewAngle = jsonData["viewAngle"].toDouble();
24 mirror = jsonData["mirror"].toBool();
25 inverted = jsonData["inverted"].toBool();
26 fov = jsonData["fov"].isNull() ? NaN::d : jsonData["fov"].toDouble();
27 erectObserver = jsonData["erectObserver"].toBool();
28}
29
30QJsonObject SkyMapView::toJson() const
31{
32 return QJsonObject
33 {
34 {"version", "1.0.0"},
35 {"useAltAz", useAltAz},
36 {"viewAngle", viewAngle},
37 {"mirror", mirror},
38 {"inverted", inverted},
39 {"fov", fov},
40 {"erectObserver", erectObserver}
41 };
42}
43
44// // // SkyMapViewManager // // //
45
46QList<SkyMapView> SkyMapViewManager::m_views;
47
48QList<SkyMapView> SkyMapViewManager::defaults()
49{
51
52 SkyMapView view;
53
54 view.name = i18nc("Set the sky-map view to zenith up", "Zenith Up");
55 view.useAltAz = true;
56 view.viewAngle = 0;
57 view.mirror = false;
58 view.inverted = false;
59 view.fov = NaN::d;
60 view.erectObserver = false;
61 views << view;
62
63 view.name = i18nc("Set the sky-map view to zenith down", "Zenith Down");
64 view.useAltAz = true;
65 view.viewAngle = 0;
66 view.mirror = false;
67 view.inverted = true;
68 view.fov = NaN::d;
69 view.erectObserver = false;
70 views << view;
71
72 view.name = i18nc("Set the sky-map view to north up", "North Up");
73 view.useAltAz = false;
74 view.viewAngle = 0;
75 view.mirror = false;
76 view.inverted = false;
77 view.fov = NaN::d;
78 view.erectObserver = false;
79 views << view;
80
81 view.name = i18nc("Set the sky-map view to north down", "North Down");
82 view.useAltAz = false;
83 view.viewAngle = 0;
84 view.mirror = false;
85 view.inverted = true;
86 view.fov = NaN::d;
87 view.erectObserver = false;
88 views << view;
89
90 view.name = i18nc("Set the sky-map view to match a Schmidt-Cassegrain telescope with erecting prism pointed upwards",
91 "SCT with upward diagonal");
92 view.useAltAz = true;
93 view.viewAngle = 0;
94 view.mirror = true;
95 view.inverted = false;
96 view.fov = NaN::d;
97 view.erectObserver = false;
98 views << view;
99
100 view.name = i18nc("Set the sky-map view to match the view through a typical Dobsonian telescope", "Typical Dobsonian");
101 view.useAltAz = true;
102 view.viewAngle = 45;
103 view.mirror = false;
104 view.inverted = true;
105 view.fov = NaN::d;
106 view.erectObserver = true;
107 views << view;
108
109 return views;
110}
111
113{
114 // FIXME, this is very inefficient
115 bool success = true;
116 Q_ASSERT(!!KStarsData::Instance());
117 if (!KStarsData::Instance())
118 {
119 qCCritical(KSTARS) << "Cannot save sky map views, no KStarsData instance.";
120 return false;
121 }
122 KSUserDB *db = KStarsData::Instance()->userdb();
123 Q_ASSERT(!!db);
124 if (!db)
125 {
126 qCCritical(KSTARS) << "Cannot save sky map views, no KSUserDB instance.";
127 return false;
128 }
129 success = success && db->DeleteAllSkyMapViews();
130 if (!success)
131 {
132 qCCritical(KSTARS) << "Failed to flush sky map views from the database";
133 return success;
134 }
135 for (const auto &view : m_views)
136 {
137 bool result = db->AddSkyMapView(view);
138 success = success && result;
139 Q_ASSERT(result);
140 if (!result)
141 {
142 qCCritical(KSTARS) << "Failed to commit Sky Map View " << view.name << " to the database!";
143 }
144 }
145 return success;
146}
147
149{
150 Q_ASSERT(!!KStarsData::Instance());
151 if (!KStarsData::Instance())
152 {
153 qCCritical(KSTARS) << "Cannot read sky map views, no KStarsData instance.";
154 return m_views;
155 }
156 KSUserDB *db = KStarsData::Instance()->userdb();
157 Q_ASSERT(!!db);
158 if (!db)
159 {
160 qCCritical(KSTARS) << "Cannot save sky map views, no KSUserDB instance.";
161 return m_views;
162 }
163 m_views.clear();
164 bool result = db->GetAllSkyMapViews(m_views);
165 Q_ASSERT(result);
166 if (!result)
167 {
168 qCCritical(KSTARS) << "Failed to read sky map views from the database!";
169 }
170 if (m_views.isEmpty())
171 {
172 m_views = defaults();
173 }
174 return m_views;
175}
176
178{
179 m_views.clear();
180}
181
182std::optional<SkyMapView> SkyMapViewManager::viewNamed(const QString &name)
183{
184 for (auto it = m_views.begin(); it != m_views.end(); ++it)
185 {
186 if (it->name == name)
187 {
188 return *it;
189 }
190 }
191 return std::nullopt;
192}
193
195{
196 for (auto it = m_views.begin(); it != m_views.end(); ++it)
197 {
198 if (it->name == name)
199 {
200 m_views.erase(it);
201 return true;
202 }
203 }
204 return false;
205}
Single class to delegate all User database I/O.
Definition ksuserdb.h:42
bool AddSkyMapView(const SkyMapView &view)
Adds a new sky map view row in the database.
bool GetAllSkyMapViews(QList< SkyMapView > &skyMapViewList)
Gets all the sky map view rows from the database.
bool DeleteAllSkyMapViews()
Deletes all the sky map views stored in the database.
KSUserDB * userdb()
Definition kstarsdata.h:217
static void drop()
Drop the list.
static bool save()
Commit the list of views to the database.
static bool removeView(const QString &name)
Remove a view Note: This is currently an O(N) operation.
static std::optional< SkyMapView > viewNamed(const QString &name)
Get the view with the given name.
static const QList< SkyMapView > & readViews()
Read the list of views from the database.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
QString name(StandardAction id)
iterator begin()
void clear()
iterator end()
iterator erase(const_iterator begin, const_iterator end)
bool isEmpty() const const
Carries parameters of a sky map view.
Definition skymapview.h:19
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:04:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.