Kstars

flagcomponent.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Jerome SONRIER <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "flagcomponent.h"
8 
9 #include "ksfilereader.h"
10 #include "kstarsdata.h"
11 #include "Options.h"
12 #ifdef KSTARS_LITE
13 #include "skymaplite.h"
14 #else
15 #include "skymap.h"
16 #endif
17 #include "skypainter.h"
18 #include "auxiliary/kspaths.h"
19 #include "projections/projector.h"
20 #include "skyobjects/skypoint.h"
21 
22 #include <KLocalizedString>
23 
24 #include <QDir>
25 #include <QtMath>
26 #include <QStandardPaths>
27 
29 {
30  // Add the default flag images to available images list
31  m_Names.append(i18n("No icon"));
32  m_Images.append(QImage());
33  m_Names.append(i18n("Default"));
34  m_Images.append(QImage(KSPaths::locate(QStandardPaths::AppLocalDataLocation, "defaultflag.gif")));
35 
36  QDir appDir(KSPaths::writableLocation(QStandardPaths::AppLocalDataLocation));
37  appDir.setNameFilters(QStringList() << "flag*");
38  // Add all other images found in user appdata directory
39  for (auto &item : appDir.entryList())
40  {
41  QString path = appDir.absoluteFilePath(item);
42  m_Images.append(QImage(path));
43 
44  QString fileName = item.replace(QRegExp("\\.[^.]*$"), QString()).replace(QRegExp("^flag"), QString()).replace('_', ' ');
45 
46  m_Names.append(fileName);
47  }
48 
49  loadFromFile();
50 }
51 
53 {
54  // Return if flags must not be draw
55  if (!selected())
56  return;
57 
58  // Return if no images are available
59  if (m_Names.size() < 1)
60  return;
61 
62  // Draw all flags
63  skyp->drawFlags();
64 }
65 
67 {
68  return Options::showFlags();
69 }
70 
72 {
73  bool imageFound = false;
74  QList<QStringList> flagList = KStarsData::Instance()->userdb()->GetAllFlags();
75 
76  for (auto &flagEntry : flagList)
77  {
78  // Read coordinates
79  dms r(flagEntry.at(0));
80  dms d(flagEntry.at(1));
81 
82  m_EpochCoords.append(qMakePair(r.Degrees(), d.Degrees()));
83 
84  std::shared_ptr<SkyPoint> flagPoint(new SkyPoint(r, d));
85 
86  // Convert to JNow
87  toJ2000(flagPoint.get(), flagEntry.at(2));
88 
89  flagPoint->updateCoordsNow(KStarsData::Instance()->updateNum());
90 
91  pointList().append(std::move(flagPoint));
92 
93  // Read epoch
94  m_Epoch.append(flagEntry.at(2));
95 
96  // Read image name
97  QString str = flagEntry.at(3);
98  str = str.replace('_', ' ');
99  for (int i = 0; i < m_Names.size(); ++i)
100  {
101  if (str == m_Names.at(i))
102  {
103  m_FlagImages.append(i);
104  imageFound = true;
105  }
106  }
107 
108  // If the image specified in db does not exist,
109  // use the default one
110  if (!imageFound)
111  m_FlagImages.append(0);
112 
113  imageFound = false;
114 
115  // If there is no label, use an empty string, red color and continue.
116  m_Labels.append(flagEntry.at(4));
117 
118  // color label
119 
120  QRegExp rxLabelColor("^#[a-fA-F0-9]{6}$");
121  if (rxLabelColor.exactMatch(flagEntry.at(5)))
122  {
123  m_LabelColors.append(QColor(flagEntry.at(5)));
124  }
125  else
126  {
127  m_LabelColors.append(QColor("red"));
128  }
129  }
130 }
131 
133 {
134  /*
135  TODO: This is a really bad way of storing things. Adding one flag shouldn't
136  involve writing a new file/table every time. Needs fixing.
137  */
138  KStarsData::Instance()->userdb()->DeleteAllFlags();
139 
140  for (int i = 0; i < size(); ++i)
141  {
142  KStarsData::Instance()->userdb()->AddFlag(QString::number(epochCoords(i).first),
143  QString::number(epochCoords(i).second), epoch(i),
144  imageName(i).replace(' ', '_'), label(i), labelColor(i).name());
145  }
146 }
147 
148 void FlagComponent::add(const SkyPoint &flagPoint, QString epoch, QString image, QString label, QColor labelColor)
149 {
150  //JM 2015-02-21: Insert original coords in list and convert skypint to JNow
151  // JM 2017-02-07: Discard above! We add RAW epoch coordinates to list.
152  // If not J2000, we convert to J2000
153  m_EpochCoords.append(qMakePair(flagPoint.ra().Degrees(), flagPoint.dec().Degrees()));
154 
155  std::shared_ptr<SkyPoint> newFlagPoint(new SkyPoint(flagPoint.ra(), flagPoint.dec()));
156 
157  toJ2000(newFlagPoint.get(), epoch);
158 
159  newFlagPoint->updateCoordsNow(KStarsData::Instance()->updateNum());
160 
161  pointList().append(std::move(newFlagPoint));
162  m_Epoch.append(epoch);
163 
164  for (int i = 0; i < m_Names.size(); i++)
165  {
166  if (image == m_Names.at(i))
167  m_FlagImages.append(i);
168  }
169 
170  m_Labels.append(label);
171  m_LabelColors.append(labelColor);
172 }
173 
174 void FlagComponent::remove(int index)
175 {
176  // check if flag of required index exists
177  if (index > pointList().size() - 1)
178  {
179  return;
180  }
181 
182  pointList().removeAt(index);
183  m_EpochCoords.removeAt(index);
184  m_Epoch.removeAt(index);
185  m_FlagImages.removeAt(index);
186  m_Labels.removeAt(index);
187  m_LabelColors.removeAt(index);
188 
189 // request SkyMap update
190 #ifndef KSTARS_LITE
191  SkyMap::Instance()->forceUpdate();
192 #endif
193 }
194 
195 void FlagComponent::updateFlag(int index, const SkyPoint &flagPoint, QString epoch, QString image, QString label,
196  QColor labelColor)
197 {
198  if (index < 0 || index > pointList().size() - 1)
199  return;
200 
201  std::shared_ptr<SkyPoint> existingFlag = pointList().at(index);
202 
203  existingFlag->setRA0(flagPoint.ra());
204  existingFlag->setDec0(flagPoint.dec());
205 
206  // If epoch not J2000, to convert to J2000
207  toJ2000(existingFlag.get(), epoch);
208 
209  existingFlag->updateCoordsNow(KStarsData::Instance()->updateNum());
210 
211  m_EpochCoords.replace(index, qMakePair(flagPoint.ra().Degrees(), flagPoint.dec().Degrees()));
212 
213  m_Epoch.replace(index, epoch);
214 
215  for (int i = 0; i < m_Names.size(); i++)
216  {
217  if (image == m_Names.at(i))
218  m_FlagImages.replace(index, i);
219  }
220 
221  m_Labels.replace(index, label);
222  m_LabelColors.replace(index, labelColor);
223 }
224 
226 {
227  return m_Names;
228 }
229 
231 {
232  return pointList().size();
233 }
234 
236 {
237  if (index > m_Epoch.size() - 1)
238  {
239  return QString();
240  }
241 
242  return m_Epoch.at(index);
243 }
244 
246 {
247  if (index > m_Labels.size() - 1)
248  {
249  return QString();
250  }
251 
252  return m_Labels.at(index);
253 }
254 
256 {
257  if (index > m_LabelColors.size() - 1)
258  {
259  return QColor();
260  }
261 
262  return m_LabelColors.at(index);
263 }
264 
266 {
267  if (index > m_FlagImages.size() - 1)
268  {
269  return QImage();
270  }
271 
272  if (m_FlagImages.at(index) > m_Images.size() - 1)
273  {
274  return QImage();
275  }
276 
277  return m_Images.at(m_FlagImages.at(index));
278 }
279 
281 {
282  if (index > m_FlagImages.size() - 1)
283  {
284  return QString();
285  }
286 
287  if (m_FlagImages.at(index) > m_Names.size() - 1)
288  {
289  return QString();
290  }
291 
292  return m_Names.at(m_FlagImages.at(index));
293 }
294 
296 {
297  return m_Images;
298 }
299 
301 {
302 #ifdef KSTARS_LITE
303  const Projector *proj = SkyMapLite::Instance()->projector();
304 #else
305  const Projector *proj = SkyMap::Instance()->projector();
306 #endif
307  QPointF pos = proj->toScreen(point);
308  QList<int> retVal;
309  int ptr = 0;
310 
311  for (auto &cp : pointList())
312  {
313  if (std::isnan(cp->ra().Degrees()) || std::isnan(cp->dec().Degrees()))
314  continue;
315  cp->EquatorialToHorizontal(KStarsData::Instance()->lst(), KStarsData::Instance()->geo()->lat());
316  QPointF pos2 = proj->toScreen(cp.get());
317  int dx = (pos2 - pos).x();
318  int dy = (pos2 - pos).y();
319 
320  if (qSqrt(dx * dx + dy * dy) <= pixelRadius)
321  {
322  //point is inside pixelRadius circle
323  retVal.append(ptr);
324  }
325 
326  ptr++;
327  }
328 
329  return retVal;
330 }
331 
333 {
334  if (index < 0 || index > m_Images.size() - 1)
335  {
336  return QImage();
337  }
338 
339  return m_Images.at(index);
340 }
341 
342 void FlagComponent::toJ2000(SkyPoint *p, QString epoch)
343 {
344  KStarsDateTime dt;
345  dt.setFromEpoch(epoch);
346 
347  if (dt.djd() == J2000)
348  return;
349 
350  p->catalogueCoord(dt.djd());
351 
352  // Store J2000 coords in RA0, DEC0
353  p->setRA0(p->ra());
354  p->setDec0(p->dec());
355 }
356 
358 {
359  if (index > m_FlagImages.size() - 1)
360  {
361  QPair<double, double> coord = qMakePair(0, 0);
362  return coord;
363  }
364 
365  return m_EpochCoords.at(index);
366 }
367 
369 {
370  if (!selected())
371  return;
372  KStarsData *data = KStarsData::Instance();
373 
374  for (auto &p : pointList())
375  {
376  if (num)
377  p->updateCoordsNow(num);
378 
379  p->EquatorialToHorizontal(data->lst(), data->geo()->lat());
380  }
381 }
void append(const T &value)
Extension of QDateTime for KStars KStarsDateTime can represent the date/time as a Julian Day,...
long double djd() const
void AddFlag(const QString &ra, const QString &dec, const QString &epoch, const QString &image_name, const QString &label, const QString &labelColor)
Add a new Flag with given parameters.
Definition: ksuserdb.cpp:939
QString number(int n, int base)
void DeleteAllFlags()
Erases all the flags from the database.
Definition: ksuserdb.cpp:924
QImage image(int index)
Get image.
void loadFromFile()
Load flags from flags.dat file.
Stores dms coordinates for a point in the sky. for converting between coordinate systems.
Definition: skypoint.h:44
void setDec0(dms d)
Sets Dec0, the catalog Declination.
Definition: skypoint.h:119
void remove(int index)
Remove a flag.
CachingDms * lst()
Definition: kstarsdata.h:223
KSUserDB * userdb()
Definition: kstarsdata.h:214
int size()
Return the numbers of flags.
QColor labelColor(int index)
Get label color.
QList< QImage > imageList()
Get images.
void setRA0(dms r)
Sets RA0, the catalog Right Ascension.
Definition: skypoint.h:94
void replace(int i, const T &value)
void EquatorialToHorizontal(const CachingDms *LST, const CachingDms *lat)
Determine the (Altitude, Azimuth) coordinates of the SkyPoint from its (RA, Dec) coordinates,...
Definition: skypoint.cpp:77
void draw(SkyPainter *skyp) override
Draw the object on the SkyMap skyp a pointer to the SkyPainter to use.
void saveToFile()
Save flags to flags.dat file.
void removeAt(int i)
QList< QStringList > GetAllFlags()
Returns a QList populated with all stored flags Order: const QString &ra, const QString &dec,...
Definition: ksuserdb.cpp:960
int size() const const
bool setFromEpoch(double e, EpochType type)
Set the Date/Time from an epoch value, represented as a double.
QStringList getNames()
Return image names.
QString i18n(const char *text, const TYPE &arg...)
void update(KSNumbers *num=nullptr) override
Update the sky position(s) of this component.
Store several time-dependent astronomical quantities.
Definition: ksnumbers.h:42
const Projector * projector() const
Get the current projector.
Definition: skymaplite.h:323
const CachingDms & dec() const
Definition: skypoint.h:269
const CachingDms * lat() const
Definition: geolocation.h:70
GeoLocation * geo()
Definition: kstarsdata.h:229
void add(const SkyPoint &flagPoint, QString epoch, QString image, QString label, QColor labelColor)
Add a flag.
const T & at(int i) const const
void setNameFilters(const QStringList &nameFilters)
virtual void updateCoordsNow(const KSNumbers *num)
updateCoordsNow Shortcut for updateCoords( const KSNumbers *num, false, nullptr, nullptr,...
Definition: skypoint.h:382
Draws things on the sky, without regard to backend.
Definition: skypainter.h:37
QString epoch(int index)
Get epoch.
bool exactMatch(const QString &str) const const
QString & replace(int position, int n, QChar after)
An angle, stored as degrees, but expressible in many ways.
Definition: dms.h:37
const CachingDms & ra() const
Definition: skypoint.h:263
QString absoluteFilePath(const QString &fileName) const const
void updateFlag(int index, const SkyPoint &flagPoint, QString epoch, QString image, QString label, QColor labelColor)
Update a flag.
FlagComponent(SkyComposite *)
Constructor.
QList< int > getFlagsNearPix(SkyPoint *point, int pixelRadius)
Get list of flag indexes near specified SkyPoint with radius specified in pixels.
bool selected() override
const double & Degrees() const
Definition: dms.h:141
QString label(int index)
Get label.
QPointF toScreen(const SkyPoint *o, bool oRefract=true, bool *onVisibleHemisphere=nullptr) const
This is exactly the same as toScreenVec but it returns a QPointF.
Definition: projector.cpp:93
QString imageName(int index)
Get image name.
const QChar at(int position) const const
void forceUpdate(bool now=false)
Recalculates the positions of objects in the sky, and then repaints the sky map.
Definition: skymap.cpp:1186
const Projector * projector() const
Get the current projector.
Definition: skymap.h:299
QStringList entryList(QDir::Filters filters, QDir::SortFlags sort) const const
virtual void drawFlags()=0
Draw flags.
SkyPoint catalogueCoord(long double jdf)
Computes the J2000.0 catalogue coordinates for this SkyPoint using the epoch removing aberration,...
Definition: skypoint.cpp:710
QPair< double, double > epochCoords(int index)
epochCoords return coordinates recorded in original epoch
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 04:00:54 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.