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

Jérôme Sonrier jsid at emor3j.fr.eu.org
Sun Feb 6 02:14:28 CET 2011


SVN commit 1219059 by jsonrier:

Repair flags drawing.

CCMAIL: kstars-devel at kde.org



 M  +1 -0      data/CMakeLists.txt  
 AM            data/textures/defaultflag.png  
 M  +86 -101   skycomponents/flagcomponent.cpp  
 M  +1 -0      skycomponents/flagcomponent.h  
 M  +119 -0    skyglpainter.cpp  
 M  +2 -0      skyglpainter.h  
 M  +4 -0      skypainter.h  
 M  +34 -0     skyqpainter.cpp  
 M  +1 -0      skyqpainter.h  
 M  +24 -1     texturemanager.cpp  
 M  +5 -0      texturemanager.h  


--- trunk/KDE/kdeedu/kstars/kstars/data/CMakeLists.txt #1219058:1219059
@@ -115,6 +115,7 @@
 	       textures/moon33.png 
 	       textures/moon34.png 
 	       textures/moon35.png 
+	       textures/defaultflag.png 
          DESTINATION ${DATA_INSTALL_DIR}/kstars/textures )
 
 
--- trunk/KDE/kdeedu/kstars/kstars/skycomponents/flagcomponent.cpp #1219058:1219059
@@ -38,8 +38,91 @@
     m_Job = KIO::listDir( KUrl( KStandardDirs::locateLocal("appdata", ".") ), KIO::HideProgressInfo, false );
     connect(m_Job, SIGNAL(entries(KIO::Job*, const KIO::UDSEntryList&)), 
             SLOT(slotLoadImages(KIO::Job*, const KIO::UDSEntryList&)));
+    connect( m_Job, SIGNAL( result( KJob * ) ), this, SLOT( slotInit( KJob * ) ) );
+}
 
-    // Init
+
+FlagComponent::~FlagComponent()
+{}
+
+void FlagComponent::draw( SkyPainter *skyp ) {
+    // Return if flags must not be draw
+    if( ! selected() )
+        return;
+
+    // Return if no images are available
+    if( m_Names.size() < 1 )
+        return;
+
+    // Draw all flags
+    skyp->drawFlags();
+}
+
+bool FlagComponent::selected() {
+    return Options::showFlags();
+}
+
+double FlagComponent::getEpoch (const QString &eName) {
+    //If eName is empty (or not a number) assume 2000.0
+    bool ok;
+    double epoch = eName.toDouble( &ok );
+    if( eName.isEmpty() || !ok )
+        return 2000.0;
+    return epoch;
+}
+
+long double FlagComponent::epochToJd (double epoch) {
+    double yearsTo2000 = 2000.0 - epoch;
+    if (epoch == 1950.0) {
+        return 2433282.4235;
+    } else if ( epoch == 2000.0 ) {
+        return J2000;
+    } else {
+        return ( J2000 - yearsTo2000 * 365.2425 );
+    }
+}
+
+void FlagComponent::add( SkyPoint* flagPoint, QString epoch, QString image, QString label, QColor labelColor ) {
+    pointList().append( flagPoint );
+    m_Epoch.append( epoch );
+
+    for(int i = 0; i<m_Names.size(); i++ ) {
+        if( image == m_Names.at( i ) )
+            m_FlagImages.append( i );
+    }
+
+    m_Labels.append( label );
+    m_LabelColors.append( labelColor );
+}
+
+void FlagComponent::remove( int index ) {
+    pointList().removeAt( index );
+    m_Epoch.removeAt( index );
+    m_FlagImages.removeAt( index );
+    m_Labels.removeAt( index );
+    m_LabelColors.removeAt( index );
+}
+
+void FlagComponent::slotLoadImages( KIO::Job*, const KIO::UDSEntryList& list ) {
+    // Add the default flag images to available images list
+    m_Names.append( i18n ("Default" ) );
+    m_Images.append( QImage( KStandardDirs::locate( "appdata", "defaultflag.gif" ) ));
+
+    // Add all other images found in user appdata directory
+    foreach( KIO::UDSEntry entry, list) {
+        KFileItem item(entry, m_Job->url(), false, true);
+        if( item.name().startsWith( "_flag" ) ) {
+            QString fileName = item.name()
+                .replace(QRegExp("\\.[^.]*$"), QString())
+                .replace(QRegExp("^_flag"),   QString())
+                .replace('_',' ');
+            m_Names.append( fileName );
+            m_Images.append( QImage( item.localPath() ));
+        }
+    }
+}
+
+void FlagComponent::slotInit( KJob *job ) {
     KSFileReader fileReader;
     bool imageFound = false;
 
@@ -109,110 +192,12 @@
             str += line.at( i ) + ' ';
         m_Labels.append( str );
     }
-}
 
