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, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <pgm_base.h>
26#include <template_fieldnames.h>
28#include <grid_tricks.h>
29#include <eda_text.h>
32#include <bitmaps.h>
33#include <confirm.h>
34
35
36class TEXT_ITEMS_GRID_TABLE : public wxGridTableBase
37{
39 std::vector<TEXT_ITEM_INFO> m_items;
40
41public:
42 TEXT_ITEMS_GRID_TABLE( bool aForFieldProps ) :
43 m_forFieldProps( aForFieldProps )
44 {}
45
46 int GetNumberRows() override { return m_items.size(); }
47 int GetNumberCols() override { return m_forFieldProps ? 3 : 2; }
48
49 wxString GetColLabelValue( int aCol ) override
50 {
51 if( m_forFieldProps )
52 {
53 switch( aCol )
54 {
55 case 0: return _( "Value" );
56 case 1: return _( "Show" );
57 case 2: return _( "Layer" );
58 default: return wxEmptyString;
59 }
60 }
61 else
62 {
63 switch( aCol )
64 {
65 case 0: return _( "Text Items" );
66 case 1: return _( "Layer" );
67 default: return wxEmptyString;
68 }
69 }
70 }
71
72 wxString GetRowLabelValue( int aRow ) override
73 {
74 switch( aRow )
75 {
76 case 0: return _( "Reference designator" );
77 case 1: return _( "Value" );
78 default: return wxEmptyString;
79 }
80 }
81
82 bool CanGetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
83 {
84 if( m_forFieldProps )
85 {
86 switch( aCol )
87 {
88 case 0: return aTypeName == wxGRID_VALUE_STRING;
89 case 1: return aTypeName == wxGRID_VALUE_BOOL;
90 case 2: return aTypeName == wxGRID_VALUE_NUMBER;
91 default: wxFAIL; return false;
92 }
93 }
94 else
95 {
96 switch( aCol )
97 {
98 case 0: return aTypeName == wxGRID_VALUE_STRING;
99 case 1: return aTypeName == wxGRID_VALUE_NUMBER;
100 default: wxFAIL; return false;
101 }
102 }
103 }
104
105 bool CanSetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
106 {
107 return CanGetValueAs( aRow, aCol, aTypeName );
108 }
109
110 wxString GetValue( int row, int col ) override { return m_items[row].m_Text; }
111 void SetValue( int row, int col, const wxString& value ) override
112 {
113 if( col == 0 )
114 m_items[row].m_Text = value;
115 }
116
117 bool GetValueAsBool( int row, int col ) override { return m_items[row].m_Visible; }
118 void SetValueAsBool( int row, int col, bool value ) override
119 {
120 if( col == 1 )
121 m_items[row].m_Visible = value;
122 }
123
124 long GetValueAsLong( int row, int col ) override { return m_items[row].m_Layer; }
125 void SetValueAsLong( int row, int col, long value ) override
126 {
127 if( col == GetNumberCols() - 1 )
128 m_items[row].m_Layer = static_cast<PCB_LAYER_ID>( value );
129 }
130
131 bool AppendRows( size_t aNumRows = 1 ) override
132 {
133 for( size_t i = 0; i < aNumRows; ++i )
134 m_items.emplace_back( wxT( "" ), true, F_SilkS );
135
136 if( GetView() )
137 {
138 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, aNumRows );
139 GetView()->ProcessTableMessage( msg );
140 }
141
142 return true;
143 }
144
145 bool DeleteRows( size_t aPos, size_t aNumRows ) override
146 {
147 // aPos may be a large positive, e.g. size_t(-1), and the sum of
148 // aPos+aNumRows may wrap here, so both ends of the range are tested.
149 if( aPos < m_items.size() && aPos + aNumRows <= m_items.size() )
150 {
151 m_items.erase( m_items.begin() + aPos, m_items.begin() + aPos + aNumRows );
152
153 if( GetView() )
154 {
155 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aPos, aNumRows );
156 GetView()->ProcessTableMessage( msg );
157 }
158 return true;
159 }
160
161 return false;
162 }
163};
164
165
170
171
174 m_designSettings( GetPgmSettings().m_DesignSettings )
175{
176 m_fieldPropsGrid->SetTable( new TEXT_ITEMS_GRID_TABLE( true ), true );
177 m_fieldPropsGrid->PushEventHandler( new GRID_TRICKS( m_fieldPropsGrid ) );
178 m_fieldPropsGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
179
180 wxGridCellAttr* attr = new wxGridCellAttr;
181 attr->SetRenderer( new wxGridCellBoolRenderer() );
182 attr->SetReadOnly();
183 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
184 m_fieldPropsGrid->SetColAttr( 1, attr );
185
186 attr = new wxGridCellAttr;
187 attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( nullptr ) );
188 attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( nullptr, {} ) );
189 m_fieldPropsGrid->SetColAttr( 2, attr );
190
191 m_textItemsGrid->SetTable( new TEXT_ITEMS_GRID_TABLE( false ), true );
192 m_textItemsGrid->PushEventHandler( new GRID_TRICKS( m_textItemsGrid ) );
193 m_textItemsGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
194
195 attr = new wxGridCellAttr;
196 attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( nullptr ) );
197 attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( nullptr, {} ) );
198 m_textItemsGrid->SetColAttr( 1, attr );
199
202}
203
204
206{
207 m_fieldPropsGrid->PopEventHandler( true );
208 m_textItemsGrid->PopEventHandler( true );
209}
210
211
213{
214 wxGridTableBase* table = m_fieldPropsGrid->GetTable();
215 table->DeleteRows( 0, m_fieldPropsGrid->GetNumberRows() );
216 table->AppendRows( 2 );
217
218 for( int i = 0; i < std::min<int>( 2, (int) aCfg->m_DesignSettings.m_DefaultFPTextItems.size() ); ++i )
219 {
221
222 table->SetValue( i, 0, item.m_Text );
223 table->SetValueAsBool( i, 1, item.m_Visible );
224 table->SetValueAsLong( i, 2, item.m_Layer );
225 }
226
227 table = m_textItemsGrid->GetTable();
228 table->DeleteRows( 0, m_textItemsGrid->GetNumberRows() );
229
230 int extra_texts_cnt = (int) aCfg->m_DesignSettings.m_DefaultFPTextItems.size() - 2;
231
232 if( extra_texts_cnt > 0 )
233 table->AppendRows( extra_texts_cnt );
234
235 for( int i = 2; i < (int) aCfg->m_DesignSettings.m_DefaultFPTextItems.size(); ++i )
236 {
238
239 table->SetValue( i - 2, 0, item.m_Text );
240 table->SetValueAsLong( i - 2, 1, item.m_Layer );
241 }
242
243 Layout();
244}
245
246
248{
250
251 loadFPSettings( &cfg );
252
253 return true;
254}
255
256
258{
259 bool retVal = wxPanel::Show( aShow );
260
261 if( aShow )
262 {
265 }
266
267 return retVal;
268}
269
270
272{
273 if( !m_textItemsGrid->CommitPendingChanges() )
274 return false;
275
277
278 cfg.m_DefaultFPTextItems.clear();
279
280 wxGridTableBase* table = m_fieldPropsGrid->GetTable();
281
282 for( int i : { 0, 1 } )
283 {
284 wxString text = table->GetValue( i, 0 );
285 bool visible = table->GetValueAsBool( i, 1 );
286 PCB_LAYER_ID layer = static_cast<PCB_LAYER_ID>( table->GetValueAsLong( i, 2 ) );
287
288 cfg.m_DefaultFPTextItems.emplace_back( text, visible, layer );
289 }
290
291 table = m_textItemsGrid->GetTable();
292
293 for( int i = 0; i < m_textItemsGrid->GetNumberRows(); ++i )
294 {
295 wxString text = table->GetValue( i, 0 );
296 PCB_LAYER_ID layer = static_cast<PCB_LAYER_ID>( table->GetValueAsLong( i, 1 ) );
297
298 cfg.m_DefaultFPTextItems.emplace_back( text, true, layer );
299 }
300
301 return true;
302}
303
304
306{
307 m_textItemsGrid->OnAddRow(
308 [&]() -> std::pair<int, int>
309 {
310 wxGridTableBase* table = m_textItemsGrid->GetTable();
311
312 int newRow = m_textItemsGrid->GetNumberRows();
313 table->AppendRows( 1 );
314
315 long defaultBoardLayer = F_SilkS;
316
317 if( newRow > 0 )
318 defaultBoardLayer = table->GetValueAsLong( newRow - 1, 1 );
319
320 table->SetValueAsLong( newRow, 1, defaultBoardLayer );
321
322 return { newRow, 0 };
323 } );
324}
325
326
328{
329 m_textItemsGrid->OnDeleteRows(
330 [&]( int row )
331 {
332 m_textItemsGrid->GetTable()->DeleteRows( row, 1 );
333 } );
334}
335
336
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
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:57
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:56
@ F_SilkS
Definition layer_ids.h:96
static FOOTPRINT_EDITOR_SETTINGS & GetPgmSettings()
see class PGM_BASE
T * GetAppSettings(const char *aFilename)
std::vector< std::vector< std::string > > table