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