File indexing completed on 2024-04-14 14:11:38

0001 /*
0002     SPDX-FileCopyrightText: 2004 Pablo de Vicente <vicente@oan.es>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "modcalcangdist.h"
0008 
0009 #include "dms.h"
0010 #include "kstars.h"
0011 #include "ksnotification.h"
0012 #include "dialogs/finddialog.h"
0013 #include "skyobjects/skyobject.h"
0014 #include "skyobjects/skypoint.h"
0015 #include "widgets/dmsbox.h"
0016 
0017 #include <KLocalizedString>
0018 
0019 #include <QTextStream>
0020 #include <QPointer>
0021 
0022 modCalcAngDist::modCalcAngDist(QWidget *parentSplit) : QFrame(parentSplit)
0023 {
0024     setupUi(this);
0025     FirstRA->setUnits(dmsBox::HOURS);
0026     SecondRA->setUnits(dmsBox::HOURS);
0027 
0028     connect(FirstRA, SIGNAL(editingFinished()), this, SLOT(slotValidatePositions()));
0029     connect(FirstDec, SIGNAL(editingFinished()), this, SLOT(slotValidatePositions()));
0030     connect(SecondRA, SIGNAL(editingFinished()), this, SLOT(slotValidatePositions()));
0031     connect(SecondDec, SIGNAL(editingFinished()), this, SLOT(slotValidatePositions()));
0032     connect(FirstRA, SIGNAL(textEdited(QString)), this, SLOT(slotResetTitle()));
0033     connect(FirstDec, SIGNAL(textEdited(QString)), this, SLOT(slotResetTitle()));
0034     connect(SecondRA, SIGNAL(textEdited(QString)), this, SLOT(slotResetTitle()));
0035     connect(SecondDec, SIGNAL(textEdited(QString)), this, SLOT(slotResetTitle()));
0036     connect(FirstObjectButton, SIGNAL(clicked()), this, SLOT(slotObjectButton()));
0037     connect(SecondObjectButton, SIGNAL(clicked()), this, SLOT(slotObjectButton()));
0038     connect(runButtonBatch, SIGNAL(clicked()), this, SLOT(slotRunBatch()));
0039 
0040     show();
0041     slotValidatePositions();
0042 }
0043 
0044 SkyPoint modCalcAngDist::getCoords(dmsBox *rBox, dmsBox *dBox, bool *ok)
0045 {
0046     dms raCoord, decCoord;
0047 
0048     bool ok2 = false;
0049     raCoord  = rBox->createDms(&ok2);
0050     if (ok2)
0051         decCoord = dBox->createDms(&ok2);
0052 
0053     if (ok2)
0054     {
0055         if (ok)
0056             *ok = ok2;
0057         return SkyPoint(raCoord, decCoord);
0058     }
0059     else
0060     {
0061         if (ok)
0062             *ok = ok2;
0063         return SkyPoint();
0064     }
0065 }
0066 
0067 void modCalcAngDist::slotValidatePositions()
0068 {
0069     SkyPoint sp0, sp1;
0070     bool ok;
0071     sp0 = getCoords(FirstRA, FirstDec, &ok);
0072 
0073     if (ok)
0074         sp1 = getCoords(SecondRA, SecondDec, &ok);
0075 
0076     if (ok)
0077     {
0078         double PA = 0;
0079         AngDist->setText(sp0.angularDistanceTo(&sp1, &PA).toDMSString());
0080         PositionAngle->setText(QString::number(PA, 'f', 3));
0081     }
0082     else
0083     {
0084         AngDist->setText(" .... ");
0085         PositionAngle->setText(" .... ");
0086     }
0087 }
0088 
0089 void modCalcAngDist::slotObjectButton()
0090 {
0091     if (FindDialog::Instance()->exec() == QDialog::Accepted)
0092     {
0093         SkyObject *o = FindDialog::Instance()->targetObject();
0094         if (sender()->objectName() == QString("FirstObjectButton"))
0095         {
0096             FirstRA->show(o->ra());
0097             FirstDec->show(o->dec());
0098             FirstPositionBox->setTitle(i18n("First position: %1", o->name()));
0099         }
0100         else
0101         {
0102             SecondRA->show(o->ra());
0103             SecondDec->show(o->dec());
0104             SecondPositionBox->setTitle(i18n("Second position: %1", o->name()));
0105         }
0106 
0107         slotValidatePositions();
0108     }
0109 }
0110 
0111 void modCalcAngDist::slotResetTitle()
0112 {
0113     QString name = sender()->objectName();
0114     if (name.contains("First"))
0115         FirstPositionBox->setTitle(i18n("First position"));
0116     else
0117         SecondPositionBox->setTitle(i18n("Second position"));
0118 }
0119 
0120 void modCalcAngDist::slotRunBatch()
0121 {
0122     QString inputFileName = InputLineEditBatch->url().toLocalFile();
0123 
0124     // We open the input file and read its content
0125 
0126     if (QFile::exists(inputFileName))
0127     {
0128         QFile f(inputFileName);
0129         if (!f.open(QIODevice::ReadOnly))
0130         {
0131             QString message = i18n("Could not open file %1.", f.fileName());
0132             KSNotification::sorry(message, i18n("Could Not Open File"));
0133             inputFileName.clear();
0134             return;
0135         }
0136 
0137         //      processLines(&f);
0138         QTextStream istream(&f);
0139         processLines(istream);
0140         //      readFile( istream );
0141         f.close();
0142     }
0143     else
0144     {
0145         QString message = i18n("Invalid file: %1", inputFileName);
0146         KSNotification::sorry(message, i18n("Invalid file"));
0147         inputFileName.clear();
0148         InputLineEditBatch->setText(inputFileName);
0149         return;
0150     }
0151 }
0152 
0153 //void modCalcAngDist::processLines( const QFile * fIn ) {
0154 void modCalcAngDist::processLines(QTextStream &istream)
0155 {
0156     // we open the output file
0157 
0158     //  QTextStream istream(&fIn);
0159     QString outputFileName;
0160     outputFileName = OutputLineEditBatch->text();
0161     QFile fOut(outputFileName);
0162     fOut.open(QIODevice::WriteOnly);
0163     QTextStream ostream(&fOut);
0164 
0165     QString line;
0166     QChar space = ' ';
0167     int i       = 0;
0168     SkyPoint sp0, sp1;
0169     double PA = 0;
0170     dms ra0B, dec0B, ra1B, dec1B, dist;
0171 
0172     while (!istream.atEnd())
0173     {
0174         line = istream.readLine();
0175         line = line.trimmed();
0176 
0177         //Go through the line, looking for parameters
0178 
0179         QStringList fields = line.split(' ');
0180 
0181         i = 0;
0182 
0183         // Read RA and write in ostream if corresponds
0184 
0185         if (ra0CheckBatch->isChecked())
0186         {
0187             ra0B = dms::fromString(fields[i], false);
0188             i++;
0189         }
0190         else
0191             ra0B = ra0BoxBatch->createDms();
0192 
0193         if (allRadioBatch->isChecked())
0194             ostream << ra0B.toHMSString() << space;
0195         else if (ra0CheckBatch->isChecked())
0196             ostream << ra0B.toHMSString() << space;
0197 
0198         // Read DEC and write in ostream if corresponds
0199 
0200         if (dec0CheckBatch->isChecked())
0201         {
0202             dec0B = dms::fromString(fields[i], true);
0203             i++;
0204         }
0205         else
0206             dec0B = dec0BoxBatch->createDms();
0207 
0208         if (allRadioBatch->isChecked())
0209             ostream << dec0B.toDMSString() << space;
0210         else if (dec0CheckBatch->isChecked())
0211             ostream << dec0B.toDMSString() << space;
0212 
0213         // Read RA and write in ostream if corresponds
0214 
0215         if (ra1CheckBatch->isChecked())
0216         {
0217             ra1B = dms::fromString(fields[i], false);
0218             i++;
0219         }
0220         else
0221             ra1B = ra1BoxBatch->createDms();
0222 
0223         if (allRadioBatch->isChecked())
0224             ostream << ra1B.toHMSString() << space;
0225         else if (ra1CheckBatch->isChecked())
0226             ostream << ra1B.toHMSString() << space;
0227 
0228         // Read DEC and write in ostream if corresponds
0229 
0230         if (dec1CheckBatch->isChecked())
0231         {
0232             dec1B = dms::fromString(fields[i], true);
0233             i++;
0234         }
0235         else
0236             dec1B = dec1BoxBatch->createDms();
0237 
0238         if (allRadioBatch->isChecked())
0239             ostream << dec1B.toDMSString() << space;
0240         else if (dec1CheckBatch->isChecked())
0241             ostream << dec1B.toDMSString() << space;
0242 
0243         sp0  = SkyPoint(ra0B, dec0B);
0244         sp1  = SkyPoint(ra1B, dec1B);
0245         dist = sp0.angularDistanceTo(&sp1, &PA);
0246 
0247         ostream << dist.toDMSString() << QString::number(PA, 'f', 3) << '\n';
0248     }
0249 
0250     fOut.close();
0251 }