File indexing completed on 2024-03-24 15:18:06
0001 /* 0002 SPDX-FileCopyrightText: 2002 Pablo de Vicente <vicente@oan.es> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "modcalcjd.h" 0008 0009 #include "kstarsdatetime.h" 0010 #include "ksnotification.h" 0011 0012 #include <KLocalizedString> 0013 #include <KMessageBox> 0014 #include <KLineEdit> 0015 0016 #include <QDebug> 0017 #include <QLineEdit> 0018 #include <QTextStream> 0019 0020 // Qt version calming 0021 #include <qtskipemptyparts.h> 0022 0023 #define MJD0 2400000.5 0024 0025 modCalcJD::modCalcJD(QWidget *parentSplit) : QFrame(parentSplit) 0026 { 0027 setupUi(this); 0028 0029 // signals and slots connections 0030 connect(NowButton, SIGNAL(clicked()), this, SLOT(showCurrentTime())); 0031 connect(DateTimeBox, SIGNAL(dateTimeChanged(QDateTime)), this, SLOT(slotUpdateCalendar())); 0032 connect(JDBox, SIGNAL(editingFinished()), this, SLOT(slotUpdateJD())); 0033 connect(ModJDBox, SIGNAL(editingFinished()), this, SLOT(slotUpdateModJD())); 0034 connect(InputFileBatch, SIGNAL(urlSelected(QUrl)), this, SLOT(slotCheckFiles())); 0035 connect(OutputFileBatch, SIGNAL(urlSelected(QUrl)), this, SLOT(slotCheckFiles())); 0036 connect(RunButtonBatch, SIGNAL(clicked()), this, SLOT(slotRunBatch())); 0037 connect(ViewButtonBatch, SIGNAL(clicked()), this, SLOT(slotViewBatch())); 0038 0039 RunButtonBatch->setEnabled(false); 0040 ViewButtonBatch->setEnabled(false); 0041 0042 showCurrentTime(); 0043 slotUpdateCalendar(); 0044 show(); 0045 } 0046 0047 void modCalcJD::slotUpdateCalendar() 0048 { 0049 long double julianDay, modjulianDay; 0050 0051 julianDay = KStarsDateTime(DateTimeBox->dateTime()).djd(); 0052 showJd(julianDay); 0053 0054 modjulianDay = julianDay - MJD0; 0055 showMjd(modjulianDay); 0056 } 0057 0058 void modCalcJD::slotUpdateModJD() 0059 { 0060 long double julianDay, modjulianDay; 0061 0062 modjulianDay = ModJDBox->text().toDouble(); 0063 julianDay = MJD0 + modjulianDay; 0064 showJd(julianDay); 0065 DateTimeBox->setDateTime(KStarsDateTime(julianDay)); 0066 } 0067 0068 void modCalcJD::slotUpdateJD() 0069 { 0070 long double julianDay, modjulianDay; 0071 julianDay = JDBox->text().toDouble(); 0072 KStarsDateTime dt(julianDay); 0073 0074 DateTimeBox->setDateTime(dt); 0075 0076 modjulianDay = julianDay - MJD0; 0077 showMjd(modjulianDay); 0078 } 0079 0080 void modCalcJD::showCurrentTime(void) 0081 { 0082 DateTimeBox->setDateTime(KStarsDateTime::currentDateTime()); 0083 } 0084 0085 void modCalcJD::showJd(long double julianDay) 0086 { 0087 JDBox->setText(QLocale().toString((double)julianDay, 5)); 0088 } 0089 0090 void modCalcJD::showMjd(long double modjulianDay) 0091 { 0092 ModJDBox->setText(QLocale().toString((double)modjulianDay, 5)); 0093 } 0094 0095 void modCalcJD::slotCheckFiles() 0096 { 0097 if (!InputFileBatch->lineEdit()->text().isEmpty() && !OutputFileBatch->lineEdit()->text().isEmpty()) 0098 { 0099 RunButtonBatch->setEnabled(true); 0100 } 0101 else 0102 { 0103 RunButtonBatch->setEnabled(false); 0104 } 0105 } 0106 0107 void modCalcJD::slotRunBatch() 0108 { 0109 QString inputFileName = InputFileBatch->url().toLocalFile(); 0110 0111 if (QFile::exists(inputFileName)) 0112 { 0113 QFile f(inputFileName); 0114 if (!f.open(QIODevice::ReadOnly)) 0115 { 0116 QString message = i18n("Could not open file %1.", f.fileName()); 0117 KSNotification::sorry(message, i18n("Could Not Open File")); 0118 return; 0119 } 0120 0121 QTextStream istream(&f); 0122 processLines(istream, InputComboBatch->currentIndex()); 0123 ViewButtonBatch->setEnabled(true); 0124 0125 f.close(); 0126 } 0127 else 0128 { 0129 QString message = i18n("Invalid file: %1", inputFileName); 0130 KSNotification::sorry(message, i18n("Invalid file")); 0131 return; 0132 } 0133 } 0134 0135 void modCalcJD::processLines(QTextStream &istream, int inputData) 0136 { 0137 QFile fOut(OutputFileBatch->url().toLocalFile()); 0138 fOut.open(QIODevice::WriteOnly); 0139 QTextStream ostream(&fOut); 0140 0141 QString line; 0142 long double jd(0); 0143 double mjd(0); 0144 KStarsDateTime dt; 0145 0146 while (!istream.atEnd()) 0147 { 0148 line = istream.readLine(); 0149 line = line.trimmed(); 0150 QStringList data = line.split(' ', Qt::SkipEmptyParts); 0151 0152 if (inputData == 0) //Parse date & time 0153 { 0154 //Is the first field parseable as a date or date&time? 0155 if (data[0].length() > 10) 0156 dt = KStarsDateTime::fromString(data[0]); 0157 else 0158 dt = KStarsDateTime(QDate::fromString(data[0]), QTime(0, 0, 0)); 0159 0160 //DEBUG 0161 qDebug() << Q_FUNC_INFO << data[0]; 0162 if (dt.isValid()) 0163 qDebug() << Q_FUNC_INFO << dt.toString(); 0164 0165 if (dt.isValid()) 0166 { 0167 //Try to parse the second field as a time 0168 if (data.size() > 1) 0169 { 0170 QString s = data[1]; 0171 if (s.length() == 4) 0172 s = '0' + s; 0173 QTime t = QTime::fromString(s); 0174 if (t.isValid()) 0175 dt.setTime(t); 0176 } 0177 } 0178 else //Did not parse the first field as a date; try it as a time 0179 { 0180 QTime t = QTime::fromString(data[0]); 0181 if (t.isValid()) 0182 { 0183 dt.setTime(t); 0184 //Now try the second field as a date 0185 if (data.size() > 1) 0186 { 0187 QDate d = QDate::fromString(data[1]); 0188 if (d.isValid()) 0189 dt.setDate(d); 0190 else 0191 dt.setDate(QDate::currentDate()); 0192 } 0193 } 0194 } 0195 0196 if (dt.isValid()) 0197 { 0198 //Compute JD and MJD 0199 jd = dt.djd(); 0200 mjd = jd - MJD0; 0201 } 0202 } 0203 else if (inputData == 1) //Parse Julian day 0204 { 0205 bool ok(false); 0206 jd = data[0].toDouble(&ok); 0207 if (ok) 0208 { 0209 dt.setDJD(jd); 0210 mjd = jd - MJD0; 0211 } 0212 } 0213 else if (inputData == 2) //Parse Modified Julian day 0214 { 0215 bool ok(false); 0216 mjd = data[0].toDouble(&ok); 0217 if (ok) 0218 { 0219 jd = mjd + MJD0; 0220 dt.setDJD(jd); 0221 } 0222 } 0223 0224 //Write to output file 0225 ostream << QLocale().toString(dt, QLocale::LongFormat) << " " << QString::number(jd, 'f', 2) << " " 0226 << QString::number(mjd, 'f', 2) << '\n'; 0227 } 0228 0229 fOut.close(); 0230 } 0231 0232 void modCalcJD::slotViewBatch() 0233 { 0234 QFile fOut(OutputFileBatch->url().toLocalFile()); 0235 fOut.open(QIODevice::ReadOnly); 0236 QTextStream istream(&fOut); 0237 QStringList text; 0238 0239 while (!istream.atEnd()) 0240 text.append(istream.readLine()); 0241 0242 fOut.close(); 0243 0244 KMessageBox::informationList(nullptr, i18n("Results of Julian day calculation"), text, 0245 OutputFileBatch->url().toLocalFile()); 0246 }