moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kstars / kstars / tools / modcalcgeodcoord.cpp
blobdd913ef60144176827c773b223e10c3e61845ec7
1 /***************************************************************************
2 modcalcgeodcoord.cpp - description
3 -------------------
4 begin : Tue Jan 15 2002
5 copyright : (C) 2002 by Pablo de Vicente
6 email : vicente@oan.es
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include "modcalcgeodcoord.h"
19 #include "modcalcgeodcoord.moc"
20 #include "dms.h"
21 #include "dmsbox.h"
22 #include "geolocation.h"
23 #include "kstars.h"
25 #include <qradiobutton.h>
26 #include <qstring.h>
27 #include <qcheckbox.h>
28 #include <qtextstream.h>
30 #include <kcombobox.h>
31 #include <klineedit.h>
32 #include <kapplication.h>
33 #include <knumvalidator.h>
34 #include <klocale.h>
35 #include <klineedit.h>
36 #include <kfiledialog.h>
37 #include <kmessagebox.h>
40 modCalcGeodCoord::modCalcGeodCoord(QWidget *parentSplit, const char *name) : modCalcGeodCoordDlg(parentSplit,name) {
42 static const char *ellipsoidList[] = {
43 "IAU76", "GRS80", "MERIT83", "WGS84", "IERS89"};
45 spheRadio->setChecked(TRUE);
46 ellipsoidBox->insertStrList (ellipsoidList,5);
47 geoPlace = new GeoLocation();
48 showLongLat();
49 setEllipsoid(0);
50 show();
54 modCalcGeodCoord::~modCalcGeodCoord(){
55 delete geoPlace;
58 void modCalcGeodCoord::showLongLat(void)
61 KStars *ks = (KStars*) parent()->parent()->parent(); // QSplitter->AstroCalc->KStars
62 lonGeoBox->show( ks->geo()->lng() );
63 latGeoBox->show( ks->geo()->lat() );
64 altGeoBox->setText( QString("0.0") );
67 void modCalcGeodCoord::setEllipsoid(int index) {
69 geoPlace->changeEllipsoid(index);
73 void modCalcGeodCoord::getCartGeoCoords (void)
76 geoPlace->setXPos( KGlobal::locale()->readNumber(xGeoName->text())*1000. );
77 geoPlace->setYPos( KGlobal::locale()->readNumber(yGeoName->text())*1000. );
78 geoPlace->setZPos( KGlobal::locale()->readNumber(zGeoName->text())*1000. );
79 //geoPlace->setXPos( xGeoName->text().toDouble()*1000. );
80 //geoPlace->setYPos( yGeoName->text().toDouble()*1000. );
81 //geoPlace->setZPos( zGeoName->text().toDouble()*1000. );
84 void modCalcGeodCoord::getSphGeoCoords (void)
86 geoPlace->setLong( lonGeoBox->createDms() );
87 geoPlace->setLat( latGeoBox->createDms() );
88 geoPlace->setHeight( altGeoBox->text().toDouble() );
91 void modCalcGeodCoord::slotClearGeoCoords (void)
94 geoPlace->setLong( 0.0 );
95 geoPlace->setLat( 0.0 );
96 geoPlace->setHeight( 0.0 );
97 latGeoBox->clearFields();
98 lonGeoBox->clearFields();
99 //showSpheGeoCoords();
100 //showCartGeoCoords();
104 void modCalcGeodCoord::slotComputeGeoCoords (void)
107 if(cartRadio->isChecked()) {
108 getCartGeoCoords();
109 showSpheGeoCoords();
110 } else {
111 getSphGeoCoords();
112 showCartGeoCoords();
117 void modCalcGeodCoord::showSpheGeoCoords(void)
119 lonGeoBox->show( geoPlace->lng() );
120 latGeoBox->show( geoPlace->lat() );
121 altGeoBox->setText( KGlobal::locale()->formatNumber( geoPlace->height(), 3) );
124 void modCalcGeodCoord::showCartGeoCoords(void)
127 xGeoName->setText( KGlobal::locale()->formatNumber( geoPlace->xPos()/1000. ,6));
128 yGeoName->setText( KGlobal::locale()->formatNumber( geoPlace->yPos()/1000. ,6));
129 zGeoName->setText( KGlobal::locale()->formatNumber( geoPlace->zPos()/1000. ,6));
132 void modCalcGeodCoord::geoCheck(void) {
134 xBoxBatch->setEnabled( false );
135 xCheckBatch->setChecked( false );
136 yBoxBatch->setEnabled( false );
137 yCheckBatch->setChecked( false );
138 zBoxBatch->setEnabled( false );
139 zCheckBatch->setChecked( false );
140 xyzInputCoords = FALSE;
143 void modCalcGeodCoord::xyzCheck(void) {
145 longBoxBatch->setEnabled( false );
146 longCheckBatch->setChecked( false );
147 latBoxBatch->setEnabled( false );
148 latCheckBatch->setChecked( false );
149 elevBoxBatch->setEnabled( false );
150 elevCheckBatch->setChecked( false );
151 xyzInputCoords = TRUE;
155 void modCalcGeodCoord::slotLongCheckedBatch(){
157 if ( longCheckBatch->isChecked() ) {
158 longBoxBatch->setEnabled( false );
159 geoCheck();
160 } else {
161 longBoxBatch->setEnabled( true );
165 void modCalcGeodCoord::slotLatCheckedBatch(){
167 if ( latCheckBatch->isChecked() ) {
168 latBoxBatch->setEnabled( false );
169 geoCheck();
170 } else {
171 latBoxBatch->setEnabled( true );
175 void modCalcGeodCoord::slotElevCheckedBatch(){
177 if ( elevCheckBatch->isChecked() ) {
178 elevBoxBatch->setEnabled( false );
179 geoCheck();
180 } else {
181 elevBoxBatch->setEnabled( true );
185 void modCalcGeodCoord::slotXCheckedBatch(){
187 if ( xCheckBatch->isChecked() ) {
188 xBoxBatch->setEnabled( false );
189 xyzCheck();
190 } else {
191 xBoxBatch->setEnabled( true );
195 void modCalcGeodCoord::slotYCheckedBatch(){
197 if ( yCheckBatch->isChecked() ) {
198 yBoxBatch->setEnabled( false );
199 xyzCheck();
200 } else {
201 yBoxBatch->setEnabled( true );
205 void modCalcGeodCoord::slotZCheckedBatch(){
207 if ( zCheckBatch->isChecked() ) {
208 zBoxBatch->setEnabled( false );
209 xyzCheck();
210 } else {
211 zBoxBatch->setEnabled( true );
214 void modCalcGeodCoord::slotInputFile() {
216 QString inputFileName;
217 inputFileName = KFileDialog::getOpenFileName( );
218 InputLineEditBatch->setText( inputFileName );
221 void modCalcGeodCoord::slotOutputFile() {
223 QString outputFileName;
224 outputFileName = KFileDialog::getSaveFileName( );
225 OutputLineEditBatch->setText( outputFileName );
228 void modCalcGeodCoord::slotRunBatch(void) {
230 QString inputFileName;
232 inputFileName = InputLineEditBatch->text();
234 // We open the input file and read its content
236 if ( QFile::exists(inputFileName) ) {
237 QFile f( inputFileName );
238 if ( !f.open( IO_ReadOnly) ) {
239 QString message = i18n( "Could not open file %1.").arg( f.name() );
240 KMessageBox::sorry( 0, message, i18n( "Could Not Open File" ) );
241 inputFileName = "";
242 return;
245 // processLines(&f);
246 QTextStream istream(&f);
247 processLines(istream);
248 // readFile( istream );
249 f.close();
250 } else {
251 QString message = i18n( "Invalid file: %1" ).arg( inputFileName );
252 KMessageBox::sorry( 0, message, i18n( "Invalid file" ) );
253 inputFileName = "";
254 InputLineEditBatch->setText( inputFileName );
255 return;
259 void modCalcGeodCoord::processLines( QTextStream &istream ) {
261 // we open the output file
263 // QTextStream istream(&fIn);
264 QString outputFileName;
265 outputFileName = OutputLineEditBatch->text();
266 QFile fOut( outputFileName );
267 fOut.open(IO_WriteOnly);
268 QTextStream ostream(&fOut);
270 QString line;
271 QString space = " ";
272 int i = 0;
273 GeoLocation *geoPl = new GeoLocation();
274 geoPl->setEllipsoid(0);
276 double xB, yB, zB, hB;
277 dms latB, longB;
280 while ( ! istream.eof() ) {
281 line = istream.readLine();
282 line.stripWhiteSpace();
284 //Go through the line, looking for parameters
286 QStringList fields = QStringList::split( " ", line );
288 i = 0;
290 // Input coords are XYZ:
292 if (xyzInputCoords) {
294 // Read X and write in ostream if corresponds
296 if(xCheckBatch->isChecked() ) {
297 xB = fields[i].toDouble();
298 i++;
299 } else
300 xB = KGlobal::locale()->readNumber(xBoxBatch->text()) ;
302 if ( allRadioBatch->isChecked() )
303 ostream << xB << space;
304 else
305 if(xCheckBatch->isChecked() )
306 ostream << xB << space;
308 // Read Y and write in ostream if corresponds
310 if(yCheckBatch->isChecked() ) {
311 yB = fields[i].toDouble();
312 i++;
313 } else
314 yB = KGlobal::locale()->readNumber( yBoxBatch->text()) ;
316 if ( allRadioBatch->isChecked() )
317 ostream << yB << space;
318 else
319 if(yCheckBatch->isChecked() )
320 ostream << yB << space;
321 // Read Z and write in ostream if corresponds
323 if(zCheckBatch->isChecked() ) {
324 zB = fields[i].toDouble();
325 i++;
326 } else
327 zB = KGlobal::locale()->readNumber( zBoxBatch->text());
329 if ( allRadioBatch->isChecked() )
330 ostream << zB << space;
331 else
332 if(yCheckBatch->isChecked() )
333 ostream << zB << space;
335 geoPl->setXPos( xB*1000.0 );
336 geoPl->setYPos( yB*1000.0 );
337 geoPl->setZPos( zB*1000.0 );
338 ostream << geoPl->lng()->toDMSString() << space <<
339 geoPl->lat()->toDMSString() << space <<
340 geoPl->height() << endl;
342 // Input coords. are Long, Lat and Height
344 } else {
346 // Read Longitude and write in ostream if corresponds
348 if(longCheckBatch->isChecked() ) {
349 longB = dms::fromString( fields[i],TRUE);
350 i++;
351 } else
352 longB = longBoxBatch->createDms(TRUE);
354 if ( allRadioBatch->isChecked() )
355 ostream << longB.toDMSString() << space;
356 else
357 if(longCheckBatch->isChecked() )
358 ostream << longB.toDMSString() << space;
360 // Read Latitude and write in ostream if corresponds
362 if(latCheckBatch->isChecked() ) {
363 latB = dms::fromString( fields[i], TRUE);
364 i++;
365 } else
366 latB = latBoxBatch->createDms(TRUE);
368 if ( allRadioBatch->isChecked() )
369 ostream << latB.toDMSString() << space;
370 else
371 if(latCheckBatch->isChecked() )
372 ostream << latB.toDMSString() << space;
374 // Read Height and write in ostream if corresponds
376 if(elevCheckBatch->isChecked() ) {
377 hB = fields[i].toDouble();
378 i++;
379 } else
380 hB = elevBoxBatch->text().toDouble() ;
382 if ( allRadioBatch->isChecked() )
383 ostream << hB << space;
384 else
385 if(elevCheckBatch->isChecked() )
386 ostream << hB << space;
388 geoPl->setLong( longB );
389 geoPl->setLat( latB );
390 geoPl->setHeight( hB );
392 ostream << geoPl->xPos()/1000.0 << space <<
393 geoPl->yPos()/1000.0 << space <<
394 geoPl->zPos()/1000.0 << endl;
401 fOut.close();