[Kstars-devel] KDE/kdeedu/kstars/kstars/skycomponents

Alexey Khudyakov alexey.skladnoy at gmail.com
Thu Nov 5 00:14:08 CET 2009


SVN commit 1044935 by khudyakov:

Refactor MilkyWay & MagellanicClouds.
 * Move code for loading data to parent class to reduce duplication
   and improve readability

CCMAIL: kstars-devel at kde.org

 M  +3 -111    magellanicclouds.cpp  
 M  +1 -60     milkyway.cpp  
 M  +42 -0     skiplistindex.cpp  
 M  +7 -3      skiplistindex.h  


--- trunk/KDE/kdeedu/kstars/kstars/skycomponents/magellanicclouds.cpp #1044934:1044935
@@ -38,114 +38,11 @@
         SkipListIndex( parent, i18n("Magellanic Clouds") )
 {}
 
-void MagellanicClouds::init()
-{
+void MagellanicClouds::init() {
     intro();
-
-    const char* fname = "lmc.dat";
-    QString line;
-    double ra, dec, lastRa, lastDec;
-    SkipList *skipList = 0;
-    bool ok;
-    int iSkip = 0;
-
-    lastRa = lastDec = -1000.0;
-
-    KSFileReader fileReader;
-    if ( ! fileReader.open( fname ) ) return;
-
-    fileReader.setProgress( i18n("Loading Large Magellanic Clouds"), 2136, 5 );
-
-    while ( fileReader.hasMoreLines() ) {
-        line = fileReader.readLine();
-
-        fileReader.showProgress();
-
-        QChar firstChar = line.at( 0 );
-        if ( firstChar == '#' ) continue;
-
-        ra = line.mid( 2, 8 ).toDouble(&ok);
-        if ( ok ) dec = line.mid( 11, 8 ).toDouble(&ok);
-        if ( !ok ) {
-            fprintf(stderr, "%s: conversion error on line: %d\n",
-                    fname, fileReader.lineNumber());
-            continue;
-        }
-
-        if ( firstChar == 'M' )  {
-            if (  skipList )  appendBoth( skipList );
-            skipList = 0;
-            iSkip = 0;
-            lastRa = lastDec = -1000.0;
-        }
-
-        if ( ! skipList ) skipList = new SkipList();
-
-        if ( ra == lastRa && dec == lastDec ) {
-            fprintf(stderr, "%s: tossing dupe on line %4d: (%f, %f)\n",
-                    fname, fileReader.lineNumber(), ra, dec);
-            continue;
-        }
-
-        skipList->append( new SkyPoint(ra, dec) );
-        lastRa = ra;
-        lastDec = dec;
-        if ( firstChar == 'S' ) skipList->setSkip( iSkip );
-        iSkip++;
-    }
-    if ( skipList ) appendBoth( skipList );
-
-    fname = "smc.dat";
-    skipList = 0;
-    iSkip = 0;
-
-    lastRa = lastDec = -1000.0;
-
-    if ( ! fileReader.open( fname ) ) return;
-
-    fileReader.setProgress( i18n("Loading Small Magellanic Clouds"), 2136, 5 );
-
-    while ( fileReader.hasMoreLines() ) {
-        line = fileReader.readLine();
-
-        fileReader.showProgress();
-
-        QChar firstChar = line.at( 0 );
-        if ( firstChar == '#' ) continue;
-
-        ra = line.mid( 2, 8 ).toDouble(&ok);
-        if ( ok ) dec = line.mid( 11, 8 ).toDouble(&ok);
-        if ( !ok ) {
-            fprintf(stderr, "%s: conversion error on line: %d\n",
-                    fname, fileReader.lineNumber());
-            continue;
-        }
-
-        if ( firstChar == 'M' )  {
-            if (  skipList )  appendBoth( skipList );
-            skipList = 0;
-            iSkip = 0;
-            lastRa = lastDec = -1000.0;
-        }
-
-        if ( ! skipList ) skipList = new SkipList();
-
-        if ( ra == lastRa && dec == lastDec ) {
-            fprintf(stderr, "%s: tossing dupe on line %4d: (%f, %f)\n",
-                    fname, fileReader.lineNumber(), ra, dec);
-            continue;
-        }
-
-        skipList->append( new SkyPoint(ra, dec) );
-        lastRa = ra;
-        lastDec = dec;
-        if ( firstChar == 'S' ) skipList->setSkip( iSkip );
-        iSkip++;
-    }
-    if ( skipList ) appendBoth( skipList );
-
+    loadSkipLists("lmc.dat", i18n("Loading Large Magellanic Clouds"));
+    loadSkipLists("smc.dat", i18n("Loading Small Magellanic Clouds"));
     summary();
-    //printf("Done.\n");
 }
 
 bool MagellanicClouds::selected()
@@ -165,11 +62,6 @@
     psky.setPen( QPen( color, 3, Qt::SolidLine ) );
     psky.setBrush( QBrush( color ) );
 
-    // Uncomment these two lines to get more visible images for debugging.  -jbb
-    //
-    //psky.setPen( QPen( QColor( "red" ), 1, Qt::SolidLine ) );
-    //psky.setBrush( QBrush( QColor("green"  ) ) );
-
     if ( Options::fillMilkyWay() ) {
         drawFilled( psky );
     }
--- trunk/KDE/kdeedu/kstars/kstars/skycomponents/milkyway.cpp #1044934:1044935
@@ -41,62 +41,8 @@
 void MilkyWay::init()
 {
     intro();
-
-    const char* fname = "milkyway.dat";
-    QString line;
-    double ra, dec, lastRa, lastDec;
-    SkipList *skipList = 0;
-    bool ok;
-    int iSkip = 0;
-
-    lastRa = lastDec = -1000.0;
-
-    KSFileReader fileReader;
-    if ( ! fileReader.open( fname ) ) return;
-
-    fileReader.setProgress( i18n("Loading Milky Way"), 2136, 5 );
-
-    while ( fileReader.hasMoreLines() ) {
-        line = fileReader.readLine();
-
-        fileReader.showProgress();
-
-        QChar firstChar = line.at( 0 );
-        if ( firstChar == '#' ) continue;
-
-        ra = line.mid( 2, 8 ).toDouble(&ok);
-        if ( ok ) dec = line.mid( 11, 8 ).toDouble(&ok);
-        if ( !ok ) {
-            fprintf(stderr, "%s: conversion error on line: %d\n",
-                    fname, fileReader.lineNumber());
-            continue;
-        }
-
-        if ( firstChar == 'M' )  {
-            if (  skipList )  appendBoth( skipList );
-            skipList = 0;
-            iSkip = 0;
-            lastRa = lastDec = -1000.0;
-        }
-
-        if ( ! skipList ) skipList = new SkipList();
-
-        if ( ra == lastRa && dec == lastDec ) {
-            fprintf(stderr, "%s: tossing dupe on line %4d: (%f, %f)\n",
-                    fname, fileReader.lineNumber(), ra, dec);
-            continue;
-        }
-
-        skipList->append( new SkyPoint(ra, dec) );
-        lastRa = ra;
-        lastDec = dec;
-        if ( firstChar == 'S' ) skipList->setSkip( iSkip );
-        iSkip++;
-    }
-    if ( skipList ) appendBoth( skipList );
-
+    loadSkipLists("milkyway.dat", i18n("Loading Milky Way"));
     summary();
-    //printf("Done.\n");
 }
 
 bool MilkyWay::selected()
@@ -116,11 +62,6 @@
     psky.setPen( QPen( color, 3, Qt::SolidLine ) );
     psky.setBrush( QBrush( color ) );
 
-    // Uncomment these two lines to get more visible images for debugging.  -jbb
-    //
-    //psky.setPen( QPen( QColor( "red" ), 1, Qt::SolidLine ) );
-    //psky.setBrush( QBrush( QColor("green"  ) ) );
-
     if ( Options::fillMilkyWay() ) {
         drawFilled( psky );
     }
--- trunk/KDE/kdeedu/kstars/kstars/skycomponents/skiplistindex.cpp #1044934:1044935
@@ -22,6 +22,7 @@
 
 #include "skyobjects/skypoint.h"
 #include "skymesh.h"
+#include "ksfilereader.h"
 
 #include "skiplist.h"
 
@@ -40,5 +41,46 @@
     return skipList->skip( i );
 }
 
