[Kstars-devel] [kstars] kstars: Fix moon phases in the Moon Phase Tool

Akarsh Simha akarsh.simha at kdemail.net
Sat Jan 26 03:19:33 UTC 2013


Git commit fc6a1af310faf97185caf735b30a188e8bba880b by Akarsh Simha.
Committed on 26/01/2013 at 02:38.
Pushed by asimha into branch 'master'.

Fix moon phases in the Moon Phase Tool

Moon phases were shown wrongly in the Moon Phase Calendary Tool.

This was because of using a KSSun that was not updated for the
date/time used by the calendar.

Therefore,
1. Made KSMoon::findPhase() accept a custom KSSun pointer
2. Create our own KSSun in Moon Phase Tool and hand it over to the MPC
   Widget.
3. MPC Widget uses that KSSun. The main KSSun in the SkyMap is
   unaffected.

BUG
CCMAIL: kstars-devel at kde.org

M  +6    -3    kstars/skyobjects/ksmoon.cpp
M  +2    -1    kstars/skyobjects/ksmoon.h
M  +4    -1    kstars/tools/moonphasetool.cpp
M  +2    -0    kstars/tools/moonphasetool.h
M  +6    -3    kstars/widgets/moonphasecalendarwidget.cpp
M  +10   -2    kstars/widgets/moonphasecalendarwidget.h

http://commits.kde.org/kstars/fc6a1af310faf97185caf735b30a188e8bba880b

diff --git a/kstars/skyobjects/ksmoon.cpp b/kstars/skyobjects/ksmoon.cpp
index 8e387fb..1372188 100644
--- a/kstars/skyobjects/ksmoon.cpp
+++ b/kstars/skyobjects/ksmoon.cpp
@@ -235,9 +235,12 @@ void KSMoon::findMagnitude(const KSNumbers*)
     setMag( MagArray[i] + (MagArray[j] - MagArray[i]) * k / 10 );
 }
 
