Marble

RoutingRunner.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2012, 2013 Bernhard Beschow <[email protected]>
4 
5 #include "RoutingRunner.h"
6 
7 #include "GeoDataExtendedData.h"
8 #include "GeoDataData.h"
9 #include "MarbleGlobal.h"
10 #include "MarbleLocale.h"
11 
12 #include <QTime>
13 #include <QString>
14 #include <QVariant>
15 
16 namespace Marble
17 {
18 
19 RoutingRunner::RoutingRunner( QObject *parent ) :
20  QObject( parent )
21 {
22 }
23 
24 const 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  }
60  break;
61  }
62 
63  return QString( "%1 %2" ).arg( length, 0, 'f', precision ).arg( distanceUnit );
64 }
65 
66 const QString RoutingRunner::durationString(const QTime& duration) const
67 {
68  const QString hoursString = duration.toString( "hh" );
69  const QString minutesString = duration.toString( "mm" );
70  const QString timeString = tr("%1:%2 h","journey duration").arg( hoursString, minutesString );
71  return timeString;
72 }
73 
74 const QString RoutingRunner::nameString(const QString& name, qreal length, const QTime& duration) const
75 {
76  const QString result = "%1; %2 (%3)";
77  return result.arg( lengthString( length ), durationString( duration ), name );
78 }
79 
80 const GeoDataExtendedData RoutingRunner::routeData(qreal length, const QTime& duration) const
81 {
82  GeoDataExtendedData result;
83  GeoDataData lengthData;
84  lengthData.setName(QStringLiteral("length"));
85  lengthData.setValue( length );
86  result.addValue( lengthData );
87  GeoDataData durationData;
88  durationData.setName(QStringLiteral("duration"));
89  durationData.setValue( duration.toString( Qt::ISODate ) );
90  result.addValue( durationData );
91  return result;
92 }
93 
94 }
95 
96 #include "moc_RoutingRunner.cpp"
Binds a QML item to a specific geodetic location in screen coordinates.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
KCALUTILS_EXPORT QString durationString(const KCalendarCore::Incidence::Ptr &incidence)
QString toString(Qt::DateFormat format) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Sep 21 2023 04:12:28 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.