Marble

MarbleLocale.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2009 Torsten Rahn <[email protected]>
4 // SPDX-FileCopyrightText: 2015 Ilya Kowalewski <[email protected]>
5 //
6 
7 #include "MarbleLocale.h"
8 #include "MarbleLocale_p.h"
9 #include "MarbleGlobal.h"
10 #include "MarbleDebug.h"
11 
12 // Qt
13 #include <QLocale>
14 
15 
16 namespace Marble
17 {
18 
19 MarbleLocalePrivate::MarbleLocalePrivate()
20  : m_measurementSystem( MarbleLocale::MetricSystem )
21 {
22 }
23 
24 MarbleLocalePrivate::~MarbleLocalePrivate()
25 {
26 }
27 
28 MarbleLocale::MarbleLocale()
29  : d ( new MarbleLocalePrivate )
30 {
31 }
32 
33 MarbleLocale::~MarbleLocale()
34 {
35  delete d;
36 }
37 
38 void MarbleLocale::setMeasurementSystem( MarbleLocale::MeasurementSystem measurementSystem )
39 {
40  d->m_measurementSystem = measurementSystem;
41 }
42 
43 MarbleLocale::MeasurementSystem MarbleLocale::measurementSystem() const
44 {
45  return d->m_measurementSystem;
46 }
47 
48 void MarbleLocale::meterToTargetUnit(qreal meters, MeasurementSystem targetSystem,
49  qreal &targetValue, MeasureUnit &targetUnit)
50 {
51  targetValue = meters;
52 
53  switch (targetSystem) {
54  case MetricSystem: {
55  if (targetValue > 1000.0) {
56  targetValue *= METER2KM;
57  targetUnit = Kilometer;
58  break;
59  }
60 
61  if (targetValue < 1.0 && targetValue >= 0.01) {
62  targetValue *= M2CM;
63  targetUnit = Centimeter;
64  break;
65  }
66 
67  if (targetValue < 0.01) {
68  targetValue *= M2MM;
69  targetUnit = Milimeter;
70  break;
71  }
72 
73  targetUnit = Meter;
74  break;
75  }
76 
77  case ImperialSystem: {
78  // meters into feet
79  qreal ftValue = targetValue * M2FT;
80 
81  if (ftValue < 1) {
82  targetValue *= M2IN;
83  targetUnit = Inch;
84  break;
85  }
86 
87  if (ftValue >= 3 && ftValue < 5280.0) {
88  targetValue *= M2YD;
89  targetUnit = Yard;
90  break;
91  }
92 
93  if (ftValue >= 5280.0) {
94  targetValue *= METER2KM * KM2MI;
95  targetUnit = Mile;
96  break;
97  }
98 
99  targetValue *= M2FT;
100  targetUnit = Foot;
101  break;
102  }
103 
104  case NauticalSystem: {
105  targetValue *= METER2KM * KM2NM;
106  targetUnit = NauticalMile;
107  break;
108  }
109 
110  default:
111  qWarning() << Q_FUNC_INFO << "Unknown measurement system!";
112  }
113 }
114 
116 {
117  switch (unit) {
118  case Meter:
119  return QObject::tr("m", "means meter");
120  case Milimeter:
121  return QObject::tr("mm", "means milimeters");
122  case Kilometer:
123  return QObject::tr("km", "means kilometers");
124  case Centimeter:
125  return QObject::tr("cm", "means centimeters");
126  case Foot:
127  return QObject::tr("ft", "means feet");
128  case Inch:
129  return QObject::tr("in", "means inches");
130  case Yard:
131  return QObject::tr("yd", "means yards");
132  case Mile:
133  return QObject::tr("mi", "means miles");
134  case NauticalMile:
135  return QObject::tr("nm", "means nautical miles");
136  default:
137  return "";
138  }
139 }
140 
141 QString MarbleLocale::languageCode()
142 {
143  const QString lang = QLocale::system().name();
144  QString code;
145 
146  int index = lang.indexOf(QLatin1Char('_'));
147  if (lang == QLatin1String("C")) {
148  code = "en";
149  }
150  else if ( index != -1 ) {
151  code = lang.left ( index );
152  }
153  else {
154  index = lang.indexOf(QLatin1Char('@'));
155  if ( index != -1 )
156  code = lang.left ( index );
157  else
158  code = lang;
159  }
160 
161  return code;
162 }
163 
164 }
static void meterToTargetUnit(qreal meters, MeasurementSystem targetSystem, qreal &targetValue, MeasureUnit &targetUnit)
meterToCustomUnit Converts meters to targetValue of targetUnit in targetSystem
QLocale system()
static QString unitAbbreviation(MeasureUnit unit)
unitAbbreviation Represents a short string form of unit, e.g.
QString name() const const
Binds a QML item to a specific geodetic location in screen coordinates.
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString left(int n) const const
QString tr(const char *sourceText, const char *disambiguation, int n)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Sep 21 2023 04:12:27 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.