-
-FlagComponent::~FlagComponent()
-{}
-
-void FlagComponent::draw( SkyPainter *skyp )
-{
-    #warning Still have to fix FlagComponent...
-    #if 0
-    if( !selected() )
-        return;
-
-    SkyMap *map = SkyMap::Instance();
-    KStarsData *data = KStarsData::Instance();
-    for(int i=0; i<pointList().size(); i++ ) {
-        // Get Screen coordinates
-        SkyPoint* p = pointList().at( i );
-        double epoch = getEpoch( m_Epoch.at( i ) );
-        long double jd = epochToJd ( epoch );
-        p->apparentCoord(jd, data->ut().djd() );
-        p->EquatorialToHorizontal( data->lst(), data->geo()->lat() );
-
-        // Draw flag image
-        QImage flagImage = m_Images.value( m_FlagImages.at( i ) );
-        QPointF o = map->toScreen( p, false );
-        o.setX( o.x() - flagImage.width()*0.5 );
-        o.setY( o.y() - flagImage.height()*0.5 );
-        psky.drawImage( o, flagImage );
-
-        // Draw flag label
-        o.setX( o.x() + 25.0 );
-        o.setY( o.y() + 12.0 );
-        psky.save();
-        psky.setPen( m_LabelColors.at( i ) );
-        psky.setFont( QFont( "Courier New", 10, QFont::Bold ) );
-        psky.drawText( o, m_Labels.at( i ) );
-        psky.restore();
+    // Redraw Skymap
+    KStars::Instance()->map()->forceUpdate(false);
     }
-    #endif
-}
 
 
-bool FlagComponent::selected() {
-    return Options::showFlags();
-}
-
-double FlagComponent::getEpoch (const QString &eName) {
-    //If eName is empty (or not a number) assume 2000.0
-    bool ok;
-    double epoch = eName.toDouble( &ok );
-    if( eName.isEmpty() || !ok )
-        return 2000.0;
-    return epoch;
-}
-
-long double FlagComponent::epochToJd (double epoch) {
-    double yearsTo2000 = 2000.0 - epoch;
-    if (epoch == 1950.0) {
-        return 2433282.4235;
-    } else if ( epoch == 2000.0 ) {
-        return J2000;
-    } else {
-        return ( J2000 - yearsTo2000 * 365.2425 );
-    }
-}
-
-void FlagComponent::add( SkyPoint* flagPoint, QString epoch, QString image, QString label, QColor labelColor ) {
-    pointList().append( flagPoint );
-    m_Epoch.append( epoch );
-
-    for(int i = 0; i<m_Names.size(); i++ ) {
-        if( image == m_Names.at( i ) )
-            m_FlagImages.append( i );
-    }
-
-    m_Labels.append( label );
-    m_LabelColors.append( labelColor );
-}
-
-void FlagComponent::remove( int index ) {
-    pointList().removeAt( index );
-    m_Epoch.removeAt( index );
-    m_FlagImages.removeAt( index );
-    m_Labels.removeAt( index );
-    m_LabelColors.removeAt( index );
-}
-
-void FlagComponent::slotLoadImages( KIO::Job*, const KIO::UDSEntryList& list ) {
-    m_Names.append( i18n ("Default" ) );
-    m_Images.append( QImage( KStandardDirs::locate( "appdata", "defaultflag.gif" ) ));
-    foreach( KIO::UDSEntry entry, list) {
-        KFileItem item(entry, m_Job->url(), false, true);
-        if( item.name().startsWith( "_flag" ) ) {
-            QString fileName = item.name()
-                .replace(QRegExp("\\.[^.]*$"), QString())
-                .replace(QRegExp("^_flag"),   QString())
-                .replace('_',' ');
-            m_Names.append( fileName );
-            m_Images.append( QImage( item.localPath() ));
-        }
-    }
-}
-
 QStringList FlagComponent::getNames() {
     return m_Names;
 }
--- trunk/KDE/kdeedu/kstars/kstars/skycomponents/flagcomponent.h #1219058:1219059
@@ -145,6 +145,7 @@
 
 private slots:
     void slotLoadImages( KIO::Job* job, const KIO::UDSEntryList& list );
+    void slotInit( KJob *job );
 };
 
 #endif
--- trunk/KDE/kdeedu/kstars/kstars/skyglpainter.cpp #1219058:1219059
@@ -37,6 +37,8 @@
 #include "skycomponents/linelist.h"
 #include "skycomponents/skiplist.h"
 #include "skycomponents/linelistlabel.h"
+#include "skycomponents/skymapcomposite.h"
+#include "skycomponents/flagcomponent.h"
 
 #include "skyobjects/deepskyobject.h"
 #include "skyobjects/kscomet.h"
@@ -498,7 +500,124 @@
     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 }
 
