moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kstars / kstars / tools / modcalcapcoord.cpp
blob0973b5493c06ed349b904784b19faca1caee03c3
1 /***************************************************************************
2 modcalcapcoord.cpp - description
3 -------------------
4 begin : Wed Apr 10 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 "modcalcapcoord.h"
19 #include "modcalcapcoord.moc"
20 #include "modcalcprec.h"
21 #include "dms.h"
22 #include "dmsbox.h"
23 #include "skypoint.h"
24 #include "kstarsdatetime.h"
25 #include "libkdeedu/extdate/extdatetimeedit.h"
27 #include <qcheckbox.h>
28 #include <qradiobutton.h>
29 #include <qdatetimeedit.h> //needed for QTimeEdit
30 #include <klineedit.h>
31 #include <qtextstream.h>
32 #include <klocale.h>
33 #include <kfiledialog.h>
34 #include <kmessagebox.h>
36 //#include <kapplication.h> ..already included in modcalcapcoord.h
38 modCalcApCoord::modCalcApCoord(QWidget *parentSplit, const char *name) : modCalcApCoordDlg(parentSplit,name) {
40 showCurrentTime();
41 ra0Box->setDegType(FALSE);
42 rafBox->setDegType(FALSE);
43 show();
47 modCalcApCoord::~modCalcApCoord(){
50 SkyPoint modCalcApCoord::getEquCoords (void) {
51 dms raCoord, decCoord;
53 raCoord = ra0Box->createDms(FALSE);
54 decCoord = dec0Box->createDms();
56 SkyPoint sp = SkyPoint (raCoord, decCoord);
58 return sp;
61 void modCalcApCoord::showCurrentTime (void)
63 KStarsDateTime dt( KStarsDateTime::currentDateTime() );
64 datBox->setDate( dt.date() );
65 timBox->setTime( dt.time() );
68 KStarsDateTime modCalcApCoord::getDateTime (void)
70 return KStarsDateTime( datBox->date() , timBox->time() );
73 double modCalcApCoord::getEpoch (QString eName) {
74 bool ok = false;
75 double epoch = eName.toDouble(&ok);
77 if ( ok )
78 return epoch;
79 else {
80 kdDebug() << i18n( "Could not parse epoch string; assuming J2000" ) << endl;
81 return 2000.0;
85 void modCalcApCoord::showEquCoords ( SkyPoint sp ) {
86 rafBox->show( sp.ra() , FALSE);
87 decfBox->show( sp.dec() );
90 void modCalcApCoord::slotClearCoords(){
92 ra0Box->clearFields();
93 dec0Box->clearFields();
94 rafBox->clearFields();
95 decfBox->clearFields();
96 epoch0Name->setText("");
97 datBox->setDate(ExtDate::currentDate());
98 timBox->setTime(QTime(0,0,0));
101 void modCalcApCoord::slotComputeCoords(){
102 long double jd = getDateTime().djd();
103 KStarsDateTime dt;
104 dt.setFromEpoch( getEpoch( epoch0Name->text() ) );
105 long double jd0 = dt.djd();
107 SkyPoint sp;
108 sp = getEquCoords();
110 sp.apparentCoord(jd0, jd);
111 showEquCoords( sp );
114 void modCalcApCoord::slotUtCheckedBatch(){
115 if ( utCheckBatch->isChecked() )
116 utBoxBatch->setEnabled( false );
117 else {
118 utBoxBatch->setEnabled( true );
122 void modCalcApCoord::slotDateCheckedBatch(){
124 if ( dateCheckBatch->isChecked() )
125 dateBoxBatch->setEnabled( false );
126 else {
127 dateBoxBatch->setEnabled( true );
131 void modCalcApCoord::slotRaCheckedBatch(){
133 if ( raCheckBatch->isChecked() )
134 raBoxBatch->setEnabled( false );
135 else {
136 raBoxBatch->setEnabled( true );
140 void modCalcApCoord::slotDecCheckedBatch(){
142 if ( decCheckBatch->isChecked() )
143 decBoxBatch->setEnabled( false );
144 else {
145 decBoxBatch->setEnabled( true );
149 void modCalcApCoord::slotEpochCheckedBatch(){
151 if ( epochCheckBatch->isChecked() )
152 epochBoxBatch->setEnabled( false );
153 else {
154 epochBoxBatch->setEnabled( true );
158 void modCalcApCoord::slotInputFile() {
159 QString inputFileName;
160 inputFileName = KFileDialog::getOpenFileName( );
161 InputLineEditBatch->setText( inputFileName );
164 void modCalcApCoord::slotOutputFile() {
165 QString outputFileName;
166 outputFileName = KFileDialog::getSaveFileName( );
167 OutputLineEditBatch->setText( outputFileName );
170 void modCalcApCoord::slotRunBatch() {
172 QString inputFileName;
174 inputFileName = InputLineEditBatch->text();
176 // We open the input file and read its content
178 if ( QFile::exists(inputFileName) ) {
179 QFile f( inputFileName );
180 if ( !f.open( IO_ReadOnly) ) {
181 QString message = i18n( "Could not open file %1.").arg( f.name() );
182 KMessageBox::sorry( 0, message, i18n( "Could Not Open File" ) );
183 inputFileName = "";
184 return;
187 // processLines(&f);
188 QTextStream istream(&f);
189 processLines(istream);
190 // readFile( istream );
191 f.close();
192 } else {
193 QString message = i18n( "Invalid file: %1" ).arg( inputFileName );
194 KMessageBox::sorry( 0, message, i18n( "Invalid file" ) );
195 inputFileName = "";
196 InputLineEditBatch->setText( inputFileName );
197 return;
201 //void modCalcApCoord::processLines( const QFile * fIn ) {
202 void modCalcApCoord::processLines( QTextStream &istream ) {
204 // we open the output file
206 // QTextStream istream(&fIn);
207 QString outputFileName;
208 outputFileName = OutputLineEditBatch->text();
209 QFile fOut( outputFileName );
210 fOut.open(IO_WriteOnly);
211 QTextStream ostream(&fOut);
213 QString line;
214 QString space = " ";
215 int i = 0;
216 long double jd, jd0;
217 SkyPoint sp;
218 QTime utB;
219 ExtDate dtB;
220 dms raB, decB;
221 double epoch0B;
223 while ( ! istream.eof() ) {
224 line = istream.readLine();
225 line.stripWhiteSpace();
227 //Go through the line, looking for parameters
229 QStringList fields = QStringList::split( " ", line );
231 i = 0;
233 // Read Ut and write in ostream if corresponds
235 if(utCheckBatch->isChecked() ) {
236 utB = QTime::fromString( fields[i] );
237 i++;
238 } else
239 utB = utBoxBatch->time();
241 if ( allRadioBatch->isChecked() )
242 ostream << utB.toString() << space;
243 else
244 if(utCheckBatch->isChecked() )
245 ostream << utB.toString() << space;
247 // Read date and write in ostream if corresponds
249 if(dateCheckBatch->isChecked() ) {
250 dtB = ExtDate::fromString( fields[i] );
251 i++;
252 } else
253 dtB = dateBoxBatch->date();
255 if ( allRadioBatch->isChecked() )
256 ostream << dtB.toString().append(space);
257 else
258 if(dateCheckBatch->isChecked() )
259 ostream << dtB.toString().append(space);
261 // Read RA and write in ostream if corresponds
263 if(raCheckBatch->isChecked() ) {
264 raB = dms::fromString( fields[i],FALSE);
265 i++;
266 } else
267 raB = raBoxBatch->createDms(FALSE);
269 if ( allRadioBatch->isChecked() )
270 ostream << raB.toHMSString() << space;
271 else
272 if(raCheckBatch->isChecked() )
273 ostream << raB.toHMSString() << space;
275 // Read DEC and write in ostream if corresponds
277 if(decCheckBatch->isChecked() ) {
278 decB = dms::fromString( fields[i], TRUE);
279 i++;
280 } else
281 decB = decBoxBatch->createDms();
283 if ( allRadioBatch->isChecked() )
284 ostream << decB.toDMSString() << space;
285 else
286 if(decCheckBatch->isChecked() )
287 ostream << decB.toHMSString() << space;
289 // Read Epoch and write in ostream if corresponds
291 if(epochCheckBatch->isChecked() ) {
292 epoch0B = fields[i].toDouble();
293 i++;
294 } else
295 epoch0B = getEpoch( epochBoxBatch->text() );
297 if ( allRadioBatch->isChecked() )
298 ostream << epoch0B;
299 else
300 if(decCheckBatch->isChecked() )
301 ostream << epoch0B;
303 KStarsDateTime dt;
304 dt.setFromEpoch( epoch0B );
305 jd = KStarsDateTime(dtB,utB).djd();
306 jd0 = dt.djd();
307 sp = SkyPoint (raB, decB);
308 sp.apparentCoord(jd0, jd);
310 ostream << sp.ra()->toHMSString() << sp.dec()->toDMSString() << endl;
313 fOut.close();