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.

129 lines
5.0 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. * @return A wxString object containing value and optionally the symbol unit (like 2.000 mm)
  56. */
  57. wxString StringFromValue( double aValue, bool aAddUnitLabel = false,
  58. EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE )
  59. {
  60. return EDA_UNIT_UTILS::UI::StringFromValue( GetIuScale(), GetUserUnits(), aValue,
  61. aAddUnitLabel, aType );
  62. }
  63. wxString StringFromValue( const EDA_ANGLE& aValue, bool aAddUnitLabel = false )
  64. {
  65. return EDA_UNIT_UTILS::UI::StringFromValue( unityScale, EDA_UNITS::DEGREES,
  66. aValue.AsDegrees(), aAddUnitLabel,
  67. EDA_DATA_TYPE::DISTANCE );
  68. }
  69. /**
  70. * A lower-precision version of StringFromValue().
  71. *
  72. * Should ONLY be used for status text and messages. Not suitable for dialogs, files, etc.
  73. * where the loss of precision matters.
  74. */
  75. wxString MessageTextFromValue( double aValue, bool aAddUnitLabel = true,
  76. EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE )
  77. {
  78. return EDA_UNIT_UTILS::UI::MessageTextFromValue( GetIuScale(), GetUserUnits(), aValue,
  79. aAddUnitLabel, aType );
  80. }
  81. wxString MessageTextFromValue( const EDA_ANGLE& aValue, bool aAddUnitLabel = true )
  82. {
  83. return EDA_UNIT_UTILS::UI::MessageTextFromValue( unityScale, EDA_UNITS::DEGREES,
  84. aValue.AsDegrees(), aAddUnitLabel,
  85. EDA_DATA_TYPE::DISTANCE );
  86. }
  87. /**
  88. * Converts \a aTextValue in \a aUnits to internal units used by the frame.
  89. * @warning This utilizes the current locale and will break if decimal formats differ
  90. *
  91. * @param aTextValue A reference to a wxString object containing the string to convert.
  92. * @return internal units value
  93. */
  94. int ValueFromString( const wxString& aTextValue, EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE )
  95. {
  96. double value = EDA_UNIT_UTILS::UI::DoubleValueFromString( GetIuScale(), GetUserUnits(),
  97. aTextValue, aType );
  98. return KiROUND<double, int>( value );
  99. }
  100. EDA_ANGLE AngleValueFromString( const wxString& aTextValue )
  101. {
  102. double angle = EDA_UNIT_UTILS::UI::DoubleValueFromString( GetIuScale(), EDA_UNITS::DEGREES,
  103. aTextValue );
  104. return EDA_ANGLE( angle, DEGREES_T );
  105. }
  106. private:
  107. const EDA_IU_SCALE& m_iuScale;
  108. EDA_UNITS m_userUnits;
  109. };
  110. #endif // UNITS_PROVIDER_H