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 const DRC_RE_FIELD_POSITION& viaTypePos = positions[4];
56
57 wxArrayString choices;
58 choices.Add( _( "Any" ) );
59 choices.Add( _( "Through" ) );
60 choices.Add( _( "Micro" ) );
61 choices.Add( _( "Blind" ) );
62 choices.Add( _( "Buried" ) );
63
64 m_viaTypeChoice = new wxChoice( this, wxID_ANY, wxPoint( viaTypePos.xStart, viaTypePos.yTop ),
65 wxSize( viaTypePos.xEnd - viaTypePos.xStart, -1 ), choices );
66
67 m_viaTypeChoice->SetSelection( static_cast<int>( m_data->GetViaType() ) );
68
69 m_viaTypeLabel = new wxStaticText( this, wxID_ANY, viaTypePos.labelText );
70 wxSize labelSize = m_viaTypeLabel->GetBestSize();
71 m_viaTypeLabel->SetPosition(
72 wxPoint( viaTypePos.xStart - labelSize.GetWidth() - 4,
73 viaTypePos.yTop + ( m_viaTypeChoice->GetBestSize().GetHeight() - labelSize.GetHeight() ) / 2 ) );
74
75 // Create via diameter fields (min/max)
76 auto* minViaDiameterField =
77 AddField<wxTextCtrl>( wxS( "min_via_diameter" ), positions[0], wxTE_PROCESS_ENTER | wxTE_CENTRE );
79 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, minViaDiameterField->GetControl(),
80 minViaDiameterField->GetLabel(), false, false );
81 minViaDiameterField->SetUnitBinder( m_minViaDiameterBinder.get() );
82
83 auto* maxViaDiameterField =
84 AddField<wxTextCtrl>( wxS( "max_via_diameter" ), positions[1], wxTE_PROCESS_ENTER | wxTE_CENTRE );
86 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, maxViaDiameterField->GetControl(),
87 maxViaDiameterField->GetLabel(), false, false );
88 maxViaDiameterField->SetUnitBinder( m_maxViaDiameterBinder.get() );
89
90 // Create via hole size fields (min/max)
91 auto* minViaHoleField =
92 AddField<wxTextCtrl>( wxS( "min_via_hole" ), positions[2], wxTE_PROCESS_ENTER | wxTE_CENTRE );
94 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, minViaHoleField->GetControl(),
95 minViaHoleField->GetLabel(), false, false );
96 minViaHoleField->SetUnitBinder( m_minViaHoleSizeBinder.get() );
97
98 auto* maxViaHoleField =
99 AddField<wxTextCtrl>( wxS( "max_via_hole" ), positions[3], wxTE_PROCESS_ENTER | wxTE_CENTRE );
101 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, maxViaHoleField->GetControl(),
102 maxViaHoleField->GetLabel(), false, false );
103 maxViaHoleField->SetUnitBinder( m_maxViaHoleSizeBinder.get() );
104
105 // Diameter row decorations
106 {
107 const DRC_RE_FIELD_POSITION& minPos = positions[0];
108 const DRC_RE_FIELD_POSITION& maxPos = positions[1];
109 int fieldHeight = minViaDiameterField->GetControl()->GetBestSize().GetHeight();
110
111 auto* openDia = new wxStaticText( this, wxID_ANY, wxS( "(" ) );
112 wxSize openSize = openDia->GetBestSize();
113 openDia->SetPosition( wxPoint( minPos.xStart - openSize.GetWidth() - 2,
114 minPos.yTop + ( fieldHeight - openSize.GetHeight() ) / 2 ) );
115
116 auto* dashDia = new wxStaticText( this, wxID_ANY, wxS( "-" ) );
117 wxSize dashSize = dashDia->GetBestSize();
118 wxStaticText* minMmLabel = minViaDiameterField->GetLabel();
119 int afterMinLabel = minMmLabel->GetPosition().x + minMmLabel->GetBestSize().GetWidth();
120 int gapMid = ( afterMinLabel + maxPos.xStart ) / 2;
121 dashDia->SetPosition(
122 wxPoint( gapMid - dashSize.GetWidth() / 2, minPos.yTop + ( fieldHeight - dashSize.GetHeight() ) / 2 ) );
123
124 auto* closeDia = new wxStaticText( this, wxID_ANY, wxS( ")" ) );
125 wxSize closeSize = closeDia->GetBestSize();
126 wxStaticText* mmLabel = maxViaDiameterField->GetLabel();
127 int afterLabel = mmLabel->GetPosition().x + mmLabel->GetBestSize().GetWidth();
128 closeDia->SetPosition( wxPoint( afterLabel + 2, minPos.yTop + ( fieldHeight - closeSize.GetHeight() ) / 2 ) );
129 }
130
131 // Hole row decorations
132 {
133 const DRC_RE_FIELD_POSITION& minPos = positions[2];
134 const DRC_RE_FIELD_POSITION& maxPos = positions[3];
135 int fieldHeight = minViaHoleField->GetControl()->GetBestSize().GetHeight();
136
137 auto* openHole = new wxStaticText( this, wxID_ANY, wxS( "(" ) );
138 wxSize openSize = openHole->GetBestSize();
139 openHole->SetPosition( wxPoint( minPos.xStart - openSize.GetWidth() - 2,
140 minPos.yTop + ( fieldHeight - openSize.GetHeight() ) / 2 ) );
141
142
143 auto* dashHole = new wxStaticText( this, wxID_ANY, wxS( "-" ) );
144 wxSize dashSize = dashHole->GetBestSize();
145 wxStaticText* minMmLabel = minViaHoleField->GetLabel();
146 int afterMinLabel = minMmLabel->GetPosition().x + minMmLabel->GetBestSize().GetWidth();
147 int gapMid = ( afterMinLabel + maxPos.xStart ) / 2;
148 dashHole->SetPosition(
149 wxPoint( gapMid - dashSize.GetWidth() / 2, minPos.yTop + ( fieldHeight - dashSize.GetHeight() ) / 2 ) );
150
151 auto* closeHole = new wxStaticText( this, wxID_ANY, wxS( ")" ) );
152 wxSize closeSize = closeHole->GetBestSize();
153 wxStaticText* mmLabel = maxViaHoleField->GetLabel();
154 int afterLabel = mmLabel->GetPosition().x + mmLabel->GetBestSize().GetWidth();
155 closeHole->SetPosition( wxPoint( afterLabel + 2, minPos.yTop + ( fieldHeight - closeSize.GetHeight() ) / 2 ) );
156 }
157
158 auto notifyModified = [this]( wxCommandEvent& )
159 {
161 if( dlg )
162 dlg->SetModified();
163 };
164
165 auto notifySave = [this]( wxCommandEvent& aEvent )
166 {
168 if( dlg )
169 dlg->OnSave( aEvent );
170 };
171
172 minViaDiameterField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
173 maxViaDiameterField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
174 minViaHoleField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
175 maxViaHoleField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
176
177 minViaDiameterField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
178 maxViaDiameterField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
179 minViaHoleField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
180 maxViaHoleField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
181
182 m_viaTypeChoice->Bind( wxEVT_CHOICE, notifyModified );
183
184 // Position all fields and update the panel layout
187}
188
189
191{
192 if( !m_data )
193 return false;
194
195 // Convert mm values to internal units and set them in the unit binders
196 // Use ChangeDoubleValue to avoid triggering modification events during loading
197 m_minViaDiameterBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetMinViaDiameter() ) );
198 m_maxViaDiameterBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetMaxViaDiameter() ) );
199
200 m_minViaHoleSizeBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetMinViaHoleSize() ) );
201 m_maxViaHoleSizeBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetMaxViaHoleSize() ) );
202
203 m_viaTypeChoice->SetSelection( static_cast<int>( m_data->GetViaType() ) );
204
205 return true;
206}
207
208
210{
211 if( !m_data )
212 return false;
213
214 // Read values from unit binders and convert from internal units to mm
215 m_data->SetMinViaDiameter( pcbIUScale.IUTomm( m_minViaDiameterBinder->GetDoubleValue() ) );
216 m_data->SetMaxViaDiameter( pcbIUScale.IUTomm( m_maxViaDiameterBinder->GetDoubleValue() ) );
217
218 m_data->SetMinViaHoleSize( pcbIUScale.IUTomm( m_minViaHoleSizeBinder->GetDoubleValue() ) );
219 m_data->SetMaxViaHoleSize( pcbIUScale.IUTomm( m_maxViaHoleSizeBinder->GetDoubleValue() ) );
220
221 m_data->SetViaType( static_cast<VIA_STYLE_TYPE>( m_viaTypeChoice->GetSelection() ) );
222
223 return true;
224}
225
226
227bool DRC_RE_VIA_STYLE_OVERLAY_PANEL::ValidateInputs( int* aErrorCount, wxString* aValidationMessage )
228{
230
231 VALIDATION_RESULT result = m_data->Validate();
232
233 if( !result.isValid )
234 {
235 *aErrorCount = result.errors.size();
236
237 for( size_t i = 0; i < result.errors.size(); i++ )
238 *aValidationMessage += DRC_RULE_EDITOR_UTILS::FormatErrorMessage( i + 1, result.errors[i] );
239
240 return false;
241 }
242
243 return true;
244}
245
246
248{
249 if( !m_data )
250 return wxEmptyString;
251
252 return m_data->GenerateRule( aContext );
253}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
@ constraint_via_style
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
Specifies the position and size of a field overlaid on a constraint bitmap.
int xEnd
Right edge X coordinate where the field ends.
int xStart
Left edge X coordinate where the field starts.
wxString labelText
Optional label text (empty for no label)
int yTop
Top edge Y coordinate of the field.
Result of a validation operation.
wxString result
Test unit parsing edge cases and error handling.