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 <widgets/unit_binder.h>
29
30#include <wx/textctrl.h>
31
32
34 wxWindow* aParent, DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA* aData, EDA_UNITS aUnits ) :
36 m_data( aData ),
38{
39 SetBackgroundBitmap( m_data->GetOverlayBitmap() );
40
41 std::vector<DRC_RE_FIELD_POSITION> positions = m_data->GetFieldPositions();
42
43 wxWindow* eventSource = nullptr;
44
45 for( wxWindow* win = aParent; win; win = win->GetParent() )
46 {
47 if( dynamic_cast<EDA_BASE_FRAME*>( win ) )
48 {
49 eventSource = win;
50 break;
51 }
52 }
53
54 auto* valueField = AddField<wxTextCtrl>( wxS( "value" ), positions[0], wxTE_PROCESS_ENTER | wxTE_CENTRE );
55 m_valueBinder = std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, valueField->GetControl(),
56 valueField->GetLabel(), false, false );
57 valueField->SetUnitBinder( m_valueBinder.get() );
58
59 valueField->GetControl()->SetValidator( VALIDATOR_NUMERIC_CTRL( false, m_data->IsIntegerOnly() ) );
60
61 auto notifyModified = [this]( wxCommandEvent& )
62 {
64 if( dlg )
65 dlg->SetModified();
66 };
67
68 auto notifySave = [this]( wxCommandEvent& aEvent )
69 {
71 if( dlg )
72 dlg->OnSave( aEvent );
73 };
74
75 valueField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
76 valueField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
77
80}
81
82
84{
85 if( !m_data )
86 return false;
87
88 if( m_data->IsIntegerOnly() )
89 {
90 auto* ctrl = dynamic_cast<wxTextCtrl*>( m_fields[0]->GetControl() );
91
92 if( ctrl )
93 ctrl->ChangeValue( wxString::Format( "%d", (int) m_data->GetNumericInputValue() ) );
94 }
95 else
96 {
97 m_valueBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetNumericInputValue() ) );
98 }
99
100 return true;
101}
102
103
105{
106 if( !m_data )
107 return false;
108
109 if( m_data->IsIntegerOnly() )
110 {
111 auto* ctrl = dynamic_cast<wxTextCtrl*>( m_fields[0]->GetControl() );
112
113 if( ctrl )
114 {
115 long val = 0;
116 ctrl->GetValue().Strip( wxString::both ).ToLong( &val );
117 m_data->SetNumericInputValue( static_cast<double>( val ) );
118 }
119 }
120 else
121 {
122 m_data->SetNumericInputValue( pcbIUScale.IUTomm( m_valueBinder->GetDoubleValue() ) );
123 }
124
125 return true;
126}
127
128
130 wxString* aValidationMessage )
131{
133
134 wxTextCtrl* ctrl = dynamic_cast<wxTextCtrl*>( m_fields[0]->GetControl() ); // "value" field is index 0
135
136 if( ctrl )
137 {
138 wxValidator* validator = ctrl->GetValidator();
139
140 if( validator && !validator->Validate( this ) )
141 {
142 auto* numValidator = dynamic_cast<VALIDATOR_NUMERIC_CTRL*>( validator );
143
144 if( numValidator )
145 {
146 wxString errorMsg;
147
148 switch( numValidator->GetValidationState() )
149 {
150 case VALIDATOR_NUMERIC_CTRL::VALIDATION_STATE::Empty: errorMsg = wxS( "Value is required." ); break;
152 errorMsg = wxS( "Value must be a number." );
153 break;
155 errorMsg = wxS( "Value must be a whole number." );
156 break;
158 errorMsg = wxS( "Value must be greater than 0." );
159 break;
160 default: break;
161 }
162
163 if( !errorMsg.IsEmpty() )
164 {
165 ( *aErrorCount )++;
166 *aValidationMessage += DRC_RULE_EDITOR_UTILS::FormatErrorMessage( *aErrorCount, errorMsg );
167 ShowFieldError( wxS( "value" ) );
168 return false;
169 }
170 }
171 }
172 }
173
175
176 VALIDATION_RESULT result = m_data->Validate();
177
178 if( !result.isValid )
179 {
180 *aErrorCount = result.errors.size();
181
182 for( size_t i = 0; i < result.errors.size(); i++ )
183 *aValidationMessage += DRC_RULE_EDITOR_UTILS::FormatErrorMessage( i + 1, result.errors[i] );
184
185 return false;
186 }
187
188 return true;
189}
190
191
193{
194 if( !m_data )
195 return wxEmptyString;
196
197 return m_data->GenerateRule( aContext );
198}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
void ShowFieldError(const wxString &aFieldId)
Show an error indicator on the specified field.
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 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
Base window classes and related definitions.
EDA_UNITS
Definition eda_units.h:44
Result of a validation operation.
wxString result
Test unit parsing edge cases and error handling.