moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kstars / kstars / tools / modcalcplanets.cpp
blob450876e103be507dba412400ca8a2f139ec0b147
1 /***************************************************************************
2 modcalcequinox.cpp - description
3 -------------------
4 begin : dom may 2 2004
5 copyright : (C) 2004 by Pablo de Vicente
6 email : p.devicentea@wanadoo.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 "modcalcplanets.h"
19 #include "modcalcplanets.moc"
20 #include "dms.h"
21 #include "dmsbox.h"
22 #include "kstars.h"
23 #include "kstarsdata.h"
24 #include "kssun.h"
25 #include "ksplanet.h"
26 #include "ksmoon.h"
27 #include "kspluto.h"
28 #include "libkdeedu/extdate/extdatetimeedit.h"
29 #include "ksnumbers.h"
31 #include <qcombobox.h>
32 #include <qdatetimeedit.h>
33 #include <qstring.h>
34 #include <qtextstream.h>
35 #include <kfiledialog.h>
36 #include <kmessagebox.h>
37 #include <qcheckbox.h>
38 #include <qradiobutton.h>
41 modCalcPlanets::modCalcPlanets(QWidget *parentSplit, const char *name) : modCalcPlanetsDlg (parentSplit,name) {
42 showCurrentDateTime();
43 showLongLat();
44 raBox->setDegType(FALSE);
45 show();
48 modCalcPlanets::~modCalcPlanets(){
51 void modCalcPlanets::showCurrentDateTime (void)
53 KStarsDateTime dt( KStarsDateTime::currentDateTime() );
55 dateBox->setDate( dt.date() );
56 timeBox->setTime( dt.time() );
58 dateBoxBatch->setDate( dt.date() );
59 utBoxBatch->setTime( dt.time() );
62 KStarsDateTime modCalcPlanets::getDateTime (void)
64 return KStarsDateTime( dateBox->date() , timeBox->time() );
67 void modCalcPlanets::showLongLat(void)
70 KStars *ks = (KStars*) parent()->parent()->parent(); // QSplitter->AstroCalc->KStars
71 longBox->show( ks->geo()->lng() );
72 latBox->show( ks->geo()->lat() );
74 longBoxBatch->show( ks->geo()->lng() );
75 latBoxBatch->show( ks->geo()->lat() );
79 GeoLocation modCalcPlanets::getObserverPosition (void)
81 GeoLocation geoPlace;
83 geoPlace.setLong( longBox->createDms() );
84 geoPlace.setLat( latBox->createDms() );
85 geoPlace.setHeight( 0.0 );
87 return geoPlace;
90 void modCalcPlanets::slotComputePosition (void)
92 KStarsData *kd = (KStarsData*) parent()->parent()->parent();
93 KStarsDateTime dt = getDateTime();
94 long double julianDay = dt.djd();
95 GeoLocation position( getObserverPosition() );
96 KSNumbers num( julianDay );
97 dms LST( position.GSTtoLST( dt.gst() ) );
99 // Earth
100 KSPlanet Earth( kd, I18N_NOOP( "Earth" ));
101 Earth.findPosition( &num );
103 // Mercury
104 if (planetComboBox->currentItem() == 0 ) {
105 KSPlanet p( kd, I18N_NOOP( "Mercury" ));
106 p.findPosition( &num, position.lat(), &LST, &Earth);
107 p.EquatorialToHorizontal( position.lat(), &LST);
108 showCoordinates( p );
110 else if(planetComboBox->currentItem() == 1) {
111 KSPlanet p( kd, I18N_NOOP( "Venus" ));
112 p.findPosition( &num, position.lat(), &LST, &Earth);
113 p.EquatorialToHorizontal( position.lat(), &LST);
114 showCoordinates( p );
116 else if(planetComboBox->currentItem() == 2) {
117 showCoordinates( Earth );
119 else if(planetComboBox->currentItem() == 3) {
120 KSPlanet p( kd, I18N_NOOP( "Mars" ));
121 p.findPosition( &num, position.lat(), &LST, &Earth);
122 p.EquatorialToHorizontal( position.lat(), &LST);
123 showCoordinates( p );
125 else if(planetComboBox->currentItem() == 4) {
126 KSPlanet p( kd, I18N_NOOP( "Jupiter" ));
127 p.findPosition( &num, position.lat(), &LST, &Earth);
128 p.EquatorialToHorizontal( position.lat(), &LST);
129 showCoordinates( p );
131 else if(planetComboBox->currentItem() == 5) {
132 KSPlanet p( kd, I18N_NOOP( "Saturn" ));
133 p.findPosition( &num, position.lat(), &LST, &Earth);
134 p.EquatorialToHorizontal( position.lat(), &LST);
135 showCoordinates( p );
137 else if(planetComboBox->currentItem() == 6) {
138 KSPlanet p( kd, I18N_NOOP( "Uranus" ));
139 p.findPosition( &num, position.lat(), &LST, &Earth);
140 p.EquatorialToHorizontal( position.lat(), &LST);
141 showCoordinates( p );
143 else if(planetComboBox->currentItem() == 7) {
144 KSPlanet p( kd, I18N_NOOP( "Neptune" ));
145 p.findPosition( &num, position.lat(), &LST, &Earth);
146 p.EquatorialToHorizontal( position.lat(), &LST);
147 showCoordinates( p );
149 else if(planetComboBox->currentItem() == 8) {
150 KSPluto p( kd );
151 p.findPosition( &num, position.lat(), &LST, &Earth);
152 p.EquatorialToHorizontal( position.lat(), &LST);
153 showCoordinates( p );
155 else if(planetComboBox->currentItem() == 9) {
156 KSMoon p( kd );
157 p.findPosition( &num, position.lat(), &LST, &Earth);
158 p.EquatorialToHorizontal( position.lat(), &LST);
159 showCoordinates( p );
161 else if(planetComboBox->currentItem() == 10) {
162 KSSun p( kd );
163 p.findPosition( &num, position.lat(), &LST, &Earth);
164 p.EquatorialToHorizontal( position.lat(), &LST);
165 p.setRsun(0.0);
166 showCoordinates( p );
170 void modCalcPlanets::showCoordinates( const KSPlanet &ksp) {
172 showHeliocentricEclipticCoords(ksp.helEcLong(), ksp.helEcLat(), ksp.rsun() );
173 showGeocentricEclipticCoords(ksp.ecLong(), ksp.ecLat(), ksp.rearth() );
174 showEquatorialCoords(ksp.ra(), ksp.dec() );
175 showTopocentricCoords(ksp.az(), ksp.alt() );
179 void modCalcPlanets::showCoordinates( const KSMoon &ksp ) {
181 showHeliocentricEclipticCoords(ksp.helEcLong(), ksp.helEcLat(), ksp.rsun() );
182 showGeocentricEclipticCoords(ksp.ecLong(), ksp.ecLat(), ksp.rearth() );
183 showEquatorialCoords(ksp.ra(), ksp.dec() );
184 showTopocentricCoords(ksp.az(), ksp.alt() );
187 void modCalcPlanets::showCoordinates( const KSPluto &ksp ) {
189 showHeliocentricEclipticCoords(ksp.helEcLong(), ksp.helEcLat(), ksp.rsun() );
190 showGeocentricEclipticCoords(ksp.ecLong(), ksp.ecLat(), ksp.rearth() );
191 showEquatorialCoords(ksp.ra(), ksp.dec() );
192 showTopocentricCoords(ksp.az(), ksp.alt() );
196 void modCalcPlanets::showCoordinates( const KSSun &ksp ) {
198 showHeliocentricEclipticCoords(ksp.helEcLong(), ksp.helEcLat(), ksp.rsun() );
199 showGeocentricEclipticCoords(ksp.ecLong(), ksp.ecLat(), ksp.rearth() );
200 showEquatorialCoords(ksp.ra(), ksp.dec() );
201 showTopocentricCoords(ksp.az(), ksp.alt() );
205 void modCalcPlanets::slotClear(void){
206 helLongBox->setText( "" );
207 helLatBox->setText( "" );
208 sunDistBox->setText( "" );
209 geoLongBox->setText("");
210 geoLatBox->setText("");
211 earthDistBox->setText("");
212 raBox->clearFields();
213 decBox->clearFields();
214 azBox->setText("");
215 altBox->setText("");
218 void modCalcPlanets::showHeliocentricEclipticCoords(const dms *hLong, const dms *hLat, double dist)
220 helLongBox->show( hLong );
221 helLatBox->show( hLat );
222 sunDistBox->setText( KGlobal::locale()->formatNumber( dist,6));
225 void modCalcPlanets::showGeocentricEclipticCoords(const dms *eLong, const dms *eLat, double dist)
227 geoLongBox->show( eLong );
228 geoLatBox->show( eLat );
229 earthDistBox->setText( KGlobal::locale()->formatNumber( dist,6));
232 void modCalcPlanets::showEquatorialCoords(const dms *ra, const dms *dec)
234 raBox->show( ra, FALSE );
235 decBox->show( dec );
238 void modCalcPlanets::showTopocentricCoords(const dms *az, const dms *el)
240 azBox->show( az );
241 altBox->show( el );
244 void modCalcPlanets::slotPlanetsCheckedBatch(){
246 if ( planetCheckBatch->isChecked() )
247 planetComboBoxBatch->setEnabled( false );
248 else {
249 planetComboBoxBatch->setEnabled( true );
253 void modCalcPlanets::slotUtCheckedBatch(){
255 if ( utCheckBatch->isChecked() )
256 utBoxBatch->setEnabled( false );
257 else {
258 utBoxBatch->setEnabled( true );
262 void modCalcPlanets::slotDateCheckedBatch(){
264 if ( dateCheckBatch->isChecked() )
265 dateBoxBatch->setEnabled( false );
266 else {
267 dateBoxBatch->setEnabled( true );
271 void modCalcPlanets::slotLongCheckedBatch(){
273 if ( longCheckBatch->isChecked() )
274 longBoxBatch->setEnabled( false );
275 else {
276 longBoxBatch->setEnabled( true );
280 void modCalcPlanets::slotLatCheckedBatch(){
282 if ( latCheckBatch->isChecked() )
283 latBoxBatch->setEnabled( false );
284 else {
285 latBoxBatch->setEnabled( true );
289 void modCalcPlanets::slotInputFile() {
290 QString inputFileName;
291 inputFileName = KFileDialog::getOpenFileName( );
292 InputLineEditBatch->setText( inputFileName );
295 void modCalcPlanets::slotOutputFile() {
296 QString outputFileName;
297 outputFileName = KFileDialog::getSaveFileName( );
298 OutputLineEditBatch->setText( outputFileName );
301 void modCalcPlanets::slotRunBatch() {
303 QString inputFileName;
305 inputFileName = InputLineEditBatch->text();
307 // We open the input file and read its content
309 if ( QFile::exists(inputFileName) ) {
310 QFile f( inputFileName );
311 if ( !f.open( IO_ReadOnly) ) {
312 QString message = i18n( "Could not open file %1.").arg( f.name() );
313 KMessageBox::sorry( 0, message, i18n( "Could Not Open File" ) );
314 inputFileName = "";
315 return;
318 QTextStream istream(&f);
319 processLines(istream);
320 f.close();
321 } else {
322 QString message = i18n( "Invalid file: %1" ).arg( inputFileName );
323 KMessageBox::sorry( 0, message, i18n( "Invalid file" ) );
324 inputFileName = "";
325 InputLineEditBatch->setText( inputFileName );
326 return;
330 void modCalcPlanets::processLines( QTextStream &istream ) {
332 // we open the output file
334 QString outputFileName;
335 outputFileName = OutputLineEditBatch->text();
336 QFile fOut( outputFileName );
337 fOut.open(IO_WriteOnly);
338 QTextStream ostream(&fOut);
340 QString line;
341 QString space = " ";
342 QString planetB;
343 int i = 0;
344 QTime utB;
345 ExtDate dtB;
346 dms longB, latB, hlongB, hlatB, glongB, glatB, raB, decB, azmB, altB;
347 double rSunB(0.0), rEarthB(0.0);
348 KStarsData *kd = (KStarsData*) parent()->parent()->parent(); // QSplitter->AstroCalc->KStars
349 PlanetCatalog PCat( kd );
350 PCat.initialize();
352 QString pName[11], pNamei18n[11];
354 pName[0] = "Mercury"; pNamei18n[0]= i18n("Mercury");
355 pName[1] = "Venus"; pNamei18n[1]= i18n("Venus");
356 pName[2] = "Earth"; pNamei18n[2]= i18n("Earth");
357 pName[3] = "Mars"; pNamei18n[3]= i18n("Mars");
358 pName[4] = "Jupiter"; pNamei18n[4]= i18n("Jupiter");
359 pName[5] = "Saturn"; pNamei18n[5]= i18n("Saturn");
360 pName[6] = "Uranus"; pNamei18n[6]= i18n("Uranus");
361 pName[7] = "Neptune"; pNamei18n[7]= i18n("Neptune");
362 pName[8] = "Pluto"; pNamei18n[8]= i18n("Pluto");
363 pName[9] = "Sun"; pNamei18n[9]= i18n("Sun");
364 pName[10] = "Moon"; pNamei18n[10]= i18n("Moon");
367 while ( ! istream.eof() ) {
368 line = istream.readLine();
369 line.stripWhiteSpace();
371 //Go through the line, looking for parameters
373 QStringList fields = QStringList::split( " ", line );
375 i = 0;
376 if(planetCheckBatch->isChecked() ) {
377 planetB = fields[i];
378 i++;
379 } else
380 planetB = planetComboBoxBatch->currentText( );
382 if ( allRadioBatch->isChecked() )
383 ostream << planetB << space;
384 else
385 if(planetCheckBatch->isChecked() )
386 ostream << planetB << space;
388 // Read Ut and write in ostream if corresponds
390 if(utCheckBatch->isChecked() ) {
391 utB = QTime::fromString( fields[i] );
392 i++;
393 } else
394 utB = utBoxBatch->time();
396 if ( allRadioBatch->isChecked() )
397 ostream << utB.toString() << space;
398 else
399 if(utCheckBatch->isChecked() )
400 ostream << utB.toString() << space;
402 // Read date and write in ostream if corresponds
404 if(dateCheckBatch->isChecked() ) {
405 dtB = ExtDate::fromString( fields[i] );
406 i++;
407 } else
408 dtB = dateBoxBatch->date();
409 if ( allRadioBatch->isChecked() )
410 ostream << dtB.toString().append(space);
411 else
412 if(dateCheckBatch->isChecked() )
413 ostream << dtB.toString().append(space);
415 // Read Longitude and write in ostream if corresponds
417 if (longCheckBatch->isChecked() ) {
418 longB = dms::fromString( fields[i],TRUE);
419 i++;
420 } else
421 longB = longBoxBatch->createDms(TRUE);
423 if ( allRadioBatch->isChecked() )
424 ostream << longB.toDMSString() << space;
425 else
426 if (longCheckBatch->isChecked() )
427 ostream << longB.toDMSString() << space;
429 // Read Latitude
431 if (latCheckBatch->isChecked() ) {
432 latB = dms::fromString( fields[i], TRUE);
433 i++;
434 } else
435 latB = latBoxBatch->createDms(TRUE);
436 if ( allRadioBatch->isChecked() )
437 ostream << latB.toDMSString() << space;
438 else
439 if (latCheckBatch->isChecked() )
440 ostream << latB.toDMSString() << space;
442 KStarsDateTime edt( dtB, utB );
443 dms LST = edt.gst().Degrees() + longB.Degrees();
445 KSNumbers num( edt.djd() );
447 PCat.findPosition( &num, &latB, &LST );
448 PCat.EquatorialToHorizontal(&latB, &LST);
450 KSPlanet Earth( kd, I18N_NOOP( "Earth" ));
451 Earth.findPosition( &num );
453 KSMoon Moon( kd );
454 Moon.findPosition( &num, &latB, &LST, &Earth );
455 Moon.EquatorialToHorizontal( &latB, &LST );
457 int result = 1;
458 int jp = -1;
459 while (result != 0 && jp < 10) {
460 jp++;
461 result = QString::compare( pNamei18n[jp] , planetB );
464 if (jp < 11) {
465 if (jp < 10) {
466 // Heliocentric Ecl. coords.
467 hlongB.setD(PCat.findByName( pName[jp] )->helEcLong()->Degrees());
468 hlatB.setD( PCat.findByName( pName[jp] )->helEcLat()->Degrees());
469 rSunB = PCat.findByName( pName[jp] )->rsun();
471 // Geocentric Ecl. coords.
472 glongB .setD( PCat.findByName( pName[jp] )->ecLong()->Degrees() );
473 glatB.setD( PCat.findByName( pName[jp] )->ecLat()->Degrees() );
474 rEarthB = PCat.findByName( pName[jp] )->rearth();
476 // Equatorial coords.
477 decB.setD( PCat.findByName( pName[jp] )->dec()->Degrees() );
478 raB.setD( PCat.findByName( pName[jp] )->ra()->Degrees() );
480 // Topocentric Coords.
481 azmB.setD( PCat.findByName( pName[jp] )->az()->Degrees() );
482 altB.setD( PCat.findByName( pName[jp] )->alt()->Degrees() );
483 } else {
485 // Heliocentric Ecl. coords.
486 hlongB.setD( Moon.helEcLong()->Degrees() );
487 hlatB.setD( Moon.helEcLat()->Degrees() );
489 // Geocentric Ecl. coords.
490 glongB .setD( Moon.ecLong()->Degrees() );
491 glatB.setD( Moon.ecLat()->Degrees() );
492 rEarthB = Moon.rearth();
494 // Equatorial coords.
495 decB.setD( Moon.dec()->Degrees() );
496 raB.setD( Moon.ra()->Degrees() );
498 // Topocentric Coords.
499 azmB.setD( Moon.az()->Degrees() );
500 altB.setD( Moon.alt()->Degrees() );
504 if ( helEclCheckBatch->isChecked() )
505 ostream << hlongB.toDMSString() << space << hlatB.toDMSString() << space << rSunB << space ;
506 if ( geoEclCheckBatch->isChecked() )
507 ostream << glongB.toDMSString() << space << glatB.toDMSString() << space << rEarthB << space ;
508 if ( equGeoCheckBatch->isChecked() )
509 ostream << raB.toHMSString() << space << decB.toDMSString() << space ;
510 if ( topoCheckBatch->isChecked() )
511 ostream << azmB.toDMSString() << space << altB.toDMSString() << space ;
513 ostream << endl;
518 fOut.close();