Kstars

eclipsetool.cpp
1/*
2 SPDX-FileCopyrightText: 2018 Valentin Boettcher <valentin@boettcher.cf>
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
21EclipseTool::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#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
53 QtConcurrent::run(&EclipseTool::slotCompute, this);
54#else
55 QtConcurrent::run(this, &EclipseTool::slotCompute);
56#endif
57 });
58
59 connect(ui->ClearButton, &QPushButton::clicked, &m_model, &EclipseModel::reset);
60 connect(ui->ExportButton, &QPushButton::clicked, &m_model, &EclipseModel::exportAsCsv);
61
62 // eclipse table
63 ui->tableView->setModel(&m_model);
64 ui->tableView->horizontalHeader()->setStretchLastSection(true);
65 ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
66
67 // double click to view
68 connect(ui->tableView, &QTableView::doubleClicked, this, [this](const QModelIndex &index){
69 slotView(m_model.getEvent(index.row()));
70 });
71
72 connect(ui->tableView, &QTableView::customContextMenuRequested, this, &EclipseTool::slotContextMenu);
73
74 ui->progressBar->setMinimum(0);
75 ui->progressBar->setMaximum(100);
76}
77
78EclipseTool::~EclipseTool()
79{
80 delete ui;
81}
82
83void EclipseTool::slotLocation()
84{
86 if (ld->exec() == QDialog::Accepted && ld)
87 {
88 m_geoLocation = ld->selectedCity();
89 ui->LocationButton->setText(m_geoLocation->fullName());
90 }
91 delete ld;
92}
93
94// NOTE: This will need redesign some day.
95void EclipseTool::slotCompute()
96{
97 EclipseHandler * handler;
98 KStarsDateTime dtStart(ui->startDate->dateTime()); // start date
99 KStarsDateTime dtStop(ui->stopDate->dateTime()); // stop date
100 long double startJD = dtStart.djd(); // start julian day
101 long double stopJD = dtStop.djd();
102
103 if(ui->Obj1ComboBox->currentData().toInt() == KSPlanetBase::EARTH_SHADOW && ui->Obj2ComboBox->currentData().toInt() == KSPlanetBase::MOON){
104 handler = new LunarEclipseHandler();
105 } else
106 return;
107
110 connect(handler, &EclipseHandler::signalComputationFinished, this, &EclipseTool::slotFinished);
111
112 handler->setGeoLocation(m_geoLocation);
113
114 // let's go
115 EclipseHandler::EclipseVector eclipses = handler->computeEclipses(startJD, stopJD);
116 delete handler;
117}
118
119void EclipseTool::slotFinished()
120{
121 ui->computeStack->setCurrentIndex(0);
122}
123
124void EclipseTool::slotContextMenu(QPoint pos)
125{
126 int row = ui->tableView->indexAt(pos).row();
127 EclipseEvent_s event = m_model.getEvent(row);
128
129 QMenu * menu = new QMenu(this);
130 QAction * view = new QAction(i18n("View in SkyMap"), menu);
131 connect(view, &QAction::triggered, this, [=]{
132 slotView(event);
133 });
134 menu->addAction(view);
135
136 if(event->hasDetails()){
137 QAction * details = new QAction(i18n("Show Details"), menu);
138 connect(details, &QAction::triggered, this, [=] {
139 event->slotShowDetails(); // call virtual
140 });
141 menu->addAction(details);
142 }
143
144 menu->popup(ui->tableView->viewport()->mapToGlobal(pos));
145}
146
147void EclipseTool::slotView(EclipseEvent_s event)
148{
150 KStarsData * data = KStarsData::Instance();
152
153 dt.setDJD(event->getJD());
154 data->changeDateTime(dt);
155 data->setLocation(event->getGeolocation());
156
157 map->setClickedObject(event->getEclipsingObjectFromSkyComposite());
158 map->setClickedPoint(map->clickedObject());
159 map->slotCenter();
160}
161
162EclipseModel::EclipseModel(QObject *parent) : QAbstractTableModel (parent)
163{
164
165}
166
167int EclipseModel::rowCount(const QModelIndex &) const
168{
169 return m_eclipses.length();
170}
171
172QVariant EclipseModel::data(const QModelIndex &index, int role) const
173{
174 int row = index.row();
175 int col = index.column();
176
177 if(role != Qt::DisplayRole)
178 return QVariant();
179
180 EclipseEvent * event = m_eclipses[row].get();
181
182 switch(col) {
183 case 0: /* Date */ {
184 KStarsDateTime dt(event->getJD());
185 return dt.toString(Qt::ISODate);
186 }
187 case 1: // Eclipsing Obj
188 return event->getEclipsingObjectName();
189 case 2: // Eclipsed Obj
190 return event->getEclipsedObjectName();
191 case 3: /* Eclipse Type */ {
192 switch(event->getType()) {
193 case EclipseEvent::FULL:
194 return QString(i18n("Full"));
195 case EclipseEvent::PARTIAL:
196 return QString(i18n("Partial"));
197 }
198 break;
199 }
200 case 4: // Extra Info
201 return event->getExtraInfo();
202 }
203
204 return QVariant();
205}
206
207void EclipseModel::slotAddEclipse(EclipseEvent_s eclipse)
208{
209 m_eclipses.append(eclipse);
210 emit layoutChanged(); // there must be a better way
211}
212
214{
215 int i, j;
216 QByteArray line;
217
218 QFileDialog dialog;
219 dialog.setNameFilter(i18n("CSV Files (*.csv)"));
220 dialog.setDefaultSuffix("csv");
221 dialog.setWindowTitle(i18nc("@title:window", "Export Eclipses"));
222
223 QString fname;
224 if(dialog.exec())
225 fname = dialog.selectedFiles()[0];
226 else {
227 QErrorMessage msg;
228 msg.showMessage(i18n("Could not export."));
229 return;
230 }
231
232 QFile file(fname);
233
235
236 for (i = 0; i < rowCount(); ++i)
237 {
238 for (j = 0; j < columnCount(); ++j)
239 {
240 line.append(data(index(i, j), Qt::DisplayRole).toByteArray());
241 if (j < columnCount() - 1)
242 line.append(";");
243 else
244 line.append("\n");
245 }
246 file.write(line);
247 line.clear();
248 }
249
250 file.close();
251}
252
253QVariant EclipseModel::headerData(int section, Qt::Orientation orientation, int role) const
254{
255 if (role == Qt::DisplayRole)
256 {
257 if (orientation == Qt::Horizontal) {
258 switch (section)
259 {
260 case 0:
261 return QString("Date");
262 case 1:
263 return QString("Eclipsing Object");
264 case 2:
265 return QString("Eclipsed Object");
266 case 3:
267 return QString("Eclipse Type");
268 case 4:
269 return QString("Extra Information");
270 }
271 }
272 }
273 return QVariant();
274}
275
277{
278 emit beginResetModel();
279 emit endResetModel();
280}
void setGeoLocation(GeoLocation *geo)
Sets the geographic location to compute conjunctions at.
The EclipseEvent class.
The EclipseHandler class.
void signalProgress(int)
signalProgress
void signalEventFound(EclipseEvent_s event)
signalEventFound
void signalComputationFinished()
signalComputationFinished
virtual EclipseVector computeEclipses(long double startJD, long double endJD)=0
compute
void reset()
reset
EclipseEvent_s getEvent(int index)
getEvent
Definition eclipsetool.h:66
void slotAddEclipse(EclipseEvent_s eclipse)
slotAddEclipse
void exportAsCsv()
exportAsCsv
The EclipseTool class.
Definition eclipsetool.h:90
QString fullName() const
KStarsData is the backbone of KStars.
Definition kstarsdata.h:74
void setLocation(const GeoLocation &l)
Set the GeoLocation according to the argument.
void changeDateTime(const KStarsDateTime &newDate)
Change the current simulation date/time to the KStarsDateTime argument.
GeoLocation * geo()
Definition kstarsdata.h:232
Extension of QDateTime for KStars KStarsDateTime can represent the date/time as a Julian Day,...
void setDJD(long double jd)
Assign the static_cast<long double> Julian Day value, which includes the time of day encoded in the f...
static KStarsDateTime currentDateTime()
SkyMap * map() const
Definition kstars.h:139
static KStars * Instance()
Definition kstars.h:121
Dialog for changing the geographic location of the observer.
The LunarEclipseHandler class.
This is the canvas on which the sky is painted.
Definition skymap.h:54
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void clicked(bool checked)
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
void doubleClicked(const QModelIndex &index)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
void triggered(bool checked)
QByteArray & append(QByteArrayView data)
void clear()
QString toString(QStringView format, QCalendar cal) const const
virtual int exec()
void showMessage(const QString &message)
bool open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
virtual void close() override
void setDefaultSuffix(const QString &suffix)
QStringList selectedFiles() const const
void setNameFilter(const QString &filter)
virtual bool event(QEvent *e) override
QIcon fromTheme(const QString &name)
qint64 write(const QByteArray &data)
void append(QList< T > &&value)
qsizetype length() const const
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
void popup(const QPoint &p, QAction *atAction)
int column() const const
int row() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool event(QEvent *e)
void setValue(int value)
CustomContextMenu
DisplayRole
Orientation
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void customContextMenuRequested(const QPoint &pos)
void setupUi(QWidget *widget)
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.