KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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
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{
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;
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 = [this]( int idx ) -> int
221 {
222 if( idx < 0 || idx >= (int) m_grids.size() )
223 return 0;
224
225 return idx;
226 };
227
228 Layout();
229
230 m_grids = gridCfg.grids;
232
233 m_currentGridCtrl->SetSelection( safeGrid( gridCfg.last_size_idx ) );
234
235 m_grid1Ctrl->SetSelection( safeGrid( gridCfg.fast_grid_1 ) );
236 m_grid2Ctrl->SetSelection( safeGrid( gridCfg.fast_grid_2 ) );
237
238 m_gridOverrideConnectedChoice->SetSelection( safeGrid( gridCfg.override_connected_idx ) );
239 m_gridOverrideWiresChoice->SetSelection( safeGrid( gridCfg.override_wires_idx ) );
240 m_gridOverrideViasChoice->SetSelection( safeGrid( gridCfg.override_vias_idx ) );
241 m_gridOverrideTextChoice->SetSelection( safeGrid( gridCfg.override_text_idx ) );
242 m_gridOverrideGraphicsChoice->SetSelection( safeGrid( gridCfg.override_graphics_idx ) );
243
245 m_checkGridOverrideWires->SetValue( gridCfg.override_wires );
246 m_checkGridOverrideVias->SetValue( gridCfg.override_vias );
247 m_checkGridOverrideText->SetValue( gridCfg.override_text );
249
250 return RESETTABLE_PANEL::TransferDataToWindow();
251}
252
253
254void PANEL_GRID_SETTINGS::OnAddGrid( wxCommandEvent& event )
255{
256 GRID newGrid = GRID{ wxEmptyString, "", "" };
257 DIALOG_GRID_SETTINGS dlg( wxGetTopLevelParent( this ), m_eventSource, m_unitsProvider,
258 newGrid );
259
260 if( dlg.ShowModal() != wxID_OK )
261 return;
262
263 int row = m_currentGridCtrl->GetSelection();
264
265 for( GRID& g : m_grids )
266 {
267 if( newGrid == g )
268 {
269 wxWindow* topLevelParent = wxGetTopLevelParent( this );
270
271 DisplayError( topLevelParent,
272 wxString::Format( _( "Grid size '%s' already exists." ),
273 g.UserUnitsMessageText( m_unitsProvider ) ) );
274 return;
275 }
276 }
277
278 m_grids.insert( m_grids.begin() + row, newGrid );
280 m_currentGridCtrl->SetSelection( row );
281}
282
283
284void PANEL_GRID_SETTINGS::OnEditGrid( wxCommandEvent& event )
285{
286 onEditGrid();
287}
288
289
291{
292 int row = m_currentGridCtrl->GetSelection();
293
294 if( row < 0 )
295 return;
296
297 GRID editGrid = m_grids[row];
298 DIALOG_GRID_SETTINGS dlg( wxGetTopLevelParent( this ), m_eventSource, m_unitsProvider,
299 editGrid );
300
301 if( dlg.ShowModal() != wxID_OK )
302 return;
303
304 // If the user just clicked OK without changing anything,
305 // then return or we'll trigger the same grid check
306 if( editGrid == m_grids[row] )
307 return;
308
309 for( GRID& g : m_grids )
310 {
311 if( editGrid == g )
312 {
313 wxWindow* topLevelParent = wxGetTopLevelParent( this );
314
315 DisplayError( topLevelParent,
316 wxString::Format( _( "Grid size '%s' already exists." ),
317 g.UserUnitsMessageText( m_unitsProvider ) ) );
318 return;
319 }
320 }
321
322 m_grids[row] = editGrid;
323
325 m_currentGridCtrl->SetSelection( row );
326}
327
328
329void PANEL_GRID_SETTINGS::OnRemoveGrid( wxCommandEvent& event )
330{
331 int row = m_currentGridCtrl->GetSelection();
332
333 if( m_grids.size() <= 1 )
334 return;
335
336 m_grids.erase( m_grids.begin() + row );
338
339 if( row != 0 )
340 m_currentGridCtrl->SetSelection( row - 1 );
341}
342
343
344void PANEL_GRID_SETTINGS::OnMoveGridUp( wxCommandEvent& event )
345{
346 int row = m_currentGridCtrl->GetSelection();
347
348 if( m_grids.size() <= 1 || row == 0 )
349 return;
350
351 std::swap( m_grids[row], m_grids[row - 1] );
353
354 if( row != 0 )
355 m_currentGridCtrl->SetSelection( row - 1 );
356}
357
358
359void PANEL_GRID_SETTINGS::OnMoveGridDown( wxCommandEvent& event )
360{
361 int row = m_currentGridCtrl->GetSelection();
362
363 if( m_grids.size() <= 1 || row == ( (int) m_grids.size() - 1 ) )
364 return;
365
366 std::swap( m_grids[row], m_grids[row + 1] );
368
369 if( row != 0 )
370 m_currentGridCtrl->SetSelection( row + 1 );
371}
372
373
374void PANEL_GRID_SETTINGS::OnUpdateEditGrid( wxUpdateUIEvent& event )
375{
376 // Enable edit when there is a valid selection
377 event.Enable( m_currentGridCtrl->GetSelection() >= 0 );
378}
379
380
381void PANEL_GRID_SETTINGS::OnUpdateMoveUp( wxUpdateUIEvent& event )
382{
383 int curRow = m_currentGridCtrl->GetSelection();
384 int numRows = (int) m_grids.size();
385
386 // Enable move up when there are multiple grids and it is not the first row
387 event.Enable( ( numRows > 1 ) && ( curRow > 0 ) );
388}
389
390
391void PANEL_GRID_SETTINGS::OnUpdateMoveDown( wxUpdateUIEvent& event )
392{
393 int curRow = m_currentGridCtrl->GetSelection();
394 int numRows = (int) m_grids.size();
395
396 // Enable move down when there are multiple grids and it is not the last row
397 event.Enable( ( numRows > 1 ) && ( curRow < ( numRows - 1 ) ) );
398}
399
400
401void PANEL_GRID_SETTINGS::OnUpdateRemove( wxUpdateUIEvent& event )
402{
403 // Enable remove if there is more than 1 grid
404 event.Enable( m_grids.size() > 1 );
405}
const char * name
Definition: DXF_plotter.cpp:59
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition: bitmap.cpp:110
static TOOL_ACTION gridFast1
Definition: actions.h:190
static TOOL_ACTION gridFast2
Definition: actions.h:191
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:92
WINDOW_SETTINGS m_Window
Definition: app_settings.h:215
const std::vector< GRID > DefaultGridSizeList() const
int ShowModal() override
Class PANEL_GRID_SETTINGS_BASE.
STD_BITMAP_BUTTON * m_removeGridButton
STD_BITMAP_BUTTON * m_editGridButton
STD_BITMAP_BUTTON * m_moveDownButton
STD_BITMAP_BUTTON * m_addGridButton
STD_BITMAP_BUTTON * m_moveUpButton
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
void SetBitmap(const wxBitmapBundle &aBmp)
int GetHotKey() const
Return the hotkey keycode which initiates the action.
Definition: tool_action.h:348
const EDA_IU_SCALE & GetIuScale() const
virtual void GetUnitPair(EDA_UNITS &aPrimaryUnit, EDA_UNITS &aSecondaryUnits)
The common library.
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:170
This file is part of the common library.
#define _(s)
EDA_UNITS
Definition: eda_units.h:46
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_SCH_SYMBOL_EDITOR
Definition: frame_type.h:35
@ FRAME_SCH_VIEWER
Definition: frame_type.h:36
@ FRAME_SCH
Definition: frame_type.h:34
@ FRAME_SIMULATOR
Definition: frame_type.h:38
@ 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
Definition: grid_settings.h:80
bool override_graphics
Definition: grid_settings.h:87
bool override_connected
Definition: grid_settings.h:79
std::vector< GRID > grids
Definition: grid_settings.h:66
int override_graphics_idx
Definition: grid_settings.h:88
int override_wires_idx
Definition: grid_settings.h:82
Common grid settings, available to every frame.
Definition: grid_settings.h:34
GRID_SETTINGS grid
Definition: app_settings.h:81