Debian Codegraph @

src/Base/UnitsSchemas.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 <algorithm>
25 #include <memory>
26 #include <string>
27 #include <vector>
28
29 #include <QCoreApplication>
30
31 #include "UnitsSchemas.h"
32 #include "Exception.h"
33 #include "Quantity.h"
34 #include "UnitsApi.h"
35 #include "UnitsSchema.h"
36 #include "UnitsSchemasSpecs.h"
37 #include "UnitsSchemasData.h"
38
39 using Base::Quantity;
40 using Base::UnitsSchema;
41 using Base::UnitsSchemas;
42 using Base::UnitsSchemaSpec;
43
44 UnitsSchemas::UnitsSchemas(const UnitsSchemasDataPack& pack)
45 : pack {pack}
46 , denominator {pack.defDenominator}
47 , decimals {pack.defDecimals}
48 {}
49
50 size_t UnitsSchemas::count() const
51 {
52 return pack.specs.size();
53 }
54
55 std::vector<std::string> UnitsSchemas::getVec(const std::function<std::string(UnitsSchemaSpec)>& fn)
56 {
57 std::vector<std::string> vec;
58 auto specs = pack.specs;
59 std::sort(specs.begin(), specs.end(), [](const UnitsSchemaSpec& a, const UnitsSchemaSpec& b) {
60 return a.num < b.num;
61 });
62 std::transform(specs.begin(), specs.end(), std::back_inserter(vec), fn);
63
64 return vec;
65 }
66
67 std::vector<std::string> UnitsSchemas::names()
68 {
69 return getVec([](const UnitsSchemaSpec& spec) { return spec.name; });
70 }
71
72 std::vector<std::string> UnitsSchemas::descriptions()
73 {
74 return getVec([](const UnitsSchemaSpec& spec) {
75 return QCoreApplication::translate("UnitsApi", spec.description).toStdString();
76 });
77 }
78
79 std::size_t UnitsSchemas::getDecimals() const
80 {
81 return pack.defDecimals;
82 }
83
84 std::size_t UnitsSchemas::defFractDenominator() const
85 {
86 return pack.defDenominator;
87 }
88
89 void UnitsSchemas::setdefFractDenominator(const std::size_t size)
90 {
91 denominator = size;
92 }
93
94 void UnitsSchemas::select()
95 {
96 makeCurr(spec());
97 }
98
99 void UnitsSchemas::select(const std::string_view& name)
100 {
101 makeCurr(spec(name));
102 }
103
104 void UnitsSchemas::select(const std::size_t num)
105 {
106 makeCurr(spec(num));
107 }
108
109 UnitsSchema* UnitsSchemas::currentSchema() const
110 {
111 return current.get();
112 }
113
114 void UnitsSchemas::makeCurr(const UnitsSchemaSpec& spec)
115 {
116 current = std::make_unique<UnitsSchema>(spec);
117 }
118
119 UnitsSchemaSpec UnitsSchemas::findSpec(const std::function<bool(UnitsSchemaSpec)>& fn)
120 {
121 const auto found = std::find_if(pack.specs.begin(), pack.specs.end(), fn);
122
123 if (found == pack.specs.end()) {
124 throw RuntimeError {"UnitSchemaSpec not found"};
125 }
126
127 return *found;
128 }
129
130 UnitsSchemaSpec UnitsSchemas::spec()
131 {
132 return findSpec([](const UnitsSchemaSpec& spec) { return spec.isDefault; });
133 }
134
135 UnitsSchemaSpec UnitsSchemas::spec(const std::string_view& name)
136 {
137 return findSpec([&name](const UnitsSchemaSpec& spec) { return spec.name == name; });
138 }
139
140 UnitsSchemaSpec UnitsSchemas::spec(const std::size_t num)
141 {
142 return findSpec([&num](const UnitsSchemaSpec& spec) { return spec.num == num; });
143 }