+void SkipListIndex::loadSkipLists(QString fname, QString greeting) {
+    
+    KSFileReader fileReader;
+    if ( !fileReader.open( fname ) )
+        return;
+    fileReader.setProgress( greeting, 2136, 5 );
 
+    SkipList *skipList = 0;
+    int iSkip = 0;
+    while ( fileReader.hasMoreLines() ) {
+        QString line = fileReader.readLine();
+        fileReader.showProgress();
 
+        QChar firstChar = line.at( 0 );
+        if ( firstChar == '#' )
+            continue;
+
+        bool okRA, okDec;
+        double ra  = line.mid( 2,  8 ).toDouble(&okRA);
+        double dec = line.mid( 11, 8 ).toDouble(&okDec);
+        if( !okRA || !okDec) {
+            kDebug() << QString("%1: conversion error on line: %2\n").arg(fname).arg(fileReader.lineNumber());
+            continue;
+        }
+
+        if ( firstChar == 'M' )  {
+            if( skipList )
+                appendBoth( skipList );
+            skipList = 0;
+            iSkip    = 0;
+        }
+
+        if( !skipList )
+            skipList = new SkipList();
+
+        skipList->append( new SkyPoint(ra, dec) );
+        if ( firstChar == 'S' )
+            skipList->setSkip( iSkip );
+        iSkip++;
+    }  
+    if ( skipList )
+        appendBoth( skipList );
+}
--- trunk/KDE/kdeedu/kstars/kstars/skycomponents/skiplistindex.h #1044934:1044935
@@ -18,6 +18,8 @@
 #ifndef SKIPLISTINDEX_H
 #define SKIPLISTINDEX_H
 
+#include <QString>
+
 #include "linelistindex.h"
 #include "skiplist.h"
 
@@ -34,25 +36,27 @@
 class SkipListIndex : public LineListIndex
 {
 public:
-    /*
+    /**
      * @short Constructor
      */
     SkipListIndex( SkyComponent *parent, const QString& name );
 
-    /* @short Returns an IndexHash from the SkyMesh that contains the set
+    /**@short Returns an IndexHash from the SkyMesh that contains the set
      * of trixels that cover the _SkipList_ lineList excluding skipped
      * lines as specified in the SkipList.  SkipList is a subclass of
      * LineList.
      */
     const IndexHash& getIndexHash( LineList* skipList );
 
-    /* @short Returns a boolean indicating whether to skip the i-th line
+    /**@short Returns a boolean indicating whether to skip the i-th line
      * segment in the _SkipList_ skipList.  Note that SkipList is a
      * subclass of LineList.  This routine allows us to use the drawing
      * code in LineListIndex instead of repeating it all here.
      */
     bool skipAt( LineList* skpiList, int i );
 
+    /** Load skiplists from file */
+    void loadSkipLists(QString fname, QString greeting);
 };
 
 #endif


More information about the Kstars-devel mailing list