[Marble-commits] KDE/kdeedu/marble/src/lib/routing

Dennis Nienhüser earthwings at gentoo.org
Wed Sep 8 22:22:36 CEST 2010


SVN commit 1173158 by nienhueser:

AlternativeRoutesModel now handles the active route instead of RoutingWidget.
CCBUG: 248086
Add saveSettings() and restoreSettings() methods to RoutingManager. They work, but lack saving of route preferences and are not yet called in the code.

 M  +22 -1     AlternativeRoutesModel.cpp  
 M  +6 -1      AlternativeRoutesModel.h  
 M  +1 -0      RouteRequest.cpp  
 M  +125 -2    RoutingManager.cpp  
 M  +14 -0     RoutingManager.h  
 M  +1 -1      RoutingModel.cpp  
 M  +6 -5      RoutingModel.h  
 M  +10 -8     RoutingWidget.cpp  


--- trunk/KDE/kdeedu/marble/src/lib/routing/AlternativeRoutesModel.cpp #1173157:1173158
@@ -25,6 +25,8 @@
 
     MarbleModel* m_marbleModel;
 
+    int m_currentIndex;
+
     AlternativeRoutesModelPrivate( MarbleModel* marbleModel );
 
     /**
@@ -85,7 +87,7 @@
 
 
 AlternativeRoutesModelPrivate::AlternativeRoutesModelPrivate( MarbleModel* marbleModel ) :
-        m_marbleModel( marbleModel )
+        m_marbleModel( marbleModel ), m_currentIndex( 0 )
 {
     // nothing to do
 }
@@ -393,6 +395,25 @@
     return AlternativeRoutesModelPrivate::waypoints( document );
 }
 
+void AlternativeRoutesModel::setCurrentRoute( int index )
+{
+    if ( index >= 0 && index < rowCount() ) {
+        d->m_currentIndex = index;
+        emit currentRouteChanged( currentRoute() );
+    }
+}
+
+GeoDataDocument * AlternativeRoutesModel::currentRoute()
+{
+    GeoDataDocument* result = 0;
+    if ( d->m_currentIndex >= 0 && d->m_currentIndex < rowCount() ) {
+        result = d->m_routes[d->m_currentIndex];
+    }
+
+    return result;
+}
+
+
 } // namespace Marble
 
 #include "AlternativeRoutesModel.moc"
--- trunk/KDE/kdeedu/marble/src/lib/routing/AlternativeRoutesModel.h #1173157:1173158
@@ -65,8 +65,10 @@
       */
     void addRoute( GeoDataDocument* document );
 
-    // Convenience methods
+    void setCurrentRoute( int index );
 
+    GeoDataDocument* currentRoute();
+
     /** Returns the waypoints contained in the route as a linestring */
     static GeoDataLineString* waypoints( const GeoDataDocument* document );
 
@@ -76,6 +78,9 @@
     /** Returns the minimal distance of each waypoint of routeA to routeB */
     static QVector<qreal> deviation( const GeoDataDocument* routeA, const GeoDataDocument* routeB );
 
+Q_SIGNALS:
+    void currentRouteChanged( GeoDataDocument* newRoute );
+
 private Q_SLOTS:
     void addRestrainedRoutes();
 
--- trunk/KDE/kdeedu/marble/src/lib/routing/RouteRequest.cpp #1173157:1173158
@@ -206,6 +206,7 @@
     GeoDataPlacemark placemark;
     placemark.setCoordinate( GeoDataPoint( coordinates ) );
     d->m_route.append( placemark );
+    emit positionAdded( d->m_route.size()-1 );
 }
 
 void RouteRequest::remove( int index )
--- trunk/KDE/kdeedu/marble/src/lib/routing/RoutingManager.cpp #1173157:1173158
@@ -17,8 +17,16 @@
 #include "RoutingModel.h"
 #include "MarbleRunnerManager.h"
 #include "AdjustNavigation.h"
+#include "GeoWriter.h"
+#include "GeoDataDocument.h"
+#include "GeoDataParser.h"
+#include "GeoDataFolder.h"
+#include "MarbleDirs.h"
+#include "MarbleDebug.h"
 
+#include <QtCore/QFile>
 #include <QtGui/QMessageBox>
+#include <QMutexLocker>
 
 namespace Marble
 {
@@ -38,13 +46,19 @@
 
     bool m_workOffline;
 
-    RoutingManagerPrivate( MarbleModel *marbleModel, RoutingManager* manager, QObject *parent );
-
     MarbleRunnerManager* m_runnerManager;
 
     bool m_haveRoute;
 
     AdjustNavigation *m_adjustNavigation;
+
+    QMutex m_fileMutex;
+
+    RoutingManagerPrivate( MarbleModel *marbleModel, RoutingManager* manager, QObject *parent );
+
+    GeoDataFolder* routeRequest() const;
+
+    QString stateFile() const;
 };
 
 RoutingManagerPrivate::RoutingManagerPrivate( MarbleModel *model, RoutingManager* manager, QObject *parent ) :
