Kstars

starhopperdialog.cpp
1 /*
2  SPDX-FileCopyrightText: 2014 Utkarsh Simha <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "starhopperdialog.h"
8 
9 #include "kstars.h"
10 #include "ksutils.h"
11 #include "skymap.h"
12 #include "ksnotification.h"
13 #include "skymapcomposite.h"
14 #include "starhopper.h"
15 #include "targetlistcomponent.h"
16 #include "dialogs/detaildialog.h"
17 
18 StarHopperDialog::StarHopperDialog(QWidget *parent) : QDialog(parent), ui(new Ui::StarHopperDialog)
19 {
20  ui->setupUi(this);
21  m_lw = ui->listWidget;
22  m_Metadata = new QStringList();
23  ui->directionsLabel->setWordWrap(true);
24  m_sh.reset(new StarHopper());
25  connect(ui->NextButton, SIGNAL(clicked()), this, SLOT(slotNext()));
26  connect(ui->GotoButton, SIGNAL(clicked()), this, SLOT(slotGoto()));
27  connect(ui->DetailsButton, SIGNAL(clicked()), this, SLOT(slotDetails()));
28  connect(m_lw, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(slotGoto()));
29  connect(m_lw, SIGNAL(itemSelectionChanged()), this, SLOT(slotRefreshMetadata()));
30  connect(this, SIGNAL(finished(int)), this, SLOT(deleteLater()));
31 }
32 
33 StarHopperDialog::~StarHopperDialog()
34 {
35  TargetListComponent *t = getTargetListComponent();
36 
37  if (t->list)
38  t->list->clear();
39  SkyMap::Instance()->forceUpdate(true);
40  delete ui;
41 }
42 
43 void StarHopperDialog::starHop(const SkyPoint &startHop, const SkyPoint &stopHop, float fov, float maglim)
44 {
45  QList<StarObject *> *starList = m_sh->computePath(startHop, stopHop, fov, maglim, m_Metadata);
46 
47  if (!starList->empty())
48  {
49  foreach (StarObject *so, *starList)
50  {
51  setData(so);
52  }
53  slotRefreshMetadata();
54  m_skyObjList = KSUtils::castStarObjListToSkyObjList(starList);
55  starList->clear();
56  delete starList;
57  TargetListComponent *t = getTargetListComponent();
58  t->list.reset(m_skyObjList);
59  SkyMap::Instance()->forceUpdate(true);
60  }
61  else
62  {
63  delete starList;
64  KSNotification::error(i18n("Star-hopper algorithm failed. If you're trying a large star hop, try using a "
65  "smaller FOV or changing the source point"));
66  this->done(QDialog::Rejected);
67  }
68 }
69 
70 void StarHopperDialog::setData(StarObject *sobj)
71 {
72  QListWidgetItem *item = new QListWidgetItem();
73  QString starName;
74 
75  if (sobj->name() != "star")
76  {
77  starName = sobj->translatedLongName();
78  }
79  else if (sobj->getHDIndex())
80  {
81  starName = QString("HD%1").arg(QString::number(sobj->getHDIndex()));
82  }
83  else
84  {
85  starName = "";
86  starName += sobj->spchar();
87  starName += QString(" Star of mag %2").arg(QString::number(sobj->mag()));
88  }
89  item->setText(starName);
90  QVariant qv;
91 
92  qv.setValue(sobj);
93  item->setData(Qt::UserRole, qv);
94  m_lw->addItem(item);
95 }
96 
97 void StarHopperDialog::slotNext()
98 {
99  m_lw->setCurrentRow(m_lw->currentRow() + 1);
100  slotGoto();
101 }
102 
103 void StarHopperDialog::slotGoto()
104 {
105  slotRefreshMetadata();
106  SkyObject *skyobj = getStarData(m_lw->currentItem());
107 
108  if (skyobj != nullptr)
109  {
110  KStars *ks = KStars::Instance();
111  ks->map()->setClickedObject(skyobj);
112  ks->map()->setClickedPoint(skyobj);
113  ks->map()->slotCenter();
114  }
115 }
116 
117 void StarHopperDialog::slotDetails()
118 {
119  SkyObject *skyobj = getStarData(m_lw->currentItem());
120 
121  if (skyobj != nullptr)
122  {
123  DetailDialog *detailDialog =
124  new DetailDialog(skyobj, KStarsData::Instance()->ut(), KStarsData::Instance()->geo(), KStars::Instance());
125  detailDialog->exec();
126  delete detailDialog;
127  }
128 }
129 
130 SkyObject *StarHopperDialog::getStarData(QListWidgetItem *item)
131 {
132  if (!item)
133  return nullptr;
134  else
135  {
136  QVariant v = item->data(Qt::UserRole);
137  StarObject *starobj = v.value<StarObject *>();
138  return starobj;
139  }
140 }
141 
142 inline TargetListComponent *StarHopperDialog::getTargetListComponent()
143 {
144  return KStarsData::Instance()->skyComposite()->getStarHopRouteList();
145 }
146 
147 void StarHopperDialog::slotRefreshMetadata()
148 {
149  int row = m_lw->currentRow();
150 
151  qDebug() << Q_FUNC_INFO << "Slot RefreshMetadata";
152  if (row >= 0)
153  {
154  ui->directionsLabel->setText(m_Metadata->at(row));
155  }
156  else
157  {
158  ui->directionsLabel->setText(m_Metadata->at(0));
159  }
160  qDebug() << Q_FUNC_INFO << "Slot RefreshMetadata";
161 }
Helps planning star hopping.
Definition: starhopper.h:24
UserRole
QString number(int n, int base)
QString translatedLongName() const
Definition: skyobject.h:169
QString name(void) const override
If star is unnamed return "star" otherwise return the name.
Definition: starobject.h:130
Stores dms coordinates for a point in the sky. for converting between coordinate systems.
Definition: skypoint.h:44
T value() const const
void setClickedPoint(const SkyPoint *f)
Set the ClickedPoint to the skypoint given as an argument.
Definition: skymap.cpp:996
float mag() const
Definition: skyobject.h:206
SkyMap * map() const
Definition: kstars.h:143
void setValue(const T &value)
int getHDIndex() const
Definition: starobject.h:248
static KStars * Instance()
Definition: kstars.h:125
virtual QVariant data(int role) const const
bool empty() const const
QString i18n(const char *text, const TYPE &arg...)
virtual void setData(int role, const QVariant &value)
subclass of SkyObject specialized for stars.
Definition: starobject.h:32
void setClickedObject(SkyObject *o)
Set the ClickedObject pointer to the argument.
Definition: skymap.cpp:331
virtual int exec()
GeoCoordinates geo(const QVariant &location)
This is the main window for KStars. In addition to the GUI elements, the class contains the program c...
Definition: kstars.h:92
void setupUi(QWidget *widget)
void slotCenter()
Center the display at the point ClickedPoint.
Definition: skymap.cpp:345
SkyMapComposite * skyComposite()
Definition: kstarsdata.h:165
char spchar() const
Returns just the first character of the spectral type string.
Definition: starobject.cpp:554
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
Highlights objects present in certain lists by drawing "target" symbols around them.
void clear()
void forceUpdate(bool now=false)
Recalculates the positions of objects in the sky, and then repaints the sky map.
Definition: skymap.cpp:1186
void setText(const QString &text)
std::unique_ptr< SkyObjectList > list
Pointer to list of objects to draw.
Information about an object in the sky.
Definition: skyobject.h:41
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 04:00:58 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.