KiCad PCB EDA Suite
panel_modedit_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-2019 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 <widgets/paged_dialog.h>
25 #include <footprint_edit_frame.h>
27 #include <widgets/wx_grid.h>
28 #include <grid_tricks.h>
29 
30 #include <panel_modedit_defaults.h>
31 #include <grid_layer_box_helpers.h>
32 #include <bitmaps.h>
33 
34 class TEXT_ITEMS_GRID_TABLE : public wxGridTableBase
35 {
36  std::vector<TEXT_ITEM_INFO> m_items;
37 
38 public:
40  { }
41 
42  int GetNumberRows() override { return m_items.size(); }
43  int GetNumberCols() override { return 3; }
44 
45  wxString GetColLabelValue( int aCol ) override
46  {
47  switch( aCol )
48  {
49  case 0: return _( "Text Items" );
50  case 1: return _( "Show" );
51  case 2: return _( "Layer" );
52  default: return wxEmptyString;
53  }
54  }
55 
56  wxString GetRowLabelValue( int aRow ) override
57  {
58  switch( aRow )
59  {
60  case 0: return _( "Reference designator" );
61  case 1: return _( "Value" );
62  default: return wxEmptyString;
63  }
64  }
65 
66  bool CanGetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
67  {
68  switch( aCol )
69  {
70  case 0: return aTypeName == wxGRID_VALUE_STRING;
71  case 1: return aTypeName == wxGRID_VALUE_BOOL;
72  case 2: return aTypeName == wxGRID_VALUE_NUMBER;
73  default: wxFAIL; return false;
74  }
75  }
76 
77  bool CanSetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
78  {
79  return CanGetValueAs( aRow, aCol, aTypeName );
80  }
81 
82  wxString GetValue( int row, int col ) override
83  {
84  return m_items[row].m_Text;
85  }
86  void SetValue( int row, int col, const wxString& value ) override
87  {
88  if( col == 0 )
89  m_items[row].m_Text = value;
90  }
91 
92  bool GetValueAsBool( int row, int col ) override
93  {
94  return m_items[row].m_Visible;
95  }
96  void SetValueAsBool( int row, int col, bool value ) override
97  {
98  if( col == 1 )
99  m_items[row].m_Visible = value;
100  }
101 
102  long GetValueAsLong( int row, int col ) override
103  {
104  return m_items[row].m_Layer;
105  }
106  void SetValueAsLong( int row, int col, long value ) override
107  {
108  if( col == 2 )
109  m_items[row].m_Layer = (int) value;
110  }
111 
112  bool AppendRows( size_t aNumRows = 1 ) override
113  {
114  for( size_t i = 0; i < aNumRows; ++i )
115  m_items.emplace_back( wxT( "" ), true, F_SilkS );
116 
117  if( GetView() )
118  {
119  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, aNumRows );
120  GetView()->ProcessTableMessage( msg );
121  }
122 
123  return true;
124  }
125 
126  bool DeleteRows( size_t aPos, size_t aNumRows ) override
127  {
128  // aPos may be a large positive, e.g. size_t(-1), and the sum of
129  // aPos+aNumRows may wrap here, so both ends of the range are tested.
130  if( aPos < m_items.size() && aPos + aNumRows <= m_items.size() )
131  {
132  m_items.erase( m_items.begin() + aPos, m_items.begin() + aPos + aNumRows );
133 
134  if( GetView() )
135  {
136  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aPos, aNumRows );
137  GetView()->ProcessTableMessage( msg );
138  }
139  return true;
140  }
141 
142  return false;
143  }
144 };
145 
146 
147 // Columns of layer classes grid
148 enum
149 {
155 };
156 
157 enum
158 {
159  ROW_SILK = 0,
165 
167 };
168 
169 
171  PANEL_MODEDIT_DEFAULTS_BASE( aParent->GetTreebook() ),
172  m_brdSettings( aFrame->GetDesignSettings() ),
173  m_frame( aFrame ),
174  m_Parent( aParent )
175 {
176  m_textItemsGrid->SetDefaultRowSize( m_textItemsGrid->GetDefaultRowSize() + 4 );
177  m_layerClassesGrid->SetDefaultRowSize( m_layerClassesGrid->GetDefaultRowSize() + 4 );
178 
180 
181  wxGridCellAttr* attr = new wxGridCellAttr;
182  attr->SetRenderer( new wxGridCellBoolRenderer() );
183  attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
184  m_textItemsGrid->SetColAttr( 1, attr );
185 
186  attr = new wxGridCellAttr;
187  attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( m_frame ) );
188  attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( m_frame, {} ) );
189  m_textItemsGrid->SetColAttr( 2, attr );
190 
191  // Work around a bug in wxWidgets where it fails to recalculate the grid height
192  // after changing the default row size
193  m_layerClassesGrid->AppendRows( 1 );
194  m_layerClassesGrid->DeleteRows( m_layerClassesGrid->GetNumberRows() - 1, 1 );
195 
196  m_layerClassesGrid->PushEventHandler( new GRID_TRICKS( m_layerClassesGrid ) );
197 
198  wxFont infoFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
199  infoFont.SetSymbolicSize( wxFONTSIZE_SMALL );
200  m_staticTextInfo->SetFont( infoFont );
201 }
202 
203 
205 {
206  // destroy GRID_TRICKS before m_layerClassesGrid.
207  m_layerClassesGrid->PopEventHandler( true );
208 }
209 
210 
212 {
213  wxColour disabledColour = wxSystemSettings::GetColour( wxSYS_COLOUR_BACKGROUND );
214 
215 #define SET_MILS_CELL( row, col, val ) \
216  m_layerClassesGrid->SetCellValue( row, col, StringFromValue( m_frame->GetUserUnits(), val, true ) )
217 
218 #define DISABLE_CELL( row, col ) \
219  m_layerClassesGrid->SetReadOnly( row, col ); m_layerClassesGrid->SetCellBackgroundColour( row, col, disabledColour );
220 
221  for( int i = 0; i < ROW_COUNT; ++i )
222  {
224 
225  if( i == ROW_EDGES || i == ROW_COURTYARD )
226  {
231  }
232  else
233  {
237  m_layerClassesGrid->SetCellValue( i, COL_TEXT_ITALIC, m_brdSettings.m_TextItalic[ i ] ? "1" : "" );
238 
239  auto attr = new wxGridCellAttr;
240  attr->SetRenderer( new wxGridCellBoolRenderer() );
241  attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
242  attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
243  m_layerClassesGrid->SetAttr( i, COL_TEXT_ITALIC, attr );
244  }
245  }
246 
247  // Footprint defaults
248  m_textItemsGrid->GetTable()->AppendRows( m_brdSettings.m_DefaultFPTextItems.size() );
249 
250  for( size_t i = 0; i < m_brdSettings.m_DefaultFPTextItems.size(); ++i )
251  {
253 
254  m_textItemsGrid->GetTable()->SetValue( i, 0, item.m_Text );
255  m_textItemsGrid->GetTable()->SetValueAsBool( i, 1, item.m_Visible );
256  m_textItemsGrid->GetTable()->SetValueAsLong( i, 2, item.m_Layer );
257  }
258 
259  for( int col = 0; col < m_layerClassesGrid->GetNumberCols(); col++ )
260  {
261  // Set the minimal width to the column label size.
262  m_layerClassesGrid->SetColMinimalWidth( col, m_layerClassesGrid->GetVisibleWidth( col, true, false, false ) );
263 
264  // Set the width to see the full contents
265  if( m_layerClassesGrid->IsColShown( col ) )
266  m_layerClassesGrid->SetColSize( col, m_layerClassesGrid->GetVisibleWidth( col, true, true, true ) );
267  }
268 
269  m_layerClassesGrid->SetRowLabelSize( m_layerClassesGrid->GetVisibleWidth( -1, true, true, true ) );
270 
271  Layout();
272 
273  return true;
274 }
275 
276 
278 {
279  bool retVal = wxPanel::Show( aShow );
280 
281  if( aShow )
282  {
283  // These *should* work in the constructor, and indeed they do if this panel is the
284  // first displayed. However, on OSX 3.0.5 (at least), if another panel is displayed
285  // first then the icons will be blank unless they're set here.
286  m_bpAdd->SetBitmap( KiBitmap( small_plus_xpm ) );
287  m_bpDelete->SetBitmap( KiBitmap( trash_xpm ) );
288  }
289 
290  if( aShow && m_firstShow )
291  {
292  m_layerClassesGrid->SetColSize( 0, m_layerClassesGrid->GetColSize( 0 ) + 1 );
293  m_firstShow = false;
294  }
295 
296  return retVal;
297 }
298 
299 
300 int PANEL_MODEDIT_DEFAULTS::getGridValue( int aRow, int aCol )
301 {
303  m_layerClassesGrid->GetCellValue( aRow, aCol ) );
304 }
305 
306 
308 {
310  return false;
311 
312  // Test text parameters.
313  for( int row : { ROW_SILK, ROW_COPPER, ROW_FAB, ROW_OTHERS } )
314  {
315  int textSize = std::min( getGridValue( row, COL_TEXT_WIDTH ),
316  getGridValue( row, COL_TEXT_HEIGHT ) );
317 
318  if( getGridValue( row, COL_TEXT_THICKNESS ) > textSize / 4 )
319  {
320  wxString msg = _( "Text will not be readable with a thickness greater than\n"
321  "1/4 its width or height." );
323  return false;
324  }
325  }
326 
327  return true;
328 }
329 
330 
332 {
333  if( !validateData() )
334  return false;
335 
336  for( int i = 0; i < ROW_COUNT; ++i )
337  {
339 
340  if( i == ROW_EDGES || i == ROW_COURTYARD )
341  continue;
342 
346 
347  wxString msg = m_layerClassesGrid->GetCellValue( i, COL_TEXT_ITALIC );
348  m_brdSettings.m_TextItalic[ i ] = wxGridCellBoolEditor::IsTrueValue( msg );
349  }
350 
351  // Footprint defaults
352  wxGridTableBase* table = m_textItemsGrid->GetTable();
354 
355  for( int i = 0; i < m_textItemsGrid->GetNumberRows(); ++i )
356  {
357  wxString text = table->GetValue( i, 0 );
358  bool visible = table->GetValueAsBool( i, 1 );
359  int layer = (int) table->GetValueAsLong( i, 2 );
360 
361  m_brdSettings.m_DefaultFPTextItems.emplace_back( text, visible, layer );
362  }
363 
365 
367  cfg->m_DesignSettings = m_brdSettings;
368 
369  return true;
370 }
371 
372 
373 void PANEL_MODEDIT_DEFAULTS::OnAddTextItem( wxCommandEvent& event )
374 {
376  return;
377 
378  wxGridTableBase* table = m_textItemsGrid->GetTable();
379 
380  int newRow = m_textItemsGrid->GetNumberRows();
381  table->AppendRows( 1 );
382  table->SetValueAsBool( newRow, 1, table->GetValueAsBool( newRow - 1, 1 ) );
383  table->SetValueAsLong( newRow, 2, table->GetValueAsLong( newRow - 1, 2 ) );
384 
385  m_textItemsGrid->MakeCellVisible( newRow, 0 );
386  m_textItemsGrid->SetGridCursor( newRow, 0 );
387 
388  m_textItemsGrid->EnableCellEditControl( true );
389  m_textItemsGrid->ShowCellEditControl();
390 }
391 
392 
393 void PANEL_MODEDIT_DEFAULTS::OnDeleteTextItem( wxCommandEvent& event )
394 {
396  return;
397 
398  int curRow = m_textItemsGrid->GetGridCursorRow();
399 
400  if( curRow < 2 ) // First two rows are required
401  return;
402 
403  m_textItemsGrid->GetTable()->DeleteRows( curRow, 1 );
404 
405  curRow = std::max( 0, curRow - 1 );
406  m_textItemsGrid->MakeCellVisible( curRow, m_textItemsGrid->GetGridCursorCol() );
407  m_textItemsGrid->SetGridCursor( curRow, m_textItemsGrid->GetGridCursorCol() );
408 }
409 
410 
const BITMAP_OPAQUE trash_xpm[1]
Definition: trash.cpp:34
std::vector< TEXT_ITEM_INFO > m_DefaultFPTextItems
BOARD_DESIGN_SETTINGS & GetDesignSettings() const override
Returns the BOARD_DESIGN_SETTINGS for the open project Overloaded in FOOTPRINT_EDIT_FRAME.
int getGridValue(int aRow, int aCol)
BOARD_DESIGN_SETTINGS m_brdSettings
bool AppendRows(size_t aNumRows=1) override
bool GetValueAsBool(int row, int col) override
GRID_TRICKS is used to add mouse and command handling (such as cut, copy, and paste) to a WX_GRID ins...
Definition: grid_tricks.h:52
bool CanSetValueAs(int aRow, int aCol, const wxString &aTypeName) override
void SetError(const wxString &aMessage, const wxString &aPageName, int aCtrlId, int aRow=-1, int aCol=-1)
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:65
bool DeleteRows(size_t aPos, size_t aNumRows) override
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=false, bool aKeep=true)
Calculates the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:236
void SetValueAsLong(int row, int col, long value) override
wxSize m_TextSize[LAYER_CLASS_COUNT]
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:82
void SetValue(int row, int col, const wxString &value) override
int m_TextThickness[LAYER_CLASS_COUNT]
bool Show(bool aShow) override
long long int ValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application.
Definition: base_units.cpp:444
wxString GetColLabelValue(int aCol) override
wxString GetRowLabelValue(int aRow) override
bool m_TextItalic[LAYER_CLASS_COUNT]
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:180
int m_LineThickness[LAYER_CLASS_COUNT]
std::vector< TEXT_ITEM_INFO > m_items
#define _(s)
Definition: 3d_actions.cpp:33
#define SET_MILS_CELL(row, col, val)
virtual void OnAddTextItem(wxCommandEvent &event) override
virtual void OnDeleteTextItem(wxCommandEvent &event) override
FOOTPRINT_EDITOR_SETTINGS * GetSettings()
long GetValueAsLong(int row, int col) override
bool CanGetValueAs(int aRow, int aCol, const wxString &aTypeName) override
Class PANEL_MODEDIT_DEFAULTS_BASE.
void SetValueAsBool(int row, int col, bool value) override
const BITMAP_OPAQUE small_plus_xpm[1]
Definition: small_plus.cpp:20
PANEL_MODEDIT_DEFAULTS(FOOTPRINT_EDIT_FRAME *aFrame, PAGED_DIALOG *aParent)
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
wxString GetValue(int row, int col) override
FOOTPRINT_EDIT_FRAME * m_frame
#define DISABLE_CELL(row, col)