@@ -60,11 +74,45 @@
     // nothing to do
 }
 
+GeoDataFolder* RoutingManagerPrivate::routeRequest() const
+{
+    GeoDataFolder* result = new GeoDataFolder;
+    result->setName( "Route Request" );
+    for ( int i=0; i<m_routeRequest->size(); ++i ) {
+        GeoDataPlacemark* placemark = new GeoDataPlacemark;
+        placemark->setName( m_routeRequest->name( i ) );
+        placemark->setCoordinate( GeoDataPoint( m_routeRequest->at( i ) ) );
+        result->append( placemark );
+    }
+
+    return result;
+}
+
+QString RoutingManagerPrivate::stateFile() const
+{
+    QString const subdir = "routing";
+    QDir dir( MarbleDirs::localPath() );
+    if ( !dir.exists( subdir ) ) {
+        if ( !dir.mkdir( subdir ) ) {
+            mDebug() << "Unable to create dir " << dir.absoluteFilePath( subdir );
+            return dir.absolutePath();
+        }
+    }
+
+    if ( !dir.cd( subdir ) ) {
+        mDebug() << "Cannot change into " << dir.absoluteFilePath( subdir );
+    }
+
+    return dir.absoluteFilePath( "route.kml" );
+}
+
 RoutingManager::RoutingManager( MarbleModel *marbleModel, QObject *parent ) : QObject( parent ),
         d( new RoutingManagerPrivate( marbleModel, this, this ) )
 {
     connect( d->m_runnerManager, SIGNAL( routeRetrieved( GeoDataDocument* ) ),
              this, SLOT( retrieveRoute( GeoDataDocument* ) ) );
+    connect( d->m_alternativeRoutesModel, SIGNAL( currentRouteChanged( GeoDataDocument* ) ),
+             d->m_routingModel, SLOT( setCurrentRoute( GeoDataDocument* ) ) );
 }
 
 RoutingManager::~RoutingManager()
@@ -147,6 +195,81 @@
     return d->m_routeRequest;
 }
 
+void RoutingManager::saveSettings() const
+{
+    GeoWriter writer;
+    writer.setDocumentType( "http://earth.google.com/kml/2.2" );
+
+    QMutexLocker locker( &d->m_fileMutex );
+    QFile file( d->stateFile() );
+    if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
+    {
+        mDebug() << "Cannot write to " << file.fileName();
+        return;
+    }
+
+    GeoDataDocument container;
+    GeoDataFolder* request = d->routeRequest();
+    if ( request ) {
+        container.append( request );
+    }
+
+    GeoDataDocument *route = d->m_alternativeRoutesModel->currentRoute();
+    if ( route ) {
+        container.append( new GeoDataDocument( *route ) );
+    }
+
+    if ( !writer.write( &file, container ) ) {
+        mDebug() << "Can not write route state to " << file.fileName();
+    }
+    file.close();
+}
+
+void RoutingManager::restoreSettings()
+{
+    QFile file( d->stateFile() );
+    if ( !file.open( QIODevice::ReadOnly ) ) {
+        mDebug() << "Can not read route state from " << file.fileName();
+        return;
+    }
+
+    GeoDataParser parser( GeoData_KML );
+    if ( !parser.read( &file ) ) {
+        mDebug() << "Could not parse file: " << parser.errorString();
+        return;
+    }
+
+    GeoDocument *doc = parser.releaseDocument();
+    GeoDataDocument* container = dynamic_cast<GeoDataDocument*>( doc );
+    if ( container && container->size() == 2 ) {
+        GeoDataFolder* viaPoints = dynamic_cast<GeoDataFolder*>( &container->first() );
+        if ( viaPoints ) {
+            QVector<GeoDataPlacemark*> placemarks = viaPoints->placemarkList();
+            for( int i=0; i<placemarks.size(); ++i ) {
+                if ( i < d->m_routeRequest->size() ) {
+                    d->m_routeRequest->setPosition( i, placemarks[i]->coordinate() );
+                } else {
+                    d->m_routeRequest->append( placemarks[i]->coordinate() );
+                }
+                d->m_routeRequest->setName( d->m_routeRequest->size()-1, placemarks[i]->name() );
+            }
+        } else {
+            mDebug() << "Expected a GeoDataDocument, didn't get one though";
+        }
+
+        GeoDataDocument* route = dynamic_cast<GeoDataDocument*>(&container->last());
+        if ( route ) {
+            routingModel()->setCurrentRoute( route );
+        } else {
+            mDebug() << "Expected a GeoDataDocument, didn't get one though";
+        }
+    } else {
+        mDebug() << "Expected a GeoDataDocument, didn't get one though";
+    }
+
+    file.close();
+}
+
 } // namespace Marble
 
 #include "RoutingManager.moc"
