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 26 #ifndef __UNIT_BINDER_H_ 27 #define __UNIT_BINDER_H_ 28 29 #include <eda_units.h> 30 #include <base_units.h> 31 #include <origin_transforms.h> 32 #include <libeval/numeric_evaluator.h> 33 #include <wx/event.h> 34 35 class EDA_DRAW_FRAME; 36 class wxTextEntry; 37 class wxSpinButton; 38 class wxStaticText; 39 40 41 class UNIT_BINDER : public wxEvtHandler 42 { 43 public: 44 45 /** 46 * @param aParent is the parent EDA_DRAW_FRAME. 47 * @param aLabel is the static text used to label the text input widget (note: the label 48 * text, trimmed of its colon, will also be used in error messages) 49 * @param aValueCtrl is the control used to edit or display the given value (wxTextCtrl, 50 * wxComboBox, wxStaticText, etc.). 51 * @param aUnitLabel is the units label displayed after the text input widget 52 * Can be nullptr. 53 * @param aAllowEval indicates \a aTextInput's content should be eval'ed before storing 54 */ 55 UNIT_BINDER( EDA_DRAW_FRAME* aParent, 56 wxStaticText* aLabel, wxWindow* aValueCtrl, wxStaticText* aUnitLabel, 57 bool aAllowEval = true ); 58 59 ~UNIT_BINDER() override; 60 61 /** 62 * Normally not needed (as the UNIT_BINDER inherits from the parent frame), but can be 63 * used to set to DEGREES for angular controls. 64 */ 65 virtual void SetUnits( EDA_UNITS aUnits ); 66 SetNegativeZero()67 virtual void SetNegativeZero() { m_negativeZero = true; } 68 69 /** 70 * Normally not needed, but can be used to set the precision when using 71 * internal units that are floats (not integers) like DEGREES or PERCENT. 72 * Not used for integer values in IU 73 * @param aLength is the number of digits for mantissa (0 = no truncation) 74 * must be <= 6 75 */ 76 virtual void SetPrecision( int aLength ); 77 78 /** 79 * Used to override the datatype of the displayed property (default is DISTANCE) 80 * @param aDataType is the datatype to use for the units text display 81 */ 82 void SetDataType( EDA_DATA_TYPE aDataType ); 83 84 /** 85 * Set new value (in Internal Units) for the text field, taking care of units conversion. 86 */ 87 virtual void SetValue( int aValue ); 88 89 void SetValue( const wxString& aValue ); 90 91 /** 92 * Set new value (in Internal Units) for the text field, taking care of units conversion. 93 * 94 * The value will be truncated according to the precision set by SetPrecision() (if not <= 0). 95 */ 96 virtual void SetDoubleValue( double aValue ); 97 98 /** 99 * Set new value (in Internal Units) for the text field, taking care of units conversion 100 * WITHOUT triggering the update routine. 101 */ 102 virtual void ChangeValue( int aValue ); 103 104 void ChangeValue( const wxString& aValue ); 105 106 /** 107 * Set new value (in Internal Units) for the text field, taking care of units conversion 108 * WITHOUT triggering the update routine. 109 * 110 * The value will be truncated according to the precision set by SetPrecision() (if not <= 0). 111 */ 112 virtual void ChangeDoubleValue( double aValue ); 113 114 /** 115 * Return the current value in Internal Units. 116 */ 117 virtual long long int GetValue(); 118 119 /** 120 * Return the current value in Internal Units. 121 * 122 * The returned value will be truncated according to the precision set by 123 * SetPrecision() (if not <= 0) 124 */ 125 virtual double GetDoubleValue(); 126 127 /** 128 * Return true if the control holds the indeterminate value (for instance, if it 129 * represents a multiple selection of differing values). 130 */ 131 bool IsIndeterminate() const; 132 133 /** 134 * Return the pre-evaluated text (or the current text if evaluation is not supported). 135 * Used primarily to remember values between dialog invocations. 136 */ 137 wxString GetOriginalText() const; 138 139 /** 140 * Validate the control against the given range, informing the user of any errors found. 141 * 142 * @param aMin a minimum value for validation 143 * @param aMax a maximum value for validation 144 * @param aUnits the units of the min/max parameters (use UNSCALED for internal units) 145 * @return false on error. 146 */ 147 virtual bool Validate( double aMin, double aMax, EDA_UNITS aUnits = EDA_UNITS::UNSCALED ); 148 149 void SetLabel( const wxString& aLabel ); 150 151 /** 152 * Enable/disable the label, widget and units label. 153 */ 154 void Enable( bool aEnable ); 155 156 /** 157 * Show/hide the label, widget and units label. 158 * 159 * @param aShow called for the Show() routine in wx 160 * @param aResize if true, the element will be sized to 0 on hide and -1 on show 161 */ 162 void Show( bool aShow, bool aResize = false ); 163 164 /** 165 * Get the origin transforms coordinate type 166 * 167 * @returns the origin transforms coordinate type 168 */ GetCoordType()169 ORIGIN_TRANSFORMS::COORD_TYPES_T GetCoordType() const 170 { 171 return m_coordType; 172 } 173 174 /** 175 * Set the current origin transform mode 176 */ SetCoordType(ORIGIN_TRANSFORMS::COORD_TYPES_T aCoordType)177 void SetCoordType( ORIGIN_TRANSFORMS::COORD_TYPES_T aCoordType ) 178 { 179 m_coordType = aCoordType; 180 } 181 182 protected: 183 184 void onSetFocus( wxFocusEvent& aEvent ); 185 void onKillFocus( wxFocusEvent& aEvent ); 186 void delayedFocusHandler( wxCommandEvent& aEvent ); 187 188 void onUnitsChanged( wxCommandEvent& aEvent ); 189 190 /** 191 * When m_precision > 0 truncate the value aValue to show only 192 * m_precision digits in mantissa. 193 * used in GetDoubleValue to return a rounded value. 194 * Mainly for units set to DEGREES. 195 * 196 * @param aValue is the value to modify. 197 * @param aValueUsesUserUnits must be set to true if aValue is a user value, 198 * and set to false if aValue is a internal unit value. 199 * @return the "rounded" value. 200 */ 201 double setPrecision( double aValue, bool aValueUsesUserUnits ); 202 203 EDA_DRAW_FRAME* m_frame; 204 205 ///< The bound widgets 206 wxStaticText* m_label; 207 wxWindow* m_valueCtrl; 208 wxStaticText* m_unitLabel; ///< Can be nullptr 209 210 ///< Currently used units. 211 EDA_UNITS m_units; 212 bool m_negativeZero; ///< Indicates "-0" should be displayed for 0. 213 EDA_DATA_TYPE m_dataType; 214 int m_precision; ///< 0 to 6 215 216 wxString m_errorMessage; 217 218 NUMERIC_EVALUATOR m_eval; 219 bool m_allowEval; 220 bool m_needsEval; 221 222 long m_selStart; ///< Selection start and end of the original text 223 long m_selEnd; 224 225 /// A reference to an ORIGIN_TRANSFORMS object 226 ORIGIN_TRANSFORMS& m_originTransforms; 227 228 /// Type of coordinate for display origin transforms 229 ORIGIN_TRANSFORMS::COORD_TYPES_T m_coordType; 230 }; 231 232 #endif /* __UNIT_BINDER_H_ */ 233