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