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#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
26 QtConcurrent::run(&SatellitesComponent::loadData, this);
27#else
28 QtConcurrent::run(this, &SatellitesComponent::loadData);
29#endif
30}
31
33{
34 qDeleteAll(m_groups);
35 m_groups.clear();
36}
37
38void SatellitesComponent::loadData()
39{
40 KSFileReader fileReader;
41 QString line;
42 QStringList group_infos;
43
44 if (!fileReader.open("satellites.dat"))
45 return;
46
47 emitProgressText(i18n("Loading satellites"));
48
49 while (fileReader.hasMoreLines())
50 {
51 line = fileReader.readLine();
52 if (line.trimmed().isEmpty() || line.at(0) == '#')
53 continue;
54 group_infos = line.split(';');
55 m_groups.append(new SatelliteGroup(group_infos.at(0), group_infos.at(1), QUrl(group_infos.at(2))));
56 }
57
58 objectNames(SkyObject::SATELLITE).clear();
59 objectLists(SkyObject::SATELLITE).clear();
60
61 foreach (SatelliteGroup *group, m_groups)
62 {
63 for (int i = 0; i < group->size(); i++)
64 {
65 Satellite *sat = group->at(i);
66
67 if (sat->selected() && nameHash.contains(sat->name().toLower()) == false)
68 {
69 objectNames(SkyObject::SATELLITE).append(sat->name());
70 objectLists(SkyObject::SATELLITE).append(QPair<QString, const SkyObject *>(sat->name(), sat));
71 nameHash[sat->name().toLower()] = sat;
72 }
73 }
74 }
75}
76
78{
79 return Options::showSatellites();
80}
81
83{
84 // Return if satellites must not be draw
85 if (!selected())
86 return;
87
88 foreach (SatelliteGroup *group, m_groups)
89 {
90 group->updateSatellitesPos();
91 }
92}
93
95{
96#ifndef KSTARS_LITE
97 // Return if satellites must not be draw
98 if (!selected())
99 return;
100
101 bool hideLabels = (!Options::showSatellitesLabels() || (SkyMap::Instance()->isSlewing() && Options::hideLabels()));
102
103 foreach (SatelliteGroup *group, m_groups)
104 {
105 for (int i = 0; i < group->size(); i++)
106 {
107 Satellite *sat = group->at(i);
108
109 if (sat->selected())
110 {
111 bool drawn = false;
112 if (Options::showVisibleSatellites())
113 {
114 if (sat->isVisible())
115 drawn = skyp->drawSatellite(sat);
116 }
117 else
118 {
119 drawn = skyp->drawSatellite(sat);
120 }
121
122 if (drawn && !hideLabels)
123 SkyLabeler::AddLabel(sat, SkyLabeler::SATELLITE_LABEL);
124 }
125 }
126 }
127#else
128 Q_UNUSED(skyp);
129#endif
130}
131
133{
134 SkyLabeler *labeler = SkyLabeler::Instance();
135 labeler->setPen(KStarsData::Instance()->colorScheme()->colorNamed("SatLabelColor"));
136 labeler->drawNameLabel(sat, pos);
137}
138
140{
141 Q_UNUSED(skyp);
142}
143
145{
146 int i = 0;
147 QProgressDialog progressDlg(i18n("Update TLEs..."), i18n("Abort"), 0, m_groups.count());
149 progressDlg.setValue(0);
150
151 foreach (SatelliteGroup *group, m_groups)
152 {
153 if (progressDlg.wasCanceled())
154 return;
155
156 if (group->tleUrl().isEmpty())
157 continue;
158
159 progressDlg.setLabelText(i18n("Update %1 satellites", group->name()));
160 progressDlg.setWindowTitle(i18nc("@title:window", "Satellite Orbital Elements Update"));
161
162 QNetworkAccessManager manager;
163 QNetworkReply *response = manager.get(QNetworkRequest(group->tleUrl()));
164
165 // Wait synchronously
166 QEventLoop event;
167 QObject::connect(response, SIGNAL(finished()), &event, SLOT(quit()));
168 event.exec();
169
170 if (response->error() == QNetworkReply::NoError)
171 {
172 QFile file(group->tleFilename().toLocalFile());
173 if (file.open(QFile::WriteOnly))
174 {
175 file.write(response->readAll());
176 file.close();
177 group->readTLE();
178 group->updateSatellitesPos();
179 progressDlg.setValue(++i);
180 }
181 else
182 {
183 KSNotification::error(file.errorString());
184 return;
185 }
186 }
187 else
188 {
189 KSNotification::error(response->errorString());
190 return;
191 }
192 }
193}
194
199
201{
202 foreach (SatelliteGroup *group, m_groups)
203 {
204 for (int i = 0; i < group->size(); i++)
205 {
206 Satellite *sat = group->at(i);
207 if (sat->name() == name)
208 return sat;
209 }
210 }
211
212 return nullptr;
213}
214
216{
217 if (!selected())
218 return nullptr;
219
220 //KStarsData* data = KStarsData::Instance();
221
222 SkyObject *oBest = nullptr;
223 double rBest = maxrad;
224 double r;
225
226 foreach (SatelliteGroup *group, m_groups)
227 {
228 for (int i = 0; i < group->size(); i++)
229 {
230 Satellite *sat = group->at(i);
231 if (!sat->selected())
232 continue;
233
234 r = sat->angularDistanceTo(p).Degrees();
235 //qDebug() << Q_FUNC_INFO << sat->name();
236 //qDebug() << Q_FUNC_INFO << "r = " << r << " - max = " << rBest;
237 //qDebug() << Q_FUNC_INFO << "ra2=" << sat->ra().Degrees() << " - dec2=" << sat->dec().Degrees();
238 if (r < rBest)
239 {
240 rBest = r;
241 oBest = sat;
242 }
243 }
244 }
245
246 maxrad = rBest;
247 return oBest;
248}
249
251{
252 Q_UNUSED(exact)
253 return nameHash[name.toLower()];
254}
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...
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
void setPen(const QPen &pen)
sets the pen used for drawing labels on the sky.
bool drawNameLabel(SkyObject *obj, const QPointF &_p, const qreal padding_factor=1)
Tries to draw a label for an object.
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
Draws things on the sky, without regard to backend.
Definition skypainter.h:40
virtual bool drawSatellite(Satellite *sat)=0
Draw a satellite.
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
QByteArray readAll()
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)
NetworkError error() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setLabelText(const QString &text)
void setValue(int progress)
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
void setWindowModality(Qt::WindowModality windowModality)
void setWindowTitle(const QString &)
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.