1 /***************************************************************************
2 modcalcsidtime.cpp - description
4 begin : Wed Jan 23 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 <qdatetimeedit.h> //need for QTimeEdit
19 #include <qradiobutton.h>
20 #include <qcheckbox.h>
22 #include <qtextstream.h>
23 #include <kfiledialog.h>
24 #include <kmessagebox.h>
27 #include "modcalcsidtime.h"
28 #include "modcalcsidtime.moc"
30 #include "kstarsdata.h"
31 #include "kstarsdatetime.h"
33 #include "libkdeedu/extdate/extdatetimeedit.h"
35 modCalcSidTime::modCalcSidTime(QWidget
*parentSplit
, const char *name
) : modCalcSidTimeDlg (parentSplit
,name
) {
37 showCurrentTimeAndLong();
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
)
76 void modCalcSidTime::showST( QTime st
)
81 QTime
modCalcSidTime::getUT( void )
86 QTime
modCalcSidTime::getST( void )
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());
104 UtBox
->setTime(time
);
105 StBox
->setTime(time
);
108 void modCalcSidTime::slotComputeTime(){
111 ExtDate dt
= getDate();
112 dms longitude
= getLongitude();
114 if(UtRadio
->isChecked()) {
116 st
= computeUTtoST( ut
, dt
, longitude
);
120 ut
= computeSTtoUT( st
, dt
, longitude
);
126 void modCalcSidTime::slotUtChecked(){
128 if ( utCheckBatch
->isChecked() )
129 utBoxBatch
->setEnabled( false );
131 utBoxBatch
->setEnabled( true );
134 void modCalcSidTime::slotDateChecked(){
136 if ( dateCheckBatch
->isChecked() )
137 dateBoxBatch
->setEnabled( false );
139 dateBoxBatch
->setEnabled( true );
142 void modCalcSidTime::slotStChecked(){
144 if ( stCheckBatch
->isChecked() )
145 stBoxBatch
->setEnabled( false );
147 stBoxBatch
->setEnabled( true );
150 void modCalcSidTime::slotLongChecked(){
152 if ( longCheckBatch
->isChecked() )
153 longBoxBatch
->setEnabled( false );
155 longBoxBatch
->setEnabled( true );
158 void modCalcSidTime::sidNoCheck() {
160 stBoxBatch
->setEnabled(false);
165 void modCalcSidTime::utNoCheck() {
167 utBoxBatch
->setEnabled(false);
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" ) );
202 QTextStream
istream(&f
);
203 processLines(istream
);
204 // readFile( istream );
207 QString message
= i18n( "Invalid file: %1" ).arg( inputFileName
);
208 KMessageBox::sorry( 0, message
, i18n( "Invalid file" ) );
210 InputLineEditBatch
->setText( inputFileName
);
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
);
229 long double jd0
, jdf
;
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
);
245 // Read Longitude and write in ostream if corresponds
247 if (longCheckBatch
->isChecked() ) {
248 longB
= dms::fromString( fields
[i
],TRUE
);
251 longB
= longBoxBatch
->createDms(TRUE
);
253 if ( allRadioBatch
->isChecked() )
254 ostream
<< longB
.toDMSString() << space
;
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
] );
265 dtB
= dateBoxBatch
->date();
266 if ( allRadioBatch
->isChecked() )
267 ostream
<< dtB
.toString().append(space
);
269 if(dateCheckBatch
->isChecked() )
270 ostream
<< dtB
.toString().append(space
);
273 // We make the first calculations
275 dt
.setFromEpoch( epoch0B
);
276 jdf
= KStarsDateTime(dtB
,utB
).djd();
279 LST
= dms( longB
.Degrees() + KStarsDateTime(dtB
,utB
).gst().Degrees() );
281 // Universal Time is the input time.
284 // Read Ut and write in ostream if corresponds
286 if(utCheckBatch
->isChecked() ) {
287 utB
= QTime::fromString( fields
[i
] );
290 utB
= utBoxBatch
->time();
292 if ( allRadioBatch
->isChecked() )
293 ostream
<< utB
.toString() << space
;
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
306 if(stCheckBatch
->isChecked() ) {
307 stB
= QTime::fromString( fields
[i
] );
310 stB
= stBoxBatch
->time();
312 if ( allRadioBatch
->isChecked() )
313 ostream
<< stB
.toString() << space
;
315 if(stCheckBatch
->isChecked() )
316 ostream
<< stB
.toString() << space
;
319 utB
= computeSTtoUT( stB
, dtB
, longB
);
320 ostream
<< utB
.toString() << endl
;