+void SkyGLPainter::drawFlags()
+{
+    KStarsData *data = KStarsData::Instance();
+    SkyPoint* point;
+    QImage image;
+    const Texture *tex;
+    const QString label;
+    bool visible = false;
+    Vector2f vec;
+    int i;
 
+    for ( i=0; i<data->skyComposite()->flags()->size(); i++ ) {
+        point = data->skyComposite()->flags()->pointList().at( i );
+        image = data->skyComposite()->flags()->image( i );
+
+        // Set Horizontal coordinates
+        point->EquatorialToHorizontal( data->lst(), data->geo()->lat() );
+
+        // Get flag position on screen
+        vec = m_proj->toScreenVec( point, true, &visible );
+
+        // Return if flag is not visible
+        if( !visible || !m_proj->onScreen( vec ) ) continue;
+
+        // Get texture from TextureManager
+        if ( data->skyComposite()->flags()->imageName( i ) == "Default" )
+            tex = TextureManager::getTexture("defaultflag");
+        else
+            tex = TextureManager::createTexture( image );
+        
+        tex->bind();
+
+        // Draw image
+        if( tex->isReady() ) {
+            glEnable(GL_TEXTURE_2D);
+            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+            glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
+            Vector2f vertex;
+            glBegin(GL_QUADS);
+                vertex = vec + Vector2f( image.width()/2 * -1, image.height()/2 );
+                glTexCoord2f(0.,0.);
+                glVertex2fv(vertex.data());
+                vertex = vec + Vector2f( image.width()/2, image.height()/2 );
+                glTexCoord2f(1.,0.);
+                glVertex2fv(vertex.data());
+                vertex = vec + Vector2f( image.width()/2, image.height()/2 * -1  );
+                glTexCoord2f(1.,1.);
+                glVertex2fv(vertex.data());
+                vertex = vec + Vector2f( image.width()/2 * -1, image.height()/2 * -1 );
+                glTexCoord2f(0.,1.);
+                glVertex2fv(vertex.data());
+            glEnd();
+        }
+
+        // Draw label
+        drawText( vec.x(), vec.y(), data->skyComposite()->flags()->label( i ), QFont( "Courier New", 10, QFont::Bold ), data->skyComposite()->flags()->labelColor( i ) );
+    }
+}
+
+void SkyGLPainter::drawText( int x, int y, const QString text, QFont font, QColor color )
+{
+    // Return if text is empty
+    if ( text.isEmpty() )
+        return;
+
+    int longest, tex_size = 2;
+    
+    // Get size of text
+    QFontMetrics fm( font );
+    const QRect bounding_rect = fm.boundingRect( text );
+
+    // Compute texture size
+    if ( bounding_rect.width() > bounding_rect.height() )
+        longest = bounding_rect.width();
+    else
+        longest = bounding_rect.height();
+
+    while ( tex_size < longest ) {
+        tex_size *= 2;
+    }
+
+    // Create image of text
+    QImage text_image( tex_size, tex_size, QImage::Format_ARGB32 );
+    text_image.fill( Qt::transparent );
+    QPainter p( &text_image );
+    p.setFont( font );
+    p.setPen( color );
+    p.drawText( 0, tex_size/2, text );
+    p.end();
+
+    // Create texture
+    Texture *texture = TextureManager::createTexture( text_image );
+    texture->bind();
+
+    // Render image
+    if( texture->isReady() ) {
+        glEnable(GL_TEXTURE_2D);
+        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
+        Vector2f vec( x, y );
+        Vector2f vertex;
+        glBegin(GL_QUADS);
+            vertex = vec + Vector2f( 10, text_image.height()/2 -10 );
+            glTexCoord2f(0.,0.);
+            glVertex2fv(vertex.data());
+            vertex = vec + Vector2f( text_image.width() + 10, text_image.height()/2 -10 );
+            glTexCoord2f(1.,0.);
+            glVertex2fv(vertex.data());
+            vertex = vec + Vector2f( text_image.width() + 10, text_image.height()/2*(-1) - 10 );
+            glTexCoord2f(1.,1.);
+            glVertex2fv(vertex.data());
+            vertex = vec + Vector2f( 10, text_image.height()/2*(-1) - 10 );;
+            glTexCoord2f(0.,1.);
+            glVertex2fv(vertex.data());
+        glEnd();
+    }
+}
+
 void SkyGLPainter::drawSkyLine(SkyPoint* a, SkyPoint* b)
 {
 
--- trunk/KDE/kdeedu/kstars/kstars/skyglpainter.h #1219058:1219059
@@ -41,11 +41,13 @@
     virtual void drawSkyLine(SkyPoint* a, SkyPoint* b);
     virtual void drawSkyBackground();
     virtual void drawObservingList(const QList<SkyObject*>& obs);
+    virtual void drawFlags();
     virtual void end();
     virtual void begin();
     virtual void setBrush(const QBrush& brush);
     virtual void setPen(const QPen& pen);
     virtual void drawHorizon( bool filled, SkyPoint *labelPoint = 0, bool *drawLabel = 0);
+    void drawText( int x, int y, const QString text, QFont font, QColor color );
 private:
     bool addItem(SkyPoint* p, int type, float width, char sp = 'a');
     void drawBuffer(int type);
--- trunk/KDE/kdeedu/kstars/kstars/skypainter.h #1219058:1219059
@@ -137,6 +137,10 @@
         */
     virtual void drawObservingList( const QList<SkyObject*>& obs ) = 0;
 
+    /** @short Draw flags
+        */
+    virtual void drawFlags() = 0;
+
     virtual void drawHorizon( bool filled, SkyPoint *labelPoint = 0, bool *drawLabel = 0) = 0;
 
 protected:
--- trunk/KDE/kdeedu/kstars/kstars/skyqpainter.cpp #1219058:1219059
@@ -29,6 +29,8 @@
 #include "skycomponents/linelist.h"
 #include "skycomponents/skiplist.h"
 #include "skycomponents/linelistlabel.h"
+#include "skycomponents/skymapcomposite.h"
+#include "skycomponents/flagcomponent.h"
 
 #include "skyobjects/deepskyobject.h"
 #include "skyobjects/kscomet.h"
@@ -655,6 +657,38 @@
     }
 }
 
