• 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
planetviewer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  planetviewer.cpp - Display overhead view of the solar system
3  -------------------
4  begin : Sun May 25 2003
5  copyright : (C) 2003 by Jason Harris
6  email : jharris@30doradus.org
7  ***************************************************************************/
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "planetviewer.h"
18 
19 #include <cstdlib> //needed for abs() on some platforms
20 
21 #include <QFile>
22 #include <QMouseEvent>
23 #include <QVBoxLayout>
24 #include <QTextStream>
25 #include <QKeyEvent>
26 #include <QPaintEvent>
27 
28 #include <kdebug.h>
29 #include <klocale.h>
30 #include <kglobal.h>
31 #include <kicon.h>
32 #include <KPlotAxis>
33 #include <KPlotObject>
34 #include <KPlotPoint>
35 
36 #include "ui_planetviewer.h"
37 #include "kstarsdata.h"
38 #include "ksutils.h"
39 #include "ksnumbers.h"
40 #include "ksfilereader.h"
41 #include "skyobjects/ksplanetbase.h"
42 #include "skyobjects/ksplanet.h"
43 #include "skyobjects/kspluto.h"
44 #include "dms.h"
45 #include "widgets/timestepbox.h"
46 
47 PlanetViewerUI::PlanetViewerUI( QWidget *p ) : QFrame( p ) {
48  setupUi( this );
49 }
50 
51 PlanetViewer::PlanetViewer(QWidget *parent)
52  : KDialog( parent ), scale(1.0), isClockRunning(false), tmr(this)
53 {
54  KStarsData *data = KStarsData::Instance();
55  pw = new PlanetViewerUI( this );
56  setMainWidget( pw );
57  setCaption( i18n("Solar System Viewer") );
58  setButtons( KDialog::Close );
59  setModal( false );
60 
61  pw->map->setLimits( -48.0, 48.0, -48.0, 48.0 );
62  pw->map->axis( KPlotWidget::BottomAxis )->setLabel( i18nc( "axis label for x-coordinate of solar system viewer. AU means astronomical unit.", "X-position (AU)" ) );
63  pw->map->axis( KPlotWidget::LeftAxis )->setLabel( i18nc( "axis label for y-coordinate of solar system viewer. AU means astronomical unit.", "Y-position (AU)" ) );
64 
65  pw->TimeStep->setDaysOnly( true );
66  pw->TimeStep->tsbox()->setValue( 1 ); //start with 1-day timestep
67 
68  pw->RunButton->setIcon( KIcon("arrow-right") );
69  pw->ZoomInButton->setIcon( KIcon("zoom-in") );
70  pw->ZoomOutButton->setIcon( KIcon("zoom-out") );
71  pw->DateBox->setDate( data->lt().date() );
72 
73  resize( 500, 500 );
74  pw->map->QWidget::setFocus(); //give keyboard focus to the plot widget for key and mouse events
75 
76  setCenterPlanet(QString());
77 
78  PlanetList.append( KSPlanetBase::createPlanet( KSPlanetBase::MERCURY ) );
79  PlanetList.append( KSPlanetBase::createPlanet( KSPlanetBase::VENUS ) );
80  PlanetList.append( new KSPlanet( "Earth" ) );
81  PlanetList.append( KSPlanetBase::createPlanet( KSPlanetBase::MARS ) );
82  PlanetList.append( KSPlanetBase::createPlanet( KSPlanetBase::JUPITER ) );
83  PlanetList.append( KSPlanetBase::createPlanet( KSPlanetBase::SATURN ) );
84  PlanetList.append( KSPlanetBase::createPlanet( KSPlanetBase::URANUS ) );
85  PlanetList.append( KSPlanetBase::createPlanet( KSPlanetBase::NEPTUNE ) );
86  PlanetList.append( KSPlanetBase::createPlanet( KSPlanetBase::PLUTO ) );
87 
88  ut = data->ut();
89  KSNumbers num( ut.djd() );
90 
91  for ( uint i=0; i<9; ++i ) {
92  PlanetList[i]->findPosition( &num, 0, 0 ); //NULL args: don't need geocent. coords.
93  LastUpdate[i] = int( ut.date().toJulianDay() );
94  }
95 
96  //The planets' update intervals are 0.25% of one period:
97  UpdateInterval[0] = 0;
98  UpdateInterval[1] = 0;
99  UpdateInterval[2] = 0;
100  UpdateInterval[3] = 1;
101  UpdateInterval[4] = 5;
102  UpdateInterval[5] = 13;
103  UpdateInterval[6] = 38;
104  UpdateInterval[7] = 75;
105  UpdateInterval[8] = 113;
106 
107  QTimer::singleShot( 0, this, SLOT( initPlotObjects() ) );
108 
109  connect( &tmr, SIGNAL( timeout() ), SLOT( tick() ) );
110  connect( pw->TimeStep, SIGNAL( scaleChanged(float) ), SLOT( setTimeScale(float) ) );
111  connect( pw->RunButton, SIGNAL( clicked() ), SLOT( slotRunClock() ) );
112  connect( pw->ZoomInButton, SIGNAL( clicked() ), pw->map, SLOT( slotZoomIn() ) );
113  connect( pw->ZoomOutButton, SIGNAL( clicked() ), pw->map, SLOT( slotZoomOut() ) );
114  connect( pw->DateBox, SIGNAL( dateChanged(const QDate&) ), SLOT( slotChangeDate() ) );
115  connect( pw->TodayButton, SIGNAL( clicked() ), SLOT( slotToday() ) );
116  connect( this, SIGNAL( closeClicked() ), SLOT( slotCloseWindow() ) );
117 }
118 
119 PlanetViewer::~PlanetViewer()
120 {
121 }
122 
123 QString PlanetViewer::planetName(uint i) const {
124  return PlanetList[i]->name();
125 }
126 
127 void PlanetViewer::tick() {
128  //Update the time/date
129  ut.setDJD( ut.djd() + scale*0.1 );
130  pw->DateBox->setDate( ut.date() );
131 
132  updatePlanets();
133 }
134 
135 void PlanetViewer::setTimeScale(float f) {
136  scale = f/86400.; //convert seconds to days
137 }
138 
139 void PlanetViewer::slotRunClock() {
140  isClockRunning = !isClockRunning;
141 
142  if ( isClockRunning ) {
143  pw->RunButton->setIcon( KIcon("media-playback-pause") );
144  tmr.start( 100 );
145  // pw->DateBox->setEnabled( false );
146  } else {
147  pw->RunButton->setIcon( KIcon("arrow-right") );
148  tmr.stop();
149  // pw->DateBox->setEnabled( true );
150  }
151 }
152 
153 void PlanetViewer::slotChangeDate() {
154  ut.setDate( pw->DateBox->date() );
155  updatePlanets();
156 }
157 
158 void PlanetViewer::slotCloseWindow() {
159  //Stop the clock if it's running
160  if ( isClockRunning ) {
161  tmr.stop();
162  isClockRunning = false;
163  }
164 }
165 
166 void PlanetViewer::updatePlanets() {
167  KSNumbers num( ut.djd() );
168  bool changed(false);
169 
170  //Check each planet to see if it needs to be updated
171  for ( unsigned int i=0; i<9; ++i ) {
172  if ( abs( int(ut.date().toJulianDay()) - LastUpdate[i] ) > UpdateInterval[i] ) {
173  KSPlanetBase *p = PlanetList[i];
174  p->findPosition( &num );
175 
176  double s, c, s2, c2;
177  p->helEcLong().SinCos( s, c );
178  p->helEcLat().SinCos( s2, c2 );
179  QList<KPlotPoint*> points = planet[i]->points();
180  points.at(0)->setX( p->rsun()*c*c2 );
181  points.at(0)->setY( p->rsun()*s*c2 );
182 
183  if ( centerPlanet() == p->name() ) {
184  QRectF dataRect = pw->map->dataRect();
185  double xc = (dataRect.right() + dataRect.left())*0.5;
186  double yc = (dataRect.bottom() + dataRect.top())*0.5;
187  double dx = points.at(0)->x() - xc;
188  double dy = points.at(0)->y() - yc;
189  pw->map->setLimits( dataRect.x() + dx, dataRect.right() + dx,
190  dataRect.y() + dy, dataRect.bottom() + dy );
191  }
192 
193  LastUpdate[i] = int(ut.date().toJulianDay());
194  changed = true;
195  }
196  }
197 
198  if ( changed ) pw->map->update();
199 }
200 
201 void PlanetViewer::slotToday() {
202  pw->DateBox->setDate( KStarsData::Instance()->lt().date() );
203 }
204 
205 void PlanetViewer::paintEvent( QPaintEvent* ) {
206  pw->map->update();
207 }
208 
209 void PlanetViewer::initPlotObjects() {
210  // Planets
211  ksun = new KPlotObject( Qt::yellow, KPlotObject::Points, 12, KPlotObject::Circle );
212  ksun->addPoint( 0.0, 0.0 );
213  pw->map->addPlotObject( ksun );
214 
215  //Read in the orbit curves
216  KPlotObject *orbit[9];
217  for ( unsigned int i=0; i<9; ++i ) {
218  KSPlanetBase *p = PlanetList[i];
219  orbit[i] = new KPlotObject( Qt::white, KPlotObject::Lines, 1.0 );
220 
221  QFile orbitFile;
222  QString orbitFileName = ( p->isMajorPlanet() ? ((KSPlanet *)p)->untranslatedName().toLower() : p->name().toLower() ) + ".orbit";
223  if ( KSUtils::openDataFile( orbitFile, orbitFileName ) ) {
224  KSFileReader fileReader( orbitFile ); // close file is included
225  double x,y;
226  while ( fileReader.hasMoreLines() ) {
227  QString line = fileReader.readLine();
228  QStringList fields = line.split( ' ', QString::SkipEmptyParts );
229  if ( fields.size() == 3 ) {
230  x = fields[0].toDouble();
231  y = fields[1].toDouble();
232  orbit[i]->addPoint( x, y );
233  }
234  }
235  }
236 
237  pw->map->addPlotObject( orbit[i] );
238  }
239 
240  for ( unsigned int i=0; i<9; ++i ) {
241  KSPlanetBase *p = PlanetList[i];
242  planet[i] = new KPlotObject( p->color(), KPlotObject::Points, 6, KPlotObject::Circle );
243 
244  double s, c;
245  p->helEcLong().SinCos( s, c );
246 
247  planet[i]->addPoint( p->rsun()*c, p->rsun()*s, p->translatedName() );
248  pw->map->addPlotObject( planet[i] );
249  }
250 
251  update();
252 }
253 
254 void PlanetViewer::keyPressEvent( QKeyEvent *e ) {
255  if( e->key() == Qt::Key_Escape )
256  close();
257  else
258  e->ignore();
259 }
260 
261 #include "planetviewer.moc"
PlanetViewerUI
Definition: planetviewer.h:37
KSPlanetBase::findPosition
void findPosition(const KSNumbers *num, const dms *lat=0, const dms *LST=0, const KSPlanetBase *Earth=0)
Find position, including correction for Figure-of-the-Earth.
Definition: ksplanetbase.cpp:122
ksplanetbase.h
KStarsData
KStarsData is the backbone of KStars.
Definition: kstarsdata.h:66
KSPlanetBase::NEPTUNE
Definition: ksplanetbase.h:82
KSPlanet
A subclass of KSPlanetBase for seven of the major planets in the solar system (Earth and Pluto have t...
Definition: ksplanet.h:40
SkyObject::translatedName
QString translatedName() const
Definition: skyobject.h:129
KSPlanetBase::JUPITER
Definition: ksplanetbase.h:82
QWidget
KSPlanetBase::helEcLat
const dms & helEcLat() const
Definition: ksplanetbase.h:110
KStarsData::Instance
static KStarsData * Instance()
Definition: kstarsdata.h:92
KDialog
KSPlanetBase::MERCURY
Definition: ksplanetbase.h:82
PlanetViewer::planetName
QString planetName(uint i) const
Definition: planetviewer.cpp:123
KSUtils::openDataFile
bool openDataFile(QFile &file, const QString &filename)
Attempt to open the data file named filename, using the QFile object "file".
dms.h
PlanetViewer::setCenterPlanet
void setCenterPlanet(const QString &cp)
Definition: planetviewer.h:56
ksplanet.h
NaN::f
const float f
Definition: nan.h:36
KSPlanetBase::createPlanet
static KSPlanetBase * createPlanet(int n)
Definition: ksplanetbase.cpp:73
planetviewer.h
kspluto.h
timestepbox.h
KStarsData::lt
const KStarsDateTime & lt() const
Definition: kstarsdata.h:137
KStarsDateTime::djd
long double djd() const
Definition: kstarsdatetime.h:145
KSPlanetBase::MARS
Definition: ksplanetbase.h:82
KSPlanetBase::rsun
double rsun() const
Definition: ksplanetbase.h:126
i18nc
i18nc("string from libindi, used in the config dialog","100x")
KSPlanetBase::color
QColor & color()
Definition: ksplanetbase.h:190
ksnumbers.h
KSPlanetBase::isMajorPlanet
bool isMajorPlanet() const
Definition: ksplanetbase.cpp:151
PlanetViewer::~PlanetViewer
~PlanetViewer()
Definition: planetviewer.cpp:119
PlanetViewer::PlanetViewer
PlanetViewer(QWidget *parent=0)
Definition: planetviewer.cpp:51
KSPlanetBase::SATURN
Definition: ksplanetbase.h:82
KSPlanetBase::helEcLong
const dms & helEcLong() const
Definition: ksplanetbase.h:107
KSPlanetBase::PLUTO
Definition: ksplanetbase.h:82
KSNumbers
There are several time-dependent values used in position calculations, that are not specific to an ob...
Definition: ksnumbers.h:43
PlanetViewer::paintEvent
virtual void paintEvent(QPaintEvent *)
Definition: planetviewer.cpp:205
PlanetViewerUI::PlanetViewerUI
PlanetViewerUI(QWidget *parent=0)
Definition: planetviewer.cpp:47
ksfilereader.h
KSFileReader
Definition: ksfilereader.h:65
KSPlanetBase
A subclass of TrailObject that provides additional information needed for most solar system objects...
Definition: ksplanetbase.h:63
kstarsdata.h
KStarsData::ut
const KStarsDateTime & ut() const
Definition: kstarsdata.h:140
SkyObject::name
virtual QString name(void) const
Definition: skyobject.h:124
KSPlanetBase::VENUS
Definition: ksplanetbase.h:82
ksutils.h
QFrame
KSPlanetBase::URANUS
Definition: ksplanetbase.h:82
PlanetViewer::keyPressEvent
virtual void keyPressEvent(QKeyEvent *e)
Definition: planetviewer.cpp:254
QList
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