[Marble-commits] KDE/kdeedu/marble/src/plugins/render/weather

Bastian Holst bastianholst at gmx.de
Wed Aug 19 17:06:40 CEST 2009


SVN commit 1013322 by bholst:

Work on Marble's weather plugin:
* QObjecs may not be created in a thread (if the threads ends before the objects are destroyed).
* First steps to show a small weather page for the station.


 M  +6 -7      BBCItemGetter.cpp  
 M  +4 -6      BBCItemGetter.h  
 AM            BBCStation.cpp   [License: LGPL]
 AM            BBCStation.h   [License: LGPL]
 M  +16 -2     BBCWeatherService.cpp  
 M  +2 -0      BBCWeatherService.h  
 M  +2 -1      CMakeLists.txt  
 M  +18 -25    StationListParser.cpp  
 M  +5 -4      StationListParser.h  
 M  +30 -0     WeatherItem.cpp  
 M  +3 -1      WeatherItem.h  


--- trunk/KDE/kdeedu/marble/src/plugins/render/weather/BBCItemGetter.cpp #1013321:1013322
@@ -10,6 +10,7 @@
 
 // Self
 #include "BBCItemGetter.h"
+#include "BBCStation.h"
 #include "BBCWeatherItem.h"
 
 // Qt
@@ -42,7 +43,7 @@
     ensureRunning();
 }
 
