Kstars

modcalcangdist.cpp
1/*
2 SPDX-FileCopyrightText: 2004 Pablo de Vicente <vicente@oan.es>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "modcalcangdist.h"
8
9#include "dms.h"
10#include "kstars.h"
11#include "ksnotification.h"
12#include "dialogs/finddialog.h"
13#include "skyobjects/skyobject.h"
14#include "skyobjects/skypoint.h"
15#include "widgets/dmsbox.h"
16
17#include <KLocalizedString>
18
19#include <QTextStream>
20#include <QPointer>
21
22modCalcAngDist::modCalcAngDist(QWidget *parentSplit) : QFrame(parentSplit)
23{
24 setupUi(this);
25 FirstRA->setUnits(dmsBox::HOURS);
26 SecondRA->setUnits(dmsBox::HOURS);
27
28 connect(FirstRA, SIGNAL(editingFinished()), this, SLOT(slotValidatePositions()));
29 connect(FirstDec, SIGNAL(editingFinished()), this, SLOT(slotValidatePositions()));
30 connect(SecondRA, SIGNAL(editingFinished()), this, SLOT(slotValidatePositions()));
31 connect(SecondDec, SIGNAL(editingFinished()), this, SLOT(slotValidatePositions()));
32 connect(FirstRA, SIGNAL(textEdited(QString)), this, SLOT(slotResetTitle()));
33 connect(FirstDec, SIGNAL(textEdited(QString)), this, SLOT(slotResetTitle()));
34 connect(SecondRA, SIGNAL(textEdited(QString)), this, SLOT(slotResetTitle()));
35 connect(SecondDec, SIGNAL(textEdited(QString)), this, SLOT(slotResetTitle()));
36 connect(FirstObjectButton, SIGNAL(clicked()), this, SLOT(slotObjectButton()));
37 connect(SecondObjectButton, SIGNAL(clicked()), this, SLOT(slotObjectButton()));
38 connect(runButtonBatch, SIGNAL(clicked()), this, SLOT(slotRunBatch()));
39
40 show();
41 slotValidatePositions();
42}
43
44SkyPoint modCalcAngDist::getCoords(dmsBox *rBox, dmsBox *dBox, bool *ok)
45{
46 dms raCoord, decCoord;
47
48 bool ok2 = false;
49 raCoord = rBox->createDms(&ok2);
50 if (ok2)
51 decCoord = dBox->createDms(&ok2);
52
53 if (ok2)
54 {
55 if (ok)
56 *ok = ok2;
57 return SkyPoint(raCoord, decCoord);
58 }
59 else
60 {
61 if (ok)
62 *ok = ok2;
63 return SkyPoint();
64 }
65}
66
67void modCalcAngDist::slotValidatePositions()
68{
69 SkyPoint sp0, sp1;
70 bool ok;
71 sp0 = getCoords(FirstRA, FirstDec, &ok);
72
73 if (ok)
74 sp1 = getCoords(SecondRA, SecondDec, &ok);
75
76 if (ok)
77 {
78 double PA = 0;
79 AngDist->setText(sp0.angularDistanceTo(&sp1, &PA).toDMSString());
80 PositionAngle->setText(QString::number(PA, 'f', 3));
81 }
82 else
83 {
84 AngDist->setText(" .... ");
85 PositionAngle->setText(" .... ");
86 }
87}
88
89void modCalcAngDist::slotObjectButton()
90{
91 if (FindDialog::Instance()->exec() == QDialog::Accepted)
92 {
93 SkyObject *o = FindDialog::Instance()->targetObject();
94 if (sender()->objectName() == QString("FirstObjectButton"))
95 {
96 FirstRA->show(o->ra());
97 FirstDec->show(o->dec());
98 FirstPositionBox->setTitle(i18n("First position: %1", o->name()));
99 }
100 else
101 {
102 SecondRA->show(o->ra());
103 SecondDec->show(o->dec());
104 SecondPositionBox->setTitle(i18n("Second position: %1", o->name()));
105 }
106
107 slotValidatePositions();
108 }
109}
110
111void modCalcAngDist::slotResetTitle()
112{
114 if (name.contains("First"))
115 FirstPositionBox->setTitle(i18n("First position"));
116 else
117 SecondPositionBox->setTitle(i18n("Second position"));
118}
119
120void modCalcAngDist::slotRunBatch()
121{
122 QString inputFileName = InputLineEditBatch->url().toLocalFile();
123
124 // We open the input file and read its content
125
126 if (QFile::exists(inputFileName))
127 {
128 QFile f(inputFileName);
129 if (!f.open(QIODevice::ReadOnly))
130 {
131 QString message = i18n("Could not open file %1.", f.fileName());
132 KSNotification::sorry(message, i18n("Could Not Open File"));
133 inputFileName.clear();
134 return;
135 }
136
137 // processLines(&f);
138 QTextStream istream(&f);
139 processLines(istream);
140 // readFile( istream );
141 f.close();
142 }
143 else
144 {
145 QString message = i18n("Invalid file: %1", inputFileName);
146 KSNotification::sorry(message, i18n("Invalid file"));
147 inputFileName.clear();
148 InputLineEditBatch->setText(inputFileName);
149 return;
150 }
151}
152
153//void modCalcAngDist::processLines( const QFile * fIn ) {
154void modCalcAngDist::processLines(QTextStream &istream)
155{
156 // we open the output file
157
158 // QTextStream istream(&fIn);
159 QString outputFileName;
160 outputFileName = OutputLineEditBatch->text();
161 QFile fOut(outputFileName);
162 fOut.open(QIODevice::WriteOnly);
163 QTextStream ostream(&fOut);
164
165 QString line;
166 QChar space = ' ';
167 int i = 0;
168 SkyPoint sp0, sp1;
169 double PA = 0;
170 dms ra0B, dec0B, ra1B, dec1B, dist;
171
172 while (!istream.atEnd())
173 {
174 line = istream.readLine();
175 line = line.trimmed();
176
177 //Go through the line, looking for parameters
178
179 QStringList fields = line.split(' ');
180
181 i = 0;
182
183 // Read RA and write in ostream if corresponds
184
185 if (ra0CheckBatch->isChecked())
186 {
187 ra0B = dms::fromString(fields[i], false);
188 i++;
189 }
190 else
191 ra0B = ra0BoxBatch->createDms();
192
193 if (allRadioBatch->isChecked())
194 ostream << ra0B.toHMSString() << space;
195 else if (ra0CheckBatch->isChecked())
196 ostream << ra0B.toHMSString() << space;
197
198 // Read DEC and write in ostream if corresponds
199
200 if (dec0CheckBatch->isChecked())
201 {
202 dec0B = dms::fromString(fields[i], true);
203 i++;
204 }
205 else
206 dec0B = dec0BoxBatch->createDms();
207
208 if (allRadioBatch->isChecked())
209 ostream << dec0B.toDMSString() << space;
210 else if (dec0CheckBatch->isChecked())
211 ostream << dec0B.toDMSString() << space;
212
213 // Read RA and write in ostream if corresponds
214
215 if (ra1CheckBatch->isChecked())
216 {
217 ra1B = dms::fromString(fields[i], false);
218 i++;
219 }
220 else
221 ra1B = ra1BoxBatch->createDms();
222
223 if (allRadioBatch->isChecked())
224 ostream << ra1B.toHMSString() << space;
225 else if (ra1CheckBatch->isChecked())
226 ostream << ra1B.toHMSString() << space;
227
228 // Read DEC and write in ostream if corresponds
229
230 if (dec1CheckBatch->isChecked())
231 {
232 dec1B = dms::fromString(fields[i], true);
233 i++;
234 }
235 else
236 dec1B = dec1BoxBatch->createDms();
237
238 if (allRadioBatch->isChecked())
239 ostream << dec1B.toDMSString() << space;
240 else if (dec1CheckBatch->isChecked())
241 ostream << dec1B.toDMSString() << space;
242
243 sp0 = SkyPoint(ra0B, dec0B);
244 sp1 = SkyPoint(ra1B, dec1B);
245 dist = sp0.angularDistanceTo(&sp1, &PA);
246
247 ostream << dist.toDMSString() << QString::number(PA, 'f', 3) << '\n';
248 }
249
250 fOut.close();
251}
SkyObject * targetObject()
Definition finddialog.h:53
Provides all necessary information about an object in the sky: its coordinates, name(s),...
Definition skyobject.h:42
virtual QString name(void) const
Definition skyobject.h:146
The sky coordinates of a point in the sky.
Definition skypoint.h:45
const CachingDms & dec() const
Definition skypoint.h:269
const CachingDms & ra() const
Definition skypoint.h:263
dms angularDistanceTo(const SkyPoint *sp, double *const positionAngle=nullptr) const
Computes the angular distance between two SkyObjects.
Definition skypoint.cpp:899
A QLineEdit which is capable of displaying and parsing angle values flexibly and robustly.
Definition dmsbox.h:34
dms createDms(bool *ok=nullptr)
Parse the text in the dmsBox as an angle.
Definition dmsbox.cpp:110
An angle, stored as degrees, but expressible in many ways.
Definition dms.h:38
static dms fromString(const QString &s, bool deg)
Static function to create a DMS object from a QString.
Definition dms.cpp:429
const QString toDMSString(const bool forceSign=false, const bool machineReadable=false, const bool highPrecision=false) const
Definition dms.cpp:287
const QString toHMSString(const bool machineReadable=false, const bool highPrecision=false) const
Definition dms.cpp:378
modCalcAngDist(QWidget *p)
Constructor.
QString i18n(const char *text, const TYPE &arg...)
QString name(StandardAction id)
bool exists() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * sender() const const
void clear()
bool contains(QChar ch, Qt::CaseSensitivity cs) 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
bool atEnd() const const
QString readLine(qint64 maxlen)
void setupUi(QWidget *widget)
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:04:47 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.