KiCad PCB EDA Suite
Loading...
Searching...
No Matches
units_provider.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef UNITS_PROVIDER_H
21#define UNITS_PROVIDER_H
22
23#include <eda_units.h>
24#include <origin_transforms.h>
25#include <core/minoptmax.h>
26#include <optional>
27#include <utility>
28
29
31{
32public:
33 UNITS_PROVIDER( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits ) :
34 m_iuScale( aIuScale ),
35 m_userUnits( aUnits )
36 {}
37
39 {}
40
42 void SetUserUnits( EDA_UNITS aUnits ) { m_userUnits = aUnits; }
43
44 virtual void GetUnitPair( EDA_UNITS& aPrimaryUnit, EDA_UNITS& aSecondaryUnits )
45 {
46 aPrimaryUnit = GetUserUnits();
47 aSecondaryUnits = EDA_UNIT_UTILS::IsImperialUnit( aPrimaryUnit ) ? EDA_UNITS::MM
49 }
50
51 const EDA_IU_SCALE& GetIuScale() const { return m_iuScale; }
52 // No SetIuScale(); scale is invariant
53
55 {
56 static ORIGIN_TRANSFORMS identityTransform;
57
58 return identityTransform;
59 }
60
75 wxString StringFromValue( double aValue, bool aAddUnitLabel = false,
77 {
79 aAddUnitLabel, aType );
80 }
81
96 wxString StringFromOptionalValue( std::optional<int> aValue, bool aAddUnitLabel = false,
98 {
99 if( !aValue )
100 {
101 return NullUiString;
102 }
103 else
104 {
106 aValue.value(), aAddUnitLabel, aType );
107 }
108 }
109
110 wxString StringFromValue( const EDA_ANGLE& aValue, bool aAddUnitLabel = false ) const
111 {
113 aValue.AsDegrees(), aAddUnitLabel,
115 }
116
123 wxString MessageTextFromValue( double aValue, bool aAddUnitLabel = true,
125 {
127 aValue, aAddUnitLabel, aType );
128 }
129
130 wxString MessageTextFromValue( const EDA_ANGLE& aValue, bool aAddUnitLabel = true ) const
131 {
133 aValue.AsDegrees(), aAddUnitLabel,
135 }
136
137 wxString MessageTextFromUnscaledValue( double aValue, bool aAddUnitLabel = true,
139 {
141 aValue, aAddUnitLabel, aType );
142 }
143
146 {
148 };
149
159 int ValueFromString( const wxString& aTextValue, EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) const
160 {
162 aTextValue, aType );
163
164
165 return KiROUND<double, int>( value );
166 }
167
178 std::optional<int> OptionalValueFromString( const wxString& aTextValue,
180 {
181 // Handle null (empty) values
182 if( aTextValue == NullUiString )
183 return {};
184
186 aTextValue, aType );
187
188 return KiROUND<double, int>( value );
189 }
190
191 EDA_ANGLE AngleValueFromString( const wxString& aTextValue ) const
192 {
194 aTextValue );
195
196 return EDA_ANGLE( angle, DEGREES_T );
197 }
198
203 {
204 // Get the unit type depending on the requested unit type and the underlying user setting
205 if( aType == EDA_DATA_TYPE::TIME )
206 {
207 return EDA_UNITS::PS;
208 }
209 else if( aType == EDA_DATA_TYPE::LENGTH_DELAY )
210 {
213 else
215 }
216
217 return GetUserUnits();
218 }
219
225 {
226 switch( aUnits )
227 {
228 case EDA_UNITS::INCH:
229 case EDA_UNITS::MM:
230 case EDA_UNITS::UM:
231 case EDA_UNITS::CM:
232 case EDA_UNITS::MILS:
238 case EDA_UNITS::FS:
239 case EDA_UNITS::PS:
240 return EDA_DATA_TYPE::TIME;
245 }
246
247 wxString msg = wxString::Format( wxT( "Unhandled unit data type %d" ), static_cast<int>( aUnits ) );
248 wxCHECK_MSG( false, EDA_DATA_TYPE::UNITLESS, msg );
249 return EDA_DATA_TYPE::UNITLESS; // Note that this is unreachable but g++-12 doesn't know that.
250 }
251
253 static inline const wxString NullUiString = "";
254
255private:
258};
259
260#endif // UNITS_PROVIDER_H
constexpr EDA_IU_SCALE unityScale
Definition base_units.h:124
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
double AsDegrees() const
Definition eda_angle.h:116
A class to perform either relative or absolute display origin transforms for a single axis of a point...
wxString MessageTextFromValue(const EDA_ANGLE &aValue, bool aAddUnitLabel=true) const
EDA_UNITS m_userUnits
EDA_ANGLE AngleValueFromString(const wxString &aTextValue) const
const EDA_IU_SCALE & m_iuScale
const EDA_IU_SCALE & GetIuScale() const
virtual void GetUnitPair(EDA_UNITS &aPrimaryUnit, EDA_UNITS &aSecondaryUnits)
EDA_UNITS GetUnitsFromType(EDA_DATA_TYPE aType) const
Gets the units to use in the conversion based on the underlying user units.
wxString MessageTextFromUnscaledValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
static const wxString NullUiString
The string that is used in the UI to represent a null value.
virtual ~UNITS_PROVIDER()
std::optional< int > OptionalValueFromString(const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aTextValue in aUnits to internal units used by the frame.
virtual ORIGIN_TRANSFORMS & GetOriginTransforms()
UNITS_PROVIDER(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits)
wxString StringFromOptionalValue(std::optional< int > aValue, bool aAddUnitLabel=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts an optional aValue in internal units into a united string.
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
A lower-precision version of StringFromValue().
wxString MessageTextFromMinOptMax(const MINOPTMAX< int > &aValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
EDA_UNITS GetUserUnits() const
void SetUserUnits(EDA_UNITS aUnits)
static EDA_DATA_TYPE GetTypeFromUnits(const EDA_UNITS aUnits)
Gets the inferred type from the given units.
wxString StringFromValue(double aValue, bool aAddUnitLabel=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aValue in internal units into a united string.
wxString StringFromValue(const EDA_ANGLE &aValue, bool aAddUnitLabel=false) const
int ValueFromString(const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aTextValue in aUnits to internal units used by the frame.
@ DEGREES_T
Definition eda_angle.h:31
EDA_DATA_TYPE
The type of unit.
Definition eda_units.h:34
EDA_UNITS
Definition eda_units.h:44
@ PS_PER_INCH
Definition eda_units.h:55
KICOMMON_API wxString MessageTextFromValue(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, double aValue, bool aAddUnitsText=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A helper to convert the double length aValue to a string in inches, millimeters, or unscaled units.
KICOMMON_API wxString StringFromValue(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, double aValue, bool aAddUnitsText=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Return the string from aValue according to aUnits (inch, mm ...) for display.
KICOMMON_API wxString MessageTextFromMinOptMax(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const MINOPTMAX< int > &aValue)
KICOMMON_API double DoubleValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Convert aTextValue to a double.
KICOMMON_API bool IsImperialUnit(EDA_UNITS aUnit)
Definition eda_units.cpp:43
KICOMMON_API bool IsMetricUnit(EDA_UNITS aUnit)
Definition eda_units.cpp:57