Kstars

satellitescomponent.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Jerome SONRIER <jsid@emor3j.fr.eu.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "satellitescomponent.h"
8
9#include "ksfilereader.h"
10#include "ksnotification.h"
11#include "kstarsdata.h"
12#include "Options.h"
13#include "skylabeler.h"
14#include "skymap.h"
15#include "skypainter.h"
16#include "skyobjects/satellite.h"
17
18#include <QNetworkAccessManager>
19#include <QNetworkReply>
20#include <QProgressDialog>
21#include <QtConcurrent>
22
24{
25 QtConcurrent::run(this, &SatellitesComponent::loadData);
26}
27
29{
30 qDeleteAll(m_groups);
31 m_groups.clear();
32}
33
34void SatellitesComponent::loadData()
35{
37 QString line;
39
40 if (!fileReader.open("satellites.dat"))
41 return;
42
43 emitProgressText(i18n("Loading satellites"));
44
45 while (fileReader.hasMoreLines())
46 {
47 line = fileReader.readLine();
48 if (line.trimmed().isEmpty() || line.at(0) == '#')
49 continue;
50 group_infos = line.split(';');
51 m_groups.append(new SatelliteGroup(group_infos.at(0), group_infos.at(1), QUrl(group_infos.at(2))));
52 }
53
54 objectNames(SkyObject::SATELLITE).clear();
55 objectLists(SkyObject::SATELLITE).clear();
56
57 foreach (SatelliteGroup *group, m_groups)
58 {
59 for (int i = 0; i < group->size(); i++)
60 {
61 Satellite *sat = group->at(i);
62
63 if (sat->selected() && nameHash.contains(sat->name().toLower()) == false)
64 {
65 objectNames(SkyObject::SATELLITE).append(sat->name());
66 objectLists(SkyObject::SATELLITE).append(QPair<QString, const SkyObject *>(sat->name(), sat));
67 nameHash[sat->name().toLower()] = sat;
68 }
69 }
70 }
71}
72
74{
75 return Options::showSatellites();
76}
77
79{
80 // Return if satellites must not be draw
81 if (!selected())
82 return;
83
84 foreach (SatelliteGroup *group, m_groups)
85 {
86 group->updateSatellitesPos();
87 }
88}
89
91{
92#ifndef KSTARS_LITE
93 // Return if satellites must not be draw
94 if (!selected())
95 return;
96
97 bool hideLabels = (!Options::showSatellitesLabels() || (SkyMap::Instance()->isSlewing() && Options::hideLabels()));
98
99 foreach (SatelliteGroup *group, m_groups)
100 {
101 for (int i = 0; i < group->size(); i++)
102 {
103 Satellite *sat = group->at(i);
104
105 if (sat->selected())
106 {
107 bool drawn = false;
108 if (Options::showVisibleSatellites())
109 {
110 if (sat->isVisible())
111 drawn = skyp->drawSatellite(sat);
112 }
113 else
114 {
115 drawn = skyp->drawSatellite(sat);
116 }
117
118 if (drawn && !hideLabels)
119 SkyLabeler::AddLabel(sat, SkyLabeler::SATELLITE_LABEL);
120 }
121 }
122 }
123#else
124 Q_UNUSED(skyp);
125#endif
126}
127
129{
130 SkyLabeler *labeler = SkyLabeler::Instance();
131 labeler->setPen(KStarsData::Instance()->colorScheme()->colorNamed("SatLabelColor"));
132 labeler->drawNameLabel(sat, pos);
133}
134
139
141{
142 int i = 0;
143 QProgressDialog progressDlg(i18n("Update TLEs..."), i18n("Abort"), 0, m_groups.count());
144 progressDlg.setWindowModality(Qt::WindowModal);
145 progressDlg.setValue(0);
146
147 foreach (SatelliteGroup *group, m_groups)
148 {
149 if (progressDlg.wasCanceled())
150 return;
151
152 if (group->tleUrl().isEmpty())
153 continue;
154
155 progressDlg.setLabelText(i18n("Update %1 satellites", group->name()));
156 progressDlg.setWindowTitle(i18nc("@title:window", "Satellite Orbital Elements Update"));
157
158 QNetworkAccessManager manager;
159 QNetworkReply *response = manager.get(QNetworkRequest(group->tleUrl()));
160
161 // Wait synchronously
162 QEventLoop event;
163 QObject::connect(response, SIGNAL(finished()), &event, SLOT(quit()));
164 event.exec();
165
166 if (response->error() == QNetworkReply::NoError)
167 {
168 QFile file(group->tleFilename().toLocalFile());
169 if (file.open(QFile::WriteOnly))
170 {
171 file.write(response->readAll());
172 file.close();
173 group->readTLE();
174 group->updateSatellitesPos();
175 progressDlg.setValue(++i);
176 }
177 else
178 {
179 KSNotification::error(file.errorString());
180 return;
181 }
182 }
183 else
184 {
185 KSNotification::error(response->errorString());
186 return;
187 }
188 }
189}
190
195
197{
198 foreach (SatelliteGroup *group, m_groups)
199 {
200 for (int i = 0; i < group->size(); i++)
201 {
202 Satellite *sat = group->at(i);
203 if (sat->name() == name)
204 return sat;
205 }
206 }
207
208 return nullptr;
209}
210
212{
213 if (!selected())
214 return nullptr;
215
216 //KStarsData* data = KStarsData::Instance();
217
218 SkyObject *oBest = nullptr;
219 double rBest = maxrad;
220 double r;
221
222 foreach (SatelliteGroup *group, m_groups)
223 {
224 for (int i = 0; i < group->size(); i++)
225 {
226 Satellite *sat = group->at(i);
227 if (!sat->selected())
228 continue;
229
230 r = sat->angularDistanceTo(p).Degrees();
231 //qDebug() << Q_FUNC_INFO << sat->name();
232 //qDebug() << Q_FUNC_INFO << "r = " << r << " - max = " << rBest;
233 //qDebug() << Q_FUNC_INFO << "ra2=" << sat->ra().Degrees() << " - dec2=" << sat->dec().Degrees();
234 if (r < rBest)
235 {
236 rBest = r;
237 oBest = sat;
238 }
239 }
240 }
241
242 maxrad = rBest;
243 return oBest;
244}
245
247{
249 return nameHash[name.toLower()];
250}
I totally rewrote this because the earlier scheme of reading all the lines of a file into a buffer be...
There are several time-dependent values used in position calculations, that are not specific to an ob...
Definition ksnumbers.h:43
Represents a group of artificial satellites.
void updateSatellitesPos()
Compute current position of the each satellites in the group.
void readTLE()
Read TLE file of the group and create all satellites found in the file.
Represents an artificial satellites.
Definition satellite.h:23
bool isVisible()
bool selected()
SkyObject * objectNearest(SkyPoint *p, double &maxrad) override
Search the nearest satellite from point p.
Satellite * findSatellite(QString name)
Search a satellite by name.
void drawTrails(SkyPainter *skyp) override
Draw trails for objects.
QList< SatelliteGroup * > groups()
void updateTLEs()
Download new TLE files.
void drawLabel(Satellite *sat, const QPointF &pos)
Draw label of a satellite.
SatellitesComponent(SkyComposite *parent=nullptr)
Constructor.
~SatellitesComponent() override
Destructor.
void draw(SkyPainter *skyp) override
Draw all satellites.
SkyObject * findByName(const QString &name, bool exact=true) override
Return object given name.
void update(KSNumbers *num) override
Update satellites position.
SkyComponent represents an object on the sky map.
virtual void emitProgressText(const QString &message)
Emit signal about progress.
SkyComposite is a kind of container class for SkyComponent objects.
The purpose of this class is to prevent labels from overlapping.
Definition skylabeler.h:99
static void AddLabel(SkyObject *obj, label_t type)
static version of addLabel() below.
Definition skylabeler.h:135
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:145
Draws things on the sky, without regard to backend.
Definition skypainter.h:40
The sky coordinates of a point in the sky.
Definition skypoint.h:45
dms angularDistanceTo(const SkyPoint *sp, double *const positionAngle=nullptr) const
Computes the angular distance between two SkyObjects.
Definition skypoint.cpp:899
const double & Degrees() const
Definition dms.h:141
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
bool open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
virtual void close() override
void clear()
bool contains(const Key &key) const const
QString errorString() const const
qint64 write(const QByteArray &data)
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
void clear()
qsizetype count() const const
qsizetype size() const const
QNetworkReply * get(const QNetworkRequest &request)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
const QChar at(qsizetype position) const const
bool isEmpty() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString toLower() const const
QString trimmed() const const
WindowModal
QFuture< T > run(Function function,...)
bool isEmpty() const const
QString toLocalFile() const const
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.