KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_fp_editor_field_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-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
25
26#include <pgm_base.h>
29#include <template_fieldnames.h>
31#include <grid_tricks.h>
32#include <eda_text.h>
34#include <bitmaps.h>
35#include <confirm.h>
36
37
38class TEXT_ITEMS_GRID_TABLE : public wxGridTableBase
39{
41 std::vector<TEXT_ITEM_INFO> m_items;
42
43public:
44 TEXT_ITEMS_GRID_TABLE( bool aForFieldProps ) : m_forFieldProps( aForFieldProps ) {}
45
46 int GetNumberRows() override { return m_items.size(); }
47 int GetNumberCols() override { return 3; }
48
49 wxString GetColLabelValue( int aCol ) override
50 {
51 switch( aCol )
52 {
53 case 0: return m_forFieldProps ? _( "Value" ) : _( "Text Items" );
54 case 1: return _( "Show" );
55 case 2: return _( "Layer" );
56 default: return wxEmptyString;
57 }
58 }
59
60 wxString GetRowLabelValue( int aRow ) override
61 {
62 switch( aRow )
63 {
64 case 0: return _( "Reference designator" );
65 case 1: return _( "Value" );
66 default: return wxEmptyString;
67 }
68 }
69
70 bool CanGetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
71 {
72 switch( aCol )
73 {
74 case 0: return aTypeName == wxGRID_VALUE_STRING;
75 case 1: return aTypeName == wxGRID_VALUE_BOOL;
76 case 2: return aTypeName == wxGRID_VALUE_NUMBER;
77 default: wxFAIL; return false;
78 }
79 }
80
81 bool CanSetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
82 {
83 return CanGetValueAs( aRow, aCol, aTypeName );
84 }
85
86 wxString GetValue( int row, int col ) override { return m_items[row].m_Text; }
87 void SetValue( int row, int col, const wxString& value ) override
88 {
89 if( col == 0 )
90 m_items[row].m_Text = value;
91 }
92
93 bool GetValueAsBool( int row, int col ) override { return m_items[row].m_Visible; }
94 void SetValueAsBool( int row, int col, bool value ) override
95 {
96 if( col == 1 )
97 m_items[row].m_Visible = value;
98 }
99
100 long GetValueAsLong( int row, int col ) override { return m_items[row].m_Layer; }
101 void SetValueAsLong( int row, int col, long value ) override
102 {
103 if( col == 2 )
104 m_items[row].m_Layer = (int) value;
105 }
106
107 bool AppendRows( size_t aNumRows = 1 ) override
108 {
109 for( size_t i = 0; i < aNumRows; ++i )
110 m_items.emplace_back( wxT( "" ), true, F_SilkS );
111
112 if( GetView() )
113 {
114 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, aNumRows );
115 GetView()->ProcessTableMessage( msg );
116 }
117
118 return true;
119 }
120
121 bool DeleteRows( size_t aPos, size_t aNumRows ) override
122 {
123 // aPos may be a large positive, e.g. size_t(-1), and the sum of
124 // aPos+aNumRows may wrap here, so both ends of the range are tested.
125 if( aPos < m_items.size() && aPos + aNumRows <= m_items.size() )
126 {
127 m_items.erase( m_items.begin() + aPos, m_items.begin() + aPos + aNumRows );
128
129 if( GetView() )
130 {
131 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aPos, aNumRows );
132 GetView()->ProcessTableMessage( msg );
133 }
134 return true;
135 }
136
137 return false;
138 }
139};
140
141
143{
145 return *mgr.GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" );
146}
147
148
150 UNITS_PROVIDER* aUnitsProvider ) :
151 PANEL_FP_EDITOR_FIELD_DEFAULTS_BASE( aParent ), m_unitProvider( aUnitsProvider ),
152 m_designSettings( GetPgmSettings().m_DesignSettings )
153{
154 m_fieldPropsGrid->SetDefaultRowSize( m_fieldPropsGrid->GetDefaultRowSize() + 4 );
155
156 m_fieldPropsGrid->SetTable( new TEXT_ITEMS_GRID_TABLE( true ), true );
157 m_fieldPropsGrid->PushEventHandler( new GRID_TRICKS( m_fieldPropsGrid ) );
158 m_fieldPropsGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
159
160 wxGridCellAttr* attr = new wxGridCellAttr;
161 attr->SetRenderer( new wxGridCellBoolRenderer() );
162 attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
163 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
164 m_fieldPropsGrid->SetColAttr( 1, attr );
165
166 attr = new wxGridCellAttr;
167 attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( nullptr ) );
168 attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( nullptr, {} ) );
169 m_fieldPropsGrid->SetColAttr( 2, attr );
170
171 m_textItemsGrid->SetDefaultRowSize( m_textItemsGrid->GetDefaultRowSize() + 4 );
172
173 m_textItemsGrid->SetTable( new TEXT_ITEMS_GRID_TABLE( false ), true );
174 m_textItemsGrid->PushEventHandler( new GRID_TRICKS( m_textItemsGrid ) );
175 m_textItemsGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
176
177 attr = new wxGridCellAttr;
178 attr->SetRenderer( new wxGridCellBoolRenderer() );
179 attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
180 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
181 m_textItemsGrid->SetColAttr( 1, attr );
182
183 attr = new wxGridCellAttr;
184 attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( nullptr ) );
185 attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( nullptr, {} ) );
186 m_textItemsGrid->SetColAttr( 2, attr );
187}
188
189
191{
192 // destroy GRID_TRICKS before grids.
193 m_fieldPropsGrid->PopEventHandler( true );
194 m_textItemsGrid->PopEventHandler( true );
195}
196
197
199{
200 // Footprint defaults
201 m_fieldPropsGrid->GetTable()->DeleteRows( 0, m_fieldPropsGrid->GetNumberRows() );
202 m_fieldPropsGrid->GetTable()->AppendRows( 2 );
203
204 for( int i : { REFERENCE_FIELD, VALUE_FIELD } )
205 {
207
208 m_fieldPropsGrid->GetTable()->SetValue( i, 0, item.m_Text );
209 m_fieldPropsGrid->GetTable()->SetValueAsBool( i, 1, item.m_Visible );
210 m_fieldPropsGrid->GetTable()->SetValueAsLong( i, 2, item.m_Layer );
211 }
212
213 m_textItemsGrid->GetTable()->DeleteRows( 0, m_textItemsGrid->GetNumberRows() );
214 m_textItemsGrid->GetTable()->AppendRows( aCfg->m_DesignSettings.m_DefaultFPTextItems.size()
215 - 2 );
216
217 for( int i = 2; i < (int) aCfg->m_DesignSettings.m_DefaultFPTextItems.size(); ++i )
218 {
220
221 m_textItemsGrid->GetTable()->SetValue( i - 2, 0, item.m_Text );
222 m_textItemsGrid->GetTable()->SetValueAsBool( i - 2, 1, item.m_Visible );
223 m_textItemsGrid->GetTable()->SetValueAsLong( i - 2, 2, item.m_Layer );
224 }
225
226 Layout();
227}
228
229
231{
233
234 loadFPSettings( &cfg );
235
236 return true;
237}
238
239
241{
242 bool retVal = wxPanel::Show( aShow );
243
244 if( aShow )
245 {
246 // These *should* work in the constructor, and indeed they do if this panel is the
247 // first displayed. However, on OSX 3.0.5 (at least), if another panel is displayed
248 // first then the icons will be blank unless they're set here.
249 m_bpAdd->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
250 m_bpDelete->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
251 }
252
253 return retVal;
254}
255
256
258{
260 return false;
261
263
264 // Footprint defaults
265 cfg.m_DefaultFPTextItems.clear();
266
267 wxGridTableBase* table = m_fieldPropsGrid->GetTable();
268
269 for( int i : { REFERENCE_FIELD, VALUE_FIELD } )
270 {
271 wxString text = table->GetValue( i, 0 );
272 bool visible = table->GetValueAsBool( i, 1 );
273 int layer = (int) table->GetValueAsLong( i, 2 );
274
275 cfg.m_DefaultFPTextItems.emplace_back( text, visible, layer );
276 }
277
278 table = m_textItemsGrid->GetTable();
279
280 for( int i = 0; i < m_textItemsGrid->GetNumberRows(); ++i )
281 {
282 wxString text = table->GetValue( i, 0 );
283 bool visible = table->GetValueAsBool( i, 1 );
284 int layer = (int) table->GetValueAsLong( i, 2 );
285
286 cfg.m_DefaultFPTextItems.emplace_back( text, visible, layer );
287 }
288
289 return true;
290}
291
292
294{
296 return;
297
298 wxGridTableBase* table = m_textItemsGrid->GetTable();
299
300 int newRow = m_textItemsGrid->GetNumberRows();
301 table->AppendRows( 1 );
302 table->SetValueAsBool( newRow, 1, table->GetValueAsBool( newRow - 1, 1 ) );
303 table->SetValueAsLong( newRow, 2, table->GetValueAsLong( newRow - 1, 2 ) );
304
305 m_textItemsGrid->MakeCellVisible( newRow, 0 );
306 m_textItemsGrid->SetGridCursor( newRow, 0 );
307
308 m_textItemsGrid->EnableCellEditControl( true );
309 m_textItemsGrid->ShowCellEditControl();
310}
311
312
314{
315 wxArrayInt selectedRows = m_textItemsGrid->GetSelectedRows();
316
317 if( selectedRows.empty() && m_textItemsGrid->GetGridCursorRow() >= 0 )
318 selectedRows.push_back( m_textItemsGrid->GetGridCursorRow() );
319
320 if( selectedRows.empty() )
321 return;
322
324 return;
325
326 // Reverse sort so deleting a row doesn't change the indexes of the other rows.
327 selectedRows.Sort(
328 []( int* first, int* second )
329 {
330 return *second - *first;
331 } );
332
333 for( int row : selectedRows )
334 {
335 m_textItemsGrid->GetTable()->DeleteRows( row, 1 );
336
337 if( m_textItemsGrid->GetNumberRows() > 0 )
338 {
339 m_textItemsGrid->MakeCellVisible( std::max( 0, row - 1 ),
340 m_textItemsGrid->GetGridCursorCol() );
341 m_textItemsGrid->SetGridCursor( std::max( 0, row - 1 ),
342 m_textItemsGrid->GetGridCursorCol() );
343 }
344 }
345}
346
347
349{
351 cfg.Load(); // Loading without a file will init to defaults
352
353 loadFPSettings( &cfg );
354}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
Container for design settings for a BOARD object.
std::vector< TEXT_ITEM_INFO > m_DefaultFPTextItems
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_FIELD_DEFAULTS_BASE.
PANEL_FP_EDITOR_FIELD_DEFAULTS(wxWindow *aParent, UNITS_PROVIDER *aUnitsProvider)
virtual void OnAddTextItem(wxCommandEvent &event) override
virtual void OnDeleteTextItem(wxCommandEvent &event) override
void loadFPSettings(const FOOTPRINT_EDITOR_SETTINGS *aCfg)
void ResetPanel() override
Reset the contents of this panel.
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:142
T * GetAppSettings(const wxString &aFilename)
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
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:270
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:637
This file is part of the common library.
#define _(s)
@ F_SilkS
Definition: layer_ids.h:100
static FOOTPRINT_EDITOR_SETTINGS & GetPgmSettings()
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
see class PGM_BASE
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".