KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifndef UNITS_PROVIDER_H
25#define UNITS_PROVIDER_H
26
27#include <eda_units.h>
28#include <origin_transforms.h>
29#include <core/minoptmax.h>
30#include <optional>
31#include <utility>
32
33
35{
36public:
37 UNITS_PROVIDER( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits ) :
38 m_iuScale( aIuScale ),
39 m_userUnits( aUnits )
40 {}
41
43 {}
44
46 void SetUserUnits( EDA_UNITS aUnits ) { m_userUnits = aUnits; }
47
48 virtual void GetUnitPair( EDA_UNITS& aPrimaryUnit, EDA_UNITS& aSecondaryUnits )
49 {
50 aPrimaryUnit = GetUserUnits();
51 aSecondaryUnits = EDA_UNIT_UTILS::IsImperialUnit( aPrimaryUnit ) ? EDA_UNITS::MM
52 : EDA_UNITS::MILS;
53 }
54
55 const EDA_IU_SCALE& GetIuScale() const { return m_iuScale; }
56 // No SetIuScale(); scale is invariant
57
59 {
60 static ORIGIN_TRANSFORMS identityTransform;
61
62 return identityTransform;
63 }
64
79 wxString StringFromValue( double aValue, bool aAddUnitLabel = false,
80 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) const
81 {
83 aAddUnitLabel, aType );
84 }
85
100 wxString StringFromOptionalValue( std::optional<int> aValue, bool aAddUnitLabel = false,
101 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) const
102 {
103 if( !aValue )
104 return NullUiString;
105 else
107 aValue.value(), aAddUnitLabel, aType );
108 }
109
110 wxString StringFromValue( const EDA_ANGLE& aValue, bool aAddUnitLabel = false ) const
111 {
112 return EDA_UNIT_UTILS::UI::StringFromValue( unityScale, EDA_UNITS::DEGREES,
113 aValue.AsDegrees(), aAddUnitLabel,
114 EDA_DATA_TYPE::DISTANCE );
115 }
116
123 wxString MessageTextFromValue( double aValue, bool aAddUnitLabel = true,
124 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) const
125 {
127 aAddUnitLabel, aType );
128 }
129
130 wxString MessageTextFromValue( const EDA_ANGLE& aValue, bool aAddUnitLabel = true ) const
131 {
132 return EDA_UNIT_UTILS::UI::MessageTextFromValue( unityScale, EDA_UNITS::DEGREES,
133 aValue.AsDegrees(), aAddUnitLabel,
134 EDA_DATA_TYPE::DISTANCE );
135 }
136
137 wxString MessageTextFromMinOptMax( const MINOPTMAX<int>& aValue ) const
138 {
140 };
141
151 int ValueFromString( const wxString& aTextValue,
152 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) const
153 {
155 aTextValue, aType );
156
157 return KiROUND<double, int>( value );
158 }
159
170 std::optional<int>
171 OptionalValueFromString( const wxString& aTextValue,
172 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) const
173 {
174 // Handle null (empty) values
175 if( aTextValue == NullUiString )
176 return {};
177
179 aTextValue, aType );
180
181 return KiROUND<double, int>( value );
182 }
183
184 EDA_ANGLE AngleValueFromString( const wxString& aTextValue ) const
185 {
186 double angle = EDA_UNIT_UTILS::UI::DoubleValueFromString( GetIuScale(), EDA_UNITS::DEGREES,
187 aTextValue );
188
189 return EDA_ANGLE( angle, DEGREES_T );
190 }
191
193 static inline const wxString NullUiString = "";
194
195private:
198};
199
200#endif // UNITS_PROVIDER_H
constexpr EDA_IU_SCALE unityScale
Definition: base_units.h:111
double AsDegrees() const
Definition: eda_angle.h:113
A class to perform either relative or absolute display origin transforms for a single axis of a point...
wxString MessageTextFromMinOptMax(const MINOPTMAX< int > &aValue) const
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)
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().
EDA_UNITS GetUserUnits() const
void SetUserUnits(EDA_UNITS aUnits)
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:38
EDA_UNITS
Definition: eda_units.h:46
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.
Definition: eda_units.cpp:361
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.
Definition: eda_units.cpp:277
KICOMMON_API wxString MessageTextFromMinOptMax(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const MINOPTMAX< int > &aValue)
Definition: eda_units.cpp:411
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.
Definition: eda_units.cpp:497
KICOMMON_API bool IsImperialUnit(EDA_UNITS aUnit)
Definition: eda_units.cpp:47