• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
  • tools
modcalcsidtime.cpp
Go to the documentation of this file.
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  ***************************************************************************/
8 
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  ***************************************************************************/
17 
18 #include "modcalcsidtime.h"
19 
20 #include <QTextStream>
21 #include <KGlobal>
22 #include <KLocale>
23 #include <kfiledialog.h>
24 #include <kmessagebox.h>
25 
26 #include "kstarsdata.h"
27 #include "kstarsdatetime.h"
28 #include "simclock.h"
29 #include "dialogs/locationdialog.h"
30 #include "widgets/dmsbox.h"
31 
32 modCalcSidTime::modCalcSidTime(QWidget *parent) :
33  QFrame(parent)
34 {
35  setupUi(this);
36 
37  //Preset date and location
38  showCurrentTimeAndLocation();
39 
40  // signals and slots connections
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&)));
45 
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()));
51 
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()));
57 
58  RunButtonBatch->setEnabled( false );
59  ViewButtonBatch->setEnabled( false );
60 
61  show();
62 }
63 
64 modCalcSidTime::~modCalcSidTime()
65 {}
66 
67 void modCalcSidTime::showCurrentTimeAndLocation()
68 {
69  KStarsData* data = KStarsData::Instance();
70  LT->setTime( data->lt().time() );
71  Date->setDate( data->lt().date() );
72 
73  geo = data->geo();
74  LocationButton->setText( geo->fullName() );
75  geoBatch = data->geo();
76  LocationButtonBatch->setText( geoBatch->fullName() );
77 
78  slotConvertST( LT->time() );
79 }
80 
81 void modCalcSidTime::slotChangeLocation() {
82  QPointer<LocationDialog> ld = new LocationDialog( this );
83 
84  if ( ld->exec() == QDialog::Accepted ) {
85  GeoLocation *newGeo = ld->selectedCity();
86  if ( newGeo ) {
87  geo = newGeo;
88  LocationButton->setText( geo->fullName() );
89 
90  //Update the displayed ST
91  slotConvertST( LT->time() );
92  }
93  }
94  delete ld;
95 }
96 
97 void modCalcSidTime::slotChangeDate() {
98  slotConvertST( LT->time() );
99 }
100 
101 void modCalcSidTime::slotConvertST(const QTime &lt){
102  // blockSignals is used to break signal loop
103  ST->blockSignals(true);
104  ST->setTime( computeLTtoST( lt ) );
105  ST->blockSignals(false);
106 }
107 
108 void modCalcSidTime::slotConvertLT(const QTime &st){
109  // blockSignals is used to break signal loop
110  LT->blockSignals(true);
111  LT->setTime( computeSTtoLT( st ) );
112  LT->blockSignals(false);
113 }
114 
115 QTime modCalcSidTime::computeLTtoST( QTime lt )
116 {
117  KStarsDateTime utdt = geo->LTtoUT( KStarsDateTime( Date->date(), lt ) );
118  dms st = geo->GSTtoLST( utdt.gst() );
119  return QTime( st.hour(), st.minute(), st.second() );
120 }
121 
122 QTime modCalcSidTime::computeSTtoLT( QTime st )
123 {
124  KStarsDateTime dt0 = KStarsDateTime( Date->date(), QTime(0,0,0));
125  dms lst;
126  lst.setH( st.hour(), st.minute(), st.second() );
127  dms gst = geo->LSTtoGST( lst );
128  return geo->UTtoLT(KStarsDateTime(Date->date(), dt0.GSTtoUT(gst))).time();
129 }
130 
131 //** Batch mode **//
132 void modCalcSidTime::slotDateChecked(){
133  DateBatch->setEnabled( ! DateCheckBatch->isChecked() );
134 }
135 
136 void modCalcSidTime::slotLocationChecked(){
137  LocationButtonBatch->setEnabled( ! LocationCheckBatch->isChecked() );
138 
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.");
144 
145  KMessageBox::information( 0, message, i18n("Hint for writing location strings"),
146  "DontShowLocationStringMessageBox" );
147  }
148 }
149 
150 void modCalcSidTime::slotHelpLabel() {
151  QStringList inList;
152  if ( ComputeComboBatch->currentIndex() == 0 )
153  inList.append( i18n("local time") );
154  else
155  inList.append( i18n("sidereal time") );
156 
157  if ( DateCheckBatch->checkState() == Qt::Checked )
158  inList.append( i18n("date") );
159 
160  if ( LocationCheckBatch->checkState() == Qt::Checked )
161  inList.append( i18n("location") );
162 
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]);
168 
169  HelpLabel->setText( i18n("Specify %1 in the input file.", inListString) );
170 }
171 
172 void modCalcSidTime::slotLocationBatch() {
173  QPointer<LocationDialog> ld = new LocationDialog( this );
174 
175  if ( ld->exec() == QDialog::Accepted ) {
176  GeoLocation *newGeo = ld->selectedCity();
177  if ( newGeo ) {
178  geoBatch = newGeo;
179  LocationButtonBatch->setText( geoBatch->fullName() );
180  }
181  }
182  delete ld;
183 }
184 
185 void modCalcSidTime::slotCheckFiles() {
186  if ( ! InputFileBatch->lineEdit()->text().isEmpty() && ! OutputFileBatch->lineEdit()->text().isEmpty() ) {
187  RunButtonBatch->setEnabled( true );
188  } else {
189  RunButtonBatch->setEnabled( false );
190  }
191 }
192 
193 void modCalcSidTime::slotRunBatch() {
194  QString inputFileName = InputFileBatch->url().toLocalFile();
195 
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();
202  return;
203  }
204 
205  QTextStream istream(&f);
206  processLines(istream);
207 
208  ViewButtonBatch->setEnabled( true );
209 
210  f.close();
211  } else {
212  QString message = i18n( "Invalid file: %1", inputFileName );
213  KMessageBox::sorry( 0, message, i18n( "Invalid file" ) );
214  inputFileName.clear();
215  return;
216  }
217 }
218 
219 void modCalcSidTime::processLines( QTextStream &istream ) {
220  QFile fOut( OutputFileBatch->url().toLocalFile() );
221  fOut.open(QIODevice::WriteOnly);
222  QTextStream ostream(&fOut);
223 
224  QString line;
225  dms LST;
226  QTime inTime, outTime;
227  QDate dt;
228 
229  if ( ! DateCheckBatch->isChecked() )
230  dt = DateBatch->date();
231 
232  while ( ! istream.atEnd() ) {
233  line = istream.readLine();
234  line = line.trimmed();
235 
236  QStringList fields = line.split( ' ', QString::SkipEmptyParts );
237 
238  //Find and parse the location string
239  if (LocationCheckBatch->isChecked() ) {
240  //First, look for a pair of quotation marks, and parse the string between them
241  QChar q = '\"';
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 );
250 
251  QStringList locationFields = locationString.split( ',', QString::SkipEmptyParts );
252  for (int i=0; i<locationFields.size(); i++)
253  locationFields[i] = locationFields[i].trimmed();
254 
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;
259  continue;
260  }
261 
262  geoBatch = KStarsData::Instance()->locationNamed( locationFields[0], locationFields[1], locationFields[2] );
263  if ( ! geoBatch ) {
264  kDebug() << i18n("Error: location not found in database: ") << locationString;
265  continue;
266  }
267  }
268  }
269 
270  if ( DateCheckBatch->isChecked() ) {
271  //Parse one of the fields as the date
272  foreach ( const QString &s, fields ) {
273  dt = QDate::fromString( s );
274  if ( dt.isValid() ) break;
275  }
276  if ( ! dt.isValid() ) {
277  kDebug() << i18n("Error: did not find a valid date string in: ") << line;
278  continue;
279  }
280  }
281 
282  //Parse one of the fields as the time
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;
287  }
288  }
289  if ( ! inTime.isValid() ) {
290  kDebug() << i18n("Error: did not find a valid time string in: ") << line;
291  continue;
292  }
293 
294  if ( ComputeComboBatch->currentIndex() == 0 ) {
295  //inTime is the local time, compute LST
296  KStarsDateTime ksdt( dt, inTime );
297  ksdt = geoBatch->LTtoUT( ksdt );
298  dms lst = geoBatch->GSTtoLST( ksdt.gst() );
299  outTime = QTime( lst.hour(), lst.minute(), lst.second() );
300  } else {
301  //inTime is the sidereal time, compute the local time
302  KStarsDateTime ksdt( dt, QTime(0,0,0) );
303  dms lst;
304  lst.setH( inTime.hour(), inTime.minute(), inTime.second() );
305  QTime ut = ksdt.GSTtoUT( geoBatch->LSTtoGST( lst ) );
306  ksdt.setTime( ut );
307  ksdt = geoBatch->UTtoLT( ksdt );
308  outTime = ksdt.time();
309  }
310 
311  //Write to output file
312  ostream << KGlobal::locale()->formatDate( dt, KLocale::LongDate ) << " \""
313  << geoBatch->fullName() << "\" "
314  << KGlobal::locale()->formatTime( inTime, true ) << " "
315  << KGlobal::locale()->formatTime( outTime, true ) << endl;
316  }
317 
318  fOut.close();
319 }
320 
321 void modCalcSidTime::slotViewBatch() {
322  QFile fOut( OutputFileBatch->url().toLocalFile() );
323  fOut.open(QIODevice::ReadOnly);
324  QTextStream istream(&fOut);
325  QStringList text;
326 
327  while ( ! istream.atEnd() )
328  text.append( istream.readLine() );
329 
330  fOut.close();
331 
332  KMessageBox::informationList( 0, i18n("Results of Sidereal time calculation"), text, OutputFileBatch->url().toLocalFile() );
333 }
334 
335 #include "modcalcsidtime.moc"
dms::hour
int hour() const
Definition: dms.h:104
LocationDialog
Dialog for changing the geographic location of the observer.
Definition: locationdialog.h:57
KStarsData::locationNamed
GeoLocation * locationNamed(const QString &city, const QString &province=QString(), const QString &country=QString())
Definition: kstarsdata.cpp:306
KStarsData
KStarsData is the backbone of KStars.
Definition: kstarsdata.h:66
dms::minute
int minute() const
Definition: dms.cpp:174
KStarsDateTime::GSTtoUT
QTime GSTtoUT(dms GST) const
Convert a given Greenwich Sidereal Time to Universal Time (=Greenwich Mean Time). ...
Definition: kstarsdatetime.cpp:176
QWidget
KStarsData::Instance
static KStarsData * Instance()
Definition: kstarsdata.h:92
modCalcSidTime::modCalcSidTime
modCalcSidTime(QWidget *p)
Definition: modcalcsidtime.cpp:32
GeoLocation::LTtoUT
KStarsDateTime LTtoUT(const KStarsDateTime &lt) const
Definition: geolocation.h:234
KStarsData::geo
GeoLocation * geo()
Definition: kstarsdata.h:164
dms::second
int second() const
Definition: dms.cpp:182
NaN::f
const float f
Definition: nan.h:36
KStarsData::lt
const KStarsDateTime & lt() const
Definition: kstarsdata.h:137
locationdialog.h
GeoLocation
Contains all relevant information for specifying a location on Earth: City Name, State/Province name...
Definition: geolocation.h:39
GeoLocation::GSTtoLST
dms GSTtoLST(const dms &gst) const
Definition: geolocation.h:230
KStarsDateTime
Extension of KDateTime for KStars KStarsDateTime can represent the date/time as a Julian Day...
Definition: kstarsdatetime.h:45
dms
An angle, stored as degrees, but expressible in many ways.
Definition: dms.h:42
simclock.h
modCalcSidTime::~modCalcSidTime
~modCalcSidTime()
Definition: modcalcsidtime.cpp:64
QTextStream
NaN::ld
const long double ld
Definition: nan.h:37
GeoLocation::fullName
QString fullName() const
Definition: geolocation.cpp:56
kstarsdatetime.h
GeoLocation::UTtoLT
KStarsDateTime UTtoLT(const KStarsDateTime &ut) const
Definition: geolocation.h:233
kstarsdata.h
dmsbox.h
GeoLocation::LSTtoGST
dms LSTtoGST(const dms &lst) const
Definition: geolocation.h:231
KStarsDateTime::gst
dms gst() const
Definition: kstarsdatetime.cpp:138
QFrame
modcalcsidtime.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

Skip menu "kstars"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal