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 "MarbleDebug.h"
9#include "MarbleGlobal.h"
10#include "MarbleLocale_p.h"
11
12// Qt
13#include <QLocale>
14
15namespace Marble
16{
17
18MarbleLocalePrivate::MarbleLocalePrivate()
19 : m_measurementSystem(MarbleLocale::MetricSystem)
20{
21}
22
23MarbleLocalePrivate::~MarbleLocalePrivate() = default;
24
25MarbleLocale::MarbleLocale()
26 : d(new MarbleLocalePrivate)
27{
28}
29
30MarbleLocale::~MarbleLocale()
31{
32 delete d;
33}
34
35void MarbleLocale::setMeasurementSystem(MarbleLocale::MeasurementSystem measurementSystem)
36{
37 d->m_measurementSystem = measurementSystem;
38}
39
40MarbleLocale::MeasurementSystem MarbleLocale::measurementSystem() const
41{
42 return d->m_measurementSystem;
43}
44
45void MarbleLocale::meterToTargetUnit(qreal meters, MeasurementSystem targetSystem, qreal &targetValue, MeasureUnit &targetUnit)
46{
47 targetValue = meters;
48
49 switch (targetSystem) {
50 case MetricSystem: {
51 if (targetValue > 1000.0) {
52 targetValue *= METER2KM;
53 targetUnit = Kilometer;
54 break;
55 }
56
57 if (targetValue < 1.0 && targetValue >= 0.01) {
58 targetValue *= M2CM;
59 targetUnit = Centimeter;
60 break;
61 }
62
63 if (targetValue < 0.01) {
64 targetValue *= M2MM;
65 targetUnit = Milimeter;
66 break;
67 }
68
69 targetUnit = Meter;
70 break;
71 }
72
73 case ImperialSystem: {
74 // meters into feet
75 qreal ftValue = targetValue * M2FT;
76
77 if (ftValue < 1) {
78 targetValue *= M2IN;
79 targetUnit = Inch;
80 break;
81 }
82
83 if (ftValue >= 3 && ftValue < 5280.0) {
84 targetValue *= M2YD;
85 targetUnit = Yard;
86 break;
87 }
88
89 if (ftValue >= 5280.0) {
90 targetValue *= METER2KM * KM2MI;
91 targetUnit = Mile;
92 break;
93 }
94
95 targetValue *= M2FT;
96 targetUnit = Foot;
97 break;
98 }
99
100 case NauticalSystem: {
101 targetValue *= METER2KM * KM2NM;
102 targetUnit = NauticalMile;
103 break;
104 }
105
106 default:
107 qWarning() << "Unknown measurement system!";
108 }
109}
110
111QString MarbleLocale::unitAbbreviation(MeasureUnit unit)
112{
113 switch (unit) {
114 case Meter:
115 return QObject::tr("m", "means meter");
116 case Milimeter:
117 return QObject::tr("mm", "means milimeters");
118 case Kilometer:
119 return QObject::tr("km", "means kilometers");
120 case Centimeter:
121 return QObject::tr("cm", "means centimeters");
122 case Foot:
123 return QObject::tr("ft", "means feet");
124 case Inch:
125 return QObject::tr("in", "means inches");
126 case Yard:
127 return QObject::tr("yd", "means yards");
128 case Mile:
129 return QObject::tr("mi", "means miles");
130 case NauticalMile:
131 return QObject::tr("nm", "means nautical miles");
132 default:
133 return {};
134 }
135}
136
137QString MarbleLocale::languageCode()
138{
139 const QString lang = QLocale::system().name();
140 QString code;
141
142 int index = lang.indexOf(QLatin1Char('_'));
143 if (lang == QLatin1StringView("C")) {
144 code = QStringLiteral("en");
145 } else if (index != -1) {
146 code = lang.left(index);
147 } else {
148 index = lang.indexOf(QLatin1Char('@'));
149 if (index != -1)
150 code = lang.left(index);
151 else
152 code = lang;
153 }
154
155 return code;
156}
157
158}
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 Mon Nov 4 2024 16:37:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.