-void KSMoon::findPhase() {
-    KSSun *Sun = (KSSun*)KStarsData::Instance()->skyComposite()->findByName( "Sun" );
-    Phase = (ecLong()- Sun->ecLong()).Degrees(); // Phase is obviously in degrees
+void KSMoon::findPhase( const KSSun *Sun ) {
+
+    if( !Sun )
+      Sun = ( const KSSun* ) KStarsData::Instance()->skyComposite()->findByName( "Sun" );
+
+    Phase = (ecLong() - Sun->ecLong()).Degrees(); // Phase is obviously in degrees
     double DegPhase = dms( Phase ).reduce().Degrees();
     iPhase = int( 0.1*DegPhase+0.5 ) % 36; // iPhase must be in [0,36) range
 
diff --git a/kstars/skyobjects/ksmoon.h b/kstars/skyobjects/ksmoon.h
index 434f730..559d203 100644
--- a/kstars/skyobjects/ksmoon.h
+++ b/kstars/skyobjects/ksmoon.h
@@ -49,9 +49,10 @@ public:
     /**
      *Determine the phase angle of the moon, and assign the appropriate
      *moon image
+     * @param Sun a KSSun pointer with coordinates updated to the time of computation. If not supplied, the findByName() method will be used to find the sun.
      *@note Overrides KSPlanetBase::findPhase()
      */
-    virtual void findPhase();
+    virtual void findPhase( const KSSun *Sun = 0 );
 
     /**@return the illuminated fraction of the Moon as seen from Earth */
     double illum() const { return 0.5*(1.0 - cos( Phase * dms::PI / 180.0 ) ); }
diff --git a/kstars/tools/moonphasetool.cpp b/kstars/tools/moonphasetool.cpp
index bf7b1ff..f117d28 100644
--- a/kstars/tools/moonphasetool.cpp
+++ b/kstars/tools/moonphasetool.cpp
@@ -17,6 +17,7 @@
 
 #include "moonphasetool.h"
 #include "skyobjects/ksmoon.h"
+#include "skyobjects/kssun.h"
 
 MoonPhaseTool::MoonPhaseTool(QWidget *parent)
     : KDialog( parent, Qt::Dialog )
@@ -24,7 +25,8 @@ MoonPhaseTool::MoonPhaseTool(QWidget *parent)
     setButtons( 0 );
     KStarsDateTime dtStart ( KStarsDateTime::currentDateTime() );
     m_Moon = new KSMoon;
-    mpc = new MoonPhaseCalendar( *m_Moon );
+    m_Sun = new KSSun;
+    mpc = new MoonPhaseCalendar( *m_Moon, *m_Sun );
     gcw = new GenericCalendarWidget( *mpc, this );
     setFixedSize( gcw->size() );
     setCaption( i18n("Moon Phase Calendar") );
@@ -33,5 +35,6 @@ MoonPhaseTool::MoonPhaseTool(QWidget *parent)
 
 MoonPhaseTool::~MoonPhaseTool() {
     delete m_Moon;
+    delete m_Sun;
     delete mpc;
 }
diff --git a/kstars/tools/moonphasetool.h b/kstars/tools/moonphasetool.h
index 40a05b2..a87fc86 100644
--- a/kstars/tools/moonphasetool.h
+++ b/kstars/tools/moonphasetool.h
@@ -26,6 +26,7 @@
 #include <KDialog>
 
 class KSMoon;
+class KSSun;
 
 /**
  *@class MoonPhaseTool
@@ -59,6 +60,7 @@ public slots:
 
 private:
     KSMoon *m_Moon;
+    KSSun *m_Sun;
     KSNumbers *m_Num;
     unsigned short month;
     unsigned int year;
diff --git a/kstars/widgets/moonphasecalendarwidget.cpp b/kstars/widgets/moonphasecalendarwidget.cpp
index 3f658ae..01a6687 100644
--- a/kstars/widgets/moonphasecalendarwidget.cpp
+++ b/kstars/widgets/moonphasecalendarwidget.cpp
@@ -18,6 +18,7 @@
 #include "moonphasecalendarwidget.h"
 
 #include "skyobjects/ksmoon.h"
+#include "skyobjects/kssun.h"
 #include "skyobjects/ksplanet.h"
 #include "ksnumbers.h"
 #include "kstarsdatetime.h"
@@ -37,9 +38,9 @@
 
 #include <cmath>
 
-MoonPhaseCalendar::MoonPhaseCalendar( KSMoon &moon, QWidget *parent ) :
+MoonPhaseCalendar::MoonPhaseCalendar( KSMoon &moon, KSSun &sun, QWidget *parent ) :
     KDateTable(parent),
-    m_Moon(moon)
+    m_Moon(moon), m_Sun(sun)
 {
     // Populate moon images from disk into the hash
     numDayColumns = calendar()->daysInWeek( QDate::currentDate() );
@@ -296,8 +297,10 @@ unsigned short MoonPhaseCalendar::computeMoonPhase( const KStarsDateTime &date )
     KSPlanet earth( I18N_NOOP( "Earth" ), QString(), QColor( "white" ), 12756.28 /*diameter in km*/ );
     earth.findPosition( &num );
 
+    m_Sun.findPosition( &num, 0, 0, &earth ); // Find position is overkill for this purpose. Wonder if it is worth making findGeocentricPosition public instead of protected.
     m_Moon.findGeocentricPosition( &num, &earth );
-    m_Moon.findPhase();
+
+    m_Moon.findPhase( &m_Sun );
 
     return m_Moon.getIPhase();
 
diff --git a/kstars/widgets/moonphasecalendarwidget.h b/kstars/widgets/moonphasecalendarwidget.h
index 7eb899c..e31ba80 100644
--- a/kstars/widgets/moonphasecalendarwidget.h
+++ b/kstars/widgets/moonphasecalendarwidget.h
@@ -22,6 +22,7 @@
 #include <KDateTable>
 
 class KSMoon;
+class KSSun;
 class KStarsDateTime;
 
 class MoonPhaseCalendar : public KDateTable {
@@ -29,8 +30,14 @@ class MoonPhaseCalendar : public KDateTable {
     Q_OBJECT
 
  public:
-    
-    explicit MoonPhaseCalendar( KSMoon &moon, QWidget *parent = 0 );
+
+    /**
+     * Constructor
+     * @param moon A reference to a (non-const) KSMoon object, that will be updated
+     * @param sun A reference to a (non-const) KSSun object, that will be updated
+     */
+    explicit MoonPhaseCalendar( KSMoon &moon, KSSun &sun, QWidget *parent = 0 );
+
     ~MoonPhaseCalendar();
 
     /**
@@ -100,6 +107,7 @@ class MoonPhaseCalendar : public KDateTable {
     bool imagesLoaded;
 
     KSMoon &m_Moon;
+    KSSun &m_Sun;
 };
 
 #endif



More information about the Kstars-devel mailing list