KiCad PCB EDA Suite
Loading...
Searching...
No Matches
unit_binder.cpp
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#include <wx/clipbrd.h>
23#include <wx/combobox.h>
24#include <wx/stattext.h>
25#include <wx/textentry.h>
26#include <eda_units.h>
27#include <eda_draw_frame.h>
28#include <confirm.h>
29#include <dialog_shim.h>
30
31#include "widgets/unit_binder.h"
33#include "wx/dcclient.h"
34
35using namespace EDA_UNIT_UTILS::UI;
36
37
38wxDEFINE_EVENT( DELAY_FOCUS, wxCommandEvent );
39
40
41UNIT_BINDER::UNIT_BINDER( EDA_DRAW_FRAME* aParent, wxStaticText* aLabel, wxWindow* aValueCtrl,
42 wxStaticText* aUnitLabel, bool allowEval, bool aBindFrameEvents ) :
43 UNIT_BINDER( aParent, aParent, aLabel, aValueCtrl, aUnitLabel, allowEval, aBindFrameEvents )
44{
45}
46
47UNIT_BINDER::UNIT_BINDER( UNITS_PROVIDER* aUnitsProvider, wxWindow* aEventSource, wxStaticText* aLabel,
48 wxWindow* aValueCtrl, wxStaticText* aUnitLabel, bool aAllowEval,
49 bool aBindFocusEvent ) :
50 m_bindFocusEvent( aBindFocusEvent ),
51 m_label( aLabel ),
52 m_valueCtrl( aValueCtrl ),
53 m_eventSource( aEventSource ),
54 m_unitLabel( aUnitLabel ),
55 m_iuScale( &aUnitsProvider->GetIuScale() ),
56 m_units( aUnitsProvider->GetUserUnits() ),
57 m_negativeZero( false ),
59 m_precision( 0 ),
60 m_eval( aUnitsProvider->GetUserUnits() ),
61 m_allowEval( aAllowEval && ( !m_valueCtrl || dynamic_cast<wxTextEntry*>( m_valueCtrl ) ) ),
62 m_needsEval( false ),
63 m_selStart( 0 ),
64 m_selEnd( 0 ),
65 m_unitsInValue( false ),
66 m_originTransforms( aUnitsProvider->GetOriginTransforms() ),
67 m_coordType( ORIGIN_TRANSFORMS::NOT_A_COORD ),
68 m_dialogShim( nullptr )
69{
70 // Two evaluators on one control would fight over its text, and only this one knows the units
71 if( TEXT_CTRL_EVAL* evalCtrl = m_allowEval ? dynamic_cast<TEXT_CTRL_EVAL*>( m_valueCtrl ) : nullptr )
72 evalCtrl->DeferToBinder();
73
74 if( m_valueCtrl )
75 {
76 // Register the UNIT_BINDER for control state save/restore
77 wxWindow* parent = m_valueCtrl->GetParent();
78
79 while( parent && !dynamic_cast<DIALOG_SHIM*>( parent ) )
80 parent = parent->GetParent();
81
82 if( parent )
83 {
84 m_dialogShim = static_cast<DIALOG_SHIM*>( parent );
85 m_dialogShim->RegisterUnitBinder( this, m_valueCtrl );
86 }
87 }
88
89 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( m_valueCtrl );
90
91 if( textEntry )
92 {
93 wxClientDC dc( m_valueCtrl );
94
95 wxSize minSize = m_valueCtrl->GetMinSize();
96 int minWidth;
97
98 if( m_iuScale->IU_PER_MM <= schIUScale.IU_PER_MM )
99 {
100 // PLEditor and SCH editors don't need as much room
101 minWidth = dc.GetTextExtent( wxT( "XXX.XXXXX" ) ).GetWidth();
102 }
103 else
104 {
105 // Gives enough room to display a value in inches in PCBNew
106 // 3 digits + '.' + 8 digits
107 minWidth = dc.GetTextExtent( wxT( "XXX.XXXXXXXX" ) ).GetWidth();
108 }
109
110 if( minSize.GetWidth() < minWidth )
111 m_valueCtrl->SetMinSize( wxSize( minWidth, minSize.GetHeight() ) );
112
113 // Use ChangeValue() instead of SetValue() so we don't generate events.
114 if( m_negativeZero )
115 textEntry->ChangeValue( wxT( "-0" ) );
116 else
117 textEntry->ChangeValue( wxT( "0" ) );
118 }
119
120 if( m_unitLabel )
121 m_unitLabel->SetLabel( EDA_UNIT_UTILS::GetLabel( m_units, m_dataType ) );
122
123 if( m_valueCtrl )
124 {
125 m_valueCtrl->Connect( wxEVT_DESTROY, wxWindowDestroyEventHandler( UNIT_BINDER::onValueCtrlDestroyed ),
126 nullptr, this );
127 m_valueCtrl->Connect( wxEVT_SET_FOCUS, wxFocusEventHandler( UNIT_BINDER::onSetFocus ), nullptr, this );
128 m_valueCtrl->Connect( wxEVT_KILL_FOCUS, wxFocusEventHandler( UNIT_BINDER::onKillFocus ), nullptr, this );
129 m_valueCtrl->Connect( wxEVT_LEFT_UP, wxMouseEventHandler( UNIT_BINDER::onClick ), nullptr, this );
130 m_valueCtrl->Connect( wxEVT_COMBOBOX, wxCommandEventHandler( UNIT_BINDER::onComboBox ), nullptr, this );
131 }
132
133 if( m_bindFocusEvent )
134 Connect( DELAY_FOCUS, wxCommandEventHandler( UNIT_BINDER::delayedFocusHandler ), nullptr, this );
135
136 if( m_eventSource )
137 {
138 m_eventSource->Connect( EDA_EVT_UNITS_CHANGED, wxCommandEventHandler( UNIT_BINDER::onUnitsChanged ),
139 nullptr, this );
140 }
141}
142
143
145{
146 if( m_dialogShim )
147 m_dialogShim->UnregisterUnitBinder( this );
148
149 if( m_valueCtrl )
150 {
151 m_valueCtrl->Disconnect( wxEVT_DESTROY, wxWindowDestroyEventHandler( UNIT_BINDER::onValueCtrlDestroyed ),
152 nullptr, this );
153 m_valueCtrl->Disconnect( wxEVT_SET_FOCUS, wxFocusEventHandler( UNIT_BINDER::onSetFocus ), nullptr, this );
154 m_valueCtrl->Disconnect( wxEVT_KILL_FOCUS, wxFocusEventHandler( UNIT_BINDER::onKillFocus ), nullptr, this );
155 m_valueCtrl->Disconnect( wxEVT_LEFT_UP, wxMouseEventHandler( UNIT_BINDER::onClick ), nullptr, this );
156 m_valueCtrl->Disconnect( wxEVT_COMBOBOX, wxCommandEventHandler( UNIT_BINDER::onComboBox ), nullptr, this );
157 }
158
159 if( m_bindFocusEvent )
160 Disconnect( DELAY_FOCUS, wxCommandEventHandler( UNIT_BINDER::delayedFocusHandler ), nullptr, this );
161
162 if( m_eventSource )
163 {
164 m_eventSource->Disconnect( EDA_EVT_UNITS_CHANGED, wxCommandEventHandler( UNIT_BINDER::onUnitsChanged ),
165 nullptr, this );
166 }
167}
168
169
171{
172 m_units = aUnits;
173
174 m_eval.SetDefaultUnits( m_units );
175 m_eval.LocaleChanged(); // In case locale changed since last run
176
177 if( m_unitLabel )
179}
180
181
182void UNIT_BINDER::SetPrecision( int aLength )
183{
184 m_precision = std::min( aLength, 6 );
185}
186
187
189{
190 m_dataType = aDataType;
191
192 if( m_unitLabel )
194}
195
196
197void UNIT_BINDER::onUnitsChanged( wxCommandEvent& aEvent )
198{
199 if( !UnitsInvariant() )
200 {
201 if( EDA_BASE_FRAME* provider = static_cast<EDA_BASE_FRAME*>( aEvent.GetClientData() ) )
202 {
203 int temp = GetIntValue();
204
205 wxComboBox* const combo = dynamic_cast<wxComboBox*>( m_valueCtrl );
206 std::vector<long long int> comboValues;
207
208 // Read out the current values
209 if( combo )
210 {
211 for( unsigned int i = 0; i < combo->GetCount(); i++ )
212 {
213 const wxString value = combo->GetString( i );
214 long long int conv = ValueFromString( *m_iuScale, m_units, value, m_dataType );
215 comboValues.push_back( conv );
216 }
217 }
218
219 SetUnits( provider->GetUserUnits() );
220 m_iuScale = &provider->GetIuScale();
221
222 // Re-populate the combo box with updated values
223 if( combo )
224 SetOptionsList( comboValues );
225
226 if( !IsIndeterminate() )
227 SetValue( temp );
228 }
229 }
230
231 aEvent.Skip();
232}
233
234
235void UNIT_BINDER::onClick( wxMouseEvent& aEvent )
236{
237 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( m_valueCtrl );
238
239 if( textEntry && ( textEntry->GetValue() == INDETERMINATE_ACTION
240 || textEntry->GetValue() == INDETERMINATE_STATE ) )
241 {
242 // These are tokens, not strings, so do a select all
243 textEntry->SelectAll();
244 }
245
246 // Needed at least on Windows to avoid hanging
247 aEvent.Skip();
248}
249
250
251void UNIT_BINDER::onComboBox( wxCommandEvent& aEvent )
252{
253 wxComboBox* combo = dynamic_cast<wxComboBox*>( m_valueCtrl );
254 wxCHECK( combo, /*void*/ );
255
256 const wxString value = combo->GetStringSelection();
257 const long long int conv = ValueFromString( *m_iuScale, m_units, value, m_dataType );
258
259 CallAfter(
260 [this, conv]
261 {
262 SetValue( conv );
263 } );
264
265 aEvent.Skip();
266}
267
268
269void UNIT_BINDER::onValueCtrlDestroyed( wxWindowDestroyEvent& aEvent )
270{
271 // The bound control is being destroyed before this binder. Drop the dialog registration and
272 // the control reference so neither the binder destructor nor SaveControlState() touches the
273 // freed window.
274 if( aEvent.GetEventObject() == m_valueCtrl )
275 {
276 if( m_dialogShim )
277 {
278 m_dialogShim->UnregisterUnitBinder( this );
279 m_dialogShim = nullptr;
280 }
281
282 m_valueCtrl = nullptr;
283 }
284
285 aEvent.Skip();
286}
287
288
289void UNIT_BINDER::onSetFocus( wxFocusEvent& aEvent )
290{
291 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( m_valueCtrl );
292
293 if( textEntry )
294 {
295 if( m_allowEval )
296 {
297 wxString oldStr = m_eval.OriginalText();
298
299 if( oldStr.length() && oldStr != textEntry->GetValue() )
300 {
301 textEntry->ChangeValue( oldStr );
302 textEntry->SetSelection( m_selStart, m_selEnd );
303 }
304
305 m_needsEval = true;
306 }
307
308 if( textEntry->GetValue() == INDETERMINATE_ACTION
309 || textEntry->GetValue() == INDETERMINATE_STATE )
310 {
311 // These are tokens, not strings, so do a select all
312 textEntry->SelectAll();
313 }
314 }
315
316 aEvent.Skip();
317}
318
319
320void UNIT_BINDER::onKillFocus( wxFocusEvent& aEvent )
321{
322 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( m_valueCtrl );
323
324 if( m_allowEval && textEntry )
325 {
326 wxString value = textEntry->GetValue();
327 bool success = m_eval.Process( value );
328
329 if( success && !value.IsEmpty() )
330 {
331 textEntry->GetSelection( &m_selStart, &m_selEnd );
332
333 value = m_eval.Result();
334
335 if( m_unitsInValue && !value.IsEmpty() )
336 {
338 value += wxT( " " );
339
341 }
342
343 textEntry->ChangeValue( value );
344
345#ifdef __WXGTK__
346 // Manually copy the selected text to the primary selection clipboard
347 if( wxTheClipboard->Open() )
348 {
349 wxString sel = textEntry->GetStringSelection();
350 bool clipTarget = wxTheClipboard->IsUsingPrimarySelection();
351 wxTheClipboard->UsePrimarySelection( true );
352 wxTheClipboard->SetData( new wxTextDataObject( sel ) );
353 wxTheClipboard->UsePrimarySelection( clipTarget );
354 wxTheClipboard->Close();
355 }
356#endif
357 }
358
359 m_needsEval = false;
360 }
361
362 aEvent.Skip();
363}
364
365
366static wxString valueDescriptionFromLabel( wxStaticText* aLabel )
367{
368 wxString desc = aLabel->GetLabel();
369
370 desc.EndsWith( wxT( ":" ), &desc );
371 return desc;
372}
373
374
375void UNIT_BINDER::delayedFocusHandler( wxCommandEvent& )
376{
377 if( !m_errorMessage.IsEmpty() )
379
380 m_errorMessage = wxEmptyString;
381 m_valueCtrl->SetFocus();
382}
383
384
386 double aBound, EDA_UNITS aDisplayUnits,
387 EDA_DATA_TYPE aDataType )
388{
389 // StringFromValue scales the user value once per dimension (twice for area, thrice for
390 // volume), so invert that same chain to bring the bound into internal units.
391 int conversions = 1;
392
393 switch( aDataType )
394 {
395 case EDA_DATA_TYPE::AREA: conversions = 2; break;
396 case EDA_DATA_TYPE::VOLUME: conversions = 3; break;
397 case EDA_DATA_TYPE::UNITLESS: conversions = 0; break;
398 case EDA_DATA_TYPE::ANGLE: conversions = 0; break;
399 default: conversions = 1; break;
400 }
401
402 double boundIU = aBound;
403
404 for( int i = 0; i < conversions; ++i )
405 boundIU = FromUserUnit( aIuScale, aBoundUnits, boundIU );
406
407 // GetText has no UNITLESS label, so asking for units text would assert
408 bool addUnitsText = aDataType != EDA_DATA_TYPE::UNITLESS;
409
410 return { boundIU, StringFromValue( aIuScale, aDisplayUnits, boundIU, addUnitsText, aDataType ) };
411}
412
413
414bool UNIT_BINDER::Validate( double aMin, double aMax, EDA_UNITS aUnits )
415{
416 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( m_valueCtrl );
417
418 if( !textEntry
419 || textEntry->GetValue() == INDETERMINATE_ACTION
420 || textEntry->GetValue() == INDETERMINATE_STATE )
421 {
422 return true;
423 }
424
425 // Note: aMin and aMax are not always given in internal units
426 RANGE_BOUND minBound = ConvertRangeBound( *m_iuScale, aUnits, aMin, m_units, m_dataType );
427 RANGE_BOUND maxBound = ConvertRangeBound( *m_iuScale, aUnits, aMax, m_units, m_dataType );
428
429 if( GetValue() < minBound.internalUnits )
430 {
431 m_errorMessage = wxString::Format( _( "%s must be at least %s." ),
433 minBound.displayText );
434
435 textEntry->SelectAll();
436
437 // Don't focus directly; we might be inside a KillFocus event handler
438 wxPostEvent( this, wxCommandEvent( DELAY_FOCUS ) );
439
440 return false;
441 }
442
443 if( GetValue() > maxBound.internalUnits )
444 {
445 m_errorMessage = wxString::Format( _( "%s must be less than %s." ),
447 maxBound.displayText );
448
449 textEntry->SelectAll();
450
451 // Don't focus directly; we might be inside a KillFocus event handler
452 wxPostEvent( this, wxCommandEvent( DELAY_FOCUS ) );
453
454 return false;
455 }
456
457 return true;
458}
459
460
461void UNIT_BINDER::SetValue( long long int aValue )
462{
463 double displayValue = m_originTransforms.ToDisplay( aValue, m_coordType );
464 wxString textValue = StringFromValue( *m_iuScale, m_units, displayValue, false, m_dataType );
465 wxString formula = formulaYielding( aValue );
466
467 if( displayValue == 0 && m_negativeZero )
468 SetValue( wxT( "-" ) + textValue );
469 else
470 SetValue( textValue );
471
472 if( !formula.IsEmpty() )
473 m_eval.Process( formula );
474}
475
476
477void UNIT_BINDER::SetDoubleValue( double aValue )
478{
479 double displayValue = m_originTransforms.ToDisplay( aValue, m_coordType );
480 wxString textValue = StringFromValue( *m_iuScale, m_units, setPrecision( displayValue, false ), false,
481 m_dataType );
482
483 if( displayValue == 0 && !std::signbit( displayValue ) && m_negativeZero )
484 SetValue( wxT( "-" ) + textValue );
485 else
486 SetValue( textValue );
487}
488
489
491{
492 SetDoubleValue( m_originTransforms.ToDisplay( aValue, m_coordType ) );
493}
494
495
496void UNIT_BINDER::SetValue( const wxString& aValue )
497{
498 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( m_valueCtrl );
499 wxStaticText* staticText = dynamic_cast<wxStaticText*>( m_valueCtrl );
500
501 wxString value = aValue;
502
503 if( m_unitsInValue && !value.IsEmpty() )
504 {
506 value += wxT( " " );
507
509 }
510
511 if( textEntry )
512 textEntry->SetValue( value );
513 else if( staticText )
514 staticText->SetLabel( value );
515
516 if( m_allowEval )
517 m_eval.Clear();
518
519 if( m_unitLabel )
521
522}
523
524
525wxString UNIT_BINDER::getTextForValue( long long int aValue ) const
526{
527 const double displayValue = m_originTransforms.ToDisplay( aValue, m_coordType );
528 wxString textValue = StringFromValue( *m_iuScale, m_units, setPrecision( displayValue, false ), false,
529 m_dataType );
530
531 if( displayValue == 0 && m_negativeZero )
532 textValue = wxT( "-" ) + textValue;
533
534 return textValue;
535}
536
537
538wxString UNIT_BINDER::getTextForDoubleValue( double aValue ) const
539{
540 const double displayValue = m_originTransforms.ToDisplay( aValue, m_coordType );
541 wxString textValue = StringFromValue( *m_iuScale, m_units, setPrecision( displayValue, false ), false,
542 m_dataType );
543
544 if( displayValue == 0 && !std::signbit( displayValue ) && m_negativeZero )
545 textValue = wxT( "-" ) + textValue;
546
547 return textValue;
548}
549
550
551wxString UNIT_BINDER::formulaYielding( long long int aValue ) const
552{
553 wxString formula = m_eval.OriginalText();
554
555 // Evaluate afresh as the units may have changed since the expression was entered
556 if( !m_allowEval || m_needsEval || formula.IsEmpty() || !m_eval.Process( formula ) )
557 return wxEmptyString;
558
559 long long int displayValue = ValueFromString( *m_iuScale, m_units, m_eval.Result(), m_dataType );
560
561 if( std::abs( m_originTransforms.FromDisplay( displayValue, m_coordType ) - aValue ) > 1 )
562 return wxEmptyString;
563
564 return formula;
565}
566
567
568void UNIT_BINDER::ChangeValue( int aValue )
569{
570 wxString formula = formulaYielding( aValue );
571
572 ChangeValue( getTextForValue( aValue ) );
573
574 if( !formula.IsEmpty() )
575 m_eval.Process( formula );
576}
577
578
580{
582}
583
584
586{
587 ChangeDoubleValue( m_originTransforms.ToDisplay( aValue, m_coordType ) );
588}
589
590
591void UNIT_BINDER::ChangeValue( const wxString& aValue )
592{
593 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( m_valueCtrl );
594 wxStaticText* staticText = dynamic_cast<wxStaticText*>( m_valueCtrl );
595
596 wxString value = aValue;
597
598 if( m_unitsInValue && !value.IsEmpty() )
599 {
601 value += wxT( " " );
602
604 }
605
606 if( textEntry )
607 textEntry->ChangeValue( value );
608 else if( staticText )
609 staticText->SetLabel( value );
610
611 if( m_allowEval )
612 m_eval.Clear();
613
614 if( m_unitLabel )
616}
617
618
620{
621 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( m_valueCtrl );
622
623 if( !m_allowEval || !textEntry )
624 return wxEmptyString;
625
626 // The control holds the expression while it is being edited and its result afterwards
627 wxString formula = m_needsEval ? textEntry->GetValue() : m_eval.OriginalText();
628 double plainNumber;
629
630 formula.Trim( true ).Trim( false );
631
632 if( formula.IsEmpty() || formula.ToDouble( &plainNumber ) || formula.ToCDouble( &plainNumber ) )
633 return wxEmptyString;
634
635 return m_eval.Process( formula ) ? formula : wxString();
636}
637
638
639bool UNIT_BINDER::RestoreFormula( const wxString& aFormula )
640{
641 if( !m_allowEval || !dynamic_cast<wxTextEntry*>( m_valueCtrl ) || aFormula.IsEmpty() )
642 return false;
643
644 if( !m_eval.Process( aFormula ) )
645 {
646 // A failed expression must not be handed back to the control on the next focus
647 m_eval.Clear();
648 return false;
649 }
650
651 // ChangeValue() resets the evaluator, so the expression has to be processed again afterwards
652 ChangeValue( m_eval.Result() );
653 m_eval.Process( aFormula );
654 m_needsEval = false;
655
656 return true;
657}
658
659
660void UNIT_BINDER::RestoreValue( int aValue, const wxString& aFormula )
661{
662 RestoreFormula( aFormula );
663 ChangeValue( aValue );
664}
665
666
667long long int UNIT_BINDER::GetValue() const
668{
669 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( m_valueCtrl );
670 wxStaticText* staticText = dynamic_cast<wxStaticText*>( m_valueCtrl );
671 wxString value;
672
673 if( textEntry )
674 {
675 value = textEntry->GetValue();
676
677 if( m_needsEval && !value.IsEmpty() && m_eval.Process( value ) )
678 value = m_eval.Result();
679 else
680 value = textEntry->GetValue();
681 }
682 else if( staticText )
683 {
684 value = staticText->GetLabel();
685 }
686 else
687 {
688 return 0;
689 }
690
691 long long int displayValue = ValueFromString( *m_iuScale, m_units, value, m_dataType );
692 return m_originTransforms.FromDisplay( displayValue, m_coordType );
693}
694
695
696double UNIT_BINDER::setPrecision( double aValue, bool aValueUsesUserUnits ) const
697{
698 if( m_precision > 1 )
699 {
700 int scale = pow( 10, m_precision );
701 int64_t tmp = aValue;
702
703 if( !aValueUsesUserUnits )
704 tmp = ToUserUnit( *m_iuScale, m_units, aValue ) * scale;
705
706 aValue = static_cast<double>( tmp ) / scale;
707
708 if( !aValueUsesUserUnits )
709 aValue = FromUserUnit( *m_iuScale, m_units, aValue );
710 }
711
712 return aValue;
713}
714
715
717{
718 wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( m_valueCtrl );
719 wxStaticText* staticText = dynamic_cast<wxStaticText*>( m_valueCtrl );
720 wxString value;
721
722 if( textEntry )
723 {
724 value = textEntry->GetValue();
725
726 if( m_needsEval && !value.IsEmpty() && m_eval.Process( value ) )
727 value = m_eval.Result();
728 else
729 value = textEntry->GetValue();
730 }
731 else if( staticText )
732 {
733 value = staticText->GetLabel();
734 }
735 else
736 {
737 return 0.0;
738 }
739
740 double displayValue = DoubleValueFromString( *m_iuScale, m_units, value, m_dataType );
741 displayValue = setPrecision( displayValue, false );
742
743 return m_originTransforms.FromDisplay( displayValue, m_coordType );
744}
745
746
751
752
753void UNIT_BINDER::SetOptionsList( std::span<const long long int> aOptions )
754{
755 wxComboBox* cb = dynamic_cast<wxComboBox*>( m_valueCtrl );
756 wxCHECK( cb, /* void */ );
757
758 cb->Clear();
759
760 for( long long int value : aOptions )
761 cb->Append( getTextForValue( value ) );
762}
763
764
765void UNIT_BINDER::SetDoubleOptionsList( std::span<const double> aOptions )
766{
767 wxComboBox* cb = dynamic_cast<wxComboBox*>( m_valueCtrl );
768 wxCHECK( cb, /* void */ );
769
770 cb->Clear();
771
772 for( double value : aOptions )
773 cb->Append( getTextForDoubleValue( value ) );
774}
775
776
778{
779 wxTextEntry* te = dynamic_cast<wxTextEntry*>( m_valueCtrl );
780
781 if( te )
782 return te->GetValue() == INDETERMINATE_STATE || te->GetValue() == INDETERMINATE_ACTION;
783
784 return false;
785}
786
787
789{
790 if( wxTextEntry* te = dynamic_cast<wxTextEntry*>( m_valueCtrl ) )
791 return te->GetValue().IsEmpty();
792
793 return false;
794}
795
796
798{
799 if( wxTextEntry* te = dynamic_cast<wxTextEntry*>( m_valueCtrl ) )
800 return te->SetValue( wxEmptyString );
801}
802
803
804void UNIT_BINDER::SetLabel( const wxString& aLabel )
805{
806 m_label->SetLabel( aLabel );
807}
808
809
810void UNIT_BINDER::Enable( bool aEnable )
811{
812 if( m_label )
813 m_label->Enable( aEnable );
814
815 m_valueCtrl->Enable( aEnable );
816
817 if( m_unitLabel )
818 m_unitLabel->Enable( aEnable );
819}
820
821
822void UNIT_BINDER::Show( bool aShow, bool aResize )
823{
824 m_label->Show( aShow );
825 m_valueCtrl->Show( aShow );
826
827 if( m_unitLabel )
828 m_unitLabel->Show( aShow );
829
830 if( aResize )
831 {
832 if( aShow )
833 {
834 m_label->SetSize( -1, -1 );
835 m_valueCtrl->SetSize( -1, -1 );
836
837 if( m_unitLabel )
838 m_unitLabel->SetSize( -1, -1 );
839 }
840 else
841 {
842 m_label->SetSize( 0, 0 );
843 m_valueCtrl->SetSize( 0, 0 );
844
845 if( m_unitLabel )
846 m_unitLabel->SetSize( 0, 0 );
847 }
848 }
849}
850
851
853 UNIT_BINDER( aParent, nullptr, nullptr, nullptr, true, false )
854{
855 m_unitsInValue = true;
856}
857
858
862
864{
865 m_valueCtrl = aControl;
866
867 if( m_valueCtrl )
868 {
869 m_valueCtrl->Bind( wxEVT_SET_FOCUS, &PROPERTY_EDITOR_UNIT_BINDER::onSetFocus, this );
870 m_valueCtrl->Bind( wxEVT_KILL_FOCUS, &PROPERTY_EDITOR_UNIT_BINDER::onKillFocus, this );
871 m_valueCtrl->Bind( wxEVT_LEFT_UP, &PROPERTY_EDITOR_UNIT_BINDER::onClick, this );
872
873 m_valueCtrl->Bind( wxEVT_SHOW,
874 [&]( wxShowEvent& e )
875 {
876 if( !e.IsShown() )
877 SetControl( nullptr );
878 } );
879 }
880}
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:130
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...
void SetControl(wxWindow *aControl)
PROPERTY_EDITOR_UNIT_BINDER(EDA_DRAW_FRAME *aParent)
wxTextCtrl wrapper to handle math expression evaluation.
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 (...
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.
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 delayedFocusHandler(wxCommandEvent &aEvent)
EDA_DATA_TYPE m_dataType
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.
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
bool IsNull() const
Return true if the control holds no value (ie: empty string, not 0).
void onUnitsChanged(wxCommandEvent &aEvent)
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
const int minSize
Push and Shove router track width and via size dialog.
#define _(s)
@ DEGREES_T
Definition eda_angle.h:30
EDA_DATA_TYPE
The type of unit.
Definition eda_units.h:34
EDA_UNITS
Definition eda_units.h:45
static wxString valueDescriptionFromLabel(wxStaticText *aLabel)
KICOMMON_API double FromUserUnit(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnit, double aValue)
Return in internal units the value aValue given in a real unit such as "in", "mm",...
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.
KICOMMON_API double DoubleValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Convert aTextValue to a double.
KICOMMON_API double ToUserUnit(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnit, double aValue)
Convert aValue in internal units to the appropriate user units defined by aUnit.
KICOMMON_API wxString GetLabel(EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Get the units string for a given units type.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:461
const int scale
A range bound converted to internal units together with its user-facing display string.
DATABASE_CONNECTION dc(odbcBackend(cs))
#define INDETERMINATE_ACTION
Definition ui_common.h:49
#define INDETERMINATE_STATE
Used for holding indeterminate values, such as with multiple selections holding different values or c...
Definition ui_common.h:48
wxDEFINE_EVENT(DELAY_FOCUS, wxCommandEvent)
static wxString valueDescriptionFromLabel(wxStaticText *aLabel)