20 #include <QTextStream>
23 #include <kfiledialog.h>
24 #include <kmessagebox.h>
38 showCurrentTimeAndLocation();
41 connect(LocationButton, SIGNAL(clicked()),
this, SLOT(slotChangeLocation()));
42 connect(Date, SIGNAL(dateChanged(
const QDate&)),
this, SLOT(slotChangeDate()));
43 connect(LT, SIGNAL(timeChanged(
const QTime&)),
this, SLOT(slotConvertST(
const QTime&)));
44 connect(ST, SIGNAL(timeChanged(
const QTime&)),
this, SLOT(slotConvertLT(
const QTime&)));
46 connect(LocationCheckBatch, SIGNAL(clicked()),
this, SLOT(slotLocationChecked()));
47 connect(DateCheckBatch, SIGNAL(clicked()),
this, SLOT(slotDateChecked()));
48 connect(LocationCheckBatch, SIGNAL(clicked()),
this, SLOT(slotHelpLabel()));
49 connect(DateCheckBatch, SIGNAL(clicked()),
this, SLOT(slotHelpLabel()));
50 connect(ComputeComboBatch, SIGNAL(currentIndexChanged(
int)),
this, SLOT(slotHelpLabel()));
52 connect( InputFileBatch, SIGNAL(urlSelected(
const KUrl&)),
this, SLOT(slotCheckFiles()) );
53 connect( OutputFileBatch, SIGNAL(urlSelected(
const KUrl&)),
this, SLOT(slotCheckFiles()) );
54 connect(LocationButtonBatch, SIGNAL(clicked()),
this, SLOT(slotLocationBatch()));
55 connect(RunButtonBatch, SIGNAL(clicked()),
this, SLOT(slotRunBatch()));
56 connect(ViewButtonBatch, SIGNAL(clicked()),
this, SLOT(slotViewBatch()));
58 RunButtonBatch->setEnabled(
false );
59 ViewButtonBatch->setEnabled(
false );
67 void modCalcSidTime::showCurrentTimeAndLocation()
70 LT->setTime( data->
lt().time() );
71 Date->setDate( data->
lt().date() );
74 LocationButton->setText( geo->
fullName() );
75 geoBatch = data->
geo();
76 LocationButtonBatch->setText( geoBatch->
fullName() );
78 slotConvertST( LT->time() );
81 void modCalcSidTime::slotChangeLocation() {
84 if ( ld->exec() == QDialog::Accepted ) {
88 LocationButton->setText( geo->
fullName() );
91 slotConvertST( LT->time() );
97 void modCalcSidTime::slotChangeDate() {
98 slotConvertST( LT->time() );
101 void modCalcSidTime::slotConvertST(
const QTime <){
103 ST->blockSignals(
true);
104 ST->setTime( computeLTtoST( lt ) );
105 ST->blockSignals(
false);
108 void modCalcSidTime::slotConvertLT(
const QTime &st){
110 LT->blockSignals(
true);
111 LT->setTime( computeSTtoLT( st ) );
112 LT->blockSignals(
false);
115 QTime modCalcSidTime::computeLTtoST( QTime lt )
122 QTime modCalcSidTime::computeSTtoLT( QTime st )
126 lst.setH( st.hour(), st.minute(), st.second() );
132 void modCalcSidTime::slotDateChecked(){
133 DateBatch->setEnabled( ! DateCheckBatch->isChecked() );
136 void modCalcSidTime::slotLocationChecked(){
137 LocationButtonBatch->setEnabled( ! LocationCheckBatch->isChecked() );
139 if ( LocationCheckBatch->isChecked() ) {
140 QString message = i18n(
"Location strings consist of the "
141 "comma-separated names of the city, province and country. "
142 "If the string contains spaces, enclose it in quotes so it "
143 "gets parsed properly.");
145 KMessageBox::information( 0, message, i18n(
"Hint for writing location strings"),
146 "DontShowLocationStringMessageBox" );
150 void modCalcSidTime::slotHelpLabel() {
152 if ( ComputeComboBatch->currentIndex() == 0 )
153 inList.append( i18n(
"local time") );
155 inList.append( i18n(
"sidereal time") );
157 if ( DateCheckBatch->checkState() == Qt::Checked )
158 inList.append( i18n(
"date") );
160 if ( LocationCheckBatch->checkState() == Qt::Checked )
161 inList.append( i18n(
"location") );
163 QString inListString = inList[0];
164 if ( inList.size() == 2 )
165 inListString = i18n(
"%1 and %2", inList[0], inList[1]);
166 if ( inList.size() == 3 )
167 inListString = i18n(
"%1, %2 and %3", inList[0], inList[1], inList[2]);
169 HelpLabel->setText( i18n(
"Specify %1 in the input file.", inListString) );
172 void modCalcSidTime::slotLocationBatch() {
175 if ( ld->exec() == QDialog::Accepted ) {
179 LocationButtonBatch->setText( geoBatch->
fullName() );
185 void modCalcSidTime::slotCheckFiles() {
186 if ( ! InputFileBatch->lineEdit()->text().isEmpty() && ! OutputFileBatch->lineEdit()->text().isEmpty() ) {
187 RunButtonBatch->setEnabled(
true );
189 RunButtonBatch->setEnabled(
false );
193 void modCalcSidTime::slotRunBatch() {
194 QString inputFileName = InputFileBatch->url().toLocalFile();
196 if ( QFile::exists(inputFileName) ) {
197 QFile
f( inputFileName );
198 if ( !
f.open( QIODevice::ReadOnly) ) {
199 QString message = i18n(
"Could not open file %1.",
f.fileName() );
200 KMessageBox::sorry( 0, message, i18n(
"Could Not Open File" ) );
201 inputFileName.clear();
206 processLines(istream);
208 ViewButtonBatch->setEnabled(
true );
212 QString message = i18n(
"Invalid file: %1", inputFileName );
213 KMessageBox::sorry( 0, message, i18n(
"Invalid file" ) );
214 inputFileName.clear();
219 void modCalcSidTime::processLines(
QTextStream &istream ) {
220 QFile fOut( OutputFileBatch->url().toLocalFile() );
221 fOut.open(QIODevice::WriteOnly);
226 QTime inTime, outTime;
229 if ( ! DateCheckBatch->isChecked() )
230 dt = DateBatch->date();
232 while ( ! istream.atEnd() ) {
233 line = istream.readLine();
234 line = line.trimmed();
236 QStringList fields = line.split(
' ', QString::SkipEmptyParts );
239 if (LocationCheckBatch->isChecked() ) {
242 if ( line.indexOf(q) == -1 ) q =
'\'';
243 if ( line.count(q)==2 ) {
244 int iStart = line.indexOf(q);
245 int iEnd = line.indexOf(q, iStart+1);
246 QString locationString = line.mid(iStart, iEnd-iStart+1);
247 line.remove( locationString );
248 fields = line.split(
' ', QString::SkipEmptyParts );
249 locationString.remove( q );
251 QStringList locationFields = locationString.split(
',', QString::SkipEmptyParts );
252 for (
int i=0; i<locationFields.size(); i++)
253 locationFields[i] = locationFields[i].trimmed();
255 if ( locationFields.size() == 1 ) locationFields.insert( 1,
"" );
256 if ( locationFields.size() == 2 ) locationFields.insert( 1,
"" );
257 if ( locationFields.size() != 3 ) {
258 kDebug() << i18n(
"Error: could not parse location string: ") << locationString;
264 kDebug() << i18n(
"Error: location not found in database: ") << locationString;
270 if ( DateCheckBatch->isChecked() ) {
272 foreach (
const QString &s, fields ) {
273 dt = QDate::fromString( s );
274 if ( dt.isValid() )
break;
276 if ( ! dt.isValid() ) {
277 kDebug() << i18n(
"Error: did not find a valid date string in: ") << line;
283 foreach (
const QString& s, fields ) {
284 if ( s.contains(
':') ) {
285 inTime = QTime::fromString( s.length() == 4?
'0'+s:s );
286 if ( inTime.isValid() )
break;
289 if ( ! inTime.isValid() ) {
290 kDebug() << i18n(
"Error: did not find a valid time string in: ") << line;
294 if ( ComputeComboBatch->currentIndex() == 0 ) {
297 ksdt = geoBatch->
LTtoUT( ksdt );
304 lst.setH( inTime.hour(), inTime.minute(), inTime.second() );
305 QTime ut = ksdt.GSTtoUT( geoBatch->
LSTtoGST( lst ) );
307 ksdt = geoBatch->
UTtoLT( ksdt );
308 outTime = ksdt.time();
312 ostream << KGlobal::locale()->formatDate( dt, KLocale::LongDate ) <<
" \""
314 << KGlobal::locale()->formatTime( inTime,
true ) <<
" "
315 << KGlobal::locale()->formatTime( outTime,
true ) << endl;
321 void modCalcSidTime::slotViewBatch() {
322 QFile fOut( OutputFileBatch->url().toLocalFile() );
323 fOut.open(QIODevice::ReadOnly);
327 while ( ! istream.atEnd() )
328 text.append( istream.readLine() );
332 KMessageBox::informationList( 0, i18n(
"Results of Sidereal time calculation"), text, OutputFileBatch->url().toLocalFile() );
335 #include "modcalcsidtime.moc"
Dialog for changing the geographic location of the observer.
GeoLocation * locationNamed(const QString &city, const QString &province=QString(), const QString &country=QString())
KStarsData is the backbone of KStars.
QTime GSTtoUT(dms GST) const
Convert a given Greenwich Sidereal Time to Universal Time (=Greenwich Mean Time). ...
static KStarsData * Instance()
modCalcSidTime(QWidget *p)
KStarsDateTime LTtoUT(const KStarsDateTime <) const
const KStarsDateTime & lt() const
Contains all relevant information for specifying a location on Earth: City Name, State/Province name...
dms GSTtoLST(const dms &gst) const
Extension of KDateTime for KStars KStarsDateTime can represent the date/time as a Julian Day...
An angle, stored as degrees, but expressible in many ways.
KStarsDateTime UTtoLT(const KStarsDateTime &ut) const
dms LSTtoGST(const dms &lst) const