moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kstars / kstars / tools / modcalcsidtime.cpp
blobb8eccfe5a6a54279fc3155a52918af65d8d51f31
1 /***************************************************************************
2 modcalcsidtime.cpp - description
3 -------------------
4 begin : Wed Jan 23 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 <qdatetimeedit.h> //need for QTimeEdit
19 #include <qradiobutton.h>
20 #include <qcheckbox.h>
21 #include <qstring.h>
22 #include <qtextstream.h>
23 #include <kfiledialog.h>
24 #include <kmessagebox.h>
26 #include "dmsbox.h"
27 #include "modcalcsidtime.h"
28 #include "modcalcsidtime.moc"
29 #include "kstars.h"
30 #include "kstarsdata.h"
31 #include "kstarsdatetime.h"
32 #include "simclock.h"
33 #include "libkdeedu/extdate/extdatetimeedit.h"
35 modCalcSidTime::modCalcSidTime(QWidget *parentSplit, const char *name) : modCalcSidTimeDlg (parentSplit,name) {
37 showCurrentTimeAndLong();
38 show();
41 modCalcSidTime::~modCalcSidTime(void) {
45 void modCalcSidTime::showCurrentTimeAndLong (void)
47 KStars *ks = (KStars*) parent()->parent()->parent();
48 // modCalcSidTimeDlg -> QSplitter->AstroCalc->KStars
50 showUT( ks->data()->ut().time() );
51 datBox->setDate( ks->data()->ut().date() );
53 longBox->show( ks->geo()->lng() );
56 QTime modCalcSidTime::computeUTtoST (QTime ut, ExtDate dt, dms longitude)
58 KStarsDateTime utdt = KStarsDateTime( dt, ut);
59 dms st = longitude.Degrees() + utdt.gst().Degrees();
60 return QTime( st.hour(), st.minute(), st.second() );
63 QTime modCalcSidTime::computeSTtoUT (QTime st, ExtDate dt, dms longitude)
65 KStarsDateTime dtt = KStarsDateTime( dt, QTime());
66 dms lst(st.hour(), st.minute(), st.second());
67 dms gst( lst.Degrees() - longitude.Degrees() );
68 return dtt.GSTtoUT( gst );
71 void modCalcSidTime::showUT( QTime dt )
73 UtBox->setTime( dt );
76 void modCalcSidTime::showST( QTime st )
78 StBox->setTime( st );
81 QTime modCalcSidTime::getUT( void )
83 return UtBox->time();
86 QTime modCalcSidTime::getST( void )
88 return StBox->time();
91 ExtDate modCalcSidTime::getDate( void )
93 return datBox->date();
96 dms modCalcSidTime::getLongitude( void )
98 return longBox->createDms();
101 void modCalcSidTime::slotClearFields(){
102 datBox->setDate(ExtDate::currentDate());
103 QTime time(0,0,0);
104 UtBox->setTime(time);
105 StBox->setTime(time);
108 void modCalcSidTime::slotComputeTime(){
109 QTime ut, st;
111 ExtDate dt = getDate();
112 dms longitude = getLongitude();
114 if(UtRadio->isChecked()) {
115 ut = getUT();
116 st = computeUTtoST( ut, dt, longitude );
117 showST( st );
118 } else {
119 st = getST();
120 ut = computeSTtoUT( st, dt, longitude );
121 showUT( ut );
126 void modCalcSidTime::slotUtChecked(){
128 if ( utCheckBatch->isChecked() )
129 utBoxBatch->setEnabled( false );
130 else
131 utBoxBatch->setEnabled( true );
134 void modCalcSidTime::slotDateChecked(){
136 if ( dateCheckBatch->isChecked() )
137 dateBoxBatch->setEnabled( false );
138 else
139 dateBoxBatch->setEnabled( true );
142 void modCalcSidTime::slotStChecked(){
144 if ( stCheckBatch->isChecked() )
145 stBoxBatch->setEnabled( false );
146 else
147 stBoxBatch->setEnabled( true );
150 void modCalcSidTime::slotLongChecked(){
152 if ( longCheckBatch->isChecked() )
153 longBoxBatch->setEnabled( false );
154 else
155 longBoxBatch->setEnabled( true );
158 void modCalcSidTime::sidNoCheck() {
160 stBoxBatch->setEnabled(false);
161 stInputTime = FALSE;
165 void modCalcSidTime::utNoCheck() {
167 utBoxBatch->setEnabled(false);
168 stInputTime = TRUE;
171 void modCalcSidTime::slotInputFile() {
172 QString inputFileName;
173 inputFileName = KFileDialog::getOpenFileName( );
174 InputLineEditBatch->setText( inputFileName );
177 void modCalcSidTime::slotOutputFile() {
178 QString outputFileName;
179 outputFileName = KFileDialog::getSaveFileName( );
180 OutputLineEditBatch->setText( outputFileName );
184 void modCalcSidTime::slotRunBatch() {
186 QString inputFileName;
188 inputFileName = InputLineEditBatch->text();
190 // We open the input file and read its content
192 if ( QFile::exists(inputFileName) ) {
193 QFile f( inputFileName );
194 if ( !f.open( IO_ReadOnly) ) {
195 QString message = i18n( "Could not open file %1.").arg( f.name() );
196 KMessageBox::sorry( 0, message, i18n( "Could Not Open File" ) );
197 inputFileName = "";
198 return;
201 // processLines(&f);
202 QTextStream istream(&f);
203 processLines(istream);
204 // readFile( istream );
205 f.close();
206 } else {
207 QString message = i18n( "Invalid file: %1" ).arg( inputFileName );
208 KMessageBox::sorry( 0, message, i18n( "Invalid file" ) );
209 inputFileName = "";
210 InputLineEditBatch->setText( inputFileName );
211 return;
215 void modCalcSidTime::processLines( QTextStream &istream ) {
217 // we open the output file
219 // QTextStream istream(&fIn);
220 QString outputFileName;
221 outputFileName = OutputLineEditBatch->text();
222 QFile fOut( outputFileName );
223 fOut.open(IO_WriteOnly);
224 QTextStream ostream(&fOut);
226 QString line;
227 QString space = " ";
228 int i = 0;
229 long double jd0, jdf;
230 dms longB, LST;
231 double epoch0B(0.0);
232 QTime utB, stB;
233 ExtDate dtB;
235 while ( ! istream.eof() ) {
236 line = istream.readLine();
237 line.stripWhiteSpace();
239 //Go through the line, looking for parameters
241 QStringList fields = QStringList::split( " ", line );
243 i = 0;
245 // Read Longitude and write in ostream if corresponds
247 if (longCheckBatch->isChecked() ) {
248 longB = dms::fromString( fields[i],TRUE);
249 i++;
250 } else
251 longB = longBoxBatch->createDms(TRUE);
253 if ( allRadioBatch->isChecked() )
254 ostream << longB.toDMSString() << space;
255 else
256 if (longCheckBatch->isChecked() )
257 ostream << longB.toDMSString() << space;
259 // Read date and write in ostream if corresponds
261 if(dateCheckBatch->isChecked() ) {
262 dtB = ExtDate::fromString( fields[i] );
263 i++;
264 } else
265 dtB = dateBoxBatch->date();
266 if ( allRadioBatch->isChecked() )
267 ostream << dtB.toString().append(space);
268 else
269 if(dateCheckBatch->isChecked() )
270 ostream << dtB.toString().append(space);
273 // We make the first calculations
274 KStarsDateTime dt;
275 dt.setFromEpoch( epoch0B );
276 jdf = KStarsDateTime(dtB,utB).djd();
277 jd0 = dt.djd();
279 LST = dms( longB.Degrees() + KStarsDateTime(dtB,utB).gst().Degrees() );
281 // Universal Time is the input time.
282 if (!stInputTime) {
284 // Read Ut and write in ostream if corresponds
286 if(utCheckBatch->isChecked() ) {
287 utB = QTime::fromString( fields[i] );
288 i++;
289 } else
290 utB = utBoxBatch->time();
292 if ( allRadioBatch->isChecked() )
293 ostream << utB.toString() << space;
294 else
295 if(utCheckBatch->isChecked() )
296 ostream << utB.toString() << space;
299 stB = computeUTtoST( utB, dtB, longB );
300 ostream << stB.toString() << endl;
302 // Input coords are horizontal coordinates
304 } else {
306 if(stCheckBatch->isChecked() ) {
307 stB = QTime::fromString( fields[i] );
308 i++;
309 } else
310 stB = stBoxBatch->time();
312 if ( allRadioBatch->isChecked() )
313 ostream << stB.toString() << space;
314 else
315 if(stCheckBatch->isChecked() )
316 ostream << stB.toString() << space;
319 utB = computeSTtoUT( stB, dtB, longB );
320 ostream << utB.toString() << endl;
327 fOut.close();