Marble

MarbleLocale.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2009 Torsten Rahn <tackat@kde.org>
4// SPDX-FileCopyrightText: 2015 Ilya Kowalewski <illya.kovalevskyy@gmail.com>
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
16namespace Marble
17{
18
19MarbleLocalePrivate::MarbleLocalePrivate()
20 : m_measurementSystem( MarbleLocale::MetricSystem )
21{
22}
23
24MarbleLocalePrivate::~MarbleLocalePrivate()
25{
26}
27
28MarbleLocale::MarbleLocale()
29 : d ( new MarbleLocalePrivate )
30{
31}
32
33MarbleLocale::~MarbleLocale()
34{
35 delete d;
36}
37
38void MarbleLocale::setMeasurementSystem( MarbleLocale::MeasurementSystem measurementSystem )
39{
40 d->m_measurementSystem = measurementSystem;
41}
42
43MarbleLocale::MeasurementSystem MarbleLocale::measurementSystem() const
44{
45 return d->m_measurementSystem;
46}
47
48void 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() << "Unknown measurement system!";
112 }
113}
114
115QString MarbleLocale::unitAbbreviation(MeasureUnit unit)
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
141QString 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}
Binds a QML item to a specific geodetic location in screen coordinates.
QString name() const const
QLocale system()
QString tr(const char *sourceText, const char *disambiguation, int n)
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
QString left(qsizetype n) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 21 2024 12:00:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.