[Kstars-devel] branches/kstars/unfrozen/kstars/kstars [POSSIBLY UNSAFE]

Jason Harris kstars at 30doradus.org
Mon Jun 30 16:44:18 CEST 2008


SVN commit 826351 by harris:

Better fix for bug #165170.  Now the order of items in the planet comboboxes is 
hard-coded in conjunctions.cpp, using a new PLANET enum I added to KSPlanetBase.  
So it's not possible for the order to be wrong.

I also added a convenience function 'static KSPlanetBase* 
KSPlanetBase::createPlanet(int)', so you can simply call: 
KSPlanetBase::createPlanet(VENUS), rather than using the KSPlanet ctor, and using 
the planet's name to determine which planet gets created.  There is also a new 
KSPlanet(int) ctor that does essentially the same thing.

Note: this commit won't be merged into trunk until after 4.1 is released.

CCMAIL: kstars-devel at kde.org
CCBUG: 165170



 M  +31 -1     ksplanet.cpp  
 M  +33 -26    ksplanet.h   [POSSIBLY UNSAFE: system]
 M  +40 -6     ksplanetbase.cpp  
 M  +11 -6     ksplanetbase.h  
 M  +21 -19    tools/conjunctions.cpp  
 M  +2 -7      tools/conjunctions.h  
 M  +0 -100    tools/conjunctions.ui  


--- branches/kstars/unfrozen/kstars/kstars/ksplanet.cpp #826350:826351
@@ -114,9 +114,39 @@
 
 KSPlanet::KSPlanet( KStarsData *kd, const QString &s,
                     const QString &imfile, const QColor & c, double pSize )
