22#include <wx/dcclient.h>
23#include <wx/stattext.h>
24#include <wx/textentry.h>
33 wxStaticText* aUnitLabel ) :
40 wxStaticText* aLabel, wxWindow* aValueCtrl, wxStaticText* aUnitLabel ) :
45 m_iuScale( &aUnitsProvider->GetIuScale() ),
46 m_units( aUnitsProvider->GetUserUnits() ),
47 m_eval( aUnitsProvider->GetUserUnits() ),
75 int minWidth =
dc.GetTextExtent( wxT(
"-XX.XXXXmm - XX%" ) ).GetWidth();
77 if(
minSize.GetWidth() < minWidth )
152 text.Trim(
true ).Trim(
false );
153 return text.IsEmpty();
231 wxString desc = aLabel->GetLabel();
233 desc.EndsWith( wxT(
":" ), &desc );
240 std::optional<int> offset;
241 std::optional<double> ratio;
245 return wxString::Format(
_(
"%s could not be parsed as a distance and/or percentage offset." ),
249 return wxEmptyString;
257 std::optional<int> offset;
258 std::optional<double> ratio;
281 m_units = provider->GetUserUnits();
294 std::optional<double>& aRatio )
const
296 aOffset = std::nullopt;
297 aRatio = std::nullopt;
299 wxString input = aInput;
300 input.Trim(
true ).Trim(
false );
302 if( input.IsEmpty() )
310 bool lastWasOperator =
true;
312 for(
size_t i = 0; i < input.Length(); ++i )
314 wxChar ch = input[i];
316 if( ( ch ==
'+' || ch ==
'-' ) && !lastWasOperator )
322 lastWasOperator =
true;
327 lastWasOperator = ( ch ==
'+' || ch ==
'-' );
333 wxString currentTerm;
335 for(
size_t i = 0; i <
normalized.Length(); ++i )
341 if( !currentTerm.IsEmpty() )
343 currentTerm.Trim(
true ).Trim(
false );
344 terms.Add( currentTerm );
354 if( !currentTerm.IsEmpty() )
356 currentTerm.Trim(
true ).Trim(
false );
357 terms.Add( currentTerm );
361 double totalOffset = 0.0;
362 double totalRatio = 0.0;
363 bool hasOffset =
false;
364 bool hasRatio =
false;
367 for(
const wxString& term : terms )
374 else if( term ==
"-" )
380 bool invalidTerm =
true;
383 if( term.EndsWith(
"%" ) )
385 wxString numPart = term.Left( term.Length() - 1 );
386 numPart.Trim(
true ).Trim(
false );
389 if( numPart.ToDouble( &value ) )
391 totalRatio +=
sign * value / 100.0;
406 if(
result.ToDouble( &value ) )
410 totalOffset +=
sign * iuValue;
424 aOffset =
static_cast<int>( totalOffset );
429 return hasOffset || hasRatio;
438 if( aOffset.has_value() && aOffset.value() != 0 )
444 if( aRatio.has_value() &&
std::abs( aRatio.value() ) > 1e-9 )
446 double percent = aRatio.value() * 100.0;
452 result += wxString::Format(
" + %.4g%%", percent );
454 result += wxString::Format(
" - %.4g%%", -percent );
458 result = wxString::Format(
"%.4g%%", percent );
461 else if( aOffset.has_value() && aOffset.value() == 0 )
473 wxTextEntry* textEntry =
dynamic_cast<wxTextEntry*
>(
m_valueCtrl );
476 return textEntry->GetValue();
478 return wxEmptyString;
484 wxTextEntry* textEntry =
dynamic_cast<wxTextEntry*
>(
m_valueCtrl );
487 textEntry->ChangeValue( aValue );
static std::optional< VECTOR2D > normalized(const VECTOR2D &aVec)
The base frame for deriving all KiCad main window classes.
The base class for create windows for drawing purpose.
bool IsNull() const
Return true if the control holds no value (ie: empty string).
std::optional< int > GetOffsetValue() const
Get the absolute offset value (in Internal Units).
void onTextChanged(wxCommandEvent &aEvent)
MARGIN_OFFSET_BINDER(EDA_DRAW_FRAME *aParent, wxStaticText *aLabel, wxWindow *aValueCtrl, wxStaticText *aUnitLabel)
std::optional< double > GetRatioValue() const
Get the ratio value as a fraction (e.g., -0.05 for -5%).
const EDA_IU_SCALE * m_iuScale
virtual ~MARGIN_OFFSET_BINDER() override
void Enable(bool aEnable)
Enable/disable the label, widget and units label.
void onSetFocus(wxFocusEvent &aEvent)
bool parseInput(const wxString &aInput, std::optional< int > &aOffset, std::optional< double > &aRatio) const
Parse the input string and extract offset and ratio values.
wxStaticText * m_unitLabel
Can be nullptr.
void SetRatioValue(std::optional< double > aRatio)
Set the ratio value as a fraction (e.g., -0.05 for -5%).
std::optional< int > m_cachedOffset
void Show(bool aShow, bool aResize=false)
Show/hide the label, widget and units label.
std::optional< double > m_cachedRatio
wxString getTextValue() const
Get the current text from the control.
void onKillFocus(wxFocusEvent &aEvent)
void setTextValue(const wxString &aValue)
Set the text in the control without triggering events.
wxString formatValue(std::optional< int > aOffset, std::optional< double > aRatio) const
Format the offset and ratio values into a display string.
void onUnitsChanged(wxCommandEvent &aEvent)
void SetOffsetValue(std::optional< int > aValue)
Set the absolute offset value (in Internal Units).
bool Process(const wxString &aString)
const int minSize
Push and Shove router track width and via size dialog.
static wxString valueDescriptionFromLabel(wxStaticText *aLabel)
KICOMMON_API long long int ValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Convert aTextValue in aUnits to internal units used by the application.
KICOMMON_API wxString StringFromValue(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, double aValue, bool aAddUnitsText=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Return the string from aValue according to aUnits (inch, mm ...) for display.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
DATABASE_CONNECTION dc(odbcBackend(cs))
wxString result
Test unit parsing edge cases and error handling.
constexpr int sign(T val)