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
26#include <eda_base_frame.h>
27#include <base_units.h>
28#include <i18n_utility.h>
29#include <widgets/unit_binder.h>
30
31#include <wx/textctrl.h>
32
33
35 wxWindow* aParent, DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA* aData, 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
62 auto notifyModified = [this]( wxCommandEvent& )
63 {
65 if( dlg )
66 dlg->SetModified();
67 };
68
69 auto notifySave = [this]( wxCommandEvent& aEvent )
70 {
72 if( dlg )
73 dlg->OnSave( aEvent );
74 };
75
76 valueField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
77 valueField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
78
81}
82
83
85{
86 if( !m_data )
87 return false;
88
89 if( m_data->IsIntegerOnly() )
90 {
91 auto* ctrl = dynamic_cast<wxTextCtrl*>( m_fields[0]->GetControl() );
92
93 if( ctrl )
94 ctrl->ChangeValue( wxString::Format( "%d", (int) m_data->GetNumericInputValue() ) );
95 }
96 else
97 {
98 m_valueBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetNumericInputValue() ) );
99 }
100
101 return true;
102}
103
104
106{
107 if( !m_data )
108 return false;
109
110 if( m_data->IsIntegerOnly() )
111 {
112 auto* ctrl = dynamic_cast<wxTextCtrl*>( m_fields[0]->GetControl() );
113
114 if( ctrl )
115 {
116 long val = 0;
117 ctrl->GetValue().Strip( wxString::both ).ToLong( &val );
118 m_data->SetNumericInputValue( static_cast<double>( val ) );
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 {
151 case VALIDATOR_NUMERIC_CTRL::VALIDATION_STATE::Empty: errorMsg = _( "Value is required." ); break;
153 errorMsg = _( "Value must be a number." );
154 break;
156 errorMsg = _( "Value must be a whole number." );
157 break;
159 errorMsg = _( "Value must be greater than 0." );
160 break;
161 default: break;
162 }
163
164 if( !errorMsg.IsEmpty() )
165 {
166 ( *aErrorCount )++;
167 *aValidationMessage += DRC_RULE_EDITOR_UTILS::FormatErrorMessage( *aErrorCount, errorMsg );
168 ShowFieldError( wxS( "value" ), errorMsg );
169 return false;
170 }
171 }
172 }
173 }
174
176
177 VALIDATION_RESULT result = m_data->Validate();
178
179 if( !result.isValid )
180 {
181 *aErrorCount = result.errors.size();
182
183 for( size_t i = 0; i < result.errors.size(); i++ )
184 *aValidationMessage += DRC_RULE_EDITOR_UTILS::FormatErrorMessage( i + 1, result.errors[i] );
185
186 return false;
187 }
188
189 return true;
190}
191
192
194{
195 if( !m_data )
196 return wxEmptyString;
197
198 return m_data->GenerateRule( aContext );
199}
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.
void SetModified()
Marks the dialog as modified, indicating unsaved changes.
static RULE_EDITOR_DIALOG_BASE * GetDialog(wxWindow *aWindow)
Static method to retrieve the rule editor dialog instance associated with a given window.
virtual void OnSave(wxCommandEvent &aEvent)=0
#define _(s)
Base window classes and related definitions.
EDA_UNITS
Definition eda_units.h:44
Some functions to handle hotkeys in KiCad.
Result of a validation operation.
JSON_SCHEMA_VALIDATOR validator(schema)
wxString result
Test unit parsing edge cases and error handling.