Kstars

satellitescomponent.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Jerome SONRIER <[email protected]>
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 
34 void SatellitesComponent::loadData()
35 {
36  KSFileReader fileReader;
37  QString line;
38  QStringList group_infos;
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 
136 {
137  Q_UNUSED(skyp);
138 }
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 
192 {
193  return m_groups;
194 }
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 {
248  Q_UNUSED(exact)
249  return nameHash[name.toLower()];
250 }
void append(const T &value)
bool isVisible()
Definition: satellite.cpp:1294
QString errorString() const const
QFuture< T > run(Function function,...)
void update(KSNumbers *num) override
Update satellites position.
void setValue(int progress)
static void AddLabel(SkyObject *obj, label_t type)
static version of addLabel() below.
Definition: skylabeler.h:135
void drawTrails(SkyPainter *skyp) override
Draw trails for objects.
~SatellitesComponent() override
Destructor.
virtual bool open(QIODevice::OpenMode mode) override
Stores dms coordinates for a point in the sky. for converting between coordinate systems.
Definition: skypoint.h:44
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
int count(const T &value) const const
bool open(const QString &fname)
opens the file fname from the QStandardPaths::AppLocalDataLocation directory and uses that file for t...
WindowModal
QNetworkReply::NetworkError error() const const
QString trimmed() const const
void clear()
virtual QString name(void) const
Definition: skyobject.h:145
void setLabelText(const QString &text)
void setPen(const QPen &pen)
sets the pen used for drawing labels on the sky.
Definition: skylabeler.cpp:197
bool selected()
Definition: satellite.cpp:1299
virtual bool drawSatellite(Satellite *sat)=0
Draw a satellite.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QNetworkReply * get(const QNetworkRequest &request)
SatellitesComponent(SkyComposite *parent=nullptr)
Constructor.
virtual void emitProgressText(const QString &message)
Emit signal about progress.
int size() const const
QString i18n(const char *text, const TYPE &arg...)
Store several time-dependent astronomical quantities.
Definition: ksnumbers.h:42
bool isEmpty() const const
Satellite * findSatellite(QString name)
Search a satellite by name.
bool hasMoreLines() const
Definition: ksfilereader.h:100
bool isEmpty() const const
void setWindowModality(Qt::WindowModality windowModality)
void setWindowTitle(const QString &)
void readTLE()
Read TLE file of the group and create all satellites found in the file.
const T & at(int i) const const
QString toLocalFile() const const
Draws things on the sky, without regard to backend.
Definition: skypainter.h:37
virtual void close() override
QList< SatelliteGroup * > groups()
dms angularDistanceTo(const SkyPoint *sp, double *const positionAngle=nullptr) const
Computes the angular distance between two SkyObjects.
Definition: skypoint.cpp:899
void updateSatellitesPos()
Compute current position of the each satellites in the group.
void draw(SkyPainter *skyp) override
Draw all satellites.
QString toLower() const const
void drawLabel(Satellite *sat, const QPointF &pos)
Draw label of a satellite.
void updateTLEs()
Download new TLE files.
bool drawNameLabel(SkyObject *obj, const QPointF &_p, const qreal padding_factor=1)
Tries to draw a label for an object.
Definition: skylabeler.cpp:161
const double & Degrees() const
Definition: dms.h:141
QString readLine()
increments the line number and returns the next line from the file as a QString.
Definition: ksfilereader.h:105
const QChar at(int position) const const
void clear()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
SkyObject * objectNearest(SkyPoint *p, double &maxrad) override
Search the nearest satellite from point p.
QByteArray readAll()
bool contains(const Key &key) const const
SkyObject * findByName(const QString &name, bool exact=true) override
Return object given name.
Information about an object in the sky.
Definition: skyobject.h:41
qint64 write(const char *data, qint64 maxSize)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 04:00:57 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.