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