File indexing completed on 2024-04-14 03:43:20

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 "modcalcapcoord.h"
0008 
0009 #include "dms.h"
0010 #include "kstars.h"
0011 #include "ksnotification.h"
0012 #include "kstarsdatetime.h"
0013 #include "dialogs/finddialog.h"
0014 #include "skyobjects/skypoint.h"
0015 #include "skyobjects/skyobject.h"
0016 
0017 #include <QTextStream>
0018 #include <QFileDialog>
0019 #include <QPointer>
0020 
0021 modCalcApCoord::modCalcApCoord(QWidget *parentSplit) : QFrame(parentSplit)
0022 {
0023     setupUi(this);
0024     showCurrentTime();
0025     RACat->setUnits(dmsBox::HOURS);
0026     DecCat->setUnits(dmsBox::DEGREES);
0027 
0028     connect(ObjectButton, SIGNAL(clicked()), this, SLOT(slotObject()));
0029     connect(NowButton, SIGNAL(clicked()), this, SLOT(showCurrentTime()));
0030     connect(RACat, SIGNAL(editingFinished()), this, SLOT(slotCompute()));
0031     connect(DecCat, SIGNAL(editingFinished()), this, SLOT(slotCompute()));
0032     connect(UT, SIGNAL(timeChanged(QTime)), this, SLOT(slotCompute()));
0033     connect(Date, SIGNAL(dateChanged(QDate)), this, SLOT(slotCompute()));
0034 
0035     connect(utCheckBatch, SIGNAL(clicked()), this, SLOT(slotUtCheckedBatch()));
0036     connect(dateCheckBatch, SIGNAL(clicked()), this, SLOT(slotDateCheckedBatch()));
0037     connect(raCheckBatch, SIGNAL(clicked()), this, SLOT(slotRaCheckedBatch()));
0038     connect(decCheckBatch, SIGNAL(clicked()), this, SLOT(slotDecCheckedBatch()));
0039     connect(epochCheckBatch, SIGNAL(clicked()), this, SLOT(slotEpochCheckedBatch()));
0040     connect(runButtonBatch, SIGNAL(clicked()), this, SLOT(slotRunBatch()));
0041 
0042     show();
0043 }
0044 
0045 void modCalcApCoord::showCurrentTime(void)
0046 {
0047     KStarsDateTime dt(KStarsDateTime::currentDateTime());
0048     Date->setDate(dt.date());
0049     UT->setTime(dt.time());
0050     EpochTarget->setText(QString::number(dt.epoch(), 'f', 3));
0051 }
0052 
0053 void modCalcApCoord::slotCompute()
0054 {
0055     KStarsDateTime dt(Date->date(), UT->time());
0056     long double jd = dt.djd();
0057 
0058     dt.setFromEpoch(EpochCat->value());
0059     long double jd0 = dt.djd();
0060 
0061     SkyPoint sp(RACat->createDms(), DecCat->createDms());
0062     sp.apparentCoord(jd0, jd);
0063 
0064     RA->setText(sp.ra().toHMSString());
0065     Dec->setText(sp.dec().toDMSString());
0066 }
0067 
0068 void modCalcApCoord::slotObject()
0069 {
0070     if (FindDialog::Instance()->exec() == QDialog::Accepted)
0071     {
0072         SkyObject *o = FindDialog::Instance()->targetObject();
0073         RACat->show(o->ra0());
0074         DecCat->show(o->dec0());
0075         EpochCat->setValue(2000.0);
0076 
0077         slotCompute();
0078     }
0079 }
0080 
0081 void modCalcApCoord::slotUtCheckedBatch()
0082 {
0083     if (utCheckBatch->isChecked())
0084         utBoxBatch->setEnabled(false);
0085     else
0086     {
0087         utBoxBatch->setEnabled(true);
0088     }
0089 }
0090 
0091 void modCalcApCoord::slotDateCheckedBatch()
0092 {
0093     if (dateCheckBatch->isChecked())
0094         dateBoxBatch->setEnabled(false);
0095     else
0096     {
0097         dateBoxBatch->setEnabled(true);
0098     }
0099 }
0100 
0101 void modCalcApCoord::slotRaCheckedBatch()
0102 {
0103     if (raCheckBatch->isChecked())
0104         raBoxBatch->setEnabled(false);
0105     else
0106     {
0107         raBoxBatch->setEnabled(true);
0108     }
0109 }
0110 
0111 void modCalcApCoord::slotDecCheckedBatch()
0112 {
0113     if (decCheckBatch->isChecked())
0114         decBoxBatch->setEnabled(false);
0115     else
0116     {
0117         decBoxBatch->setEnabled(true);
0118     }
0119 }
0120 
0121 void modCalcApCoord::slotEpochCheckedBatch()
0122 {
0123     if (epochCheckBatch->isChecked())
0124         epochBoxBatch->setEnabled(false);
0125     else
0126     {
0127         epochBoxBatch->setEnabled(true);
0128     }
0129 }
0130 
0131 void modCalcApCoord::slotRunBatch()
0132 {
0133     QString inputFileName = InputLineEditBatch->url().toLocalFile();
0134 
0135     // We open the input file and read its content
0136 
0137     if (QFile::exists(inputFileName))
0138     {
0139         QFile f(inputFileName);
0140         if (!f.open(QIODevice::ReadOnly))
0141         {
0142             KSNotification::sorry(i18n("Could not open file %1.", f.fileName()), i18n("Could Not Open File"));
0143             inputFileName.clear();
0144             return;
0145         }
0146 
0147         //      processLines(&f);
0148         QTextStream istream(&f);
0149         processLines(istream);
0150         //      readFile( istream );
0151         f.close();
0152     }
0153     else
0154     {
0155         KSNotification::sorry(i18n("Invalid file: %1", inputFileName), i18n("Invalid file"));
0156         inputFileName.clear();
0157         InputLineEditBatch->setText(inputFileName);
0158         return;
0159     }
0160 }
0161 
0162 //void modCalcApCoord::processLines( const QFile * fIn ) {
0163 void modCalcApCoord::processLines(QTextStream &istream)
0164 {
0165     // we open the output file
0166 
0167     //  QTextStream istream(&fIn);
0168     QString outputFileName;
0169     outputFileName = OutputLineEditBatch->text();
0170     QFile fOut(outputFileName);
0171     fOut.open(QIODevice::WriteOnly);
0172     QTextStream ostream(&fOut);
0173 
0174     QString line;
0175     QChar space = ' ';
0176     int i       = 0;
0177     long double jd, jd0;
0178     SkyPoint sp;
0179     QTime utB;
0180     QDate dtB;
0181     dms raB, decB;
0182     QString epoch0B;
0183 
0184     while (!istream.atEnd())
0185     {
0186         line = istream.readLine();
0187         line = line.trimmed();
0188 
0189         //Go through the line, looking for parameters
0190 
0191         QStringList fields = line.split(' ');
0192 
0193         i = 0;
0194 
0195         // Read Ut and write in ostream if corresponds
0196 
0197         if (utCheckBatch->isChecked())
0198         {
0199             utB = QTime::fromString(fields[i]);
0200             i++;
0201         }
0202         else
0203             utB = utBoxBatch->time();
0204 
0205         if (allRadioBatch->isChecked())
0206             ostream << QLocale().toString(utB) << space;
0207         else if (utCheckBatch->isChecked())
0208             ostream << QLocale().toString(utB) << space;
0209 
0210         // Read date and write in ostream if corresponds
0211 
0212         if (dateCheckBatch->isChecked())
0213         {
0214             dtB = QDate::fromString(fields[i]);
0215             i++;
0216         }
0217         else
0218             dtB = dateBoxBatch->date();
0219 
0220         if (allRadioBatch->isChecked())
0221             ostream << QLocale().toString(dtB, QLocale::LongFormat).append(space);
0222         else if (dateCheckBatch->isChecked())
0223             ostream << QLocale().toString(dtB, QLocale::LongFormat).append(space);
0224 
0225         // Read RA and write in ostream if corresponds
0226 
0227         if (raCheckBatch->isChecked())
0228         {
0229             raB = dms::fromString(fields[i], false);
0230             i++;
0231         }
0232         else
0233             raB = raBoxBatch->createDms();
0234 
0235         if (allRadioBatch->isChecked())
0236             ostream << raB.toHMSString() << space;
0237         else if (raCheckBatch->isChecked())
0238             ostream << raB.toHMSString() << space;
0239 
0240         // Read DEC and write in ostream if corresponds
0241 
0242         if (decCheckBatch->isChecked())
0243         {
0244             decB = dms::fromString(fields[i], true);
0245             i++;
0246         }
0247         else
0248             decB = decBoxBatch->createDms();
0249 
0250         if (allRadioBatch->isChecked())
0251             ostream << decB.toDMSString() << space;
0252         else if (decCheckBatch->isChecked())
0253             ostream << decB.toHMSString() << space;
0254 
0255         // Read Epoch and write in ostream if corresponds
0256 
0257         if (epochCheckBatch->isChecked())
0258         {
0259             epoch0B = fields[i];
0260             i++;
0261         }
0262         else
0263             epoch0B = epochBoxBatch->text();
0264 
0265         if (allRadioBatch->isChecked())
0266             ostream << epoch0B;
0267         else if (decCheckBatch->isChecked())
0268             ostream << epoch0B;
0269 
0270         KStarsDateTime dt;
0271         dt.setFromEpoch(epoch0B);
0272         jd  = KStarsDateTime(dtB, utB).djd();
0273         jd0 = dt.djd();
0274         sp  = SkyPoint(raB, decB);
0275         sp.apparentCoord(jd0, jd);
0276 
0277         ostream << sp.ra().toHMSString() << sp.dec().toDMSString() << '\n';
0278     }
0279 
0280     fOut.close();
0281 }