KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_via_style_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
23
25#include <eda_base_frame.h>
26#include <base_units.h>
27#include <widgets/unit_binder.h>
28
29#include <wx/textctrl.h>
30#include <wx/choice.h>
31#include <wx/stattext.h>
32
34 wxWindow* aParent, DRC_RE_VIA_STYLE_CONSTRAINT_DATA* aData, EDA_UNITS aUnits ) :
36 m_data( aData ),
38{
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 // Via type dropdown
55 wxArrayString choices;
56 choices.Add( _( "Any" ) );
57 choices.Add( _( "Through" ) );
58 choices.Add( _( "Micro" ) );
59 choices.Add( _( "Blind" ) );
60 choices.Add( _( "Buried" ) );
61
62 m_viaTypeChoice = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, choices );
63 m_viaTypeChoice->SetSelection( static_cast<int>( m_data->GetViaType() ) );
64
65 AddControl( wxS( "via_type" ), positions[4], m_viaTypeChoice );
66
67 // Create via diameter fields (min/max)
68 auto* minViaDiameterField =
69 AddField<wxTextCtrl>( wxS( "min_via_diameter" ), positions[0], wxTE_PROCESS_ENTER | wxTE_CENTRE );
71 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, minViaDiameterField->GetControl(),
72 minViaDiameterField->GetLabel(), false, false );
73 minViaDiameterField->SetUnitBinder( m_minViaDiameterBinder.get() );
74
75 auto* maxViaDiameterField =
76 AddField<wxTextCtrl>( wxS( "max_via_diameter" ), positions[1], wxTE_PROCESS_ENTER | wxTE_CENTRE );
78 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, maxViaDiameterField->GetControl(),
79 maxViaDiameterField->GetLabel(), false, false );
80 maxViaDiameterField->SetUnitBinder( m_maxViaDiameterBinder.get() );
81
82 // Create via hole size fields (min/max)
83 auto* minViaHoleField =
84 AddField<wxTextCtrl>( wxS( "min_via_hole" ), positions[2], wxTE_PROCESS_ENTER | wxTE_CENTRE );
86 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, minViaHoleField->GetControl(),
87 minViaHoleField->GetLabel(), false, false );
88 minViaHoleField->SetUnitBinder( m_minViaHoleSizeBinder.get() );
89
90 auto* maxViaHoleField =
91 AddField<wxTextCtrl>( wxS( "max_via_hole" ), positions[3], wxTE_PROCESS_ENTER | wxTE_CENTRE );
93 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, maxViaHoleField->GetControl(),
94 maxViaHoleField->GetLabel(), false, false );
95 maxViaHoleField->SetUnitBinder( m_maxViaHoleSizeBinder.get() );
96
97 auto notifyModified = [this]( wxCommandEvent& )
98 {
100 if( dlg )
101 dlg->SetModified();
102 };
103
104 auto notifySave = [this]( wxCommandEvent& aEvent )
105 {
107 if( dlg )
108 dlg->OnSave( aEvent );
109 };
110
111 minViaDiameterField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
112 maxViaDiameterField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
113 minViaHoleField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
114 maxViaHoleField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
115
116 minViaDiameterField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
117 maxViaDiameterField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
118 minViaHoleField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
119 maxViaHoleField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
120
121 m_viaTypeChoice->Bind( wxEVT_CHOICE, notifyModified );
122
123 // Position all fields and update the panel layout
126}
127
128
130{
131 if( !m_data )
132 return false;
133
134 // Convert mm values to internal units and set them in the unit binders
135 // Use ChangeDoubleValue to avoid triggering modification events during loading
136 m_minViaDiameterBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetMinViaDiameter() ) );
137 m_maxViaDiameterBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetMaxViaDiameter() ) );
138
139 m_minViaHoleSizeBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetMinViaHoleSize() ) );
140 m_maxViaHoleSizeBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetMaxViaHoleSize() ) );
141
142 m_viaTypeChoice->SetSelection( static_cast<int>( m_data->GetViaType() ) );
143
144 return true;
145}
146
147
149{
150 if( !m_data )
151 return false;
152
153 // Read values from unit binders and convert from internal units to mm
154 m_data->SetMinViaDiameter( pcbIUScale.IUTomm( m_minViaDiameterBinder->GetDoubleValue() ) );
155 m_data->SetMaxViaDiameter( pcbIUScale.IUTomm( m_maxViaDiameterBinder->GetDoubleValue() ) );
156
157 m_data->SetMinViaHoleSize( pcbIUScale.IUTomm( m_minViaHoleSizeBinder->GetDoubleValue() ) );
158 m_data->SetMaxViaHoleSize( pcbIUScale.IUTomm( m_maxViaHoleSizeBinder->GetDoubleValue() ) );
159
160 m_data->SetViaType( static_cast<VIA_STYLE_TYPE>( m_viaTypeChoice->GetSelection() ) );
161
162 return true;
163}
164
165
166bool DRC_RE_VIA_STYLE_OVERLAY_PANEL::ValidateInputs( int* aErrorCount, wxString* aValidationMessage )
167{
169
170 VALIDATION_RESULT result = m_data->Validate();
171
172 if( !result.isValid )
173 {
174 *aErrorCount = result.errors.size();
175
176 for( size_t i = 0; i < result.errors.size(); i++ )
177 *aValidationMessage += DRC_RULE_EDITOR_UTILS::FormatErrorMessage( i + 1, result.errors[i] );
178
179 return false;
180 }
181
182 return true;
183}
184
185
187{
188 if( !m_data )
189 return wxEmptyString;
190
191 return m_data->GenerateRule( aContext );
192}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
@ constraint_via_style
DRC_RE_OVERLAY_FIELD * AddControl(const wxString &aId, const DRC_RE_FIELD_POSITION &aPosition, T *aControl)
Position a control on the bitmap overlay.
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 PositionFields()
Position all fields based on the current scale factor.
std::unique_ptr< UNIT_BINDER > m_maxViaDiameterBinder
wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext) override
DRC_RE_VIA_STYLE_CONSTRAINT_DATA * m_data
std::unique_ptr< UNIT_BINDER > m_minViaHoleSizeBinder
bool ValidateInputs(int *aErrorCount, wxString *aValidationMessage) override
DRC_RE_VIA_STYLE_OVERLAY_PANEL(wxWindow *aParent, DRC_RE_VIA_STYLE_CONSTRAINT_DATA *aData, EDA_UNITS aUnits)
std::unique_ptr< UNIT_BINDER > m_minViaDiameterBinder
std::unique_ptr< UNIT_BINDER > m_maxViaHoleSizeBinder
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
Result of a validation operation.
wxString result
Test unit parsing edge cases and error handling.