1 /***************************************************************************
2 modcalcgeodcoord.cpp - description
4 begin : Tue Jan 15 2002
5 copyright : (C) 2002 by Pablo de Vicente
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
18 #include "modcalcgeodcoord.h"
19 #include "modcalcgeodcoord.moc"
22 #include "geolocation.h"
25 #include <qradiobutton.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>
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();
54 modCalcGeodCoord::~modCalcGeodCoord(){
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()) {
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 );
161 longBoxBatch
->setEnabled( true );
165 void modCalcGeodCoord::slotLatCheckedBatch(){
167 if ( latCheckBatch
->isChecked() ) {
168 latBoxBatch
->setEnabled( false );
171 latBoxBatch
->setEnabled( true );
175 void modCalcGeodCoord::slotElevCheckedBatch(){
177 if ( elevCheckBatch
->isChecked() ) {
178 elevBoxBatch
->setEnabled( false );
181 elevBoxBatch
->setEnabled( true );
185 void modCalcGeodCoord::slotXCheckedBatch(){
187 if ( xCheckBatch
->isChecked() ) {
188 xBoxBatch
->setEnabled( false );
191 xBoxBatch
->setEnabled( true );
195 void modCalcGeodCoord::slotYCheckedBatch(){
197 if ( yCheckBatch
->isChecked() ) {
198 yBoxBatch
->setEnabled( false );
201 yBoxBatch
->setEnabled( true );
205 void modCalcGeodCoord::slotZCheckedBatch(){
207 if ( zCheckBatch
->isChecked() ) {
208 zBoxBatch
->setEnabled( false );
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" ) );
246 QTextStream
istream(&f
);
247 processLines(istream
);
248 // readFile( istream );
251 QString message
= i18n( "Invalid file: %1" ).arg( inputFileName
);
252 KMessageBox::sorry( 0, message
, i18n( "Invalid file" ) );
254 InputLineEditBatch
->setText( inputFileName
);
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
);
273 GeoLocation
*geoPl
= new GeoLocation();
274 geoPl
->setEllipsoid(0);
276 double xB
, yB
, zB
, hB
;
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
);
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();
300 xB
= KGlobal::locale()->readNumber(xBoxBatch
->text()) ;
302 if ( allRadioBatch
->isChecked() )
303 ostream
<< xB
<< space
;
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();
314 yB
= KGlobal::locale()->readNumber( yBoxBatch
->text()) ;
316 if ( allRadioBatch
->isChecked() )
317 ostream
<< yB
<< space
;
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();
327 zB
= KGlobal::locale()->readNumber( zBoxBatch
->text());
329 if ( allRadioBatch
->isChecked() )
330 ostream
<< zB
<< space
;
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
346 // Read Longitude and write in ostream if corresponds
348 if(longCheckBatch
->isChecked() ) {
349 longB
= dms::fromString( fields
[i
],TRUE
);
352 longB
= longBoxBatch
->createDms(TRUE
);
354 if ( allRadioBatch
->isChecked() )
355 ostream
<< longB
.toDMSString() << space
;
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
);
366 latB
= latBoxBatch
->createDms(TRUE
);
368 if ( allRadioBatch
->isChecked() )
369 ostream
<< latB
.toDMSString() << space
;
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();
380 hB
= elevBoxBatch
->text().toDouble() ;
382 if ( allRadioBatch
->isChecked() )
383 ostream
<< hB
<< space
;
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
;