-        : KSPlanetBase(kd, s, imfile, c, pSize ), data_loaded(false) {
+    : KSPlanetBase(kd, s, imfile, c, pSize ), data_loaded(false) {
 }
 
+KSPlanet::KSPlanet( KStarsData *kd, int n ) 
+    : KSPlanetBase(kd) {
+    switch ( n ) {
+        case MERCURY:
+            KSPlanetBase::init( i18n("Mercury"), "mercury.png", QColor( "slateblue" ), 4879.4 );
+            break;
+        case VENUS:
+            KSPlanetBase::init( i18n("Venus"), "venus.png", QColor( "lightgreen" ), 12103.6 );
+            break;
+        case MARS:
+            KSPlanetBase::init( i18n("Mars"), "mars.png", QColor( "red" ), 6792.4 );
+            break;
+        case JUPITER:
+            KSPlanetBase::init( i18n("Jupiter"), "jupiter.png", QColor( "goldenrod" ), 142984. );
+            break;
+        case SATURN:
+            KSPlanetBase::init( i18n("Saturn"), "saturn.png", QColor( "khaki" ), 120536. );
+            break;
+        case URANUS:
+            KSPlanetBase::init( i18n("Uranus"), "uranus.png", QColor( "lightseagreen" ), 51118. );
+            break;
+        case NEPTUNE:
+            KSPlanetBase::init( i18n("Neptune"), "neptune.png", QColor( "skyblue" ), 49572. );
+            break;
+        default:
+            kDebug() << i18n("Error: Illegal identifier in KSPlanet constructor: %1").arg(n) << endl;
+            break;
+    }
+}
+
 //we don't need the reference to the ODC, so just give it a junk variable
 bool KSPlanet::loadData() {
     OrbitDataColl odc;
--- branches/kstars/unfrozen/kstars/kstars/ksplanet.h #826350:826351
@@ -15,6 +15,8 @@
  *                                                                         *
  ***************************************************************************/
 
+#ifndef KSPLANET_H_
+#define KSPLANET_H_
 
 #include <QVector>
 #include <QHash>
@@ -22,40 +24,45 @@
 #include "ksplanetbase.h"
 #include "dms.h"
 
-#ifndef KSPLANET_H_
-#define KSPLANET_H_
+class KStarsData;
 
 /**@class KSPlanet
-	*A subclass of KSPlanetBase for seven of the major planets in the solar system
-	*(Earth and Pluto have their own specialized classes derived from KSPlanetBase).  
-	*@note The Sun is subclassed from KSPlanet.
-	*
-	*KSPlanet contains internal classes to manage the computations of a planet's position.
-	*The position is computed as a series of sinusoidal sums, similar to a Fourier
-	*transform.  See "Astronomical Algorithms" by Jean Meeus or the file README.planetmath
-	*for details.
-	*@short Provides necessary information about objects in the solar system.
-	*@author Jason Harris
-	*@version 1.0
-	*/
-
-class KStarsData;
-
+ *A subclass of KSPlanetBase for seven of the major planets in the solar system
+ *(Earth and Pluto have their own specialized classes derived from KSPlanetBase).  
+ *@note The Sun is subclassed from KSPlanet.
+ *
+ *KSPlanet contains internal classes to manage the computations of a planet's position.
+ *The position is computed as a series of sinusoidal sums, similar to a Fourier
+ *transform.  See "Astronomical Algorithms" by Jean Meeus or the file README.planetmath
+ *for details.
+ *@short Provides necessary information about objects in the solar system.
+ *@author Jason Harris
+ *@version 1.0
+ */
 class KSPlanet : public KSPlanetBase {
 public:
 
-    /**Constructor.
-    	*@param kd Some kind of data
-    	*@param s Name of planet
-    	*@param image_file filename of the planet's image
-    	*@param c the color for the planet
-    	*@param pSize physical diameter of the planet, in km
-    	*/
+    /**
+     * Constructor.
+     * @param kd Pointer to the KStarsData object
+     * @param s Name of planet
+     * @param image_file filename of the planet's image
+     * @param c the color for the planet
+     * @param pSize physical diameter of the planet, in km
+     */
     explicit KSPlanet( KStarsData *kd, const QString &s="unnamed", const QString &image_file=QString(),
                        const QColor & c=Qt::white, double pSize=0 );
 
-    /**Destructor (empty)
-    	*/
+    /**
+     * Simplified constructor
+     * @param n identifier of the planet to be created
+     * @see PLANET enum
+     */
+    KSPlanet( KStarsData *kd, int n );
+
+    /**
+     * Destructor (empty)
+     */
     virtual ~KSPlanet() {}
 
     /**@short Preload the data used by findPosition.
--- branches/kstars/unfrozen/kstars/kstars/ksplanetbase.cpp #826350:826351
@@ -23,19 +23,23 @@
 #include <QPoint>
 #include <QMatrix>
 
-#include "ksplanet.h"
 #include "kstarsdata.h"
 #include "ksutils.h"
 #include "ksnumbers.h"
 #include "Options.h"
 #include "skymap.h"
 #include "ksasteroid.h"
+#include "kspluto.h"
+#include "ksplanet.h"
+#include "kssun.h"
+#include "ksmoon.h"
 
-KSPlanetBase::KSPlanetBase( KStarsData *kd, const QString &s, 		const QString &image_file, const QColor &c, double pSize )
-        : TrailObject( 2, 0.0, 0.0, 0.0, s ),
-        Rearth(0.0), Image(), data(kd),
-PhysicalSize(pSize), m_Color( c ) {
+KSPlanetBase::KSPlanetBase( KStarsData *kd, const QString &s, const QString &image_file, const QColor &c, double pSize )
+    : TrailObject( 2, 0.0, 0.0, 0.0, s ), Rearth(0.0), Image(), data(kd) {
+    init( s, image_file, c, pSize );
+}
 
+void KSPlanetBase::init( const QString &s, const QString &image_file, const QColor &c, double pSize ) {
     if (! image_file.isEmpty()) {
         QFile imFile;
 
@@ -48,8 +52,39 @@
     }
     PositionAngle = 0.0;
     ImageAngle = 0.0;
+    PhysicalSize = pSize;
+    m_Color = c;
+    setName( s );
 }
 
+KSPlanetBase* KSPlanetBase::createPlanet( int n ) {
+    KStarsData *kd = KStarsData::Instance();
+
+    switch ( n ) {
+        case MERCURY:
+        case VENUS:
+        case MARS:
+        case JUPITER:
+        case SATURN:
+        case URANUS:
+        case NEPTUNE:
+            return new KSPlanet( kd, n );
+            break;
+
+        case PLUTO:
+            return new KSPluto(kd);
+            break;
+        case SUN:
+            return new KSSun(kd);
+            break;
+        case MOON:
+            return new KSMoon(kd);
+            break;
+    }
+
+    return 0;
+}
+
 void KSPlanetBase::EquatorialToEcliptic( const dms *Obliquity ) {
     findEcliptic( Obliquity, ep.longitude, ep.latitude );
 }
@@ -232,7 +267,6 @@
 }
 
 void KSPlanetBase::findMagnitude(const KSNumbers *num) {
-
     double cosDec, sinDec;
     dec()->SinCos(cosDec, sinDec);
 
--- branches/kstars/unfrozen/kstars/kstars/ksplanetbase.h #826350:826351
@@ -28,6 +28,8 @@
 
 #include "trailobject.h"
 
+typedef enum { MERCURY, VENUS, MARS, JUPITER, SATURN, URANUS, NEPTUNE, PLUTO, SUN, MOON, UNKNOWN_PLANET } PLANET;
+
 class QPoint;
 class KSNumbers;
 class KSPopupMenu;
@@ -75,23 +77,26 @@
     /**
       *Constructor.  Calls SkyObject constructor with type=2 (planet),
       *coordinates=0.0, mag=0.0, primary name s, and all other QStrings empty.
-      *@param kd Some kind of data
+      *@param kd pointer to the KStarsData object
       *@param s Name of planet
       *@param image_file filename of the planet's image
       *@param c color of the symbol to use for this planet
       *@param pSize the planet's physical size, in km
-      *@param kd pointer to the KStarsData object.
       */
-    explicit KSPlanetBase( KStarsData *kd,
+    explicit KSPlanetBase( KStarsData *kd, 
                            const QString &s = i18n("unnamed"),
                            const QString &image_file=QString(),
                            const QColor &c=Qt::white, double pSize=0 );
 
-    /**
-    *Destructor (empty)
-    */
+   /**
+     *Destructor (empty)
+     */
     virtual ~KSPlanetBase() {}
 
+    void init(const QString &s, const QString &image_file, const QColor &c, double pSize );
+
+    static KSPlanetBase* createPlanet( int n );
+
     virtual bool loadData() {
         kDebug() << "no loadData() implementation for " << name() << endl; 
         return false;
--- branches/kstars/unfrozen/kstars/kstars/tools/conjunctions.cpp #826350:826351
@@ -28,6 +28,7 @@
 #include <kglobal.h>
 #include <kmessagebox.h>
 
+#include "ksconjunct.h"
 #include "geolocation.h"
 #include "locationdialog.h"
 #include "dms.h"
@@ -36,6 +37,7 @@
 #include "ksnumbers.h"
 #include "kssun.h"
 #include "ksplanet.h"
+#include "ksplanetbase.h"
 #include "ksmoon.h"
 #include "kspluto.h"
 #include "widgets/dmsbox.h"
@@ -64,6 +66,23 @@
   geoPlace = kd -> geo();
   LocationButton -> setText( geoPlace -> fullName() );
   
+  QHash<int, QString> pNames;
+  pNames[MERCURY] = i18n("Mercury");
+  pNames[VENUS] = i18n("Venus");
+  pNames[MARS] = i18n("Mars");
+  pNames[JUPITER] = i18n("Jupiter");
+  pNames[SATURN] = i18n("Saturn");
+  pNames[URANUS] = i18n("Uranus");
+  pNames[NEPTUNE] = i18n("Neptune");
+  pNames[PLUTO] = i18n("Pluto");
+  pNames[SUN] = i18n("Sun");
+  pNames[MOON] = i18n("Moon");
+
+  for ( int i=0; i<UNKNOWN_PLANET; ++i ) {
+      Obj1ComboBox->insertItem( i, pNames[i] );
+      Obj2ComboBox->insertItem( i, pNames[i] );
+  }
+
   // signals and slots connections
   connect(LocationButton, SIGNAL(clicked()), this, SLOT(slotLocation()));
   connect(ComputeButton, SIGNAL(clicked()), this, SLOT(slotCompute()));
@@ -89,7 +108,6 @@
 
   KStarsDateTime dtStart = startDate -> dateTime();
   KStarsDateTime dtStop = stopDate -> dateTime();
-  KStarsData *kd = KStarsData::Instance();
   long double startJD = dtStart.djd();
   long double stopJD = dtStop.djd();
   dms maxSeparation(1.0); // TODO: Make maxSeparation user-specifiable
@@ -97,24 +115,9 @@
   //    dms LST( geoPlace->GSTtoLST( dt.gst() ) );
   KSPlanetBase *Object1, *Object2;
   
-  if(Obj1ComboBox -> currentIndex() <= 6)
-    Object1 = (KSPlanetBase *)(new KSPlanet(kd, I18N_NOOP(Obj1ComboBox -> currentText())));
-  else if(Obj1ComboBox -> currentIndex() == 7)
-    Object1 = (KSPlanetBase *)(new KSPluto(kd));
-  else if(Obj1ComboBox -> currentIndex() == 8)
-    Object1 = (KSPlanetBase *)(new KSMoon(kd));
-  else if(Obj1ComboBox -> currentIndex() == 9)
-    Object1 = (KSPlanetBase *)(new KSSun(kd));
+  Object1 = KSPlanetBase::createPlanet( Obj1ComboBox->currentIndex() );
+  Object2 = KSPlanetBase::createPlanet( Obj2ComboBox->currentIndex() );
 
-  if(Obj2ComboBox -> currentIndex() <= 6)
-    Object2 = (KSPlanetBase *)(new KSPlanet(kd, I18N_NOOP(Obj2ComboBox -> currentText())));
-  else if(Obj2ComboBox -> currentIndex() == 7)
-    Object2 = (KSPlanetBase *)(new KSPluto(kd));
-  else if(Obj2ComboBox -> currentIndex() == 8)
-    Object2 = (KSPlanetBase *)(new KSMoon(kd));
-  else if(Obj2ComboBox -> currentIndex() == 9)
-    Object2 = (KSPlanetBase *)(new KSSun(kd));
-
   KSConjunct ksc;
   showConjunctions(ksc.findClosestApproach(*Object1, *Object2, startJD, stopJD, maxSeparation));
 
@@ -123,7 +126,6 @@
 
 }
 
-
 void ConjunctionsTool::showConjunctions(QMap<long double, dms> conjunctionlist) {
 
   KStarsDateTime dt;
--- branches/kstars/unfrozen/kstars/kstars/tools/conjunctions.h #826350:826351
@@ -28,14 +28,10 @@
 #include <QTextStream>
 
 #include "ui_conjunctions.h"
-#include "ksconjunct.h"
 
 class GeoLocation;
-class KSPlanet;
-class KSMoon;
-class KSSun;
-class KSPluto;
-class KSConjunct;
+class KSPlanetBase;
+class dms;
 
 /**
   *@short Predicts conjunctions using KSConjunct in the background
@@ -55,7 +51,6 @@
     void slotCompute();
 
 private:
-
     void showConjunctions(QMap<long double, dms> conjunctionlist);
 
     GeoLocation *geoPlace;
--- branches/kstars/unfrozen/kstars/kstars/tools/conjunctions.ui #826350:826351
@@ -55,56 +55,6 @@
            <verstretch>0</verstretch>
           </sizepolicy>
          </property>
-         <item>
-          <property name="text" >
-           <string>Mercury</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Venus</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Mars</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Jupiter</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Saturn</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Uranus</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Neptune</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Pluto</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Moon</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Sun</string>
-          </property>
-         </item>
         </widget>
        </item>
        <item>
@@ -125,56 +75,6 @@
            <verstretch>0</verstretch>
           </sizepolicy>
          </property>
-         <item>
-          <property name="text" >
-           <string>Mercury</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Venus</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Mars</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Jupiter</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Saturn</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Uranus</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Neptune</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Pluto</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Moon</string>
-          </property>
-         </item>
-         <item>
-          <property name="text" >
-           <string>Sun</string>
-          </property>
-         </item>
         </widget>
        </item>
       </layout>


More information about the Kstars-devel mailing list