2022-09-19 16:09:59 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2023-10-15 21:08:27 +00:00
|
|
|
* Copyright (C) 2022-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2022-09-19 16:09:59 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef UNITS_PROVIDER_H
|
|
|
|
#define UNITS_PROVIDER_H
|
|
|
|
|
|
|
|
#include <eda_units.h>
|
2022-12-03 19:31:41 +00:00
|
|
|
#include <origin_transforms.h>
|
2023-10-15 21:08:27 +00:00
|
|
|
#include <core/minoptmax.h>
|
2022-12-03 19:31:41 +00:00
|
|
|
|
2022-09-19 16:09:59 +00:00
|
|
|
|
|
|
|
class UNITS_PROVIDER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UNITS_PROVIDER( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits ) :
|
|
|
|
m_iuScale( aIuScale ),
|
|
|
|
m_userUnits( aUnits )
|
|
|
|
{}
|
|
|
|
|
2022-12-05 22:36:44 +00:00
|
|
|
virtual ~UNITS_PROVIDER()
|
|
|
|
{}
|
|
|
|
|
2022-09-19 16:09:59 +00:00
|
|
|
EDA_UNITS GetUserUnits() const { return m_userUnits; }
|
|
|
|
void SetUserUnits( EDA_UNITS aUnits ) { m_userUnits = aUnits; }
|
|
|
|
|
2023-08-26 12:29:24 +00:00
|
|
|
virtual void GetUnitPair( EDA_UNITS& aPrimaryUnit, EDA_UNITS& aSecondaryUnits )
|
|
|
|
{
|
|
|
|
aPrimaryUnit = GetUserUnits();
|
|
|
|
aSecondaryUnits = EDA_UNITS::MILS;
|
|
|
|
|
|
|
|
if( EDA_UNIT_UTILS::IsImperialUnit( aPrimaryUnit ) )
|
|
|
|
aSecondaryUnits = EDA_UNITS::MILLIMETRES;
|
|
|
|
else
|
|
|
|
aSecondaryUnits = EDA_UNITS::MILS;
|
|
|
|
}
|
|
|
|
|
2022-09-19 16:09:59 +00:00
|
|
|
const EDA_IU_SCALE& GetIuScale() const { return m_iuScale; }
|
|
|
|
// No SetIuScale(); scale is invariant
|
|
|
|
|
2022-12-03 19:31:41 +00:00
|
|
|
virtual ORIGIN_TRANSFORMS& GetOriginTransforms()
|
|
|
|
{
|
|
|
|
static ORIGIN_TRANSFORMS identityTransform;
|
|
|
|
|
|
|
|
return identityTransform;
|
|
|
|
}
|
|
|
|
|
2022-09-19 16:09:59 +00:00
|
|
|
/**
|
|
|
|
* Converts \a aValue in internal units into a united string.
|
|
|
|
*
|
|
|
|
* For readability, trailing 0s are removed if the mantissa has 3 or more digits.
|
|
|
|
* This function should be used to display values in dialogs because a value entered in mm
|
|
|
|
* (for instance 2.0 mm) could need up to 8 digits mantissa if displayed in inch to avoid
|
|
|
|
* truncation or rounding made just by the printf function.
|
|
|
|
*
|
|
|
|
* @param aValue = value in internal units
|
|
|
|
* @param aAddUnitLabel = true to add symbol unit to the string value
|
2023-04-19 18:24:42 +00:00
|
|
|
* @param aType is the type of this value, and controls the way the value is converted
|
|
|
|
* to a string, and the suitable unit
|
2022-09-19 16:09:59 +00:00
|
|
|
* @return A wxString object containing value and optionally the symbol unit (like 2.000 mm)
|
|
|
|
*/
|
|
|
|
wxString StringFromValue( double aValue, bool aAddUnitLabel = false,
|
2023-10-05 07:57:15 +00:00
|
|
|
EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) const
|
2022-09-19 16:09:59 +00:00
|
|
|
{
|
|
|
|
return EDA_UNIT_UTILS::UI::StringFromValue( GetIuScale(), GetUserUnits(), aValue,
|
|
|
|
aAddUnitLabel, aType );
|
|
|
|
}
|
|
|
|
|
2023-10-05 07:57:15 +00:00
|
|
|
wxString StringFromValue( const EDA_ANGLE& aValue, bool aAddUnitLabel = false ) const
|
2022-09-19 16:09:59 +00:00
|
|
|
{
|
|
|
|
return EDA_UNIT_UTILS::UI::StringFromValue( unityScale, EDA_UNITS::DEGREES,
|
|
|
|
aValue.AsDegrees(), aAddUnitLabel,
|
|
|
|
EDA_DATA_TYPE::DISTANCE );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A lower-precision version of StringFromValue().
|
|
|
|
*
|
|
|
|
* Should ONLY be used for status text and messages. Not suitable for dialogs, files, etc.
|
|
|
|
* where the loss of precision matters.
|
|
|
|
*/
|
|
|
|
wxString MessageTextFromValue( double aValue, bool aAddUnitLabel = true,
|
2023-10-05 07:57:15 +00:00
|
|
|
EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) const
|
2022-09-19 16:09:59 +00:00
|
|
|
{
|
|
|
|
return EDA_UNIT_UTILS::UI::MessageTextFromValue( GetIuScale(), GetUserUnits(), aValue,
|
|
|
|
aAddUnitLabel, aType );
|
|
|
|
}
|
|
|
|
|
2023-10-05 07:57:15 +00:00
|
|
|
wxString MessageTextFromValue( const EDA_ANGLE& aValue, bool aAddUnitLabel = true ) const
|
2022-09-19 16:09:59 +00:00
|
|
|
{
|
|
|
|
return EDA_UNIT_UTILS::UI::MessageTextFromValue( unityScale, EDA_UNITS::DEGREES,
|
|
|
|
aValue.AsDegrees(), aAddUnitLabel,
|
|
|
|
EDA_DATA_TYPE::DISTANCE );
|
|
|
|
}
|
|
|
|
|
2023-10-15 21:08:27 +00:00
|
|
|
wxString MessageTextFromMinOptMax( const MINOPTMAX<int>& aValue ) const
|
|
|
|
{
|
|
|
|
return EDA_UNIT_UTILS::UI::MessageTextFromMinOptMax( GetIuScale(), GetUserUnits(), aValue );
|
|
|
|
};
|
|
|
|
|
2022-09-19 16:09:59 +00:00
|
|
|
/**
|
|
|
|
* Converts \a aTextValue in \a aUnits to internal units used by the frame.
|
|
|
|
* @warning This utilizes the current locale and will break if decimal formats differ
|
2023-04-19 18:24:42 +00:00
|
|
|
* @param aType is the type of this value, and controls the way the string is converted
|
|
|
|
* to a value
|
2022-09-19 16:09:59 +00:00
|
|
|
*
|
|
|
|
* @param aTextValue A reference to a wxString object containing the string to convert.
|
|
|
|
* @return internal units value
|
|
|
|
*/
|
2023-10-05 07:57:15 +00:00
|
|
|
int ValueFromString( const wxString& aTextValue,
|
|
|
|
EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) const
|
2022-10-05 16:42:03 +00:00
|
|
|
{
|
|
|
|
double value = EDA_UNIT_UTILS::UI::DoubleValueFromString( GetIuScale(), GetUserUnits(),
|
|
|
|
aTextValue, aType );
|
|
|
|
|
|
|
|
return KiROUND<double, int>( value );
|
|
|
|
}
|
|
|
|
|
2023-10-05 07:57:15 +00:00
|
|
|
EDA_ANGLE AngleValueFromString( const wxString& aTextValue ) const
|
2022-09-19 16:09:59 +00:00
|
|
|
{
|
2022-10-05 16:42:03 +00:00
|
|
|
double angle = EDA_UNIT_UTILS::UI::DoubleValueFromString( GetIuScale(), EDA_UNITS::DEGREES,
|
|
|
|
aTextValue );
|
|
|
|
|
|
|
|
return EDA_ANGLE( angle, DEGREES_T );
|
2022-09-19 16:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const EDA_IU_SCALE& m_iuScale;
|
|
|
|
EDA_UNITS m_userUnits;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // UNITS_PROVIDER_H
|