KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_fp_editor_defaults.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-2022 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 <pgm_base.h>
27#include <widgets/wx_grid.h>
28#include <template_fieldnames.h>
30#include <grid_tricks.h>
31#include <eda_base_frame.h>
34#include <bitmaps.h>
35#include <confirm.h>
36
37class TEXT_ITEMS_GRID_TABLE : public wxGridTableBase
38{
40 std::vector<TEXT_ITEM_INFO> m_items;
41
42public:
43 TEXT_ITEMS_GRID_TABLE( bool aForFieldProps ) :
44 m_forFieldProps( aForFieldProps )
45 { }
46
47 int GetNumberRows() override { return m_items.size(); }
48 int GetNumberCols() override { return 3; }
49
50 wxString GetColLabelValue( int aCol ) override
51 {
52 switch( aCol )
53 {
54 case 0: return m_forFieldProps ? _( "Value" ) : _( "Text Items" );
55 case 1: return _( "Show" );
56 case 2: return _( "Layer" );
57 default: return wxEmptyString;
58 }
59 }
60
61 wxString GetRowLabelValue( int aRow ) override
62 {
63 switch( aRow )
64 {
65 case 0: return _( "Reference designator" );
66 case 1: return _( "Value" );
67 default: return wxEmptyString;
68 }
69 }
70
71 bool CanGetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
72 {
73 switch( aCol )
74 {
75 case 0: return aTypeName == wxGRID_VALUE_STRING;
76 case 1: return aTypeName == wxGRID_VALUE_BOOL;
77 case 2: return aTypeName == wxGRID_VALUE_NUMBER;
78 default: wxFAIL; return false;
79 }
80 }
81
82 bool CanSetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
83 {
84 return CanGetValueAs( aRow, aCol, aTypeName );
85 }
86
87 wxString GetValue( int row, int col ) override
88 {
89 return m_items[row].m_Text;
90 }
91 void SetValue( int row, int col, const wxString& value ) override
92 {
93 if( col == 0 )
94 m_items[row].m_Text = value;
95 }
96
97 bool GetValueAsBool( int row, int col ) override
98 {
99 return m_items[row].m_Visible;
100 }
101 void SetValueAsBool( int row, int col, bool value ) override
102 {
103 if( col == 1 )
104 m_items[row].m_Visible = value;
105 }
106
107 long GetValueAsLong( int row, int col ) override
108 {
109 return m_items[row].m_Layer;
110 }
111 void SetValueAsLong( int row, int col, long value ) override
112 {
113 if( col == 2 )
114 m_items[row].m_Layer = (int) value;
115 }
116
117 bool AppendRows( size_t aNumRows = 1 ) override
118 {
119 for( size_t i = 0; i < aNumRows; ++i )
120 m_items.emplace_back( wxT( "" ), true, F_SilkS );
121
122 if( GetView() )
123 {
124 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, aNumRows );
125 GetView()->ProcessTableMessage( msg );
126 }
127
128 return true;
129 }
130
131 bool DeleteRows( size_t aPos, size_t aNumRows ) override
132 {
133 // aPos may be a large positive, e.g. size_t(-1), and the sum of
134 // aPos+aNumRows may wrap here, so both ends of the range are tested.
135 if( aPos < m_items.size() && aPos + aNumRows <= m_items.size() )
136 {
137 m_items.erase( m_items.begin() + aPos, m_items.begin() + aPos + aNumRows );
138
139 if( GetView() )
140 {
141 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aPos, aNumRows );
142 GetView()->ProcessTableMessage( msg );
143 }
144 return true;
145 }
146
147 return false;
148 }
149};
150
151
152// Columns of graphics grid
153enum
154{
161
162enum
163{
170
173
174
176 UNITS_PROVIDER* aUnitsProvider ) :
178{
179 m_fieldPropsGrid->SetDefaultRowSize( m_fieldPropsGrid->GetDefaultRowSize() + 4 );
180
181 m_fieldPropsGrid->SetTable( new TEXT_ITEMS_GRID_TABLE( true ), true );
182 m_fieldPropsGrid->PushEventHandler( new GRID_TRICKS( m_fieldPropsGrid ) );
183 m_fieldPropsGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
184
185 wxGridCellAttr* attr = new wxGridCellAttr;
186 attr->SetRenderer( new wxGridCellBoolRenderer() );
187 attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
188 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
189 m_fieldPropsGrid->SetColAttr( 1, attr );
190
191 attr = new wxGridCellAttr;
192 attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( nullptr ) );
193 attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( nullptr, {} ) );
194 m_fieldPropsGrid->SetColAttr( 2, attr );
195
196 m_textItemsGrid->SetDefaultRowSize( m_textItemsGrid->GetDefaultRowSize() + 4 );
197
198 m_textItemsGrid->SetTable( new TEXT_ITEMS_GRID_TABLE( false ), true );
199 m_textItemsGrid->PushEventHandler( new GRID_TRICKS( m_textItemsGrid ) );
200 m_textItemsGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
201
202 attr = new wxGridCellAttr;
203 attr->SetRenderer( new wxGridCellBoolRenderer() );
204 attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
205 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
206 m_textItemsGrid->SetColAttr( 1, attr );
207
208 attr = new wxGridCellAttr;
209 attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( nullptr ) );
210 attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( nullptr, {} ) );
211 m_textItemsGrid->SetColAttr( 2, attr );
212
213 m_graphicsGrid->SetUnitsProvider( aUnitsProvider );
218
219 m_graphicsGrid->SetDefaultRowSize( m_graphicsGrid->GetDefaultRowSize() + 4 );
220
221 // Work around a bug in wxWidgets where it fails to recalculate the grid height
222 // after changing the default row size
223 m_graphicsGrid->AppendRows( 1 );
224 m_graphicsGrid->DeleteRows( m_graphicsGrid->GetNumberRows() - 1, 1 );
225
226 m_graphicsGrid->PushEventHandler( new GRID_TRICKS( m_graphicsGrid ) );
227}
228
229
231{
232 // destroy GRID_TRICKS before grids.
233 m_fieldPropsGrid->PopEventHandler( true );
234 m_textItemsGrid->PopEventHandler( true );
235 m_graphicsGrid->PopEventHandler( true );
236}
237
238
240{
241 wxColour disabledColour = wxSystemSettings::GetColour( wxSYS_COLOUR_BACKGROUND );
242
243 auto disableCell =
244 [&]( int row, int col )
245 {
246 m_graphicsGrid->SetReadOnly( row, col );
247 m_graphicsGrid->SetCellBackgroundColour( row, col, disabledColour );
248 };
249
250 for( int i = 0; i < ROW_COUNT; ++i )
251 {
253
254 if( i == ROW_EDGES || i == ROW_COURTYARD )
255 {
256 disableCell( i, COL_TEXT_WIDTH );
257 disableCell( i, COL_TEXT_HEIGHT );
258 disableCell( i, COL_TEXT_THICKNESS );
259 disableCell( i, COL_TEXT_ITALIC );
260 }
261 else
262 {
266 m_graphicsGrid->SetCellValue( i, COL_TEXT_ITALIC, aCfg->m_DesignSettings.m_TextItalic[ i ] ? wxT( "1" ) : wxT( "" ) );
267
268 auto attr = new wxGridCellAttr;
269 attr->SetRenderer( new wxGridCellBoolRenderer() );
270 attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
271 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
272 m_graphicsGrid->SetAttr( i, COL_TEXT_ITALIC, attr );
273 }
274 }
275
276 // Footprint defaults
277 m_fieldPropsGrid->GetTable()->DeleteRows( 0, m_textItemsGrid->GetNumberRows() );
278 m_fieldPropsGrid->GetTable()->AppendRows( 2 );
279
280 for( int i : { REFERENCE_FIELD, VALUE_FIELD } )
281 {
283
284 m_fieldPropsGrid->GetTable()->SetValue( i, 0, item.m_Text );
285 m_fieldPropsGrid->GetTable()->SetValueAsBool( i, 1, item.m_Visible );
286 m_fieldPropsGrid->GetTable()->SetValueAsLong( i, 2, item.m_Layer );
287 }
288
289 m_textItemsGrid->GetTable()->DeleteRows( 0, m_textItemsGrid->GetNumberRows() );
290 m_textItemsGrid->GetTable()->AppendRows( aCfg->m_DesignSettings.m_DefaultFPTextItems.size() - 2 );
291
292 for( int i = 2; i < (int) aCfg->m_DesignSettings.m_DefaultFPTextItems.size(); ++i )
293 {
295
296 m_textItemsGrid->GetTable()->SetValue( i - 2, 0, item.m_Text );
297 m_textItemsGrid->GetTable()->SetValueAsBool( i - 2, 1, item.m_Visible );
298 m_textItemsGrid->GetTable()->SetValueAsLong( i - 2, 2, item.m_Layer );
299 }
300
301 for( int col = 0; col < m_graphicsGrid->GetNumberCols(); col++ )
302 {
303 // Set the minimal width to the column label size.
304 m_graphicsGrid->SetColMinimalWidth( col, m_graphicsGrid->GetVisibleWidth( col, true, false ) );
305
306 // Set the width to see the full contents
307 if( m_graphicsGrid->IsColShown( col ) )
308 m_graphicsGrid->SetColSize( col, m_graphicsGrid->GetVisibleWidth( col, true, true, true ) );
309 }
310
311 m_graphicsGrid->SetRowLabelSize( m_graphicsGrid->GetVisibleWidth( -1, true, true, true ) );
312
313 Layout();
314}
315
316
318{
319 SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
321
322 loadFPSettings( cfg );
323
324 return true;
325}
326
327
329{
330 bool retVal = wxPanel::Show( aShow );
331
332 if( aShow )
333 {
334 // These *should* work in the constructor, and indeed they do if this panel is the
335 // first displayed. However, on OSX 3.0.5 (at least), if another panel is displayed
336 // first then the icons will be blank unless they're set here.
337 m_bpAdd->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
338 m_bpDelete->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
339 }
340
341 if( aShow && m_firstShow )
342 {
343 m_graphicsGrid->SetColSize( 0, m_graphicsGrid->GetColSize( 0 ) + 1 );
344 m_firstShow = false;
345 }
346
347 return retVal;
348}
349
350
352{
354 return false;
355
356 SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
357 BOARD_DESIGN_SETTINGS& cfg = mgr.GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>()->m_DesignSettings;
358
359 for( int i = 0; i < ROW_COUNT; ++i )
360 {
362
363 if( i == ROW_EDGES || i == ROW_COURTYARD )
364 continue;
365
369
370 wxString msg = m_graphicsGrid->GetCellValue( i, COL_TEXT_ITALIC );
371 cfg.m_TextItalic[ i ] = wxGridCellBoolEditor::IsTrueValue( msg );
372 }
373
374 // Footprint defaults
375 cfg.m_DefaultFPTextItems.clear();
376
377 wxGridTableBase* table = m_fieldPropsGrid->GetTable();
378
379 for( int i : { REFERENCE_FIELD, VALUE_FIELD } )
380 {
381 wxString text = table->GetValue( i, 0 );
382 bool visible = table->GetValueAsBool( i, 1 );
383 int layer = (int) table->GetValueAsLong( i, 2 );
384
385 cfg.m_DefaultFPTextItems.emplace_back( text, visible, layer );
386 }
387
388 table = m_textItemsGrid->GetTable();
389
390 for( int i = 0; i < m_textItemsGrid->GetNumberRows(); ++i )
391 {
392 wxString text = table->GetValue( i, 0 );
393 bool visible = table->GetValueAsBool( i, 1 );
394 int layer = (int) table->GetValueAsLong( i, 2 );
395
396 cfg.m_DefaultFPTextItems.emplace_back( text, visible, layer );
397 }
398
399 return true;
400}
401
402
403void PANEL_FP_EDITOR_DEFAULTS::OnAddTextItem( wxCommandEvent& event )
404{
406 return;
407
408 wxGridTableBase* table = m_textItemsGrid->GetTable();
409
410 int newRow = m_textItemsGrid->GetNumberRows();
411 table->AppendRows( 1 );
412 table->SetValueAsBool( newRow, 1, table->GetValueAsBool( newRow - 1, 1 ) );
413 table->SetValueAsLong( newRow, 2, table->GetValueAsLong( newRow - 1, 2 ) );
414
415 m_textItemsGrid->MakeCellVisible( newRow, 0 );
416 m_textItemsGrid->SetGridCursor( newRow, 0 );
417
418 m_textItemsGrid->EnableCellEditControl( true );
419 m_textItemsGrid->ShowCellEditControl();
420}
421
422
424{
425 wxArrayInt selectedRows = m_textItemsGrid->GetSelectedRows();
426
427 if( selectedRows.empty() && m_textItemsGrid->GetGridCursorRow() >= 0 )
428 selectedRows.push_back( m_textItemsGrid->GetGridCursorRow() );
429
430 if( selectedRows.empty() )
431 return;
432
433 for( int row : selectedRows )
434 {
435 if( row < 2 )
436 {
437 DisplayError( nullptr, _( "Reference and value are mandatory." ) );
438 return;
439 }
440 }
441
443 return;
444
445 // Reverse sort so deleting a row doesn't change the indexes of the other rows.
446 selectedRows.Sort( []( int* first, int* second ) { return *second - *first; } );
447
448 for( int row : selectedRows )
449 {
450 m_textItemsGrid->GetTable()->DeleteRows( row, 1 );
451
452 if( m_textItemsGrid->GetNumberRows() > 0 )
453 {
454 m_textItemsGrid->MakeCellVisible( std::max( 0, row-1 ),
455 m_textItemsGrid->GetGridCursorCol() );
456 m_textItemsGrid->SetGridCursor( std::max( 0, row-1 ),
457 m_textItemsGrid->GetGridCursorCol() );
458 }
459 }
460}
461
462
464{
466 cfg.Load(); // Loading without a file will init to defaults
467
468 loadFPSettings( &cfg );
469}
470
471
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
Container for design settings for a BOARD object.
std::vector< TEXT_ITEM_INFO > m_DefaultFPTextItems
int m_TextThickness[LAYER_CLASS_COUNT]
int m_LineThickness[LAYER_CLASS_COUNT]
VECTOR2I m_TextSize[LAYER_CLASS_COUNT]
bool m_TextItalic[LAYER_CLASS_COUNT]
BOARD_DESIGN_SETTINGS m_DesignSettings
Only some of these settings are actually used for footprint editing.
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:61
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.
Class PANEL_FP_EDITOR_DEFAULTS_BASE.
virtual void OnAddTextItem(wxCommandEvent &event) override
void ResetPanel() override
Reset the contents of this panel.
bool Show(bool aShow) override
PANEL_FP_EDITOR_DEFAULTS(wxWindow *aParent, UNITS_PROVIDER *aUnitsProvider)
virtual void OnDeleteTextItem(wxCommandEvent &event) override
void loadFPSettings(FOOTPRINT_EDITOR_SETTINGS *aCfg)
T * GetAppSettings()
Returns a handle to the a given settings by type If the settings have already been loaded,...
void SetBitmap(const wxBitmapBundle &aBmp)
std::vector< TEXT_ITEM_INFO > m_items
bool DeleteRows(size_t aPos, size_t aNumRows) override
wxString GetRowLabelValue(int aRow) override
TEXT_ITEMS_GRID_TABLE(bool aForFieldProps)
void SetValueAsLong(int row, int col, long value) override
void SetValue(int row, int col, const wxString &value) override
wxString GetValue(int row, int col) override
void SetValueAsBool(int row, int col, bool value) override
wxString GetColLabelValue(int aCol) override
bool CanSetValueAs(int aRow, int aCol, const wxString &aTypeName) override
bool GetValueAsBool(int row, int col) override
bool AppendRows(size_t aNumRows=1) override
bool CanGetValueAs(int aRow, int aCol, const wxString &aTypeName) override
long GetValueAsLong(int row, int col) override
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=true, bool aKeep=false)
Calculates the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:545
void SetTable(wxGridTableBase *table, bool aTakeOwnership=false)
Hide wxGrid's SetTable() method with one which doesn't mess up the grid column widths when setting th...
Definition: wx_grid.cpp:156
void SetUnitValue(int aRow, int aCol, int aValue)
Set a unitized cell's value.
Definition: wx_grid.cpp:527
int GetUnitValue(int aRow, int aCol)
Apply standard KiCad unit and eval services to a numeric cell.
Definition: wx_grid.cpp:506
void SetAutoEvalCols(const std::vector< int > &aCols)
Definition: wx_grid.h:104
void SetUnitsProvider(UNITS_PROVIDER *aProvider, int aCol=0)
Set a UNITS_PROVIDER to enable use of unit- and eval-based Getters.
Definition: wx_grid.cpp:497
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:448
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:280
This file is part of the common library.
#define _(s)
Base window classes and related definitions.
@ F_SilkS
Definition: layer_ids.h:105
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:119
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588