File indexing completed on 2024-05-05 07:49:54
0001 // SPDX-License-Identifier: LGPL-2.1-or-later 0002 // 0003 // SPDX-FileCopyrightText: 2012, 2013 Bernhard Beschow <bbeschow@cs.tu-berlin.de> 0004 0005 #include "RoutingRunner.h" 0006 0007 #include "GeoDataExtendedData.h" 0008 #include "GeoDataData.h" 0009 #include "MarbleGlobal.h" 0010 #include "MarbleLocale.h" 0011 0012 #include <QTime> 0013 #include <QString> 0014 #include <QVariant> 0015 0016 namespace Marble 0017 { 0018 0019 RoutingRunner::RoutingRunner( QObject *parent ) : 0020 QObject( parent ) 0021 { 0022 } 0023 0024 const QString RoutingRunner::lengthString(qreal length) const 0025 { 0026 MarbleLocale::MeasurementSystem const measurementSystem = MarbleGlobal::getInstance()->locale()->measurementSystem(); 0027 0028 int precision = 0; 0029 QString distanceUnit = tr( "m" ); 0030 0031 switch ( measurementSystem ) { 0032 case MarbleLocale::ImperialSystem: 0033 precision = 1; 0034 distanceUnit = tr( "mi" ); 0035 length *= METER2KM * KM2MI; 0036 if ( length < 0.1 ) { 0037 length = 10 * qRound( length * 528 ); 0038 precision = 0; 0039 distanceUnit = tr( "ft" ); 0040 } 0041 break; 0042 case MarbleLocale::MetricSystem: 0043 if ( length >= 1000 ) { 0044 length *= METER2KM; 0045 distanceUnit = tr( "km" ); 0046 precision = 1; 0047 } else if ( length >= 200 ) { 0048 length = 50 * qRound( length / 50 ); 0049 } else if ( length >= 100 ) { 0050 length = 25 * qRound( length / 25 ); 0051 } else { 0052 length = 10 * qRound( length / 10 ); 0053 } 0054 break; 0055 case MarbleLocale::NauticalSystem: { 0056 length *= METER2KM * KM2NM; 0057 distanceUnit = tr( "nm" ); 0058 precision = length < 2.0 ? 2 : 1; 0059 } 0060 break; 0061 } 0062 0063 return QString( "%1 %2" ).arg( length, 0, 'f', precision ).arg( distanceUnit ); 0064 } 0065 0066 const QString RoutingRunner::durationString(const QTime& duration) const 0067 { 0068 const QString hoursString = duration.toString( "hh" ); 0069 const QString minutesString = duration.toString( "mm" ); 0070 const QString timeString = tr("%1:%2 h","journey duration").arg( hoursString, minutesString ); 0071 return timeString; 0072 } 0073 0074 const QString RoutingRunner::nameString(const QString& name, qreal length, const QTime& duration) const 0075 { 0076 const QString result = "%1; %2 (%3)"; 0077 return result.arg( lengthString( length ), durationString( duration ), name ); 0078 } 0079 0080 const GeoDataExtendedData RoutingRunner::routeData(qreal length, const QTime& duration) const 0081 { 0082 GeoDataExtendedData result; 0083 GeoDataData lengthData; 0084 lengthData.setName(QStringLiteral("length")); 0085 lengthData.setValue( length ); 0086 result.addValue( lengthData ); 0087 GeoDataData durationData; 0088 durationData.setName(QStringLiteral("duration")); 0089 durationData.setValue( duration.toString( Qt::ISODate ) ); 0090 result.addValue( durationData ); 0091 return result; 0092 } 0093 0094 } 0095 0096 #include "moc_RoutingRunner.cpp"