Kstars

eclipsetool.cpp
1 /*
2  SPDX-FileCopyrightText: 2018 Valentin Boettcher <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "eclipsetool.h"
8 #include "ui_eclipsetool.h"
9 #include "ksplanetbase.h"
10 #include "kstarsdata.h"
11 #include "geolocation.h"
12 #include "dialogs/locationdialog.h"
13 #include "kstars.h"
14 #include "skymap.h"
15 
16 #include <QtConcurrent>
17 #include <QFileDialog>
18 #include <QErrorMessage>
19 #include <QMenu>
20 
21 EclipseTool::EclipseTool(QWidget *parent) :
22  QFrame(parent),
23  ui(new Ui::EclipseTool)
24 {
25  ui->setupUi(this);
26 
27  // set up the eclipse-partner selection
28  ui->Obj1ComboBox->addItem(i18n("Earth Shadow"), KSPlanetBase::EARTH_SHADOW);
29  ui->Obj2ComboBox->addItem(i18n("Moon"), KSPlanetBase::MOON);
30 
31  KStarsData *kd = KStarsData::Instance();
33  KStarsDateTime dtStop(dtStart.djd() + 365.24l); // one year
34 
35  m_geoLocation = kd->geo();
36  ui->LocationButton->setText(m_geoLocation->fullName());
37 
38  ui->startDate->setDateTime(dtStart);
39  ui->stopDate->setDateTime(dtStop);
40 
41  ui->ClearButton->setIcon(QIcon::fromTheme("edit-clear"));
42 
43  // set up slots
44  connect(ui->LocationButton, &QPushButton::clicked, this, &EclipseTool::slotLocation);
45  connect(ui->ComputeButton, &QPushButton::clicked, this, [&, this]() {
46  // switch to progress bar
47  ui->computeStack->setCurrentIndex(1);
48 
49  // reset progress
50  ui->progressBar->setValue(0);
51 
52  QtConcurrent::run(this, &EclipseTool::slotCompute);
53  });
54 
55  connect(ui->ClearButton, &QPushButton::clicked, &m_model, &EclipseModel::reset);
56  connect(ui->ExportButton, &QPushButton::clicked, &m_model, &EclipseModel::exportAsCsv);
57 
58  // eclipse table
59  ui->tableView->setModel(&m_model);
60  ui->tableView->horizontalHeader()->setStretchLastSection(true);
61  ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
62 
63  // double click to view
64  connect(ui->tableView, &QTableView::doubleClicked, this, [this](const QModelIndex &index){
65  slotView(m_model.getEvent(index.row()));
66  });
67 
68  connect(ui->tableView, &QTableView::customContextMenuRequested, this, &EclipseTool::slotContextMenu);
69 
70  ui->progressBar->setMinimum(0);
71  ui->progressBar->setMaximum(100);
72 }
73 
74 EclipseTool::~EclipseTool()
75 {
76  delete ui;
77 }
78 
79 void EclipseTool::slotLocation()
80 {
81  QPointer<LocationDialog> ld(new LocationDialog(this));
82  if (ld->exec() == QDialog::Accepted && ld)
83  {
84  m_geoLocation = ld->selectedCity();
85  ui->LocationButton->setText(m_geoLocation->fullName());
86  }
87  delete ld;
88 }
89 
90 // NOTE: This will need redesign some day.
91 void EclipseTool::slotCompute()
92 {
93  EclipseHandler * handler;
94  KStarsDateTime dtStart(ui->startDate->dateTime()); // start date
95  KStarsDateTime dtStop(ui->stopDate->dateTime()); // stop date
96  long double startJD = dtStart.djd(); // start julian day
97  long double stopJD = dtStop.djd();
98 
99  if(ui->Obj1ComboBox->currentData().toInt() == KSPlanetBase::EARTH_SHADOW && ui->Obj2ComboBox->currentData().toInt() == KSPlanetBase::MOON){
100  handler = new LunarEclipseHandler();
101  } else
102  return;
103 
105  connect(handler, &EclipseHandler::signalProgress, ui->progressBar, &QProgressBar::setValue);
106  connect(handler, &EclipseHandler::signalComputationFinished, this, &EclipseTool::slotFinished);
107 
108  handler->setGeoLocation(m_geoLocation);
109 
110  // let's go
111  EclipseHandler::EclipseVector eclipses = handler->computeEclipses(startJD, stopJD);
112  delete handler;
113 }
114 
115 void EclipseTool::slotFinished()
116 {
117  ui->computeStack->setCurrentIndex(0);
118 }
119 
120 void EclipseTool::slotContextMenu(QPoint pos)
121 {
122  int row = ui->tableView->indexAt(pos).row();
123  EclipseEvent_s event = m_model.getEvent(row);
124 
125  QMenu * menu = new QMenu(this);
126  QAction * view = new QAction(i18n("View in SkyMap"), menu);
127  connect(view, &QAction::triggered, this, [=]{
128  slotView(event);
129  });
130  menu->addAction(view);
131 
132  if(event->hasDetails()){
133  QAction * details = new QAction(i18n("Show Details"), menu);
134  connect(details, &QAction::triggered, this, [=] {
135  event->slotShowDetails(); // call virtual
136  });
137  menu->addAction(details);
138  }
139 
140  menu->popup(ui->tableView->viewport()->mapToGlobal(pos));
141 }
142 
143 void EclipseTool::slotView(EclipseEvent_s event)
144 {
145  SkyMap * map = KStars::Instance()->map();
146  KStarsData * data = KStarsData::Instance();
147  KStarsDateTime dt;
148 
149  dt.setDJD(event->getJD());
150  data->changeDateTime(dt);
151  data->setLocation(event->getGeolocation());
152 
153  map->setClickedObject(event->getEclipsingObjectFromSkyComposite());
154  map->setClickedPoint(map->clickedObject());
155  map->slotCenter();
156 }
157 
158 EclipseModel::EclipseModel(QObject *parent) : QAbstractTableModel (parent)
159 {
160 
161 }
162 
163 int EclipseModel::rowCount(const QModelIndex &) const
164 {
165  return m_eclipses.length();
166 }
167 
168 QVariant EclipseModel::data(const QModelIndex &index, int role) const
169 {
170  int row = index.row();
171  int col = index.column();
172 
173  if(role != Qt::DisplayRole)
174  return QVariant();
175 
176  EclipseEvent * event = m_eclipses[row].get();
177 
178  switch(col) {
179  case 0: /* Date */ {
180  KStarsDateTime dt(event->getJD());
181  return dt.toString(Qt::ISODate);
182  }
183  case 1: // Eclipsing Obj
184  return event->getEclipsingObjectName();
185  case 2: // Eclipsed Obj
186  return event->getEclipsedObjectName();
187  case 3: /* Eclipse Type */ {
188  switch(event->getType()) {
189  case EclipseEvent::FULL:
190  return QString(i18n("Full"));
191  case EclipseEvent::PARTIAL:
192  return QString(i18n("Partial"));
193  }
194  break;
195  }
196  case 4: // Extra Info
197  return event->getExtraInfo();
198  }
199 
200  return QVariant();
201 }
202 
203 void EclipseModel::slotAddEclipse(EclipseEvent_s eclipse)
204 {
205  m_eclipses.append(eclipse);
206  emit layoutChanged(); // there must be a better way
207 }
208 
210 {
211  int i, j;
212  QByteArray line;
213 
214  QFileDialog dialog;
215  dialog.setNameFilter(i18n("CSV Files (*.csv)"));
216  dialog.setDefaultSuffix("csv");
217  dialog.setWindowTitle(i18nc("@title:window", "Export Eclipses"));
218 
219  QString fname;
220  if(dialog.exec())
221  fname = dialog.selectedFiles()[0];
222  else {
223  QErrorMessage msg;
224  msg.showMessage(i18n("Could not export."));
225  return;
226  }
227 
228  QFile file(fname);
229 
231 
232  for (i = 0; i < rowCount(); ++i)
233  {
234  for (j = 0; j < columnCount(); ++j)
235  {
236  line.append(data(index(i, j), Qt::DisplayRole).toByteArray());
237  if (j < columnCount() - 1)
238  line.append(";");
239  else
240  line.append("\n");
241  }
242  file.write(line);
243  line.clear();
244  }
245 
246  file.close();
247 }
248 
249 QVariant EclipseModel::headerData(int section, Qt::Orientation orientation, int role) const
250 {
251  if (role == Qt::DisplayRole)
252  {
253  if (orientation == Qt::Horizontal) {
254  switch (section)
255  {
256  case 0:
257  return QString("Date");
258  case 1:
259  return QString("Eclipsing Object");
260  case 2:
261  return QString("Eclipsed Object");
262  case 3:
263  return QString("Eclipse Type");
264  case 4:
265  return QString("Extra Information");
266  }
267  }
268  }
269  return QVariant();
270 }
271 
273 {
274  emit beginResetModel();
275  emit endResetModel();
276 }
static KStarsDateTime currentDateTime()
Extension of QDateTime for KStars KStarsDateTime can represent the date/time as a Julian Day,...
The EclipseEvent class.
void doubleClicked(const QModelIndex &index)
void signalComputationFinished()
signalComputationFinished
DisplayRole
void setLocation(const GeoLocation &l)
Set the GeoLocation according to the argument.
Definition: kstarsdata.cpp:403
QStringList selectedFiles() const const
QString fullName() const
Definition: geolocation.cpp:46
virtual bool event(QEvent *e) override
QByteArray & append(char ch)
virtual bool open(QIODevice::OpenMode mode) override
void customContextMenuRequested(const QPoint &pos)
virtual EclipseVector computeEclipses(long double startJD, long double endJD)=0
compute
int column() const const
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
void append(const T &value)
void clicked(bool checked)
QIcon fromTheme(const QString &name)
SkyMap * map() const
Definition: kstars.h:143
void setDefaultSuffix(const QString &suffix)
void exportAsCsv()
exportAsCsv
CustomContextMenu
void clear()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
The EclipseHandler class.
static KStars * Instance()
Definition: kstars.h:125
QAction * addAction(const QString &text)
void signalProgress(int)
signalProgress
The LunarEclipseHandler class.
virtual bool event(QEvent *e)
void changeDateTime(const KStarsDateTime &newDate)
Change the current simulation date/time to the KStarsDateTime argument.
Definition: kstarsdata.cpp:327
QString i18n(const char *text, const TYPE &arg...)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
Orientation
GeoLocation * geo()
Definition: kstarsdata.h:229
void setWindowTitle(const QString &)
virtual int exec()
virtual void close() override
void setupUi(QWidget *widget)
int row() const const
void triggered(bool checked)
void popup(const QPoint &p, QAction *atAction)
void showMessage(const QString &message)
EclipseEvent_s getEvent(int index)
getEvent
Definition: eclipsetool.h:66
void signalEventFound(EclipseEvent_s event)
signalEventFound
Canvas widget for displaying the sky bitmap; also handles user interaction events.
Definition: skymap.h:52
int length() const const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void slotAddEclipse(EclipseEvent_s eclipse)
slotAddEclipse
void setNameFilter(const QString &filter)
void reset()
reset
The EclipseTool class.
Definition: eclipsetool.h:89
void setGeoLocation(GeoLocation *geo)
Sets the geographic location to compute conjunctions at.
QString toString(Qt::DateFormat format) const const
void setValue(int value)
QFuture< void > map(Sequence &sequence, MapFunctor function)
qint64 write(const char *data, qint64 maxSize)
void setDJD(long double jd)
Assign the static_cast<long double> Julian Day value, which includes the time of day encoded in the f...
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 04:00:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.