Kstars

modcalcjd.cpp
1/*
2 SPDX-FileCopyrightText: 2002 Pablo de Vicente <vicente@oan.es>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "modcalcjd.h"
8
9#include "kstarsdatetime.h"
10#include "ksnotification.h"
11
12#include <KLocalizedString>
13#include <KMessageBox>
14#include <KLineEdit>
15
16#include <QDebug>
17#include <QLineEdit>
18#include <QTextStream>
19
20// Qt version calming
21#include <qtskipemptyparts.h>
22
23#define MJD0 2400000.5
24
25modCalcJD::modCalcJD(QWidget *parentSplit) : QFrame(parentSplit)
26{
27 setupUi(this);
28
29 // signals and slots connections
30 connect(NowButton, SIGNAL(clicked()), this, SLOT(showCurrentTime()));
31 connect(DateTimeBox, SIGNAL(dateTimeChanged(QDateTime)), this, SLOT(slotUpdateCalendar()));
32 connect(JDBox, SIGNAL(editingFinished()), this, SLOT(slotUpdateJD()));
33 connect(ModJDBox, SIGNAL(editingFinished()), this, SLOT(slotUpdateModJD()));
34 connect(InputFileBatch, SIGNAL(urlSelected(QUrl)), this, SLOT(slotCheckFiles()));
35 connect(OutputFileBatch, SIGNAL(urlSelected(QUrl)), this, SLOT(slotCheckFiles()));
36 connect(RunButtonBatch, SIGNAL(clicked()), this, SLOT(slotRunBatch()));
37 connect(ViewButtonBatch, SIGNAL(clicked()), this, SLOT(slotViewBatch()));
38
39 RunButtonBatch->setEnabled(false);
40 ViewButtonBatch->setEnabled(false);
41
42 showCurrentTime();
43 slotUpdateCalendar();
44 show();
45}
46
47void modCalcJD::slotUpdateCalendar()
48{
49 long double julianDay, modjulianDay;
50
51 julianDay = KStarsDateTime(DateTimeBox->dateTime()).djd();
52 showJd(julianDay);
53
54 modjulianDay = julianDay - MJD0;
55 showMjd(modjulianDay);
56}
57
58void modCalcJD::slotUpdateModJD()
59{
60 long double julianDay, modjulianDay;
61
62 modjulianDay = ModJDBox->text().toDouble();
63 julianDay = MJD0 + modjulianDay;
64 showJd(julianDay);
65 DateTimeBox->setDateTime(KStarsDateTime(julianDay));
66}
67
68void modCalcJD::slotUpdateJD()
69{
70 long double julianDay, modjulianDay;
71 julianDay = JDBox->text().toDouble();
72 KStarsDateTime dt(julianDay);
73
74 DateTimeBox->setDateTime(dt);
75
76 modjulianDay = julianDay - MJD0;
77 showMjd(modjulianDay);
78}
79
80void modCalcJD::showCurrentTime(void)
81{
82 DateTimeBox->setDateTime(KStarsDateTime::currentDateTime());
83}
84
85void modCalcJD::showJd(long double julianDay)
86{
87 JDBox->setText(QLocale().toString((double)julianDay, 5));
88}
89
90void modCalcJD::showMjd(long double modjulianDay)
91{
92 ModJDBox->setText(QLocale().toString((double)modjulianDay, 5));
93}
94
95void modCalcJD::slotCheckFiles()
96{
97 if (!InputFileBatch->lineEdit()->text().isEmpty() && !OutputFileBatch->lineEdit()->text().isEmpty())
98 {
99 RunButtonBatch->setEnabled(true);
100 }
101 else
102 {
103 RunButtonBatch->setEnabled(false);
104 }
105}
106
107void modCalcJD::slotRunBatch()
108{
109 QString inputFileName = InputFileBatch->url().toLocalFile();
110
111 if (QFile::exists(inputFileName))
112 {
113 QFile f(inputFileName);
114 if (!f.open(QIODevice::ReadOnly))
115 {
116 QString message = i18n("Could not open file %1.", f.fileName());
117 KSNotification::sorry(message, i18n("Could Not Open File"));
118 return;
119 }
120
121 QTextStream istream(&f);
122 processLines(istream, InputComboBatch->currentIndex());
123 ViewButtonBatch->setEnabled(true);
124
125 f.close();
126 }
127 else
128 {
129 QString message = i18n("Invalid file: %1", inputFileName);
130 KSNotification::sorry(message, i18n("Invalid file"));
131 return;
132 }
133}
134
135void modCalcJD::processLines(QTextStream &istream, int inputData)
136{
137 QFile fOut(OutputFileBatch->url().toLocalFile());
138 fOut.open(QIODevice::WriteOnly);
139 QTextStream ostream(&fOut);
140
141 QString line;
142 long double jd(0);
143 double mjd(0);
145
146 while (!istream.atEnd())
147 {
148 line = istream.readLine();
149 line = line.trimmed();
150 QStringList data = line.split(' ', Qt::SkipEmptyParts);
151
152 if (inputData == 0) //Parse date & time
153 {
154 //Is the first field parseable as a date or date&time?
155 if (data[0].length() > 10)
156 dt = KStarsDateTime::fromString(data[0]);
157 else
158 dt = KStarsDateTime(QDate::fromString(data[0]), QTime(0, 0, 0));
159
160 //DEBUG
161 qDebug() << Q_FUNC_INFO << data[0];
162 if (dt.isValid())
163 qDebug() << Q_FUNC_INFO << dt.toString();
164
165 if (dt.isValid())
166 {
167 //Try to parse the second field as a time
168 if (data.size() > 1)
169 {
170 QString s = data[1];
171 if (s.length() == 4)
172 s = '0' + s;
174 if (t.isValid())
175 dt.setTime(t);
176 }
177 }
178 else //Did not parse the first field as a date; try it as a time
179 {
180 QTime t = QTime::fromString(data[0]);
181 if (t.isValid())
182 {
183 dt.setTime(t);
184 //Now try the second field as a date
185 if (data.size() > 1)
186 {
187 QDate d = QDate::fromString(data[1]);
188 if (d.isValid())
189 dt.setDate(d);
190 else
192 }
193 }
194 }
195
196 if (dt.isValid())
197 {
198 //Compute JD and MJD
199 jd = dt.djd();
200 mjd = jd - MJD0;
201 }
202 }
203 else if (inputData == 1) //Parse Julian day
204 {
205 bool ok(false);
206 jd = data[0].toDouble(&ok);
207 if (ok)
208 {
209 dt.setDJD(jd);
210 mjd = jd - MJD0;
211 }
212 }
213 else if (inputData == 2) //Parse Modified Julian day
214 {
215 bool ok(false);
216 mjd = data[0].toDouble(&ok);
217 if (ok)
218 {
219 jd = mjd + MJD0;
220 dt.setDJD(jd);
221 }
222 }
223
224 //Write to output file
225 ostream << QLocale().toString(dt, QLocale::LongFormat) << " " << QString::number(jd, 'f', 2) << " "
226 << QString::number(mjd, 'f', 2) << '\n';
227 }
228
229 fOut.close();
230}
231
232void modCalcJD::slotViewBatch()
233{
234 QFile fOut(OutputFileBatch->url().toLocalFile());
235 fOut.open(QIODevice::ReadOnly);
236 QTextStream istream(&fOut);
237 QStringList text;
238
239 while (!istream.atEnd())
240 text.append(istream.readLine());
241
242 fOut.close();
243
244 KMessageBox::informationList(nullptr, i18n("Results of Julian day calculation"), text,
245 OutputFileBatch->url().toLocalFile());
246}
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 fromString(const QString &s)
void setDate(const QDate &d)
Assign the Date according to a QDate object.
static KStarsDateTime currentDateTime()
long double djd() const
void setTime(const QTime &t)
Assign the Time according to a QTime object.
QString i18n(const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
void informationList(QWidget *parent, const QString &text, const QStringList &strlist, const QString &title=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
QDate currentDate()
QDate fromString(QStringView string, QStringView format, QCalendar cal)
bool isValid() const const
QString toString(QStringView format, QCalendar cal) const const
bool exists() const const
void append(QList< T > &&value)
QString toString(QDate date, FormatType format) const const
qsizetype length() const const
QString number(double n, char format, int precision)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString trimmed() const const
SkipEmptyParts
bool atEnd() const const
QString readLine(qint64 maxlen)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QTime fromString(QStringView string, QStringView format)
bool isValid(int h, int m, int s, int ms)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.