+void SkyQPainter::drawFlags()
+{
+    KStarsData *data = KStarsData::Instance();
+    SkyPoint* point;
+    QImage image;
+    bool visible = false;
+    QPointF pos;
+    int i;
+
+    for ( i=0;i<data->skyComposite()->flags()->size();i++ ) {
+        point = data->skyComposite()->flags()->pointList().at( i );
+        image = data->skyComposite()->flags()->image( i );
+
+        // Set Horizontal coordinates
+        point->EquatorialToHorizontal( data->lst(), data->geo()->lat() );
+
+        // Get flag position on screen
+        pos = m_proj->toScreen( point, true, &visible );
+
+        // Return if flag is not visible
+        if( !visible || !m_proj->onScreen( pos ) ) continue;
+
+        // Draw flag image
+        drawImage( pos.x()-0.5*image.width(), pos.y()-0.5*image.height(), image );
+
+        // Draw flag label
+        setPen( data->skyComposite()->flags()->labelColor( i ) );
+        setFont( QFont( "Courier New", 10, QFont::Bold ) );
+        drawText( pos.x()+10, pos.y()-10, data->skyComposite()->flags()->label( i ) );
+    }
+}
+
 void SkyQPainter::drawHorizon(bool filled, SkyPoint* labelPoint, bool* drawLabel)
 {
     QVector<Vector2f> ground = m_proj->groundPoly(labelPoint, drawLabel);
--- trunk/KDE/kdeedu/kstars/kstars/skyqpainter.h #1219058:1219059
@@ -56,6 +56,7 @@
     virtual bool drawDeepSkyObject(DeepSkyObject *obj, bool drawImage = false);
     virtual bool drawPlanet(KSPlanetBase *planet);
     virtual void drawObservingList(const QList<SkyObject*>& obs);
+    virtual void drawFlags();
     virtual void drawHorizon( bool filled, SkyPoint *labelPoint = 0, bool *drawLabel = 0);
 private:
     ///This function exists so that we can draw other objects (e.g., planets) as point sources.
--- trunk/KDE/kdeedu/kstars/kstars/texturemanager.cpp #1219058:1219059
@@ -76,8 +76,31 @@
     return m_p;
 }
 
+Texture* TextureManager::createTexture( QImage image )
+{
+    int longest, tex_size = 2;
+    Texture *texture = new Texture( m_p );
+
+    // Resize image if necessary and create texture
+    if ( image.width() != image.height() || ( image.width() & ( image.width() - 1 ) ) ) {
+        // Compute texture size
+        if ( image.width() > image.height() )
+            longest = image.width();
+        else
+            longest = image.height();
+
+        while ( tex_size < longest ) {
+            tex_size *= 2;
+        }
+
+        texture->setImage( image.scaled( tex_size, tex_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ) );
+    } else
+        texture->setImage( image );
+
+    return texture;
+}
+
 TextureManager::TextureManager(QObject* parent): QObject(parent)
 {
 
 }
-
--- trunk/KDE/kdeedu/kstars/kstars/texturemanager.h #1219058:1219059
@@ -57,6 +57,11 @@
      */
     static TextureManager *Create();
 
+    /**
+     *@short Create a texture from image
+     */
+    static Texture* createTexture( QImage image );
+
 protected:
     TextureManager(QObject* parent = 0);
     static TextureManager* m_p;


More information about the Kstars-devel mailing list