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

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 "modcalcgalcoord.h"
0008 
0009 #include "ksnotification.h"
0010 #include "dialogs/finddialog.h"
0011 #include "skyobjects/skyobject.h"
0012 #include "skyobjects/skypoint.h"
0013 
0014 #include <QTextStream>
0015 #include <QPointer>
0016 
0017 modCalcGalCoord::modCalcGalCoord(QWidget *parentSplit) : QFrame(parentSplit)
0018 {
0019     setupUi(this);
0020     RA->setUnits(dmsBox::HOURS);
0021 
0022     connect(RA, SIGNAL(editingFinished()), this, SLOT(slotComputeCoords()));
0023     connect(Dec, SIGNAL(editingFinished()), this, SLOT(slotComputeCoords()));
0024     connect(GalLongitude, SIGNAL(editingFinished()), this, SLOT(slotComputeCoords()));
0025     connect(GalLatitude, SIGNAL(editingFinished()), this, SLOT(slotComputeCoords()));
0026 
0027     connect(ObjectButton, SIGNAL(clicked()), this, SLOT(slotObject()));
0028 
0029     connect(decCheckBatch, SIGNAL(clicked()), this, SLOT(slotDecCheckedBatch()));
0030     connect(raCheckBatch, SIGNAL(clicked()), this, SLOT(slotRaCheckedBatch()));
0031     connect(epochCheckBatch, SIGNAL(clicked()), this, SLOT(slotEpochCheckedBatch()));
0032     connect(galLongCheckBatch, SIGNAL(clicked()), this, SLOT(slotGalLongCheckedBatch()));
0033     connect(galLatCheckBatch, SIGNAL(clicked()), this, SLOT(slotGalLatCheckedBatch()));
0034     connect(runButtonBatch, SIGNAL(clicked()), this, SLOT(slotRunBatch()));
0035 
0036     show();
0037 }
0038 
0039 void modCalcGalCoord::slotObject()
0040 {
0041     if (FindDialog::Instance()->exec() == QDialog::Accepted)
0042     {
0043         SkyObject *o = FindDialog::Instance()->targetObject();
0044         RA->show(o->ra());
0045         Dec->show(o->dec());
0046         slotComputeCoords();
0047     }
0048 }
0049 
0050 void modCalcGalCoord::slotComputeCoords()
0051 {
0052     if (GalLongitude->hasFocus())
0053         GalLongitude->clearFocus();
0054 
0055     //Determine whether we should compute galactic coords from
0056     //equatorial, or vice versa
0057     if (sender()->objectName() == "GalLongitude" || sender()->objectName() == "GalLatitude")
0058     {
0059         //Validate GLong and GLat
0060         bool ok(false);
0061         dms glat;
0062         dms glong = GalLongitude->createDms(&ok);
0063         if (ok)
0064             glat = GalLatitude->createDms(&ok);
0065         if (ok)
0066         {
0067             SkyPoint sp;
0068             sp.GalacticToEquatorial1950(&glong, &glat);
0069             sp.B1950ToJ2000();
0070             RA->show(sp.ra());
0071             Dec->show(sp.dec());
0072         }
0073     }
0074     else
0075     {
0076         //Validate RA and Dec
0077         bool ok(false);
0078         dms dec;
0079         dms ra = RA->createDms(&ok);
0080         if (ok)
0081             dec = Dec->createDms(&ok);
0082         if (ok)
0083         {
0084             dms glong, glat;
0085             SkyPoint sp(ra, dec);
0086             sp.J2000ToB1950();
0087             sp.Equatorial1950ToGalactic(glong, glat);
0088             GalLongitude->show(glong);
0089             GalLatitude->show(glat);
0090         }
0091     }
0092 }
0093 
0094 void modCalcGalCoord::galCheck()
0095 {
0096     galLatCheckBatch->setChecked(false);
0097     galLatBoxBatch->setEnabled(false);
0098     galLongCheckBatch->setChecked(false);
0099     galLongBoxBatch->setEnabled(false);
0100     galInputCoords = false;
0101 }
0102 
0103 void modCalcGalCoord::equCheck()
0104 {
0105     raCheckBatch->setChecked(false);
0106     raBoxBatch->setEnabled(false);
0107     decCheckBatch->setChecked(false);
0108     decBoxBatch->setEnabled(false);
0109     epochCheckBatch->setChecked(false);
0110     galInputCoords = true;
0111 }
0112 
0113 void modCalcGalCoord::slotRaCheckedBatch()
0114 {
0115     if (raCheckBatch->isChecked())
0116     {
0117         raBoxBatch->setEnabled(false);
0118         galCheck();
0119     }
0120     else
0121     {
0122         raBoxBatch->setEnabled(true);
0123     }
0124 }
0125 
0126 void modCalcGalCoord::slotDecCheckedBatch()
0127 {
0128     if (decCheckBatch->isChecked())
0129     {
0130         decBoxBatch->setEnabled(false);
0131         galCheck();
0132     }
0133     else
0134     {
0135         decBoxBatch->setEnabled(true);
0136     }
0137 }
0138 
0139 void modCalcGalCoord::slotEpochCheckedBatch()
0140 {
0141     epochCheckBatch->setChecked(false);
0142 
0143     if (epochCheckBatch->isChecked())
0144     {
0145         epochBoxBatch->setEnabled(false);
0146         galCheck();
0147     }
0148     else
0149     {
0150         epochBoxBatch->setEnabled(true);
0151     }
0152 }
0153 
0154 void modCalcGalCoord::slotGalLatCheckedBatch()
0155 {
0156     if (galLatCheckBatch->isChecked())
0157     {
0158         galLatBoxBatch->setEnabled(false);
0159         equCheck();
0160     }
0161     else
0162     {
0163         galLatBoxBatch->setEnabled(true);
0164     }
0165 }
0166 
0167 void modCalcGalCoord::slotGalLongCheckedBatch()
0168 {
0169     if (galLongCheckBatch->isChecked())
0170     {
0171         galLongBoxBatch->setEnabled(false);
0172         equCheck();
0173     }
0174     else
0175     {
0176         galLongBoxBatch->setEnabled(true);
0177     }
0178 }
0179 
0180 void modCalcGalCoord::slotRunBatch()
0181 {
0182     const QString inputFileName = InputFileBoxBatch->url().toLocalFile();
0183 
0184     // We open the input file and read its content
0185 
0186     if (QFile::exists(inputFileName))
0187     {
0188         QFile f(inputFileName);
0189         if (!f.open(QIODevice::ReadOnly))
0190         {
0191             KSNotification::sorry(i18n("Could not open file %1.", f.fileName()), i18n("Could Not Open File"));
0192             return;
0193         }
0194 
0195         //      processLines(&f);
0196         QTextStream istream(&f);
0197         processLines(istream);
0198         //      readFile( istream );
0199         f.close();
0200     }
0201     else
0202     {
0203         QString message = i18n("Invalid file: %1", inputFileName);
0204         KSNotification::sorry(message, i18n("Invalid file"));
0205         InputFileBoxBatch->setUrl(QUrl());
0206     }
0207 }
0208 
0209 void modCalcGalCoord::processLines(QTextStream &istream)
0210 {
0211     // we open the output file
0212 
0213     //  QTextStream istream(&fIn);
0214     const QString outputFileName = OutputFileBoxBatch->url().toLocalFile();
0215     QFile fOut(outputFileName);
0216     fOut.open(QIODevice::WriteOnly);
0217     QTextStream ostream(&fOut);
0218 
0219     QString line;
0220     QChar space = ' ';
0221     int i       = 0;
0222     SkyPoint sp;
0223     dms raB, decB, galLatB, galLongB;
0224     QString epoch0B;
0225 
0226     while (!istream.atEnd())
0227     {
0228         line = istream.readLine();
0229         line = line.trimmed();
0230 
0231         //Go through the line, looking for parameters
0232 
0233         QStringList fields = line.split(' ');
0234 
0235         i = 0;
0236 
0237         // Input coords are galactic coordinates:
0238 
0239         if (galInputCoords)
0240         {
0241             // Read Galactic Longitude and write in ostream if corresponds
0242 
0243             if (galLongCheckBatch->isChecked())
0244             {
0245                 galLongB = dms::fromString(fields[i], true);
0246                 i++;
0247             }
0248             else
0249                 galLongB = galLongBoxBatch->createDms();
0250 
0251             if (allRadioBatch->isChecked())
0252                 ostream << galLongB.toDMSString() << space;
0253             else if (galLongCheckBatch->isChecked())
0254                 ostream << galLongB.toDMSString() << space;
0255 
0256             // Read Galactic Latitude and write in ostream if corresponds
0257 
0258             if (galLatCheckBatch->isChecked())
0259             {
0260                 galLatB = dms::fromString(fields[i], true);
0261                 i++;
0262             }
0263             else
0264                 galLatB = galLatBoxBatch->createDms();
0265 
0266             if (allRadioBatch->isChecked())
0267                 ostream << galLatB.toDMSString() << space;
0268             else if (galLatCheckBatch->isChecked())
0269                 ostream << galLatB.toDMSString() << space;
0270 
0271             sp = SkyPoint();
0272             sp.GalacticToEquatorial1950(&galLongB, &galLatB);
0273             ostream << sp.ra().toHMSString() << space << sp.dec().toDMSString() << epoch0B << '\n';
0274             // Input coords. are equatorial coordinates:
0275         }
0276         else
0277         {
0278             // Read RA and write in ostream if corresponds
0279 
0280             if (raCheckBatch->isChecked())
0281             {
0282                 raB = dms::fromString(fields[i], false);
0283                 i++;
0284             }
0285             else
0286                 raB = raBoxBatch->createDms();
0287 
0288             if (allRadioBatch->isChecked())
0289                 ostream << raB.toHMSString() << space;
0290             else if (raCheckBatch->isChecked())
0291                 ostream << raB.toHMSString() << space;
0292 
0293             // Read DEC and write in ostream if corresponds
0294 
0295             if (decCheckBatch->isChecked())
0296             {
0297                 decB = dms::fromString(fields[i], true);
0298                 i++;
0299             }
0300             else
0301                 decB = decBoxBatch->createDms();
0302 
0303             if (allRadioBatch->isChecked())
0304                 ostream << decB.toDMSString() << space;
0305             else if (decCheckBatch->isChecked())
0306                 ostream << decB.toDMSString() << space;
0307 
0308             // Read Epoch and write in ostream if corresponds
0309 
0310             if (epochCheckBatch->isChecked())
0311             {
0312                 epoch0B = fields[i];
0313                 i++;
0314             }
0315             else
0316                 epoch0B = epochBoxBatch->text();
0317 
0318             if (allRadioBatch->isChecked())
0319                 ostream << epoch0B << space;
0320             else if (epochCheckBatch->isChecked())
0321                 ostream << epoch0B << space;
0322 
0323             sp = SkyPoint(raB, decB);
0324             sp.J2000ToB1950();
0325             sp.Equatorial1950ToGalactic(galLongB, galLatB);
0326             ostream << galLongB.toDMSString() << space << galLatB.toDMSString() << '\n';
0327         }
0328     }
0329 
0330     fOut.close();
0331 }