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 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>
36#include <bitmaps.h>
37#include <confirm.h>
38
39
40class TEXT_ITEMS_GRID_TABLE : public wxGridTableBase
41{
43 std::vector<TEXT_ITEM_INFO> m_items;
44
45public:
46 TEXT_ITEMS_GRID_TABLE( bool aForFieldProps ) :
47 m_forFieldProps( aForFieldProps )
48 {}
49
50 int GetNumberRows() override { return m_items.size(); }
51 int GetNumberCols() override { return m_forFieldProps ? 3 : 2; }
52
53 wxString GetColLabelValue( int aCol ) override
54 {
55 if( m_forFieldProps )
56 {
57 switch( aCol )
58 {
59 case 0: return _( "Value" );
60 case 1: return _( "Show" );
61 case 2: return _( "Layer" );
62 default: return wxEmptyString;
63 }
64 }
65 else
66 {
67 switch( aCol )
68 {
69 case 0: return _( "Text Items" );
70 case 1: return _( "Layer" );
71 default: return wxEmptyString;
72 }
73 }
74 }
75
76 wxString GetRowLabelValue( int aRow ) override
77 {
78 switch( aRow )
79 {
80 case 0: return _( "Reference designator" );
81 case 1: return _( "Value" );
82 default: return wxEmptyString;
83 }
84 }
85
86 bool CanGetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
87 {
88 if( m_forFieldProps )
89 {
90 switch( aCol )
91 {
92 case 0: return aTypeName == wxGRID_VALUE_STRING;
93 case 1: return aTypeName == wxGRID_VALUE_BOOL;
94 case 2: return aTypeName == wxGRID_VALUE_NUMBER;
95 default: wxFAIL; return false;
96 }
97 }
98 else
99 {
100 switch( aCol )
101 {
102 case 0: return aTypeName == wxGRID_VALUE_STRING;
103 case 1: return aTypeName == wxGRID_VALUE_NUMBER;
104 default: wxFAIL; return false;
105 }
106 }
107 }
108
109 bool CanSetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
110 {
111 return CanGetValueAs( aRow, aCol, aTypeName );
112 }
113
114 wxString GetValue( int row, int col ) override { return m_items[row].m_Text; }
115 void SetValue( int row, int col, const wxString& value ) override
116 {
117 if( col == 0 )
118 m_items[row].m_Text = value;
119 }
120
121 bool GetValueAsBool( int row, int col ) override { return m_items[row].m_Visible; }
122 void SetValueAsBool( int row, int col, bool value ) override
123 {
124 if( col == 1 )
125 m_items[row].m_Visible = value;
126 }
127
128 long GetValueAsLong( int row, int col ) override { return m_items[row].m_Layer; }
129 void SetValueAsLong( int row, int col, long value ) override
130 {
131 if( col == GetNumberCols() - 1 )
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
174
175
178 m_designSettings( GetPgmSettings().m_DesignSettings )
179{
180 m_fieldPropsGrid->SetTable( new TEXT_ITEMS_GRID_TABLE( true ), true );
181 m_fieldPropsGrid->PushEventHandler( new GRID_TRICKS( m_fieldPropsGrid ) );
182 m_fieldPropsGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
183
184 wxGridCellAttr* attr = new wxGridCellAttr;
185 attr->SetRenderer( new wxGridCellBoolRenderer() );
186 attr->SetReadOnly();
187 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
188 m_fieldPropsGrid->SetColAttr( 1, attr );
189
190 attr = new wxGridCellAttr;
191 attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( nullptr ) );
192 attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( nullptr, {} ) );
193 m_fieldPropsGrid->SetColAttr( 2, attr );
194
195 m_textItemsGrid->SetTable( new TEXT_ITEMS_GRID_TABLE( false ), true );
196 m_textItemsGrid->PushEventHandler( new GRID_TRICKS( m_textItemsGrid ) );
197 m_textItemsGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
198
199 attr = new wxGridCellAttr;
200 attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( nullptr ) );
201 attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( nullptr, {} ) );
202 m_textItemsGrid->SetColAttr( 1, attr );
203
206}
207
208
210{
211 m_fieldPropsGrid->PopEventHandler( true );
212 m_textItemsGrid->PopEventHandler( true );
213}
214
215
217{
218 wxGridTableBase* table = m_fieldPropsGrid->GetTable();
219 table->DeleteRows( 0, m_fieldPropsGrid->GetNumberRows() );
220 table->AppendRows( 2 );
221
222 for( int i = 0; i < std::min<int>( 2, (int) aCfg->m_DesignSettings.m_DefaultFPTextItems.size() ); ++i )
223 {
225
226 table->SetValue( i, 0, item.m_Text );
227 table->SetValueAsBool( i, 1, item.m_Visible );
228 table->SetValueAsLong( i, 2, item.m_Layer );
229 }
230
231 table = m_textItemsGrid->GetTable();
232 table->DeleteRows( 0, m_textItemsGrid->GetNumberRows() );
233
234 int extra_texts_cnt = (int) aCfg->m_DesignSettings.m_DefaultFPTextItems.size() - 2;
235
236 if( extra_texts_cnt > 0 )
237 table->AppendRows( extra_texts_cnt );
238
239 for( int i = 2; i < (int) aCfg->m_DesignSettings.m_DefaultFPTextItems.size(); ++i )
240 {
242
243 table->SetValue( i - 2, 0, item.m_Text );
244 table->SetValueAsLong( i - 2, 1, item.m_Layer );
245 }
246
247 Layout();
248}
249
250
252{
254
255 loadFPSettings( &cfg );
256
257 return true;
258}
259
260
262{
263 bool retVal = wxPanel::Show( aShow );
264
265 if( aShow )
266 {
269 }
270
271 return retVal;
272}
273
274
276{
277 if( !m_textItemsGrid->CommitPendingChanges() )
278 return false;
279
281
282 cfg.m_DefaultFPTextItems.clear();
283
284 wxGridTableBase* table = m_fieldPropsGrid->GetTable();
285
286 for( int i : { 0, 1 } )
287 {
288 wxString text = table->GetValue( i, 0 );
289 bool visible = table->GetValueAsBool( i, 1 );
290 PCB_LAYER_ID layer = static_cast<PCB_LAYER_ID>( table->GetValueAsLong( i, 2 ) );
291
292 cfg.m_DefaultFPTextItems.emplace_back( text, visible, layer );
293 }
294
295 table = m_textItemsGrid->GetTable();
296
297 for( int i = 0; i < m_textItemsGrid->GetNumberRows(); ++i )
298 {
299 wxString text = table->GetValue( i, 0 );
300 PCB_LAYER_ID layer = static_cast<PCB_LAYER_ID>( table->GetValueAsLong( i, 1 ) );
301
302 cfg.m_DefaultFPTextItems.emplace_back( text, true, layer );
303 }
304
305 return true;
306}
307
308
310{
311 m_textItemsGrid->OnAddRow(
312 [&]() -> std::pair<int, int>
313 {
314 wxGridTableBase* table = m_textItemsGrid->GetTable();
315
316 int newRow = m_textItemsGrid->GetNumberRows();
317 table->AppendRows( 1 );
318
319 long defaultBoardLayer = F_SilkS;
320
321 if( newRow > 0 )
322 defaultBoardLayer = table->GetValueAsLong( newRow - 1, 1 );
323
324 table->SetValueAsLong( newRow, 1, defaultBoardLayer );
325
326 return { newRow, 0 };
327 } );
328}
329
330
332{
333 m_textItemsGrid->OnDeleteRows(
334 [&]( int row )
335 {
336 m_textItemsGrid->GetTable()->DeleteRows( row, 1 );
337 } );
338}
339
340
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.
PANEL_FP_EDITOR_FIELD_DEFAULTS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
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.
std::vector< TEXT_ITEM_INFO > m_items
bool DeleteRows(size_t aPos, size_t aNumRows) override
wxString GetRowLabelValue(int aRow) override
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
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()
see class PGM_BASE
T * GetAppSettings(const char *aFilename)