--- trunk/KDE/kdeedu/marble/src/lib/routing/RoutingManager.h #1173157:1173158
@@ -24,6 +24,7 @@
 class RouteRequest;
 class MarbleModel;
 class GeoDataDocument;
+class GeoDataFolder;
 class AlternativeRoutesModel;
 class AdjustNavigation;
 
@@ -86,8 +87,21 @@
      */
     AdjustNavigation* adjustNavigation();
 
+    /**
+      * Returns the current route request
+      */
     RouteRequest* routeRequest();
 
+    /**
+      * Saves the current route request and the current route to disk
+      */
+    void saveSettings() const;
+
+    /**
+      * Restores a previously saved route request and route from disk, if any
+      */
+    void restoreSettings();
+
 public Q_SLOTS:
     /** Update the route */
     void updateRoute();
--- trunk/KDE/kdeedu/marble/src/lib/routing/RoutingModel.cpp #1173157:1173158
@@ -204,7 +204,7 @@
     return QVariant();
 }
 
-bool RoutingModel::importGeoDataDocument( GeoDataDocument* document )
+bool RoutingModel::setCurrentRoute( GeoDataDocument* document )
 {
     d->m_route.clear();
 
--- trunk/KDE/kdeedu/marble/src/lib/routing/RoutingModel.h #1173157:1173158
@@ -81,11 +81,6 @@
 
     // importGeoDataDocument() will probably become setModel(GeoDataDocument*) in the future and
     // the internal representation using a QVector<RouteElement> may go away
-    /**
-      * Old data in the model is discarded, the parsed content of the provided document
-      * is used as the new model data and a model reset is done
-      */
-    bool importGeoDataDocument( GeoDataDocument* document );
 
     /**
       * Returns the total (estimated) time it takes to travel from
@@ -155,6 +150,12 @@
     qreal currentInstructionLength() const;
 
 public Q_SLOTS:
+    /**
+      * Old data in the model is discarded, the parsed content of the provided document
+      * is used as the new model data and a model reset is done
+      */
+    bool setCurrentRoute( GeoDataDocument* document );
+
     void currentInstruction( GeoDataCoordinates, qreal );
 
 Q_SIGNALS:
--- trunk/KDE/kdeedu/marble/src/lib/routing/RoutingWidget.cpp #1173157:1173158
@@ -191,6 +191,8 @@
     d->m_routingLayer->synchronizeAlternativeRoutesWith( d->m_routingManager->alternativeRoutesModel(), d->m_ui.routeComboBox );
     d->m_widget->model()->addLayer( d->m_routingLayer );
 
+    connect( d->m_routingManager->alternativeRoutesModel(), SIGNAL( currentRouteChanged( GeoDataDocument* ) ),
+             d->m_widget, SLOT( repaint() ) );
     connect( d->m_routingLayer, SIGNAL( routeDirty() ),
              d->m_routingManager, SLOT( updateRoute() ) );
     connect( d->m_routingLayer, SIGNAL( placemarkSelected( QModelIndex ) ),
@@ -231,8 +233,14 @@
     connect( d->m_ui.routeComboBox, SIGNAL( currentIndexChanged( int ) ),
              this, SLOT( switchRoute( int ) ) );
 
+    for( int i=0; i<d->m_routeRequest->size(); ++i ) {
+        insertInputWidget( i );
+    }
+
+    for ( int i=0; i<2 && d->m_inputWidgets.size()<2; ++i ) {
+        // Start with source and destination if the route is empty yet
     addInputWidget();
-    addInputWidget(); // Start with source and destination
+    }
     d->m_ui.routePreferenceComboBox->setVisible( false );
     d->m_ui.highwaysCheckBox->setVisible( false );
     d->m_ui.tollWaysCheckBox->setVisible( false );
@@ -362,9 +370,7 @@
 
 void RoutingWidget::addInputWidget()
 {
-    int index = d->m_ui.routingLayout->count() - 4;
     d->m_routeRequest->append( GeoDataCoordinates() );
-    insertInputWidget( index );
 }
 
 void RoutingWidget::insertInputWidget( int index )
@@ -526,13 +532,9 @@
     if ( index >= 0 )
     {
         Q_ASSERT( index < d->m_ui.routeComboBox->count() );
-        GeoDataDocument* route = d->m_routingManager->alternativeRoutesModel()->route( index );
-        if ( route ) {
-            d->m_routingManager->routingModel()->importGeoDataDocument( route );
-            d->m_widget->repaint();
+        d->m_routingManager->alternativeRoutesModel()->setCurrentRoute( index );
         }
     }
-}
 
 void RoutingWidget::updateAlternativeRoutes()
 {


More information about the Marble-commits mailing list