KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_setup_via_stacks.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 The 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
20#include <algorithm>
21
22#include <wx/textdlg.h>
23
24#include <confirm.h>
25#include <board.h>
26#include <pcb_edit_frame.h>
27#include <widgets/wx_grid.h>
28
32
33
35 PANEL_SETUP_VIA_STACKS_BASE( aParentWindow ),
36 m_frame( aFrame )
37{
38 m_grid->AppendCols( 6 );
39 m_grid->SetColLabelValue( 0, _( "Name" ) );
40 m_grid->SetColLabelValue( 1, _( "Type" ) );
41 m_grid->SetColLabelValue( 2, _( "Layers" ) );
42 m_grid->SetColLabelValue( 3, _( "Diameter / Hole" ) );
43 m_grid->SetColLabelValue( 4, _( "Fill" ) );
44 m_grid->SetColLabelValue( 5, _( "Pitch" ) );
45 // Only the name is editable in place, the rest needs the dialog.
46 m_grid->EnableEditing( true );
47
48 m_grid->Bind( wxEVT_GRID_CELL_CHANGED, &PANEL_SETUP_VIA_STACKS::onCellChanged, this );
49 m_addButton->Bind( wxEVT_BUTTON, &PANEL_SETUP_VIA_STACKS::onAdd, this );
50 m_editButton->Bind( wxEVT_BUTTON, &PANEL_SETUP_VIA_STACKS::onEdit, this );
51 m_removeButton->Bind( wxEVT_BUTTON, &PANEL_SETUP_VIA_STACKS::onRemove, this );
52}
53
54
56{
57 int cursor = m_grid->GetGridCursorRow();
58
59 if( m_grid->GetNumberRows() > 0 )
60 m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
61
62 m_grid->AppendRows( (int) m_presets.size() );
63
64 for( int row = 0; row < (int) m_presets.size(); ++row )
65 {
66 const VIA_STACK_PRESET& preset = m_presets[row];
67
68 m_grid->SetCellValue( row, 0, preset.m_Name );
69 m_grid->SetCellValue( row, 1, preset.m_Staggered ? _( "Staggered" ) : _( "Stacked" ) );
70 BOARD* board = m_frame->GetBoard();
71
72 m_grid->SetCellValue( row, 2,
73 board->GetLayerName( preset.m_StartLayer ) + wxT( " -> " )
74 + board->GetLayerName( preset.m_EndLayer ) );
75 m_grid->SetCellValue( row, 3,
76 preset.m_UseNetclass
77 ? _( "netclass" )
78 : m_frame->MessageTextFromValue( preset.m_ViaSize, false ) + wxT( " / " )
79 + m_frame->MessageTextFromValue( preset.m_ViaDrill ) );
80 m_grid->SetCellValue( row, 4, preset.m_Filled ? _( "yes" ) : _( "no" ) );
81 m_grid->SetCellValue(
82 row, 5, preset.m_Staggered ? m_frame->MessageTextFromValue( preset.m_Pitch ) : wxString( wxS( "-" ) ) );
83
84 for( int col = 1; col < 6; ++col )
85 m_grid->SetReadOnly( row, col );
86 }
87
88 if( !m_presets.empty() && cursor >= 0 )
89 m_grid->SetGridCursor( std::min( cursor, (int) m_presets.size() - 1 ), 0 );
90}
91
92
94{
95 PCB_VIA_STACK temp( m_frame->GetBoard(), F_Cu );
96 temp.ApplyPreset( aPreset );
97
98 DIALOG_MICROVIA_STACK dlg( m_frame, &temp );
99
100 if( dlg.ShowModal() != wxID_OK )
101 return false;
102
103 wxString name = aPreset.m_Name;
104 aPreset = temp.ToPreset();
105 aPreset.m_Name = name;
106
107 return true;
108}
109
110
111void PANEL_SETUP_VIA_STACKS::onAdd( wxCommandEvent& aEvent )
112{
113 wxString name;
114
115 while( true )
116 {
117 wxTextEntryDialog nameDlg( this, _( "Preset name:" ), _( "New Microvia Stack Preset" ), name );
118
119 if( nameDlg.ShowModal() != wxID_OK )
120 return;
121
122 name = nameDlg.GetValue();
123 name.Trim( true ).Trim( false );
124
125 if( name.IsEmpty() )
126 return;
127
128 if( nameIsFree( name, -1 ) )
129 break;
130
131 DisplayErrorMessage( this, wxString::Format( _( "A preset named \"%s\" already exists." ), name ) );
132 }
133
134 VIA_STACK_PRESET preset;
135 preset.m_Name = name;
136
137 if( editPreset( preset ) )
138 {
139 m_presets.push_back( preset );
140 rebuildGrid();
141 }
142}
143
144
145// A preset is referenced by name, by the toolbar selector and by every placed stack, so two
146// presets sharing one is ambiguous.
147bool PANEL_SETUP_VIA_STACKS::nameIsFree( const wxString& aName, int aIgnoreRow ) const
148{
149 for( int row = 0; row < (int) m_presets.size(); ++row )
150 {
151 if( row != aIgnoreRow && m_presets[row].m_Name.CmpNoCase( aName ) == 0 )
152 return false;
153 }
154
155 return true;
156}
157
158
159void PANEL_SETUP_VIA_STACKS::onCellChanged( wxGridEvent& aEvent )
160{
161 int row = aEvent.GetRow();
162
163 if( aEvent.GetCol() != 0 || row < 0 || row >= (int) m_presets.size() )
164 return;
165
166 wxString name = m_grid->GetCellValue( row, 0 );
167 name.Trim( true ).Trim( false );
168
169 if( name.IsEmpty() || !nameIsFree( name, row ) )
170 {
171 if( !name.IsEmpty() )
172 DisplayErrorMessage( this, wxString::Format( _( "A preset named \"%s\" already exists." ), name ) );
173
174 m_grid->SetCellValue( row, 0, m_presets[row].m_Name );
175 return;
176 }
177
178 if( m_presets[row].m_Name == m_activePreset )
180
181 m_presets[row].m_Name = name;
182 m_grid->SetCellValue( row, 0, name );
183}
184
185
186void PANEL_SETUP_VIA_STACKS::onEdit( wxCommandEvent& aEvent )
187{
188 if( !m_grid->CommitPendingChanges() )
189 return;
190
191 int row = m_grid->GetGridCursorRow();
192
193 if( row < 0 || row >= (int) m_presets.size() )
194 return;
195
196 if( editPreset( m_presets[row] ) )
197 rebuildGrid();
198}
199
200
201void PANEL_SETUP_VIA_STACKS::onRemove( wxCommandEvent& aEvent )
202{
203 if( !m_grid->CommitPendingChanges() )
204 return;
205
206 int row = m_grid->GetGridCursorRow();
207
208 if( row < 0 || row >= (int) m_presets.size() )
209 return;
210
211 m_presets.erase( m_presets.begin() + row );
212 rebuildGrid();
213}
214
215
217{
219
220 int active = aBds.GetViaStackIndex();
221
222 if( active >= 0 && active < (int) m_presets.size() )
223 m_activePreset = m_presets[active].m_Name;
224 else
225 m_activePreset.Clear();
226
227 rebuildGrid();
228}
229
230
232{
233 loadFrom( m_frame->GetBoard()->GetDesignSettings() );
234 return true;
235}
236
237
242
243
245{
246 if( !m_grid->CommitPendingChanges() )
247 return false;
248
249 BOARD_DESIGN_SETTINGS& bds = m_frame->GetBoard()->GetDesignSettings();
250
252
253 // Adding or removing a preset shifts the others, so follow the selection by name.
254 int active = 0;
255
256 for( int row = 0; row < (int) m_presets.size(); ++row )
257 {
258 if( m_presets[row].m_Name == m_activePreset )
259 {
260 active = row;
261 break;
262 }
263 }
264
265 bds.SetViaStackIndex( active );
266 return true;
267}
const char * name
Container for design settings for a BOARD object.
void SetViaStackIndex(int aIndex)
Set the current via stack preset index to aIndex.
std::vector< VIA_STACK_PRESET > m_ViaStackPresets
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:409
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition board.cpp:936
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1299
int ShowModal() override
PANEL_SETUP_VIA_STACKS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(500, 300), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
PANEL_SETUP_VIA_STACKS(wxWindow *aParentWindow, PCB_EDIT_FRAME *aFrame)
void onRemove(wxCommandEvent &aEvent)
bool editPreset(VIA_STACK_PRESET &aPreset)
bool nameIsFree(const wxString &aName, int aIgnoreRow) const
void loadFrom(BOARD_DESIGN_SETTINGS &aBds)
void ImportSettingsFrom(BOARD *aBoard)
void onEdit(wxCommandEvent &aEvent)
std::vector< VIA_STACK_PRESET > m_presets
void onAdd(wxCommandEvent &aEvent)
void onCellChanged(wxGridEvent &aEvent)
The main frame for Pcbnew.
A microvia stack: several single hop microvias, plus connecting traces for staggered stacks,...
VIA_STACK_PRESET ToPreset() const
void ApplyPreset(const VIA_STACK_PRESET &aPreset)
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
#define _(s)
@ F_Cu
Definition layer_ids.h:60
A named microvia stack definition, chosen while routing instead of entering the values each time.