Kstars

modcalcgeodcoord.cpp
1/*
2 SPDX-FileCopyrightText: 2002 Pablo de Vicente <vicente@oan.es>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "modcalcgeodcoord.h"
8
9#include "dms.h"
10#include "geolocation.h"
11#include "kstars.h"
12#include "ksnotification.h"
13#include "kstarsdata.h"
14
15#include <QTextStream>
16#include <QFileDialog>
17
18modCalcGeodCoord::modCalcGeodCoord(QWidget *parentSplit) : QFrame(parentSplit)
19{
20 QStringList ellipsoidList;
21
22 ellipsoidList << "IAU76"
23 << "GRS80"
24 << "MERIT83"
25 << "WGS84"
26 << "IERS89";
27
28 setupUi(this);
29
30 spheRadio->setChecked(true);
31 ellipsoidBox->insertItems(5, ellipsoidList);
32 geoPlace.reset(new GeoLocation(dms(0), dms(0)));
33 showLongLat();
34 setEllipsoid(0);
35 show();
36
37 connect(Clear, SIGNAL(clicked()), this, SLOT(slotClearGeoCoords()));
38 connect(Compute, SIGNAL(clicked()), this, SLOT(slotComputeGeoCoords()));
39}
40
41void modCalcGeodCoord::showLongLat(void)
42{
43 KStarsData *data = KStarsData::Instance();
44
45 LongGeoBox->show(data->geo()->lng());
46 LatGeoBox->show(data->geo()->lat());
47 AltGeoBox->setText(QString("0.0"));
48}
49
50void modCalcGeodCoord::setEllipsoid(int index)
51{
52 geoPlace->changeEllipsoid(index);
53}
54
55void modCalcGeodCoord::getCartGeoCoords(void)
56{
57 geoPlace->setXPos(XGeoBox->text().toDouble() * 1000.);
58 geoPlace->setYPos(YGeoBox->text().toDouble() * 1000.);
59 geoPlace->setZPos(ZGeoBox->text().toDouble() * 1000.);
60}
61
62void modCalcGeodCoord::getSphGeoCoords(void)
63{
64 geoPlace->setLong(LongGeoBox->createDms());
65 geoPlace->setLat(LatGeoBox->createDms());
66 geoPlace->setElevation(AltGeoBox->text().toDouble());
67}
68
69void modCalcGeodCoord::slotClearGeoCoords(void)
70{
71 geoPlace->setLong(dms(0.0));
72 geoPlace->setLat(dms(0.0));
73 geoPlace->setElevation(0.0);
74 LatGeoBox->clearFields();
75 LongGeoBox->clearFields();
76}
77
78void modCalcGeodCoord::slotComputeGeoCoords(void)
79{
80 if (cartRadio->isChecked())
81 {
82 getCartGeoCoords();
83 showSpheGeoCoords();
84 }
85 else
86 {
87 getSphGeoCoords();
88 showCartGeoCoords();
89 }
90}
91
92void modCalcGeodCoord::showSpheGeoCoords(void)
93{
94 LongGeoBox->show(geoPlace->lng());
95 LatGeoBox->show(geoPlace->lat());
96 AltGeoBox->setText(QLocale().toString(geoPlace->elevation(), 3));
97}
98
99void modCalcGeodCoord::showCartGeoCoords(void)
100{
101 XGeoBox->setText(QLocale().toString(geoPlace->xPos() / 1000., 6));
102 YGeoBox->setText(QLocale().toString(geoPlace->yPos() / 1000., 6));
103 ZGeoBox->setText(QLocale().toString(geoPlace->zPos() / 1000., 6));
104}
105
106void modCalcGeodCoord::geoCheck(void)
107{
108 XGeoBoxBatch->setEnabled(false);
109 XGeoCheckBatch->setChecked(false);
110 YGeoBoxBatch->setEnabled(false);
111 YGeoCheckBatch->setChecked(false);
112 YGeoBoxBatch->setEnabled(false);
113 YGeoCheckBatch->setChecked(false);
114 xyzInputCoords = false;
115}
116
117void modCalcGeodCoord::xyzCheck(void)
118{
119 LongGeoBoxBatch->setEnabled(false);
120 LongGeoCheckBatch->setChecked(false);
121 LatGeoBoxBatch->setEnabled(false);
122 LatGeoCheckBatch->setChecked(false);
123 AltGeoBoxBatch->setEnabled(false);
124 AltGeoCheckBatch->setChecked(false);
125 xyzInputCoords = true;
126}
127
128void modCalcGeodCoord::slotLongCheckedBatch()
129{
130 if (LongGeoCheckBatch->isChecked())
131 {
132 LongGeoBoxBatch->setEnabled(false);
133 geoCheck();
134 }
135 else
136 {
137 LongGeoBoxBatch->setEnabled(true);
138 }
139}
140
141void modCalcGeodCoord::slotLatCheckedBatch()
142{
143 if (LatGeoCheckBatch->isChecked())
144 {
145 LatGeoBoxBatch->setEnabled(false);
146 geoCheck();
147 }
148 else
149 {
150 LatGeoBoxBatch->setEnabled(true);
151 }
152}
153
154void modCalcGeodCoord::slotElevCheckedBatch()
155{
156 if (AltGeoCheckBatch->isChecked())
157 {
158 AltGeoBoxBatch->setEnabled(false);
159 geoCheck();
160 }
161 else
162 {
163 AltGeoBoxBatch->setEnabled(true);
164 }
165}
166
167void modCalcGeodCoord::slotXCheckedBatch()
168{
169 if (XGeoCheckBatch->isChecked())
170 {
171 XGeoBoxBatch->setEnabled(false);
172 xyzCheck();
173 }
174 else
175 {
176 XGeoBoxBatch->setEnabled(true);
177 }
178}
179
180void modCalcGeodCoord::slotYCheckedBatch()
181{
182 if (YGeoCheckBatch->isChecked())
183 {
184 YGeoBoxBatch->setEnabled(false);
185 xyzCheck();
186 }
187 else
188 {
189 YGeoBoxBatch->setEnabled(true);
190 }
191}
192
193void modCalcGeodCoord::slotZCheckedBatch()
194{
195 if (ZGeoCheckBatch->isChecked())
196 {
197 ZGeoBoxBatch->setEnabled(false);
198 xyzCheck();
199 }
200 else
201 {
202 ZGeoBoxBatch->setEnabled(true);
203 }
204}
205void modCalcGeodCoord::slotInputFile()
206{
208 if (!inputFileName.isEmpty())
209 InputFileBoxBatch->setUrl(QUrl::fromLocalFile(inputFileName));
210}
211
212void modCalcGeodCoord::slotOutputFile()
213{
214 const QString outputFileName = QFileDialog::getSaveFileName();
215 if (!outputFileName.isEmpty())
216 OutputFileBoxBatch->setUrl(QUrl::fromLocalFile(outputFileName));
217}
218
219void modCalcGeodCoord::slotRunBatch(void)
220{
221 const QString inputFileName = InputFileBoxBatch->url().toLocalFile();
222
223 // We open the input file and read its content
224
225 if (QFile::exists(inputFileName))
226 {
227 QFile f(inputFileName);
228 if (!f.open(QIODevice::ReadOnly))
229 {
230 QString message = i18n("Could not open file %1.", f.fileName());
231 KSNotification::sorry(message, i18n("Could Not Open File"));
232 return;
233 }
234
235 // processLines(&f);
236 QTextStream istream(&f);
237 processLines(istream);
238 // readFile( istream );
239 f.close();
240 }
241 else
242 {
243 QString message = i18n("Invalid file: %1", inputFileName);
244 KSNotification::sorry(message, i18n("Invalid file"));
245 InputFileBoxBatch->setUrl(QUrl());
246 }
247}
248
249void modCalcGeodCoord::processLines(QTextStream &istream)
250{
251 // we open the output file
252
253 // QTextStream istream(&fIn);
254 const QString outputFileName = OutputFileBoxBatch->url().toLocalFile();
255 QFile fOut(outputFileName);
256 fOut.open(QIODevice::WriteOnly);
257 QTextStream ostream(&fOut);
258
259 QString line;
260 QChar space = ' ';
261 int i = 0;
262 GeoLocation geoPl(dms(0), dms(0));
263 geoPl.setEllipsoid(0);
264
265 double xB, yB, zB, hB;
266 dms latB, longB;
267
268 while (!istream.atEnd())
269 {
270 line = istream.readLine();
271 line = line.trimmed();
272
273 //Go through the line, looking for parameters
274
275 QStringList fields = line.split(' ');
276
277 i = 0;
278
279 // Input coords are XYZ:
280
281 if (xyzInputCoords)
282 {
283 // Read X and write in ostream if corresponds
284
285 if (XGeoCheckBatch->isChecked())
286 {
287 xB = fields[i].toDouble();
288 i++;
289 }
290 else
291 xB = XGeoBoxBatch->text().toDouble();
292
293 if (AllRadioBatch->isChecked())
294 ostream << xB << space;
295 else if (XGeoCheckBatch->isChecked())
296 ostream << xB << space;
297
298 // Read Y and write in ostream if corresponds
299
300 if (YGeoCheckBatch->isChecked())
301 {
302 yB = fields[i].toDouble();
303 i++;
304 }
305 else
306 yB = YGeoBoxBatch->text().toDouble();
307
308 if (AllRadioBatch->isChecked())
309 ostream << yB << space;
310 else if (YGeoCheckBatch->isChecked())
311 ostream << yB << space;
312 // Read Z and write in ostream if corresponds
313
314 if (ZGeoCheckBatch->isChecked())
315 {
316 zB = fields[i].toDouble();
317 i++;
318 }
319 else
320 zB = ZGeoBoxBatch->text().toDouble();
321
322 if (AllRadioBatch->isChecked())
323 ostream << zB << space;
324 else if (YGeoCheckBatch->isChecked())
325 ostream << zB << space;
326
327 geoPl.setXPos(xB * 1000.0);
328 geoPl.setYPos(yB * 1000.0);
329 geoPl.setZPos(zB * 1000.0);
330 ostream << geoPl.lng()->toDMSString() << space << geoPl.lat()->toDMSString() << space << geoPl.elevation()
331 << '\n';
332
333 // Input coords. are Long, Lat and Height
334 }
335 else
336 {
337 // Read Longitude and write in ostream if corresponds
338
339 if (LongGeoCheckBatch->isChecked())
340 {
341 longB = dms::fromString(fields[i], true);
342 i++;
343 }
344 else
345 longB = LongGeoBoxBatch->createDms();
346
347 if (AllRadioBatch->isChecked())
348 ostream << longB.toDMSString() << space;
349 else if (LongGeoCheckBatch->isChecked())
350 ostream << longB.toDMSString() << space;
351
352 // Read Latitude and write in ostream if corresponds
353
354 if (LatGeoCheckBatch->isChecked())
355 {
356 latB = dms::fromString(fields[i], true);
357 i++;
358 }
359 else
360 latB = LatGeoBoxBatch->createDms();
361
362 if (AllRadioBatch->isChecked())
363 ostream << latB.toDMSString() << space;
364 else if (LatGeoCheckBatch->isChecked())
365 ostream << latB.toDMSString() << space;
366
367 // Read Height and write in ostream if corresponds
368
369 if (AltGeoCheckBatch->isChecked())
370 {
371 hB = fields[i].toDouble();
372 i++;
373 }
374 else
375 hB = AltGeoBoxBatch->text().toDouble();
376
377 if (AllRadioBatch->isChecked())
378 ostream << hB << space;
379 else if (AltGeoCheckBatch->isChecked())
380 ostream << hB << space;
381
382 geoPl.setLong(longB);
383 geoPl.setLat(latB);
384 geoPl.setElevation(hB);
385
386 ostream << geoPl.xPos() / 1000.0 << space << geoPl.yPos() / 1000.0 << space << geoPl.zPos() / 1000.0
387 << '\n';
388 }
389 }
390
391 fOut.close();
392}
Contains all relevant information for specifying a location on Earth: City Name, State/Province name,...
Definition geolocation.h:28
const CachingDms * lat() const
Definition geolocation.h:70
const CachingDms * lng() const
Definition geolocation.h:64
KStarsData is the backbone of KStars.
Definition kstarsdata.h:74
GeoLocation * geo()
Definition kstarsdata.h:232
static KStars * Instance()
Definition kstars.h:121
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
QString i18n(const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
bool exists() const const
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
bool isEmpty() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString trimmed() const const
bool atEnd() const const
QString readLine(qint64 maxlen)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QUrl fromLocalFile(const QString &localFile)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.