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.

232 lines
7.6 KiB

11 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014-2015 CERN
  5. * Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
  6. * Author: Maciej Suminski <maciej.suminski@cern.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #ifndef __UNIT_BINDER_H_
  26. #define __UNIT_BINDER_H_
  27. #include <eda_units.h>
  28. #include <base_units.h>
  29. #include <origin_transforms.h>
  30. #include <libeval/numeric_evaluator.h>
  31. #include <wx/event.h>
  32. class EDA_DRAW_FRAME;
  33. class wxTextEntry;
  34. class wxSpinButton;
  35. class wxStaticText;
  36. class UNIT_BINDER : public wxEvtHandler
  37. {
  38. public:
  39. /**
  40. * @param aParent is the parent EDA_DRAW_FRAME.
  41. * @param aLabel is the static text used to label the text input widget (note: the label
  42. * text, trimmed of its colon, will also be used in error messages)
  43. * @param aValueCtrl is the control used to edit or display the given value (wxTextCtrl,
  44. * wxComboBox, wxStaticText, etc.).
  45. * @param aUnitLabel is the units label displayed after the text input widget
  46. * Can be nullptr.
  47. * @param aAllowEval indicates \a aTextInput's content should be eval'ed before storing
  48. */
  49. UNIT_BINDER( EDA_DRAW_FRAME* aParent,
  50. wxStaticText* aLabel, wxWindow* aValueCtrl, wxStaticText* aUnitLabel,
  51. bool aAllowEval = true );
  52. ~UNIT_BINDER() override;
  53. /**
  54. * Normally not needed (as the UNIT_BINDER inherits from the parent frame), but can be
  55. * used to set to DEGREES for angular controls.
  56. */
  57. virtual void SetUnits( EDA_UNITS aUnits );
  58. virtual void SetNegativeZero() { m_negativeZero = true; }
  59. /**
  60. * Normally not needed, but can be used to set the precision when using
  61. * internal units that are floats (not integers) like DEGREES or PERCENT.
  62. * Not used for integer values in IU
  63. * @param aLength is the number of digits for mantissa (0 = no truncation)
  64. * must be <= 6
  65. */
  66. virtual void SetPrecision( int aLength );
  67. /**
  68. * Used to override the datatype of the displayed property (default is DISTANCE)
  69. * @param aDataType is the datatype to use for the units text display
  70. */
  71. void SetDataType( EDA_DATA_TYPE aDataType );
  72. /**
  73. * Set new value (in Internal Units) for the text field, taking care of units conversion.
  74. */
  75. virtual void SetValue( int aValue );
  76. void SetValue( const wxString& aValue );
  77. /**
  78. * Set new value (in Internal Units) for the text field, taking care of units conversion.
  79. *
  80. * The value will be truncated according to the precision set by SetPrecision() (if not <= 0).
  81. */
  82. virtual void SetDoubleValue( double aValue );
  83. /**
  84. * Set new value (in Internal Units) for the text field, taking care of units conversion
  85. * WITHOUT triggering the update routine.
  86. */
  87. virtual void ChangeValue( int aValue );
  88. void ChangeValue( const wxString& aValue );
  89. /**
  90. * Set new value (in Internal Units) for the text field, taking care of units conversion
  91. * WITHOUT triggering the update routine.
  92. *
  93. * The value will be truncated according to the precision set by SetPrecision() (if not <= 0).
  94. */
  95. virtual void ChangeDoubleValue( double aValue );
  96. /**
  97. * Return the current value in Internal Units.
  98. */
  99. virtual long long int GetValue();
  100. /**
  101. * Return the current value in Internal Units.
  102. *
  103. * The returned value will be truncated according to the precision set by
  104. * SetPrecision() (if not <= 0)
  105. */
  106. virtual double GetDoubleValue();
  107. /**
  108. * Return true if the control holds the indeterminate value (for instance, if it
  109. * represents a multiple selection of differing values).
  110. */
  111. bool IsIndeterminate() const;
  112. /**
  113. * Return the pre-evaluated text (or the current text if evaluation is not supported).
  114. * Used primarily to remember values between dialog invocations.
  115. */
  116. wxString GetOriginalText() const;
  117. /**
  118. * Validate the control against the given range, informing the user of any errors found.
  119. *
  120. * @param aMin a minimum value for validation
  121. * @param aMax a maximum value for validation
  122. * @param aUnits the units of the min/max parameters (use UNSCALED for internal units)
  123. * @return false on error.
  124. */
  125. virtual bool Validate( double aMin, double aMax, EDA_UNITS aUnits = EDA_UNITS::UNSCALED );
  126. void SetLabel( const wxString& aLabel );
  127. /**
  128. * Enable/disable the label, widget and units label.
  129. */
  130. void Enable( bool aEnable );
  131. /**
  132. * Show/hide the label, widget and units label.
  133. *
  134. * @param aShow called for the Show() routine in wx
  135. * @param aResize if true, the element will be sized to 0 on hide and -1 on show
  136. */
  137. void Show( bool aShow, bool aResize = false );
  138. /**
  139. * Get the origin transforms coordinate type
  140. *
  141. * @returns the origin transforms coordinate type
  142. */
  143. ORIGIN_TRANSFORMS::COORD_TYPES_T GetCoordType() const
  144. {
  145. return m_coordType;
  146. }
  147. /**
  148. * Set the current origin transform mode
  149. */
  150. void SetCoordType( ORIGIN_TRANSFORMS::COORD_TYPES_T aCoordType )
  151. {
  152. m_coordType = aCoordType;
  153. }
  154. protected:
  155. void onSetFocus( wxFocusEvent& aEvent );
  156. void onKillFocus( wxFocusEvent& aEvent );
  157. void delayedFocusHandler( wxCommandEvent& aEvent );
  158. void onUnitsChanged( wxCommandEvent& aEvent );
  159. /**
  160. * When m_precision > 0 truncate the value aValue to show only
  161. * m_precision digits in mantissa.
  162. * used in GetDoubleValue to return a rounded value.
  163. * Mainly for units set to DEGREES.
  164. *
  165. * @param aValue is the value to modify.
  166. * @param aValueUsesUserUnits must be set to true if aValue is a user value,
  167. * and set to false if aValue is a internal unit value.
  168. * @return the "rounded" value.
  169. */
  170. double setPrecision( double aValue, bool aValueUsesUserUnits );
  171. EDA_DRAW_FRAME* m_frame;
  172. ///< The bound widgets
  173. wxStaticText* m_label;
  174. wxWindow* m_valueCtrl;
  175. wxStaticText* m_unitLabel; ///< Can be nullptr
  176. ///< Currently used units.
  177. EDA_UNITS m_units;
  178. bool m_negativeZero; ///< Indicates "-0" should be displayed for 0.
  179. EDA_DATA_TYPE m_dataType;
  180. int m_precision; ///< 0 to 6
  181. wxString m_errorMessage;
  182. NUMERIC_EVALUATOR m_eval;
  183. bool m_allowEval;
  184. bool m_needsEval;
  185. long m_selStart; ///< Selection start and end of the original text
  186. long m_selEnd;
  187. /// A reference to an ORIGIN_TRANSFORMS object
  188. ORIGIN_TRANSFORMS& m_originTransforms;
  189. /// Type of coordinate for display origin transforms
  190. ORIGIN_TRANSFORMS::COORD_TYPES_T m_coordType;
  191. };
  192. #endif /* __UNIT_BINDER_H_ */