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 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
74EclipseTool::~EclipseTool()
75{
76 delete ui;
77}
78
79void EclipseTool::slotLocation()
80{
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.
91void 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
106 connect(handler, &EclipseHandler::signalComputationFinished, this, &EclipseTool::slotFinished);
107
108 handler->setGeoLocation(m_geoLocation);
109
110 // let's go
112 delete handler;
113}
114
115void EclipseTool::slotFinished()
116{
117 ui->computeStack->setCurrentIndex(0);
118}
119
120void 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
143void EclipseTool::slotView(EclipseEvent_s event)
144{
145 SkyMap * map = KStars::Instance()->map();
146 KStarsData * data = KStarsData::Instance();
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
158EclipseModel::EclipseModel(QObject *parent) : QAbstractTableModel (parent)
159{
160
161}
162
163int EclipseModel::rowCount(const QModelIndex &) const
164{
165 return m_eclipses.length();
166}
167
168QVariant 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
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
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
249QVariant 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
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:72
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.
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()
static KStars * Instance()
Definition kstars.h:123
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)
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 Tue Mar 26 2024 11:19:04 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.