Debian Codegraph @

src/Base/UnitsSchema.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /************************************************************************
4 * *
5 * This file is part of the FreeCAD CAx development system. *
6 * *
7 * This library is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU Library General Public *
9 * License as published by the Free Software Foundation; either *
10 * version 2 of the License, or (at your option) any later version. *
11 * *
12 * This library is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Library General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Library General Public *
18 * License along with this library; see the file COPYING.LIB. If not, *
19 * write to the Free Software Foundation, Inc., 59 Temple Place, *
20 * Suite 330, Boston, MA 02111-1307, USA *
21 * *
22 ************************************************************************/
23
24 #include <cmath>
25 #include <iomanip>
26 #include <sstream>
27 #include <string>
28
29 #include <QLocale>
30 #include <QString>
31
32 #include "Quantity.h"
33 #include "UnitsSchema.h"
34 #include "UnitsSchemasData.h"
35 #include "UnitsSchemasSpecs.h"
36 #include "Exception.h"
37 #include "Quantity.h"
38
39 using Base::UnitsSchema;
40 using Base::UnitsSchemaSpec;
41
42
43 UnitsSchema::UnitsSchema(UnitsSchemaSpec spec)
44 : spec {std::move(spec)}
45 {}
46
47 std::string UnitsSchema::translate(const Quantity& quant) const
48 { // to satisfy GCC
49 double dummy1 {};
50 std::string dummy2;
51 return translate(quant, dummy1, dummy2);
52 }
53
54 std::string UnitsSchema::translate(const Quantity& quant, double& factor, std::string& unitString) const
55 {
56 // Use defaults without schema-level translation.
57 factor = 1.0;
58 unitString = quant.getUnit().getString();
59
60 if (spec.translationSpecs.empty()) {
61 return toLocale(quant, factor, unitString);
62 }
63
64 const auto unitName = quant.getUnit().getTypeString();
65 if (!spec.translationSpecs.contains(unitName)) {
66 return toLocale(quant, factor, unitString);
67 }
68
69 const auto value = quant.getValue();
70 const auto magnitude = std::abs(value);
71 auto isSuitable = [&](const UnitTranslationSpec& row) {
72 // Shrink threshold slightly so values at exact threshold boundaries
73 // (e.g. "1 S/m" = 1e-9 at threshold 1e-9) fall through to the next unit.
74 constexpr double relEps = 1e-12;
75 return row.threshold * (1.0 - relEps) > magnitude
76 || row.threshold == 0; // zero indicates default
77 };
78
79 auto unitSpecs = spec.translationSpecs.at(unitName);
80 const auto unitSpec = std::find_if(unitSpecs.begin(), unitSpecs.end(), isSuitable);
81 if (unitSpec == unitSpecs.end()) {
82 throw RuntimeError(
83 "Suitable threshold not found. Schema: " + spec.name + " value: " + std::to_string(value)
84 );
85 }
86
87 if (unitSpec->factor == 0) {
88 const QuantityFormat& format = quant.getFormat();
89 return UnitsSchemasData::runSpecial(
90 unitSpec->unitString,
91 value,
92 format.getPrecision(),
93 format.getDenominator(),
94 factor,
95 unitString
96 );
97 }
98
99 factor = unitSpec->factor;
100 unitString = unitSpec->unitString;
101
102 return toLocale(quant, factor, unitString);
103 }
104
105 std::string UnitsSchema::toLocale(const Quantity& quant, const double factor, const std::string& unitString)
106 {
107 QLocale Lc;
108 const QuantityFormat& format = quant.getFormat();
109 if (format.option != QuantityFormat::None) {
110 Lc.setNumberOptions(static_cast<QLocale::NumberOptions>(format.option));
111 }
112
113 auto valueString = Lc.toString(quant.getValue() / factor, format.toFormat(), format.getPrecision())
114 .toStdString();
115
116 auto notUnit = [](auto s) {
117 return s.empty() || s == "°" || s == "″" || s == "′" || s == "\"" || s == "'";
118 };
119
120 return fmt::format("{}{}{}", valueString, notUnit(unitString) ? "" : " ", unitString);
121 }
122
123 bool UnitsSchema::isMultiUnitLength() const
124 {
125 return spec.isMultUnitLen;
126 }
127
128 bool UnitsSchema::isMultiUnitAngle() const
129 {
130 return spec.isMultUnitAngle;
131 }
132
133 std::string UnitsSchema::getBasicLengthUnit() const
134 {
135 return spec.basicLengthUnitStr;
136 }
137
138 std::string UnitsSchema::getName() const
139 {
140 return spec.name;
141 }
142
143 std::string UnitsSchema::getDescription() const
144 {
145 return spec.description;
146 }
147
148 int UnitsSchema::getNum() const
149 {
150 return static_cast<int>(spec.num);
151 }