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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
28
30#include <eda_base_frame.h>
31#include <base_units.h>
32#include <widgets/unit_binder.h>
33
34#include <wx/textctrl.h>
35
36
38 wxWindow* aParent, DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA* aData, EDA_UNITS aUnits ) :
40 m_data( aData ),
42{
43 SetBackgroundBitmap( m_data->GetOverlayBitmap() );
44
45 std::vector<DRC_RE_FIELD_POSITION> positions = m_data->GetFieldPositions();
46
47 wxWindow* eventSource = nullptr;
48
49 for( wxWindow* win = aParent; win; win = win->GetParent() )
50 {
51 if( dynamic_cast<EDA_BASE_FRAME*>( win ) )
52 {
53 eventSource = win;
54 break;
55 }
56 }
57
58 auto* valueField = AddField<wxTextCtrl>( wxS( "value" ), positions[0], wxTE_PROCESS_ENTER | wxTE_CENTRE );
59 m_valueBinder = std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, valueField->GetControl(),
60 valueField->GetLabel(), false, false );
61 valueField->SetUnitBinder( m_valueBinder.get() );
62
63 valueField->GetControl()->SetValidator( VALIDATOR_NUMERIC_CTRL( false, m_data->IsIntegerOnly() ) );
64
65 auto notifyModified = [this]( wxCommandEvent& )
66 {
68 if( dlg )
69 dlg->SetModified();
70 };
71
72 auto notifySave = [this]( wxCommandEvent& aEvent )
73 {
75 if( dlg )
76 dlg->OnSave( aEvent );
77 };
78
79 valueField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
80 valueField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
81
84}
85
86
88{
89 if( !m_data )
90 return false;
91
92 if( m_data->IsIntegerOnly() )
93 {
94 auto* ctrl = dynamic_cast<wxTextCtrl*>( m_fields[0]->GetControl() );
95
96 if( ctrl )
97 ctrl->ChangeValue( wxString::Format( "%d", (int) m_data->GetNumericInputValue() ) );
98 }
99 else
100 {
101 m_valueBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetNumericInputValue() ) );
102 }
103
104 return true;
105}
106
107
109{
110 if( !m_data )
111 return false;
112
113 if( m_data->IsIntegerOnly() )
114 {
115 auto* ctrl = dynamic_cast<wxTextCtrl*>( m_fields[0]->GetControl() );
116
117 if( ctrl )
118 {
119 long val = 0;
120 ctrl->GetValue().Strip( wxString::both ).ToLong( &val );
121 m_data->SetNumericInputValue( static_cast<double>( val ) );
122 }
123 }
124 else
125 {
126 m_data->SetNumericInputValue( pcbIUScale.IUTomm( m_valueBinder->GetDoubleValue() ) );
127 }
128
129 return true;
130}
131
132
134 wxString* aValidationMessage )
135{
137
138 wxTextCtrl* ctrl = dynamic_cast<wxTextCtrl*>( m_fields[0]->GetControl() ); // "value" field is index 0
139
140 if( ctrl )
141 {
142 wxValidator* validator = ctrl->GetValidator();
143
144 if( validator && !validator->Validate( this ) )
145 {
146 auto* numValidator = dynamic_cast<VALIDATOR_NUMERIC_CTRL*>( validator );
147
148 if( numValidator )
149 {
150 wxString errorMsg;
151
152 switch( numValidator->GetValidationState() )
153 {
154 case VALIDATOR_NUMERIC_CTRL::VALIDATION_STATE::Empty: errorMsg = wxS( "Value is required." ); break;
156 errorMsg = wxS( "Value must be a number." );
157 break;
159 errorMsg = wxS( "Value must be a whole number." );
160 break;
162 errorMsg = wxS( "Value must be greater than 0." );
163 break;
164 default: break;
165 }
166
167 if( !errorMsg.IsEmpty() )
168 {
169 ( *aErrorCount )++;
170 *aValidationMessage += DRC_RULE_EDITOR_UTILS::FormatErrorMessage( *aErrorCount, errorMsg );
171 ShowFieldError( wxS( "value" ) );
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:112
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:48
Result of a validation operation.
wxString result
Test unit parsing edge cases and error handling.