[Marble-commits] KDE/kdeedu/marble/src/lib/routing
Dennis Nienhüser
earthwings at gentoo.org
Sun Mar 21 22:56:14 CET 2010
SVN commit 1106124 by nienhueser:
Include route summary (total distance and estimated time) below the instructions list view.
M +58 -0 RoutingModel.cpp
M +11 -0 RoutingModel.h
M +12 -3 RoutingWidget.cpp
M +9 -2 RoutingWidget.ui
--- trunk/KDE/kdeedu/marble/src/lib/routing/RoutingModel.cpp #1106123:1106124
@@ -26,6 +26,7 @@
#include <QtCore/QVector>
#include <QtCore/QBuffer>
+#include <QtCore/QRegExp>
#include <QtGui/QPixmap>
#include <QtXml/QDomDocument>
@@ -46,9 +47,20 @@
public:
RouteElements m_route;
+ QTime m_totalTime;
+
+ qreal m_totalDistance;
+
+ RoutingModelPrivate();
+
RouteElement parseGmlPos(const QStringList &content) const;
};
+ RoutingModelPrivate::RoutingModelPrivate() : m_totalDistance(0.0)
+ {
+ // nothing to do
+ }
+
RouteElement RoutingModelPrivate::parseGmlPos(const QStringList &content) const
{
Q_ASSERT(content.length() == 2);
@@ -133,6 +145,42 @@
QDomElement root = xml.documentElement();
+ QDomNodeList summary = root.elementsByTagName("xls:RouteSummary");
+ if (summary.size() > 0) {
+ 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");
+ if (regexp.indexIn(time.item(0).toElement().text()) == 0) {
+ QStringList matches = regexp.capturedTexts();
+ int hours(0), minutes(0), seconds(0);
+ switch(matches.size()) {
+ case 4:
+ hours = regexp.cap(matches.size()-3).toInt();
+ // Intentionally no break
+ case 3:
+ minutes = regexp.cap(matches.size()-2).toInt();
+ // Intentionally no break
+ case 2:
+ seconds = regexp.cap(matches.size()-1).toInt();
+ break;
+ default:
+ qWarning() << "Unable to parse time string " << time.item(0).toElement().text();
+ }
+
+ d->m_totalTime = 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") {
+ d->m_totalDistance *= METER2KM;
+ }
+ else if (unit != "KM") {
+ qWarning() << "Cannot parse distance unit " << unit << ", treated as km.";
+ }
+ }
+ }
+ }
+
QDomNodeList geometry = root.elementsByTagName("xls:RouteGeometry");
if (geometry.size() > 0) {
QDomNodeList waypoints = geometry.item(0).toElement().elementsByTagName("gml:pos");
@@ -173,6 +221,16 @@
reset();
}
+ QTime RoutingModel::totalTime() const
+ {
+ return d->m_totalTime;
+ }
+
+ qreal RoutingModel::totalDistance() const
+ {
+ return d->m_totalDistance;
+ }
+
} // namespace Marble
#include "RoutingModel.moc"
--- trunk/KDE/kdeedu/marble/src/lib/routing/RoutingModel.h #1106123:1106124
@@ -77,6 +77,17 @@
*/
void importOpenGis( const QByteArray &xmlData);
+ /**
+ * Returns the total (estimated) time it takes to travel from
+ * source to destination
+ */
+ QTime totalTime() const;
+
+ /**
+ * Returns the total route distance (kilometer)
+ */
+ qreal totalDistance() const;
+
private:
RoutingModelPrivate* const d;
};
--- trunk/KDE/kdeedu/marble/src/lib/routing/RoutingWidget.cpp #1106123:1106124
@@ -19,8 +19,6 @@
#include "MarbleDebug.h"
#include "MarbleWidgetInputHandler.h"
-#include <QtGui/QSortFilterProxyModel>
-
#include "RoutingManager.h"
#include "RoutingLayer.h"
#include "RoutingModel.h"
@@ -28,6 +26,9 @@
#include "RoutingInputWidget.h"
#include "RouteSkeleton.h"
+#include <QtGui/QSortFilterProxyModel>
+#include <QtCore/QTime>
+
#include "ui_RoutingWidget.h"
namespace Marble {
@@ -175,6 +176,7 @@
d->m_ui.tollWaysCheckBox->setVisible(false);
d->m_ui.preferenceLabel->setVisible(false);
d->m_ui.avoidLabel->setVisible(false);
+ d->m_ui.descriptionLabel->setVisible(false);
}
RoutingWidget::~RoutingWidget()
@@ -267,6 +269,7 @@
if (placemarks.size() > 1) {
d->m_widget->centerOn(GeoDataLatLonBox::fromLineString(placemarks));
+ d->m_ui.descriptionLabel->setVisible(false);
}
}
@@ -291,7 +294,7 @@
void RoutingWidget::addInputWidget()
{
- int index = d->m_ui.routingLayout->count()-3;
+ int index = d->m_ui.routingLayout->count()-4;
d->m_routeSkeleton->append(GeoDataCoordinates());
insertInputWidget(index);
}
@@ -346,6 +349,12 @@
if (bbox.size()>1) {
d->m_widget->centerOn(GeoDataLatLonBox::fromLineString(bbox));
+ QString label = tr("Estimated travel time: %1 (%2 km)");
+ qreal distance = d->m_routingManager->routingModel()->totalDistance();
+ QTime time = d->m_routingManager->routingModel()->totalTime();
+ QString timeString = time.toString(Qt::DefaultLocaleShortDate);
+ d->m_ui.descriptionLabel->setText(label.arg(timeString).arg(distance, 0, 'f', 1));
+ d->m_ui.descriptionLabel->setVisible(true);
}
}
--- trunk/KDE/kdeedu/marble/src/lib/routing/RoutingWidget.ui #1106123:1106124
@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
- <width>267</width>
- <height>270</height>
+ <width>278</width>
+ <height>392</height>
</rect>
</property>
<layout class="QVBoxLayout" name="routingLayout">
@@ -153,6 +153,13 @@
</property>
</widget>
</item>
+ <item>
+ <widget class="QLabel" name="descriptionLabel">
+ <property name="text">
+ <string>Route Summary</string>
+ </property>
+ </widget>
+ </item>
</layout>
</widget>
<resources/>
More information about the Marble-commits
mailing list