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