-void BBCItemGetter::setStationList( const QList<BBCWeatherItem*>& items )
+void BBCItemGetter::setStationList( const QList<BBCStation>& items )
 {
     m_items = items;
     ensureRunning();
@@ -70,14 +71,12 @@
     m_scheduleMutex.unlock();
 
     qint32 fetched = 0;
-    QList<BBCWeatherItem *>::ConstIterator it = m_items.constBegin();
-    QList<BBCWeatherItem *>::ConstIterator end = m_items.constEnd();
+    QList<BBCStation>::ConstIterator it = m_items.constBegin();
+    QList<BBCStation>::ConstIterator end = m_items.constEnd();
 
     while ( fetched < number && it != end ) {
-        if ( (*it) && box.contains( (*it)->coordinate() ) ) {
-            (*it)->setTarget( "earth" );
-            emit requestedDownload( (*it)->observationUrl(), "bbcobservation", (*it) );
-            emit requestedDownload( (*it)->forecastUrl(),    "bbcforecast",    (*it) );
+        if ( box.contains( it->coordinate() ) ) {
+            emit foundStation( (*it) );
             fetched++;
         }
         ++it;
--- trunk/KDE/kdeedu/marble/src/plugins/render/weather/BBCItemGetter.h #1013321:1013322
@@ -24,7 +24,7 @@
 {
 
 class AbstractDataPluginItem;
-class BBCWeatherItem;
+class BBCStation;
 class MarbleDataFacade;
 
 class BBCItemGetter : public AbstractWorkerThread
@@ -39,19 +39,17 @@
                       MarbleDataFacade *facade,
                       qint32 number );
 
-    void setStationList( const QList<BBCWeatherItem*>& items );
+    void setStationList( const QList<BBCStation>& items );
 
  protected:
     bool workAvailable();
     void work();
 
  Q_SIGNALS:
-    void requestedDownload( const QUrl& url,
-                            const QString& type,
-                            AbstractDataPluginItem *item );
+    void foundStation( BBCStation );
 
  public:
-    QList<BBCWeatherItem*> m_items;
+    QList<BBCStation> m_items;
     QMutex m_scheduleMutex;
     GeoDataLatLonAltBox m_scheduledBox;
     qint32 m_scheduledNumber;
** trunk/KDE/kdeedu/marble/src/plugins/render/weather/BBCStation.cpp #property svn:eol-style
   + native
** trunk/KDE/kdeedu/marble/src/plugins/render/weather/BBCStation.h #property svn:eol-style
   + native
--- trunk/KDE/kdeedu/marble/src/plugins/render/weather/BBCWeatherService.cpp #1013321:1013322
@@ -13,6 +13,7 @@
 
 // Marble
 #include "BBCItemGetter.h"
+#include "BBCStation.h"
 #include "BBCWeatherItem.h"
 #include "GeoDataCoordinates.h"
 #include "GeoDataLatLonAltBox.h"
@@ -37,6 +38,7 @@
       m_parser( 0 ),
       m_itemGetter( new BBCItemGetter( this ) )
 {
+     qRegisterMetaType<BBCStation>("BBCStation");
 }
 
 BBCWeatherService::~BBCWeatherService()
@@ -59,14 +61,26 @@
 void BBCWeatherService::fetchStationList()
 {
     connect( m_itemGetter,
-             SIGNAL( requestedDownload( QUrl, QString, AbstractDataPluginItem* ) ),
+             SIGNAL( foundStation( BBCStation ) ),
              this,
-             SIGNAL( requestedDownload( QUrl, QString, AbstractDataPluginItem* ) ) );
+             SLOT( createItem( BBCStation ) ) );
     m_itemGetter->setStationList( m_parser->stationList() );
     delete m_parser;
     m_parser = 0;
 }
 
+void BBCWeatherService::createItem( BBCStation station )
+{
+    BBCWeatherItem *item = new BBCWeatherItem( this );
+    item->setBbcId( station.bbcId() );
+    item->setCoordinate( station.coordinate() );
+    item->setPriority( station.priority() );
+    item->setStationName( station.name() );
+    item->setTarget( "earth" );
+    emit requestedDownload( item->observationUrl(), "bbcobservation", item );
+    emit requestedDownload( item->forecastUrl(),    "bbcforecast",    item );
+}
+
 void BBCWeatherService::setupList()
 {
     m_parsingStarted = true;
--- trunk/KDE/kdeedu/marble/src/plugins/render/weather/BBCWeatherService.h #1013321:1013322
@@ -19,6 +19,7 @@
 {
 
 class BBCItemGetter;
+class BBCStation;
 class BBCWeatherItem;
 class GeoDataLatLonAltBox;
 class StationListParser;
@@ -38,6 +39,7 @@
  
  private Q_SLOTS:
     void fetchStationList();
+    void createItem( BBCStation station );
 
  private:
     void setupList();
--- trunk/KDE/kdeedu/marble/src/plugins/render/weather/CMakeLists.txt #1013321:1013322
@@ -14,6 +14,7 @@
                   AbstractWeatherService.cpp
                   BBCItemGetter.cpp
                   BBCParser.cpp
+                  BBCStation.cpp
                   BBCWeatherService.cpp
                   BBCWeatherItem.cpp
                   FakeWeatherService.cpp 
@@ -24,4 +25,4 @@
 qt4_wrap_ui( weather_SRCS ${weather_UI} )
 
 marble_add_plugin( Weather ${weather_SRCS} )
-target_link_libraries( Weather ${QT_QTGUI_LIBRARY} ${QT_QTSVG_LIBRARY} )
+target_link_libraries( Weather ${QT_QTWEBKIT_LIBRARY} )
--- trunk/KDE/kdeedu/marble/src/plugins/render/weather/StationListParser.cpp #1013321:1013322
@@ -13,7 +13,7 @@
 
 // Marble
 #include "global.h"
-#include "BBCWeatherItem.h"
+#include "BBCStation.h"
 #include "GeoDataCoordinates.h"
 
 // Qt
@@ -23,23 +23,17 @@
 
 using namespace Marble;
 
-static bool lessThanByPointer( const BBCWeatherItem *item1,
-                               const BBCWeatherItem *item2 )
-{
-    if( item1 != 0 && item2 != 0 ) {
-        return item1->operator<( item2 );
-    }
-    else {
-        return false;
-    }
-}
-
 StationListParser::StationListParser( QObject *parent )
     : QThread( parent ),
       QXmlStreamReader()
 {
 }
 
+StationListParser::~StationListParser()
+{
+    wait( 1000 );
+}
+
 void StationListParser::read()
 {
     m_list.clear();
@@ -56,7 +50,7 @@
     }
 }
 
-QList<BBCWeatherItem *> StationListParser::stationList() const
+QList<BBCStation> StationListParser::stationList() const
 {
     return m_list;
 }
@@ -118,7 +112,7 @@
     Q_ASSERT( isStartElement()
               && name() == "Station" );
     
-    BBCWeatherItem *item = new BBCWeatherItem();
+    BBCStation station;
     
     while ( !atEnd() ) {
         readNext();
@@ -128,25 +122,24 @@
         
         if( isStartElement() ) {
             if( name() == "name" )
-                item->setStationName( readCharacters() );
+                station.setName( readCharacters() );
             else if ( name() == "id" )
-                item->setBbcId( readCharacters().toLong() );
+                station.setBbcId( readCharacters().toLong() );
             else if ( name() == "priority" )
-                item->setPriority( readCharacters().toInt() );
+                station.setPriority( readCharacters().toInt() );
             else if ( name() == "Point" )
-                readPoint( item );
+                readPoint( &station );
             else
                 readUnknownElement();
         }
     }
 
     // This find the right position in the sorted to insert the new item
-    QList<BBCWeatherItem*>::iterator i = qLowerBound( m_list.begin(),
-                                                      m_list.end(),
-                                                      item,
-                                                      lessThanByPointer );
+    QList<BBCStation>::iterator i = qLowerBound( m_list.begin(),
+                                                 m_list.end(),
+                                                 station );
     // Insert the item on the right position in the list
-    m_list.insert( i, item );
+    m_list.insert( i, station );
 }
 
 QString StationListParser::readCharacters()
@@ -173,7 +166,7 @@
     return string;
 }
 
-void StationListParser::readPoint( BBCWeatherItem *item )
+void StationListParser::readPoint( BBCStation *station )
 {
     Q_ASSERT( isStartElement()
               && name() == "Point" );
@@ -192,7 +185,7 @@
                 if ( coorList.size() >= 2 ) {
                     GeoDataCoordinates coordinates( coorList.at( 0 ).toFloat() * DEG2RAD,
                                                     coorList.at( 1 ).toFloat() * DEG2RAD );
-                    item->setCoordinate( coordinates );
+                    station->setCoordinate( coordinates );
                 }
             }
             else
--- trunk/KDE/kdeedu/marble/src/plugins/render/weather/StationListParser.h #1013321:1013322
@@ -23,17 +23,18 @@
 namespace Marble
 {
 
-class BBCWeatherItem;
+class BBCStation;
     
 class StationListParser : public QThread, public QXmlStreamReader
 {
     Q_OBJECT
 public:
     StationListParser( QObject *parent );
+    ~StationListParser();
 
     void read();
 
-    QList<BBCWeatherItem *> stationList() const;
+    QList<BBCStation> stationList() const;
 
     void setPath( QString path );
 
@@ -45,10 +46,10 @@
     void readStationList();
     void readStation();
     QString readCharacters();
-    void readPoint( BBCWeatherItem *item );
+    void readPoint( BBCStation *station );
 
     QString m_path;
-    QList<BBCWeatherItem *> m_list;
+    QList<BBCStation> m_list;
     QObject *m_parent;
 };
 
--- trunk/KDE/kdeedu/marble/src/plugins/render/weather/WeatherItem.cpp #1013321:1013322
@@ -19,6 +19,7 @@
 #include "FrameGraphicsItem.h"
 #include "LabelGraphicsItem.h"
 #include "MarbleGraphicsGridLayout.h"
+#include "TinyWebBrowser.h"
 
 // Qt
 #include <QtCore/QCoreApplication>
@@ -49,6 +50,7 @@
     WeatherItemPrivate( WeatherItem *parent )
         : m_priority( 0 ),
           m_action( new QAction( tr( "Weather" ), parent ) ),
+          m_browser( 0 ),
           m_parent( parent ),
           m_frameItem( new FrameGraphicsItem( m_parent ) ),
           m_conditionLabel( new LabelGraphicsItem( m_frameItem ) ),
@@ -60,6 +62,7 @@
         m_temperatureLabel->setMinimumSize( QSizeF( 0, imageSize.height() ) );
         m_windSpeedLabel->setMinimumSize( QSizeF( 0, imageSize.height() ) );
 
+        // Layouting the item
         MarbleGraphicsGridLayout *topLayout = new MarbleGraphicsGridLayout( 1, 1 );
         parent->setLayout( topLayout );
         topLayout->addItem( m_frameItem, 0, 0 );
@@ -249,6 +252,7 @@
 
     int m_priority;
     QAction *m_action;
+    TinyWebBrowser *m_browser;
     WeatherItem *m_parent;
     QString m_stationName;
     QHash<QString,QVariant> m_settings;
@@ -288,6 +292,10 @@
 
 QAction *WeatherItem::action()
 {
+    disconnect( d->m_action, SIGNAL( triggered() ),
+                this,        SLOT( openBrowser() ) );
+    connect(    d->m_action, SIGNAL( triggered() ),
+                this,        SLOT( openBrowser() ) );
     return d->m_action;
 }
 
@@ -413,6 +421,28 @@
     d->updateLabels();
 }
 
+void WeatherItem::openBrowser()
+{
+    qDebug() << "Open browser";
+    if( !d->m_browser ) {
+        d->m_browser = new TinyWebBrowser();
+    }
+    QString html;
+    html += "<html>";
+    html += "<body>";
+    html += "<h1>" + tr( "Weather for %1" ).arg( stationName() ) + "</h1>";
+    if ( d->m_currentWeather.isValid() ) {
+        html += "<h2>" + tr( "Current Observation" ) + "</h2>";
+        html += tr( "Publishing Time: %1" )
+                    .arg( d->m_currentWeather.publishingTime().toLocalTime().toString() );
+    }
+    html += "</body>";
+    html += "</html>";
+
+    d->m_browser->setHtml( html );
+    d->m_browser->show();
+}
+
 } // namespace Marble
 
 #include "WeatherItem.moc"
--- trunk/KDE/kdeedu/marble/src/plugins/render/weather/WeatherItem.h #1013321:1013322
@@ -82,7 +82,9 @@
 
     void setSettings( QHash<QString, QVariant> settings );
     
-    // Forecasts to appear later
+ public Q_SLOTS:
+    void openBrowser();
+
  private:
     Q_DISABLE_COPY(WeatherItem)
     WeatherItemPrivate * const d;


More information about the Marble-commits mailing list