KiCad PCB EDA Suite
Loading...
Searching...
No Matches
unit_binder.h
Go to the documentation of this file.
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 The KiCad Developers, see AUTHORS.txt for contributors.
6 * Author: Maciej Suminski <[email protected]>
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, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifndef __UNIT_BINDER_H_
23#define __UNIT_BINDER_H_
24
25#include <span>
26
27#include <base_units.h>
28#include <units_provider.h>
30#include <wx/event.h>
31
32class DIALOG_SHIM;
33class EDA_BASE_FRAME;
34class EDA_DRAW_FRAME;
35class wxTextEntry;
36class wxSpinButton;
37class wxStaticText;
38
39class UNIT_BINDER : public wxEvtHandler
40{
41public:
42
55 wxStaticText* aLabel, wxWindow* aValueCtrl, wxStaticText* aUnitLabel,
56 bool aAllowEval = true, bool aBindFocusEvent = true );
57
58 UNIT_BINDER( UNITS_PROVIDER* aUnitsProvider, wxWindow* aEventSource,
59 wxStaticText* aLabel, wxWindow* aValueCtrl, wxStaticText* aUnitLabel,
60 bool aAllowEval = true, bool aBindFocusEvent = true );
61
62 virtual ~UNIT_BINDER() override;
63
68 virtual void SetUnits( EDA_UNITS aUnits );
69
70 EDA_UNITS GetUnits() const { return m_units; }
71
76
77 virtual void SetNegativeZero() { m_negativeZero = true; }
78
86 virtual void SetPrecision( int aLength );
87
92 void SetDataType( EDA_DATA_TYPE aDataType );
93
97 virtual void SetValue( long long int aValue );
98
99 void SetValue( const wxString& aValue );
100
106 virtual void SetDoubleValue( double aValue );
107
108 virtual void SetAngleValue( const EDA_ANGLE& aValue );
109
114 virtual void ChangeValue( int aValue );
115
116 void ChangeValue( const wxString& aValue );
117
124 virtual void ChangeDoubleValue( double aValue );
125
126 virtual void ChangeAngleValue( const EDA_ANGLE& aValue );
127
131 virtual long long int GetValue() const;
132
133 int GetIntValue() const
134 {
135 const long long int value = GetValue();
136 const long long int clamped =
137 std::clamp<long long>( value, static_cast<long long>( std::numeric_limits<int>::lowest() ),
138 static_cast<long long>( std::numeric_limits<int>::max() ) );
139
140 return static_cast<int>( clamped );
141 }
142
149 virtual double GetDoubleValue() const;
150
151 virtual EDA_ANGLE GetAngleValue();
152
158 virtual void SetOptionsList( std::span<const long long int> aOptions );
159 virtual void SetDoubleOptionsList( std::span<const double> aOptions );
160
161
166 bool IsIndeterminate() const;
167
171 bool IsNull() const;
172 void SetNull();
173
182 virtual bool Validate( double aMin, double aMax, EDA_UNITS aUnits = EDA_UNITS::UNSCALED );
183
188 {
190 wxString displayText;
191 };
192
207 static RANGE_BOUND ConvertRangeBound( const EDA_IU_SCALE& aIuScale, EDA_UNITS aBoundUnits,
208 double aBound, EDA_UNITS aDisplayUnits, EDA_DATA_TYPE aDataType );
209
210 void SetLabel( const wxString& aLabel );
211
215 void Enable( bool aEnable );
216
223 void Show( bool aShow, bool aResize = false );
224
234
239 {
240 m_coordType = aCoordType;
241 }
242
247 virtual wxString GetFormula() const;
248
255 virtual bool RestoreFormula( const wxString& aFormula );
256
266 virtual void RestoreValue( int aValue, const wxString& aFormula );
267
272 {
273 m_needsEval = true;
274 }
275
276protected:
277 void onClick( wxMouseEvent& aEvent );
278 void onComboBox( wxCommandEvent& aEvent );
279
280 void onSetFocus( wxFocusEvent& aEvent );
281 void onKillFocus( wxFocusEvent& aEvent );
282 void delayedFocusHandler( wxCommandEvent& aEvent );
283
284 void onValueCtrlDestroyed( wxWindowDestroyEvent& aEvent );
285
286 void onUnitsChanged( wxCommandEvent& aEvent );
287
299 double setPrecision( double aValue, bool aValueUsesUserUnits ) const;
300
304 wxString formulaYielding( long long int aValue ) const;
305
306 wxString getTextForValue( long long int aValue ) const;
307 wxString getTextForDoubleValue( double aValue ) const;
308
309protected:
311
313 wxStaticText* m_label;
314 wxWindow* m_valueCtrl;
315 wxWindow* m_eventSource;
316 wxStaticText* m_unitLabel;
317
324
326
330
333
335
338
341
344
345public:
352 void DetachFromDialogShim() { m_dialogShim = nullptr; }
353};
354
355
360{
361public:
363
365
366 void SetControl( wxWindow* aControl );
367};
368
369#endif /* __UNIT_BINDER_H_ */
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition dialog_shim.h:81
The base frame for deriving all KiCad main window classes.
The base class for create windows for drawing purpose.
A class to perform either relative or absolute display origin transforms for a single axis of a point...
COORD_TYPES_T
The supported Display Origin Transform types.
void SetControl(wxWindow *aControl)
PROPERTY_EDITOR_UNIT_BINDER(EDA_DRAW_FRAME *aParent)
double setPrecision(double aValue, bool aValueUsesUserUnits) const
When m_precision > 0 truncate the value aValue to show only m_precision digits in mantissa.
ORIGIN_TRANSFORMS::COORD_TYPES_T m_coordType
Type of coordinate for display origin transforms.
wxString getTextForValue(long long int aValue) const
virtual void ChangeDoubleValue(double aValue)
Set new value (in Internal Units) for the text field, taking care of units conversion WITHOUT trigger...
void onKillFocus(wxFocusEvent &aEvent)
void Enable(bool aEnable)
Enable/disable the label, widget and units label.
void onClick(wxMouseEvent &aEvent)
DIALOG_SHIM * m_dialogShim
The dialog this binder registered itself with, or nullptr if it isn't owned by a dialog.
virtual void SetPrecision(int aLength)
Normally not needed, but can be used to set the precision when using internal units that are floats (...
EDA_UNITS GetUnits() const
Definition unit_binder.h:70
virtual void SetUnits(EDA_UNITS aUnits)
Normally not needed (as the UNIT_BINDER inherits from the parent frame), but can be used to set to DE...
virtual wxString GetFormula() const
Return the expression the user entered if it is more than a plain number, otherwise an empty string.
virtual double GetDoubleValue() const
Return the current value in Internal Units.
wxString getTextForDoubleValue(double aValue) const
const EDA_IU_SCALE * m_iuScale
Currently used units.
wxString m_errorMessage
virtual EDA_ANGLE GetAngleValue()
bool m_negativeZero
Indicates "-0" should be displayed for 0.
virtual long long int GetValue() const
Return the current value in Internal Units.
wxString formulaYielding(long long int aValue) const
Return the retained expression if it evaluates to aValue, otherwise an empty string.
int m_precision
0 to 6.
static RANGE_BOUND ConvertRangeBound(const EDA_IU_SCALE &aIuScale, EDA_UNITS aBoundUnits, double aBound, EDA_UNITS aDisplayUnits, EDA_DATA_TYPE aDataType)
Convert a range bound and format its display string honoring aDataType.
wxStaticText * m_unitLabel
Can be nullptr.
virtual void SetNegativeZero()
Definition unit_binder.h:77
bool m_unitsInValue
Units label should be included in value text.
wxWindow * m_valueCtrl
wxWindow * m_eventSource
void onSetFocus(wxFocusEvent &aEvent)
void onComboBox(wxCommandEvent &aEvent)
UNIT_BINDER(EDA_DRAW_FRAME *aParent, wxStaticText *aLabel, wxWindow *aValueCtrl, wxStaticText *aUnitLabel, bool aAllowEval=true, bool aBindFocusEvent=true)
wxStaticText * m_label
The bound widgets.
virtual void RestoreValue(int aValue, const wxString &aFormula)
Set a value (in Internal Units) as ChangeValue() does and reinstate the expression that produced it.
virtual bool RestoreFormula(const wxString &aFormula)
Show the result of an expression returned by GetFormula() and retain the expression so that it reappe...
ORIGIN_TRANSFORMS & m_originTransforms
A reference to an ORIGIN_TRANSFORMS object.
bool m_bindFocusEvent
long m_selStart
Selection start and end of the original text.
virtual void SetDoubleOptionsList(std::span< const double > aOptions)
void onValueCtrlDestroyed(wxWindowDestroyEvent &aEvent)
EDA_UNITS m_units
virtual void SetOptionsList(std::span< const long long int > aOptions)
Set the list of options for a combobox control.
bool IsIndeterminate() const
Return true if the control holds the indeterminate value (for instance, if it represents a multiple s...
void SetDataType(EDA_DATA_TYPE aDataType)
Used to override the datatype of the displayed property (default is DISTANCE)
int GetIntValue() const
void RequireEval()
Force the binder to evaluate the text.
void delayedFocusHandler(wxCommandEvent &aEvent)
EDA_DATA_TYPE m_dataType
ORIGIN_TRANSFORMS::COORD_TYPES_T GetCoordType() const
Get the origin transforms coordinate type.
virtual void SetAngleValue(const EDA_ANGLE &aValue)
void SetLabel(const wxString &aLabel)
virtual void ChangeAngleValue(const EDA_ANGLE &aValue)
virtual void SetDoubleValue(double aValue)
Set new value (in Internal Units) for the text field, taking care of units conversion.
void DetachFromDialogShim()
Sever the back-reference to the owning dialog.
virtual ~UNIT_BINDER() override
bool UnitsInvariant() const
Definition unit_binder.h:72
virtual bool Validate(double aMin, double aMax, EDA_UNITS aUnits=EDA_UNITS::UNSCALED)
Validate the control against the given range, informing the user of any errors found.
virtual void ChangeValue(int aValue)
Set new value (in Internal Units) for the text field, taking care of units conversion WITHOUT trigger...
virtual void SetValue(long long int aValue)
Set new value (in Internal Units) for the text field, taking care of units conversion.
void Show(bool aShow, bool aResize=false)
Show/hide the label, widget and units label.
NUMERIC_EVALUATOR m_eval
void SetCoordType(ORIGIN_TRANSFORMS::COORD_TYPES_T aCoordType)
Set the current origin transform mode.
bool IsNull() const
Return true if the control holds no value (ie: empty string, not 0).
void onUnitsChanged(wxCommandEvent &aEvent)
EDA_DATA_TYPE
The type of unit.
Definition eda_units.h:34
EDA_UNITS
Definition eda_units.h:45
A range bound converted to internal units together with its user-facing display string.