KiCad PCB EDA Suite
panel_fp_editor_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 
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  PAGED_DIALOG* aParent) :
172  PANEL_FP_EDITOR_DEFAULTS_BASE( aParent->GetTreebook() ),
173  m_brdSettings( aFrame->GetDesignSettings() ),
174  m_frame( aFrame ),
175  m_parent( aParent )
176 {
177  m_textItemsGrid->SetDefaultRowSize( m_textItemsGrid->GetDefaultRowSize() + 4 );
178  m_layerClassesGrid->SetDefaultRowSize( m_layerClassesGrid->GetDefaultRowSize() + 4 );
179 
181 
182  wxGridCellAttr* attr = new wxGridCellAttr;
183  attr->SetRenderer( new wxGridCellBoolRenderer() );
184  attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
185  m_textItemsGrid->SetColAttr( 1, attr );
186 
187  attr = new wxGridCellAttr;
188  attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( m_frame ) );
189  attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( m_frame, {} ) );
190  m_textItemsGrid->SetColAttr( 2, attr );
191 
192  // Work around a bug in wxWidgets where it fails to recalculate the grid height
193  // after changing the default row size
194  m_layerClassesGrid->AppendRows( 1 );
195  m_layerClassesGrid->DeleteRows( m_layerClassesGrid->GetNumberRows() - 1, 1 );
196 
197  m_layerClassesGrid->PushEventHandler( new GRID_TRICKS( m_layerClassesGrid ) );
198 
199  wxFont infoFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
200  infoFont.SetSymbolicSize( wxFONTSIZE_SMALL );
201  m_staticTextInfo->SetFont( infoFont );
202 }
203 
204 
206 {
207  // destroy GRID_TRICKS before m_layerClassesGrid.
208  m_layerClassesGrid->PopEventHandler( true );
209 }
210 
211 
213 {
214  wxColour disabledColour = wxSystemSettings::GetColour( wxSYS_COLOUR_BACKGROUND );
215 
216 #define SET_MILS_CELL( row, col, val ) \
217  m_layerClassesGrid->SetCellValue( row, col, StringFromValue( m_frame->GetUserUnits(), val, true ) )
218 
219 #define DISABLE_CELL( row, col ) \
220  m_layerClassesGrid->SetReadOnly( row, col ); m_layerClassesGrid->SetCellBackgroundColour( row, col, disabledColour );
221 
222  for( int i = 0; i < ROW_COUNT; ++i )
223  {
225 
226  if( i == ROW_EDGES || i == ROW_COURTYARD )
227  {
232  }
233  else
234  {
238  m_layerClassesGrid->SetCellValue( i, COL_TEXT_ITALIC, m_brdSettings.m_TextItalic[ i ] ? "1" : "" );
239 
240  auto attr = new wxGridCellAttr;
241  attr->SetRenderer( new wxGridCellBoolRenderer() );
242  attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
243  attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
244  m_layerClassesGrid->SetAttr( i, COL_TEXT_ITALIC, attr );
245  }
246  }
247 
248  // Footprint defaults
249  m_textItemsGrid->GetTable()->AppendRows( m_brdSettings.m_DefaultFPTextItems.size() );
250 
251  for( size_t i = 0; i < m_brdSettings.m_DefaultFPTextItems.size(); ++i )
252  {
254 
255  m_textItemsGrid->GetTable()->SetValue( i, 0, item.m_Text );
256  m_textItemsGrid->GetTable()->SetValueAsBool( i, 1, item.m_Visible );
257  m_textItemsGrid->GetTable()->SetValueAsLong( i, 2, item.m_Layer );
258  }
259 
260  for( int col = 0; col < m_layerClassesGrid->GetNumberCols(); col++ )
261  {
262  // Set the minimal width to the column label size.
263  m_layerClassesGrid->SetColMinimalWidth( col, m_layerClassesGrid->GetVisibleWidth( col, true, false, false ) );
264 
265  // Set the width to see the full contents
266  if( m_layerClassesGrid->IsColShown( col ) )
267  m_layerClassesGrid->SetColSize( col, m_layerClassesGrid->GetVisibleWidth( col, true, true, true ) );
268  }
269 
270  m_layerClassesGrid->SetRowLabelSize( m_layerClassesGrid->GetVisibleWidth( -1, true, true, true ) );
271 
272  Layout();
273 
274  return true;
275 }
276 
277 
279 {
280  bool retVal = wxPanel::Show( aShow );
281 
282  if( aShow )
283  {
284  // These *should* work in the constructor, and indeed they do if this panel is the
285  // first displayed. However, on OSX 3.0.5 (at least), if another panel is displayed
286  // first then the icons will be blank unless they're set here.
287  m_bpAdd->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
288  m_bpDelete->SetBitmap( KiBitmap( BITMAPS::small_trash ) );
289  }
290 
291  if( aShow && m_firstShow )
292  {
293  m_layerClassesGrid->SetColSize( 0, m_layerClassesGrid->GetColSize( 0 ) + 1 );
294  m_firstShow = false;
295  }
296 
297  return retVal;
298 }
299 
300 
301 int PANEL_FP_EDITOR_DEFAULTS::getGridValue( int aRow, int aCol )
302 {
304  m_layerClassesGrid->GetCellValue( aRow, aCol ) );
305 }
306 
307 
309 {
311  return false;
312 
313  // Test text parameters.
314  for( int row : { ROW_SILK, ROW_COPPER, ROW_FAB, ROW_OTHERS } )
315  {
316  int textSize = std::min( getGridValue( row, COL_TEXT_WIDTH ),
317  getGridValue( row, COL_TEXT_HEIGHT ) );
318 
319  if( getGridValue( row, COL_TEXT_THICKNESS ) > textSize / 4 )
320  {
321  wxString msg = _( "Text will not be readable with a thickness greater than\n"
322  "1/4 its width or height." );
324  return false;
325  }
326  }
327 
328  return true;
329 }
330 
331 
333 {
334  if( !validateData() )
335  return false;
336 
337  for( int i = 0; i < ROW_COUNT; ++i )
338  {
340 
341  if( i == ROW_EDGES || i == ROW_COURTYARD )
342  continue;
343 
347 
348  wxString msg = m_layerClassesGrid->GetCellValue( i, COL_TEXT_ITALIC );
349  m_brdSettings.m_TextItalic[ i ] = wxGridCellBoolEditor::IsTrueValue( msg );
350  }
351 
352  // Footprint defaults
353  wxGridTableBase* table = m_textItemsGrid->GetTable();
355 
356  for( int i = 0; i < m_textItemsGrid->GetNumberRows(); ++i )
357  {
358  wxString text = table->GetValue( i, 0 );
359  bool visible = table->GetValueAsBool( i, 1 );
360  int layer = (int) table->GetValueAsLong( i, 2 );
361 
362  m_brdSettings.m_DefaultFPTextItems.emplace_back( text, visible, layer );
363  }
364 
366 
368  cfg->m_DesignSettings = m_brdSettings;
369 
370  return true;
371 }
372 
373 
374 void PANEL_FP_EDITOR_DEFAULTS::OnAddTextItem( wxCommandEvent& event )
375 {
377  return;
378 
379  wxGridTableBase* table = m_textItemsGrid->GetTable();
380 
381  int newRow = m_textItemsGrid->GetNumberRows();
382  table->AppendRows( 1 );
383  table->SetValueAsBool( newRow, 1, table->GetValueAsBool( newRow - 1, 1 ) );
384  table->SetValueAsLong( newRow, 2, table->GetValueAsLong( newRow - 1, 2 ) );
385 
386  m_textItemsGrid->MakeCellVisible( newRow, 0 );
387  m_textItemsGrid->SetGridCursor( newRow, 0 );
388 
389  m_textItemsGrid->EnableCellEditControl( true );
390  m_textItemsGrid->ShowCellEditControl();
391 }
392 
393 
394 void PANEL_FP_EDITOR_DEFAULTS::OnDeleteTextItem( wxCommandEvent& event )
395 {
397  return;
398 
399  int curRow = m_textItemsGrid->GetGridCursorRow();
400 
401  if( curRow < 2 ) // First two rows are required
402  return;
403 
404  m_textItemsGrid->GetTable()->DeleteRows( curRow, 1 );
405 
406  curRow = std::max( 0, curRow - 1 );
407  m_textItemsGrid->MakeCellVisible( curRow, m_textItemsGrid->GetGridCursorCol() );
408  m_textItemsGrid->SetGridCursor( curRow, m_textItemsGrid->GetGridCursorCol() );
409 }
410 
411 
std::vector< TEXT_ITEM_INFO > m_DefaultFPTextItems
BOARD_DESIGN_SETTINGS & GetDesignSettings() const override
Returns the BOARD_DESIGN_SETTINGS for the open project.
bool AppendRows(size_t aNumRows=1) override
bool GetValueAsBool(int row, int col) override
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:52
bool CanSetValueAs(int aRow, int aCol, const wxString &aTypeName) override
BOARD_DESIGN_SETTINGS m_brdSettings
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:238
void SetValueAsLong(int row, int col, long value) override
wxSize m_TextSize[LAYER_CLASS_COUNT]
bool Show(bool aShow) override
void SetValue(int row, int col, const wxString &value) override
int m_TextThickness[LAYER_CLASS_COUNT]
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:437
wxString GetColLabelValue(int aCol) override
virtual void OnDeleteTextItem(wxCommandEvent &event) override
wxString GetRowLabelValue(int aRow) override
PANEL_FP_EDITOR_DEFAULTS(FOOTPRINT_EDIT_FRAME *aFrame, PAGED_DIALOG *aParent)
bool m_TextItalic[LAYER_CLASS_COUNT]
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:182
int m_LineThickness[LAYER_CLASS_COUNT]
#define SET_MILS_CELL(row, col, val)
std::vector< TEXT_ITEM_INFO > m_items
virtual void OnAddTextItem(wxCommandEvent &event) override
#define _(s)
Definition: 3d_actions.cpp:33
int getGridValue(int aRow, int aCol)
Class PANEL_FP_EDITOR_DEFAULTS_BASE.
#define DISABLE_CELL(row, col)
FOOTPRINT_EDITOR_SETTINGS * GetSettings()
long GetValueAsLong(int row, int col) override
bool CanGetValueAs(int aRow, int aCol, const wxString &aTypeName) override
FOOTPRINT_EDIT_FRAME * m_frame
void SetValueAsBool(int row, int col, bool value) override
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
wxString GetValue(int row, int col) override
wxBitmap KiBitmap(BITMAPS aBitmap)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:104