Marble

RoutingRunner.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2012, 2013 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
4
5#include "RoutingRunner.h"
6
7#include "GeoDataData.h"
8#include "GeoDataExtendedData.h"
9#include "MarbleGlobal.h"
10#include "MarbleLocale.h"
11
12#include <QString>
13#include <QTime>
14#include <QVariant>
15
16namespace Marble
17{
18
19RoutingRunner::RoutingRunner(QObject *parent)
20 : QObject(parent)
21{
22}
23
24const QString RoutingRunner::lengthString(qreal length) const
25{
26 MarbleLocale::MeasurementSystem const measurementSystem = MarbleGlobal::getInstance()->locale()->measurementSystem();
27
28 int precision = 0;
29 QString distanceUnit = tr("m");
30
31 switch (measurementSystem) {
32 case MarbleLocale::ImperialSystem:
33 precision = 1;
34 distanceUnit = tr("mi");
35 length *= METER2KM * KM2MI;
36 if (length < 0.1) {
37 length = 10 * qRound(length * 528);
38 precision = 0;
39 distanceUnit = tr("ft");
40 }
41 break;
42 case MarbleLocale::MetricSystem:
43 if (length >= 1000) {
44 length *= METER2KM;
45 distanceUnit = tr("km");
46 precision = 1;
47 } else if (length >= 200) {
48 length = 50 * qRound(length / 50);
49 } else if (length >= 100) {
50 length = 25 * qRound(length / 25);
51 } else {
52 length = 10 * qRound(length / 10);
53 }
54 break;
55 case MarbleLocale::NauticalSystem: {
56 length *= METER2KM * KM2NM;
57 distanceUnit = tr("nm");
58 precision = length < 2.0 ? 2 : 1;
59 } break;
60 }
61
62 return QStringLiteral("%1 %2").arg(length, 0, 'f', precision).arg(distanceUnit);
63}
64
65const QString RoutingRunner::durationString(const QTime &duration) const
66{
67 const QString hoursString = duration.toString("hh");
68 const QString minutesString = duration.toString("mm");
69 const QString timeString = tr("%1:%2 h", "journey duration").arg(hoursString, minutesString);
70 return timeString;
71}
72
73const QString RoutingRunner::nameString(const QString &name, qreal length, const QTime &duration) const
74{
75 const QString result = QStringLiteral("%1; %2 (%3)");
76 return result.arg(lengthString(length), durationString(duration), name);
77}
78
79const GeoDataExtendedData RoutingRunner::routeData(qreal length, const QTime &duration) const
80{
81 GeoDataExtendedData result;
82 GeoDataData lengthData;
83 lengthData.setName(QStringLiteral("length"));
84 lengthData.setValue(length);
85 result.addValue(lengthData);
86 GeoDataData durationData;
87 durationData.setName(QStringLiteral("duration"));
88 durationData.setValue(duration.toString(Qt::ISODate));
89 result.addValue(durationData);
90 return result;
91}
92
93}
94
95#include "moc_RoutingRunner.cpp"
KCALUTILS_EXPORT QString durationString(const KCalendarCore::Incidence::Ptr &incidence)
Binds a QML item to a specific geodetic location in screen coordinates.
QString arg(Args &&... args) const const
QString toString(QStringView format) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:55:54 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.