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.

133 lines
5.2 KiB

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