KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_numeric_input_overlay_panel.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) 2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
24
25#include <eda_base_frame.h>
26#include <base_units.h>
27#include <i18n_utility.h>
28#include <widgets/unit_binder.h>
29
30#include <wx/textctrl.h>
31
32
35 EDA_UNITS aUnits ) :
37 m_data( aData ),
39{
40 SetBackgroundBitmap( m_data->GetOverlayBitmap() );
41
42 std::vector<DRC_RE_FIELD_POSITION> positions = m_data->GetFieldPositions();
43
44 wxWindow* eventSource = nullptr;
45
46 for( wxWindow* win = aParent; win; win = win->GetParent() )
47 {
48 if( dynamic_cast<EDA_BASE_FRAME*>( win ) )
49 {
50 eventSource = win;
51 break;
52 }
53 }
54
55 auto* valueField = AddField<wxTextCtrl>( wxS( "value" ), positions[0], wxTE_PROCESS_ENTER | wxTE_CENTRE );
56 m_valueBinder = std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, valueField->GetControl(),
57 valueField->GetLabel(), false, false );
58 valueField->SetUnitBinder( m_valueBinder.get() );
59
60 valueField->GetControl()->SetValidator( VALIDATOR_NUMERIC_CTRL( false, m_data->IsIntegerOnly() ) );
61
64}
65
66
68{
69 if( !m_data )
70 return false;
71
72 if( m_data->IsIntegerOnly() || m_data->IsUnitless() )
73 {
74 auto* ctrl = dynamic_cast<wxTextCtrl*>( m_fields[0]->GetControl() );
75
76 if( ctrl )
77 {
78 const double value = m_data->GetNumericInputValue();
79
80 if( m_data->IsIntegerOnly() )
81 ctrl->ChangeValue( wxString::Format( "%d", (int) value ) );
82 else
83 ctrl->ChangeValue( wxString::Format( "%g", value ) );
84 }
85 }
86 else
87 {
88 m_valueBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetNumericInputValue() ) );
89 }
90
91 return true;
92}
93
94
96{
97 if( !m_data )
98 return false;
99
100 if( m_data->IsIntegerOnly() || m_data->IsUnitless() )
101 {
102 auto* ctrl = dynamic_cast<wxTextCtrl*>( m_fields[0]->GetControl() );
103
104 if( ctrl )
105 {
106 wxString text = ctrl->GetValue().Strip( wxString::both );
107
108 if( m_data->IsIntegerOnly() )
109 {
110 long val = 0;
111 text.ToLong( &val );
112 m_data->SetNumericInputValue( static_cast<double>( val ) );
113 }
114 else
115 {
117 m_data->SetNumericInputValue( val );
118 }
119 }
120 }
121 else
122 {
123 m_data->SetNumericInputValue( pcbIUScale.IUTomm( m_valueBinder->GetDoubleValue() ) );
124 }
125
126 return true;
127}
128
129
131 wxString* aValidationMessage )
132{
134
135 wxTextCtrl* ctrl = dynamic_cast<wxTextCtrl*>( m_fields[0]->GetControl() ); // "value" field is index 0
136
137 if( ctrl )
138 {
139 wxValidator* validator = ctrl->GetValidator();
140
141 if( validator && !validator->Validate( this ) )
142 {
143 auto* numValidator = dynamic_cast<VALIDATOR_NUMERIC_CTRL*>( validator );
144
145 if( numValidator )
146 {
147 wxString errorMsg;
148
149 switch( numValidator->GetValidationState() )
150 {
152 errorMsg = _( "Value is required." );
153 break;
155 errorMsg = _( "Value must be a number." );
156 break;
158 errorMsg = _( "Value must be a whole number." );
159 break;
161 errorMsg = _( "Value must be greater than 0." );
162 break;
163 default:
164 break;
165 }
166
167 if( !errorMsg.IsEmpty() )
168 {
169 ( *aErrorCount )++;
170 *aValidationMessage += DRC_RULE_EDITOR_UTILS::FormatErrorMessage( *aErrorCount, errorMsg );
171 ShowFieldError( wxS( "value" ), errorMsg );
172 return false;
173 }
174 }
175 }
176 }
177
179
180 VALIDATION_RESULT result = m_data->Validate();
181
182 if( !result.isValid )
183 {
184 *aErrorCount = result.errors.size();
185
186 for( size_t i = 0; i < result.errors.size(); i++ )
187 *aValidationMessage += DRC_RULE_EDITOR_UTILS::FormatErrorMessage( i + 1, result.errors[i] );
188
189 return false;
190 }
191
192 return true;
193}
194
195
197{
198 if( !m_data )
199 return wxEmptyString;
200
201 return m_data->GenerateRule( aContext );
202}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
DRC_RE_OVERLAY_FIELD * AddField(const wxString &aId, const DRC_RE_FIELD_POSITION &aPosition, long aStyle=0)
Create and position a field control on the bitmap overlay.
void ShowFieldError(const wxString &aFieldId, const wxString &aMessage)
Show an error indicator on the specified field.
void SetBackgroundBitmap(BITMAPS aBitmap)
Set the background bitmap for this panel.
DRC_RE_BITMAP_OVERLAY_PANEL(wxWindow *aParent, wxWindowID aId=wxID_ANY)
void ClearFieldErrors()
Clear error indicators from all fields.
void PositionFields()
Position all fields based on the current scale factor.
std::vector< std::unique_ptr< DRC_RE_OVERLAY_FIELD > > m_fields
All overlay fields.
DRC_RE_NUMERIC_INPUT_OVERLAY_PANEL(wxWindow *aParent, DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA *aData, EDA_UNITS aUnits)
bool ValidateInputs(int *aErrorCount, wxString *aValidationMessage) override
wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext) override
DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA * m_data
static wxString FormatErrorMessage(int aErrorCount, const wxString &aErrorMessage)
The base frame for deriving all KiCad main window classes.
#define _(s)
Base window classes and related definitions.
EDA_UNITS
Definition eda_units.h:45
Some functions to handle hotkeys in KiCad.
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.
Result of a validation operation.
JSON_SCHEMA_VALIDATOR validator(schema)
wxString result
Test unit parsing edge cases and error handling.