You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
5.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2022-2023 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. #ifndef UNITS_PROVIDER_H
  24. #define UNITS_PROVIDER_H
  25. #include <eda_units.h>
  26. #include <origin_transforms.h>
  27. #include <core/minoptmax.h>
  28. class UNITS_PROVIDER
  29. {
  30. public:
  31. UNITS_PROVIDER( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits ) :
  32. m_iuScale( aIuScale ),
  33. m_userUnits( aUnits )
  34. {}
  35. virtual ~UNITS_PROVIDER()
  36. {}
  37. EDA_UNITS GetUserUnits() const { return m_userUnits; }
  38. void SetUserUnits( EDA_UNITS aUnits ) { m_userUnits = aUnits; }
  39. virtual void GetUnitPair( EDA_UNITS& aPrimaryUnit, EDA_UNITS& aSecondaryUnits )
  40. {
  41. aPrimaryUnit = GetUserUnits();
  42. aSecondaryUnits = EDA_UNITS::MILS;
  43. if( EDA_UNIT_UTILS::IsImperialUnit( aPrimaryUnit ) )
  44. aSecondaryUnits = EDA_UNITS::MILLIMETRES;
  45. else
  46. aSecondaryUnits = EDA_UNITS::MILS;
  47. }
  48. const EDA_IU_SCALE& GetIuScale() const { return m_iuScale; }
  49. // No SetIuScale(); scale is invariant
  50. virtual ORIGIN_TRANSFORMS& GetOriginTransforms()
  51. {
  52. static ORIGIN_TRANSFORMS identityTransform;
  53. return identityTransform;
  54. }
  55. /**
  56. * Converts \a aValue in internal units into a united string.
  57. *
  58. * For readability, trailing 0s are removed if the mantissa has 3 or more digits.
  59. * This function should be used to display values in dialogs because a value entered in mm
  60. * (for instance 2.0 mm) could need up to 8 digits mantissa if displayed in inch to avoid
  61. * truncation or rounding made just by the printf function.
  62. *
  63. * @param aValue = value in internal units
  64. * @param aAddUnitLabel = true to add symbol unit to the string value
  65. * @param aType is the type of this value, and controls the way the value is converted
  66. * to a string, and the suitable unit
  67. * @return A wxString object containing value and optionally the symbol unit (like 2.000 mm)
  68. */
  69. wxString StringFromValue( double aValue, bool aAddUnitLabel = false,
  70. EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) const
  71. {
  72. return EDA_UNIT_UTILS::UI::StringFromValue( GetIuScale(), GetUserUnits(), aValue,
  73. aAddUnitLabel, aType );
  74. }
  75. wxString StringFromValue( const EDA_ANGLE& aValue, bool aAddUnitLabel = false ) const
  76. {
  77. return EDA_UNIT_UTILS::UI::StringFromValue( unityScale, EDA_UNITS::DEGREES,
  78. aValue.AsDegrees(), aAddUnitLabel,
  79. EDA_DATA_TYPE::DISTANCE );
  80. }
  81. /**
  82. * A lower-precision version of StringFromValue().
  83. *
  84. * Should ONLY be used for status text and messages. Not suitable for dialogs, files, etc.
  85. * where the loss of precision matters.
  86. */
  87. wxString MessageTextFromValue( double aValue, bool aAddUnitLabel = true,
  88. EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) const
  89. {
  90. return EDA_UNIT_UTILS::UI::MessageTextFromValue( GetIuScale(), GetUserUnits(), aValue,
  91. aAddUnitLabel, aType );
  92. }
  93. wxString MessageTextFromValue( const EDA_ANGLE& aValue, bool aAddUnitLabel = true ) const
  94. {
  95. return EDA_UNIT_UTILS::UI::MessageTextFromValue( unityScale, EDA_UNITS::DEGREES,
  96. aValue.AsDegrees(), aAddUnitLabel,
  97. EDA_DATA_TYPE::DISTANCE );
  98. }
  99. wxString MessageTextFromMinOptMax( const MINOPTMAX<int>& aValue ) const
  100. {
  101. return EDA_UNIT_UTILS::UI::MessageTextFromMinOptMax( GetIuScale(), GetUserUnits(), aValue );
  102. };
  103. /**
  104. * Converts \a aTextValue in \a aUnits to internal units used by the frame.
  105. * @warning This utilizes the current locale and will break if decimal formats differ
  106. * @param aType is the type of this value, and controls the way the string is converted
  107. * to a value
  108. *
  109. * @param aTextValue A reference to a wxString object containing the string to convert.
  110. * @return internal units value
  111. */
  112. int ValueFromString( const wxString& aTextValue,
  113. EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE ) const
  114. {
  115. double value = EDA_UNIT_UTILS::UI::DoubleValueFromString( GetIuScale(), GetUserUnits(),
  116. aTextValue, aType );
  117. return KiROUND<double, int>( value );
  118. }
  119. EDA_ANGLE AngleValueFromString( const wxString& aTextValue ) const
  120. {
  121. double angle = EDA_UNIT_UTILS::UI::DoubleValueFromString( GetIuScale(), EDA_UNITS::DEGREES,
  122. aTextValue );
  123. return EDA_ANGLE( angle, DEGREES_T );
  124. }
  125. private:
  126. const EDA_IU_SCALE& m_iuScale;
  127. EDA_UNITS m_userUnits;
  128. };
  129. #endif // UNITS_PROVIDER_H