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

Dennis Nienhüser earthwings at gentoo.org
Sun May 9 00:20:18 CEST 2010


SVN commit 1124343 by nienhueser:

The total duration can include days. Properly parse and display it.

 M  +10 -7     RoutingModel.cpp  
 M  +15 -2     RoutingModel.h  
 M  +8 -2      RoutingWidget.cpp  


--- trunk/KDE/kdeedu/marble/src/lib/routing/RoutingModel.cpp #1124342:1124343
@@ -21,7 +21,6 @@
 
 #include <QtCore/QBuffer>
 #include <QtCore/QRegExp>
-#include <QtCore/QTime>
 #include <QtCore/QVector>
 #include <QtGui/QPixmap>
 #include <QtXml/QDomDocument>
@@ -43,7 +42,7 @@
 public:
     RouteElements m_route;
 
-    QTime m_totalTime;
+    RoutingModel::Duration m_totalDuration;
 
     qreal m_totalDistance;
 
@@ -168,11 +167,14 @@
         QDomNodeList time = summary.item( 0 ).toElement().elementsByTagName( "xls:TotalTime" );
         QDomNodeList distance = summary.item( 0 ).toElement().elementsByTagName( "xls:TotalDistance" );
         if ( time.size() == 1 && distance.size() == 1 ) {
-            QRegExp regexp = QRegExp( "^PT(?:(\\d+)H)?(?:(\\d+)M)?(\\d+)S" );
+            QRegExp regexp = QRegExp( "^P(?:(\\d+)D)?T(?:(\\d+)H)?(?:(\\d+)M)?(\\d+)S" );
             if ( regexp.indexIn( time.item( 0 ).toElement().text() ) == 0 ) {
                 QStringList matches = regexp.capturedTexts();
-                int hours( 0 ), minutes( 0 ), seconds( 0 );
+                int days( 0 ), hours( 0 ), minutes( 0 ), seconds( 0 );
                 switch ( matches.size() ) {
+                case 5:
+                    days    = regexp.cap( matches.size() - 4 ).toInt();
+                    // Intentionally no break
                 case 4:
                     hours   = regexp.cap( matches.size() - 3 ).toInt();
                     // Intentionally no break
@@ -186,7 +188,8 @@
                     mDebug() << "Unable to parse time string " << time.item( 0 ).toElement().text();
                 }
 
-                d->m_totalTime = QTime( hours, minutes, seconds, 0 );
+                d->m_totalDuration.days = days;
+                d->m_totalDuration.time = QTime( hours, minutes, seconds, 0 );
                 d->m_totalDistance = distance.item( 0 ).attributes().namedItem( "value" ).nodeValue().toDouble();
                 QString unit = distance.item( 0 ).attributes().namedItem( "uom" ).nodeValue();
                 if ( unit == "M" ) {
@@ -236,9 +239,9 @@
     reset();
 }
 
-QTime RoutingModel::totalTime() const
+RoutingModel::Duration RoutingModel::duration() const
 {
-    return d->m_totalTime;
+    return d->m_totalDuration;
 }
 
 qreal RoutingModel::totalDistance() const
--- trunk/KDE/kdeedu/marble/src/lib/routing/RoutingModel.h #1124342:1124343
@@ -15,6 +15,7 @@
 #include "MarblePlacemarkModel.h"
 
 #include <QtCore/QAbstractListModel>
+#include <QtCore/QTime>
 
 /**
   * A QAbstractItemModel that contains a list of routing instructions.
@@ -34,6 +35,18 @@
     Q_OBJECT
 
 public:
+    struct Duration
+    {
+        unsigned int days;
+        QTime time;
+
+        explicit Duration( unsigned int days_ = 0, const QTime &time_ = QTime() )
+            :days(days_), time(time_)
+        {
+            // nothing to do
+        }
+    };
+
     enum RoutingItemDataRole {
         CoordinateRole = MarblePlacemarkModel::CoordinateRole, // synchronized with MarblePlacemarkModel
         TypeRole = CoordinateRole + 24 // avoid conflict with MarblePlacemarkModel
@@ -82,9 +95,9 @@
 
     /**
       * Returns the total (estimated) time it takes to travel from
-      * source to destination
+-     * source to destination
       */
-    QTime totalTime() const;
+    Duration duration() const;
 
     /**
       * Returns the total route distance (kilometer)
--- trunk/KDE/kdeedu/marble/src/lib/routing/RoutingWidget.cpp #1124342:1124343
@@ -373,11 +373,17 @@
         }
 
         if ( bbox.size() > 1 ) {
-            QString label = tr( "Estimated travel time: %1 (%2 km)" );
             qreal distance = d->m_routingManager->routingModel()->totalDistance();
-            QTime time = d->m_routingManager->routingModel()->totalTime();
+            unsigned int days = d->m_routingManager->routingModel()->duration().days;
+            QTime time = d->m_routingManager->routingModel()->duration().time;
             QString timeString = time.toString( Qt::DefaultLocaleShortDate );
+            if ( days ) {
+                QString label = tr( "Estimated travel time: %1 days, %2 (%3 km)", 0, days );
+                d->m_ui.descriptionLabel->setText( label.arg( days ).arg( timeString ).arg( distance, 0, 'f', 1 ) );
+            } else {
+                QString label = tr( "Estimated travel time: %1 (%2 km)" );
             d->m_ui.descriptionLabel->setText( label.arg( timeString ).arg( distance, 0, 'f', 1 ) );
+            }
             d->m_ui.descriptionLabel->setVisible( true );
 
             if ( d->m_zoomRouteAfterDownload ) {


More information about the Marble-commits mailing list