KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_grid_settings.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 <bitmaps.h>
21#include <wx/textdlg.h>
25#include <common.h>
26#include <confirm.h>
28#include <eda_draw_frame.h>
29#include <tool/tool_manager.h>
30#include <tool/actions.h>
31#include <tool/grid_menu.h>
32#include <tool/common_tools.h>
33#include <pgm_base.h>
35
36PANEL_GRID_SETTINGS::PANEL_GRID_SETTINGS( wxWindow* aParent, UNITS_PROVIDER* aUnitsProvider,
37 wxWindow* aEventSource, APP_SETTINGS_BASE* aCfg,
38 FRAME_T aFrameType ) :
39 PANEL_GRID_SETTINGS_BASE( aParent ),
40 m_unitsProvider( aUnitsProvider ),
41 m_cfg( aCfg ),
42 m_frameType( aFrameType ),
43 m_eventSource( aEventSource )
44{
45 m_currentGridCtrl->SetMinSize( FromDIP( m_currentGridCtrl->GetMinSize() ) );
46
48 {
49 m_checkGridOverrideConnected->SetLabel( _( "Pads:" ) );
50 m_checkGridOverrideWires->Show( false );
51 m_gridOverrideWiresChoice->Show( false );
52 m_checkGridOverrideVias->Show( false );
53 m_gridOverrideViasChoice->Show( false );
54 }
55 else if( m_frameType == FRAME_PCB_EDITOR )
56 {
57 m_checkGridOverrideConnected->SetLabel( _( "Footprints/pads:" ) );
58 m_checkGridOverrideWires->SetLabel( _( "Tracks:" ) );
59 }
60 else
61 {
62 m_gridOverrideViasChoice->SetSelection( 0 );
63 m_gridOverrideViasChoice->Show( false );
64 m_checkGridOverrideVias->Show( false );
65
66 if( m_frameType != FRAME_SCH
67 && m_frameType != FRAME_SCH_SYMBOL_EDITOR
68 && m_frameType != FRAME_SCH_VIEWER
69 && m_frameType != FRAME_SIMULATOR )
70 {
71 m_checkGridOverrideConnected->Show( false );
72 m_gridOverrideConnectedChoice->Show( false );
73
74 m_checkGridOverrideWires->Show( false );
75 m_gridOverrideWiresChoice->Show( false );
76 }
77
79 {
80 m_overridesLabel->Show( false );
81 m_staticline3->Show( false );
82
83 m_checkGridOverrideText->Show( false );
84 m_gridOverrideTextChoice->Show( false );
85
86 m_checkGridOverrideGraphics->Show( false );
87 m_gridOverrideGraphicsChoice->Show( false );
88 }
89 }
90
91 int hk1 = ACTIONS::gridFast1.GetHotKey();
92 int hk2 = ACTIONS::gridFast2.GetHotKey();
93 m_grid1HotKey->SetLabel( wxString::Format( wxT( "(%s)" ), KeyNameFromKeyCode( hk1 ) ) );
94 m_grid2HotKey->SetLabel( wxString::Format( wxT( "(%s)" ), KeyNameFromKeyCode( hk2 ) ) );
95
96 m_addGridButton->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
97 m_editGridButton->SetBitmap( KiBitmapBundle( BITMAPS::edit ) );
98 m_removeGridButton->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
99 m_moveUpButton->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
100 m_moveDownButton->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
101
102 Layout();
103}
104
105
107{
108 m_grids = m_cfg->DefaultGridSizeList();
110}
111
112
114{
115 wxString savedCurrentGrid = m_currentGridCtrl->GetStringSelection();
116
117 wxString savedGrid1 = m_grid1Ctrl->GetStringSelection();
118 wxString savedGrid2 = m_grid2Ctrl->GetStringSelection();
119
120 wxString savedConnectables = m_gridOverrideConnectedChoice->GetStringSelection();
121 wxString savedWires = m_gridOverrideWiresChoice->GetStringSelection();
122 wxString savedVias = m_gridOverrideViasChoice->GetStringSelection();
123 wxString savedText = m_gridOverrideTextChoice->GetStringSelection();
124 wxString savedGraphics = m_gridOverrideGraphicsChoice->GetStringSelection();
125
126 wxArrayString grids;
127 wxString msg;
128 EDA_IU_SCALE scale = m_unitsProvider->GetIuScale();
129 EDA_UNITS primaryUnit;
130 EDA_UNITS secondaryUnit;
131
132 m_unitsProvider->GetUnitPair( primaryUnit, secondaryUnit );
133
134 for( const struct GRID& grid : m_grids )
135 {
136 wxString name = grid.name;
137
138 if( !name.IsEmpty() )
139 name += wxT( ": " );
140
141 msg.Printf( _( "%s%s (%s)" ), name, grid.MessageText( scale, primaryUnit, true ),
142 grid.MessageText( scale, secondaryUnit, true ) );
143
144 grids.Add( msg );
145 }
146
147 m_currentGridCtrl->Set( grids );
148
149 m_grid1Ctrl->Set( grids );
150 m_grid2Ctrl->Set( grids );
151
152 m_gridOverrideConnectedChoice->Set( grids );
153 m_gridOverrideWiresChoice->Set( grids );
154 m_gridOverrideViasChoice->Set( grids );
155 m_gridOverrideTextChoice->Set( grids );
156 m_gridOverrideGraphicsChoice->Set( grids );
157
158 if( !m_currentGridCtrl->SetStringSelection( savedCurrentGrid ) )
159 m_currentGridCtrl->SetStringSelection( grids.front() );
160
161 if( !m_grid1Ctrl->SetStringSelection( savedGrid1 ) )
162 m_grid1Ctrl->SetStringSelection( grids.front() );
163
164 if( !m_grid2Ctrl->SetStringSelection( savedGrid2 ) )
165 m_grid2Ctrl->SetStringSelection( grids.back() );
166
167 if( !m_gridOverrideConnectedChoice->SetStringSelection( savedConnectables ) )
168 m_gridOverrideConnectedChoice->SetStringSelection( grids.front() );
169
170 if( !m_gridOverrideWiresChoice->SetStringSelection( savedWires ) )
171 m_gridOverrideWiresChoice->SetStringSelection( grids.front() );
172
173 if( !m_gridOverrideViasChoice->SetStringSelection( savedVias ) )
174 m_gridOverrideViasChoice->SetStringSelection( grids.front() );
175
176 if( !m_gridOverrideTextChoice->SetStringSelection( savedText ) )
177 m_gridOverrideTextChoice->SetStringSelection( grids.front() );
178
179 if( !m_gridOverrideGraphicsChoice->SetStringSelection( savedGraphics ) )
180 m_gridOverrideGraphicsChoice->SetStringSelection( grids.front() );
181}
182
183
185{
186 // Apply the new settings
187 GRID_SETTINGS& gridCfg = m_cfg->m_Window.grid;
188
189 gridCfg.grids = m_grids;
190
191 gridCfg.last_size_idx = m_currentGridCtrl->GetSelection();
192
193 gridCfg.fast_grid_1 = m_grid1Ctrl->GetSelection();
194 gridCfg.fast_grid_2 = m_grid2Ctrl->GetSelection();
195
198 gridCfg.override_wires = m_checkGridOverrideWires->GetValue();
199 gridCfg.override_wires_idx = m_gridOverrideWiresChoice->GetSelection();
200 gridCfg.override_vias = m_checkGridOverrideVias->GetValue();
201 gridCfg.override_vias_idx = m_gridOverrideViasChoice->GetSelection();
202 gridCfg.override_text = m_checkGridOverrideText->GetValue();
203 gridCfg.override_text_idx = m_gridOverrideTextChoice->GetSelection();
205 gridCfg.override_graphics_idx = m_gridOverrideGraphicsChoice->GetSelection();
206
207 return RESETTABLE_PANEL::TransferDataFromWindow();
208}
209
210
212{
213 GRID_SETTINGS& gridCfg = m_cfg->m_Window.grid;
214
215 // lambda that gives us a safe index into grids regardless of config idx
216 auto safeGrid =
217 [this]( int idx ) -> int
218 {
219 if( idx < 0 || idx >= (int) m_grids.size() )
220 return 0;
221
222 return idx;
223 };
224
225 Layout();
226
227 m_grids = gridCfg.grids;
229
230 m_currentGridCtrl->SetSelection( safeGrid( gridCfg.last_size_idx ) );
231
232 m_grid1Ctrl->SetSelection( safeGrid( gridCfg.fast_grid_1 ) );
233 m_grid2Ctrl->SetSelection( safeGrid( gridCfg.fast_grid_2 ) );
234
235 m_gridOverrideConnectedChoice->SetSelection( safeGrid( gridCfg.override_connected_idx ) );
236 m_gridOverrideWiresChoice->SetSelection( safeGrid( gridCfg.override_wires_idx ) );
237 m_gridOverrideViasChoice->SetSelection( safeGrid( gridCfg.override_vias_idx ) );
238 m_gridOverrideTextChoice->SetSelection( safeGrid( gridCfg.override_text_idx ) );
239 m_gridOverrideGraphicsChoice->SetSelection( safeGrid( gridCfg.override_graphics_idx ) );
240
242 m_checkGridOverrideWires->SetValue( gridCfg.override_wires );
243 m_checkGridOverrideVias->SetValue( gridCfg.override_vias );
244 m_checkGridOverrideText->SetValue( gridCfg.override_text );
246
247 return RESETTABLE_PANEL::TransferDataToWindow();
248}
249
250
251void PANEL_GRID_SETTINGS::OnAddGrid( wxCommandEvent& event )
252{
253 GRID newGrid = GRID{ wxEmptyString, "", "" };
254 DIALOG_GRID_SETTINGS dlg( wxGetTopLevelParent( this ), m_eventSource, m_unitsProvider,
255 newGrid );
256
257 if( dlg.ShowModal() != wxID_OK )
258 return;
259
260 int row = m_currentGridCtrl->GetSelection();
261
262 for( GRID& g : m_grids )
263 {
264 if( newGrid == g )
265 {
266 wxWindow* topLevelParent = wxGetTopLevelParent( this );
267
268 DisplayError( topLevelParent,
269 wxString::Format( _( "Grid size '%s' already exists." ),
270 g.UserUnitsMessageText( m_unitsProvider ) ) );
271 return;
272 }
273 }
274
275 m_grids.insert( m_grids.begin() + row, newGrid );
277 m_currentGridCtrl->SetSelection( row );
278}
279
280
281void PANEL_GRID_SETTINGS::OnEditGrid( wxCommandEvent& event )
282{
283 onEditGrid();
284}
285
286
288{
289 int row = m_currentGridCtrl->GetSelection();
290
291 if( row < 0 )
292 return;
293
294 GRID editGrid = m_grids[row];
295 DIALOG_GRID_SETTINGS dlg( wxGetTopLevelParent( this ), m_eventSource, m_unitsProvider,
296 editGrid );
297
298 if( dlg.ShowModal() != wxID_OK )
299 return;
300
301 // If the user just clicked OK without changing anything,
302 // then return or we'll trigger the same grid check
303 if( editGrid == m_grids[row] )
304 return;
305
306 for( GRID& g : m_grids )
307 {
308 if( editGrid == g )
309 {
310 wxWindow* topLevelParent = wxGetTopLevelParent( this );
311
312 DisplayError( topLevelParent,
313 wxString::Format( _( "Grid size '%s' already exists." ),
314 g.UserUnitsMessageText( m_unitsProvider ) ) );
315 return;
316 }
317 }
318
319 m_grids[row] = editGrid;
320
322 m_currentGridCtrl->SetSelection( row );
323}
324
325
326void PANEL_GRID_SETTINGS::OnRemoveGrid( wxCommandEvent& event )
327{
328 int row = m_currentGridCtrl->GetSelection();
329
330 if( m_grids.size() <= 1 )
331 return;
332
333 m_grids.erase( m_grids.begin() + row );
335
336 if( row != 0 )
337 m_currentGridCtrl->SetSelection( row - 1 );
338}
339
340
341void PANEL_GRID_SETTINGS::OnMoveGridUp( wxCommandEvent& event )
342{
343 int row = m_currentGridCtrl->GetSelection();
344
345 if( m_grids.size() <= 1 || row == 0 )
346 return;
347
348 std::swap( m_grids[row], m_grids[row - 1] );
350
351 if( row != 0 )
352 m_currentGridCtrl->SetSelection( row - 1 );
353}
354
355
356void PANEL_GRID_SETTINGS::OnMoveGridDown( wxCommandEvent& event )
357{
358 int row = m_currentGridCtrl->GetSelection();
359
360 if( m_grids.size() <= 1 || row == ( (int) m_grids.size() - 1 ) )
361 return;
362
363 std::swap( m_grids[row], m_grids[row + 1] );
365
366 if( row != 0 )
367 m_currentGridCtrl->SetSelection( row + 1 );
368}
369
370
371void PANEL_GRID_SETTINGS::OnUpdateEditGrid( wxUpdateUIEvent& event )
372{
373 // Enable edit when there is a valid selection
374 event.Enable( m_currentGridCtrl->GetSelection() >= 0 );
375}
376
377
378void PANEL_GRID_SETTINGS::OnUpdateMoveUp( wxUpdateUIEvent& event )
379{
380 int curRow = m_currentGridCtrl->GetSelection();
381 int numRows = (int) m_grids.size();
382
383 // Enable move up when there are multiple grids and it is not the first row
384 event.Enable( ( numRows > 1 ) && ( curRow > 0 ) );
385}
386
387
388void PANEL_GRID_SETTINGS::OnUpdateMoveDown( wxUpdateUIEvent& event )
389{
390 int curRow = m_currentGridCtrl->GetSelection();
391 int numRows = (int) m_grids.size();
392
393 // Enable move down when there are multiple grids and it is not the last row
394 event.Enable( ( numRows > 1 ) && ( curRow < ( numRows - 1 ) ) );
395}
396
397
398void PANEL_GRID_SETTINGS::OnUpdateRemove( wxUpdateUIEvent& event )
399{
400 // Enable remove if there is more than 1 grid
401 event.Enable( m_grids.size() > 1 );
402}
const char * name
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
static TOOL_ACTION gridFast1
Definition actions.h:186
static TOOL_ACTION gridFast2
Definition actions.h:187
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
int ShowModal() override
PANEL_GRID_SETTINGS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
void ResetPanel() override
Reset the contents of this panel.
bool TransferDataToWindow() override
void OnEditGrid(wxCommandEvent &event) override
void OnRemoveGrid(wxCommandEvent &event) override
void OnMoveGridDown(wxCommandEvent &event) override
void OnUpdateEditGrid(wxUpdateUIEvent &event) override
bool TransferDataFromWindow() override
APP_SETTINGS_BASE * m_cfg
void OnUpdateMoveDown(wxUpdateUIEvent &event) override
void OnUpdateMoveUp(wxUpdateUIEvent &event) override
void OnMoveGridUp(wxCommandEvent &event) override
std::vector< GRID > m_grids
PANEL_GRID_SETTINGS(wxWindow *aParent, UNITS_PROVIDER *aUnitsProvider, wxWindow *aEventSource, APP_SETTINGS_BASE *aCfg, FRAME_T aFrameType)
This has no dependencies on calling wxFrame derivative, such as PCB_BASE_FRAME.
UNITS_PROVIDER * m_unitsProvider
void OnUpdateRemove(wxUpdateUIEvent &event) override
void OnAddGrid(wxCommandEvent &event) override
The common library.
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:192
This file is part of the common library.
#define _(s)
EDA_UNITS
Definition eda_units.h:44
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition frame_type.h:29
@ FRAME_PCB_EDITOR
Definition frame_type.h:38
@ FRAME_FOOTPRINT_EDITOR
Definition frame_type.h:39
@ FRAME_GERBER
Definition frame_type.h:53
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Return the key name from the key code.
see class PGM_BASE
const int scale
int override_connected_idx
bool override_connected
std::vector< GRID > grids
int override_graphics_idx
Common grid settings, available to every frame.