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-2021 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 #include <wx/treebook.h>
35 
36 class TEXT_ITEMS_GRID_TABLE : public wxGridTableBase
37 {
38  std::vector<TEXT_ITEM_INFO> m_items;
39 
40 public:
42  { }
43 
44  int GetNumberRows() override { return m_items.size(); }
45  int GetNumberCols() override { return 3; }
46 
47  wxString GetColLabelValue( int aCol ) override
48  {
49  switch( aCol )
50  {
51  case 0: return _( "Text Items" );
52  case 1: return _( "Show" );
53  case 2: return _( "Layer" );
54  default: return wxEmptyString;
55  }
56  }
57 
58  wxString GetRowLabelValue( int aRow ) override
59  {
60  switch( aRow )
61  {
62  case 0: return _( "Reference designator" );
63  case 1: return _( "Value" );
64  default: return wxEmptyString;
65  }
66  }
67 
68  bool CanGetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
69  {
70  switch( aCol )
71  {
72  case 0: return aTypeName == wxGRID_VALUE_STRING;
73  case 1: return aTypeName == wxGRID_VALUE_BOOL;
74  case 2: return aTypeName == wxGRID_VALUE_NUMBER;
75  default: wxFAIL; return false;
76  }
77  }
78 
79  bool CanSetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
80  {
81  return CanGetValueAs( aRow, aCol, aTypeName );
82  }
83 
84  wxString GetValue( int row, int col ) override
85  {
86  return m_items[row].m_Text;
87  }
88  void SetValue( int row, int col, const wxString& value ) override
89  {
90  if( col == 0 )
91  m_items[row].m_Text = value;
92  }
93 
94  bool GetValueAsBool( int row, int col ) override
95  {
96  return m_items[row].m_Visible;
97  }
98  void SetValueAsBool( int row, int col, bool value ) override
99  {
100  if( col == 1 )
101  m_items[row].m_Visible = value;
102  }
103 
104  long GetValueAsLong( int row, int col ) override
105  {
106  return m_items[row].m_Layer;
107  }
108  void SetValueAsLong( int row, int col, long value ) override
109  {
110  if( col == 2 )
111  m_items[row].m_Layer = (int) value;
112  }
113 
114  bool AppendRows( size_t aNumRows = 1 ) override
115  {
116  for( size_t i = 0; i < aNumRows; ++i )
117  m_items.emplace_back( wxT( "" ), true, F_SilkS );
118 
119  if( GetView() )
120  {
121  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, aNumRows );
122  GetView()->ProcessTableMessage( msg );
123  }
124 
125  return true;
126  }
127 
128  bool DeleteRows( size_t aPos, size_t aNumRows ) override
129  {
130  // aPos may be a large positive, e.g. size_t(-1), and the sum of
131  // aPos+aNumRows may wrap here, so both ends of the range are tested.
132  if( aPos < m_items.size() && aPos + aNumRows <= m_items.size() )
133  {
134  m_items.erase( m_items.begin() + aPos, m_items.begin() + aPos + aNumRows );
135 
136  if( GetView() )
137  {
138  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aPos, aNumRows );
139  GetView()->ProcessTableMessage( msg );
140  }
141  return true;
142  }
143 
144  return false;
145  }
146 };
147 
148 
149 // Columns of graphics grid
150 enum
151 {
157 };
158 
159 enum
160 {
161  ROW_SILK = 0,
167 
169 };
170 
171 
173  PAGED_DIALOG* aParent) :
174  PANEL_FP_EDITOR_DEFAULTS_BASE( aParent->GetTreebook() ),
175  m_brdSettings( aFrame->GetDesignSettings() ),
176  m_frame( aFrame ),
177  m_parent( aParent )
178 {
179  m_textItemsGrid->SetDefaultRowSize( m_textItemsGrid->GetDefaultRowSize() + 4 );
180  m_graphicsGrid->SetDefaultRowSize( m_graphicsGrid->GetDefaultRowSize() + 4 );
181 
183 
184  wxGridCellAttr* attr = new wxGridCellAttr;
185  attr->SetRenderer( new wxGridCellBoolRenderer() );
186  attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
187  attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
188  m_textItemsGrid->SetColAttr( 1, attr );
189 
190  attr = new wxGridCellAttr;
191  attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( m_frame ) );
192  attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( m_frame, {} ) );
193  m_textItemsGrid->SetColAttr( 2, attr );
194 
195  // Work around a bug in wxWidgets where it fails to recalculate the grid height
196  // after changing the default row size
197  m_graphicsGrid->AppendRows( 1 );
198  m_graphicsGrid->DeleteRows( m_graphicsGrid->GetNumberRows() - 1, 1 );
199 
200  m_graphicsGrid->PushEventHandler( new GRID_TRICKS( m_graphicsGrid ) );
201 
202  wxFont infoFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
203  infoFont.SetSymbolicSize( wxFONTSIZE_SMALL );
204  m_staticTextInfo->SetFont( infoFont );
205 }
206 
207 
209 {
210  // destroy GRID_TRICKS before m_graphicsGrid.
211  m_graphicsGrid->PopEventHandler( true );
212 }
213 
214 
216 {
217  wxColour disabledColour = wxSystemSettings::GetColour( wxSYS_COLOUR_BACKGROUND );
218 
219 #define SET_MILS_CELL( row, col, val ) \
220  m_graphicsGrid->SetCellValue( row, col, StringFromValue( m_frame->GetUserUnits(), val, true ) )
221 
222 #define DISABLE_CELL( row, col ) \
223  m_graphicsGrid->SetReadOnly( row, col ); \
224  m_graphicsGrid->SetCellBackgroundColour( row, col, disabledColour );
225 
226  for( int i = 0; i < ROW_COUNT; ++i )
227  {
229 
230  if( i == ROW_EDGES || i == ROW_COURTYARD )
231  {
236  }
237  else
238  {
242  m_graphicsGrid->SetCellValue( i, COL_TEXT_ITALIC, m_brdSettings.m_TextItalic[ i ] ? "1" : "" );
243 
244  auto attr = new wxGridCellAttr;
245  attr->SetRenderer( new wxGridCellBoolRenderer() );
246  attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
247  attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
248  m_graphicsGrid->SetAttr( i, COL_TEXT_ITALIC, attr );
249  }
250  }
251 
252  // Footprint defaults
253  m_textItemsGrid->GetTable()->AppendRows( m_brdSettings.m_DefaultFPTextItems.size() );
254 
255  for( size_t i = 0; i < m_brdSettings.m_DefaultFPTextItems.size(); ++i )
256  {
258 
259  m_textItemsGrid->GetTable()->SetValue( i, 0, item.m_Text );
260  m_textItemsGrid->GetTable()->SetValueAsBool( i, 1, item.m_Visible );
261  m_textItemsGrid->GetTable()->SetValueAsLong( i, 2, item.m_Layer );
262  }
263 
264  for( int col = 0; col < m_graphicsGrid->GetNumberCols(); col++ )
265  {
266  // Set the minimal width to the column label size.
267  m_graphicsGrid->SetColMinimalWidth( col, m_graphicsGrid->GetVisibleWidth( col, true, false, false ) );
268 
269  // Set the width to see the full contents
270  if( m_graphicsGrid->IsColShown( col ) )
271  m_graphicsGrid->SetColSize( col, m_graphicsGrid->GetVisibleWidth( col, true, true, true ) );
272  }
273 
274  m_graphicsGrid->SetRowLabelSize( m_graphicsGrid->GetVisibleWidth( -1, true, true, true ) );
275 
276  Layout();
277 
278  return true;
279 }
280 
281 
283 {
284  bool retVal = wxPanel::Show( aShow );
285 
286  if( aShow )
287  {
288  // These *should* work in the constructor, and indeed they do if this panel is the
289  // first displayed. However, on OSX 3.0.5 (at least), if another panel is displayed
290  // first then the icons will be blank unless they're set here.
291  m_bpAdd->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
292  m_bpDelete->SetBitmap( KiBitmap( BITMAPS::small_trash ) );
293  }
294 
295  if( aShow && m_firstShow )
296  {
297  m_graphicsGrid->SetColSize( 0, m_graphicsGrid->GetColSize( 0 ) + 1 );
298  m_firstShow = false;
299  }
300 
301  return retVal;
302 }
303 
304 
305 int PANEL_FP_EDITOR_DEFAULTS::getGridValue( int aRow, int aCol )
306 {
308  m_graphicsGrid->GetCellValue( aRow, aCol ) );
309 }
310 
311 
313 {
315  return false;
316 
317  // Test text parameters.
318  for( int row : { ROW_SILK, ROW_COPPER, ROW_FAB, ROW_OTHERS } )
319  {
320  int textSize = std::min( getGridValue( row, COL_TEXT_WIDTH ),
321  getGridValue( row, COL_TEXT_HEIGHT ) );
322 
323  if( getGridValue( row, COL_TEXT_THICKNESS ) > textSize / 4 )
324  {
325  wxString msg = _( "Text will not be readable with a thickness greater than\n"
326  "1/4 its width or height." );
327  m_parent->SetError( msg, this, m_graphicsGrid, row, COL_TEXT_THICKNESS );
328  return false;
329  }
330  }
331 
332  return true;
333 }
334 
335 
337 {
338  if( !validateData() )
339  return false;
340 
341  for( int i = 0; i < ROW_COUNT; ++i )
342  {
344 
345  if( i == ROW_EDGES || i == ROW_COURTYARD )
346  continue;
347 
351 
352  wxString msg = m_graphicsGrid->GetCellValue( i, COL_TEXT_ITALIC );
353  m_brdSettings.m_TextItalic[ i ] = wxGridCellBoolEditor::IsTrueValue( msg );
354  }
355 
356  // Footprint defaults
357  wxGridTableBase* table = m_textItemsGrid->GetTable();
359 
360  for( int i = 0; i < m_textItemsGrid->GetNumberRows(); ++i )
361  {
362  wxString text = table->GetValue( i, 0 );
363  bool visible = table->GetValueAsBool( i, 1 );
364  int layer = (int) table->GetValueAsLong( i, 2 );
365 
366  m_brdSettings.m_DefaultFPTextItems.emplace_back( text, visible, layer );
367  }
368 
370 
372  cfg->m_DesignSettings = m_brdSettings;
373 
374  return true;
375 }
376 
377 
378 void PANEL_FP_EDITOR_DEFAULTS::OnAddTextItem( wxCommandEvent& event )
379 {
381  return;
382 
383  wxGridTableBase* table = m_textItemsGrid->GetTable();
384 
385  int newRow = m_textItemsGrid->GetNumberRows();
386  table->AppendRows( 1 );
387  table->SetValueAsBool( newRow, 1, table->GetValueAsBool( newRow - 1, 1 ) );
388  table->SetValueAsLong( newRow, 2, table->GetValueAsLong( newRow - 1, 2 ) );
389 
390  m_textItemsGrid->MakeCellVisible( newRow, 0 );
391  m_textItemsGrid->SetGridCursor( newRow, 0 );
392 
393  m_textItemsGrid->EnableCellEditControl( true );
394  m_textItemsGrid->ShowCellEditControl();
395 }
396 
397 
398 void PANEL_FP_EDITOR_DEFAULTS::OnDeleteTextItem( wxCommandEvent& event )
399 {
401  return;
402 
403  int curRow = m_textItemsGrid->GetGridCursorRow();
404 
405  if( curRow < 2 ) // First two rows are required
406  return;
407 
408  m_textItemsGrid->GetTable()->DeleteRows( curRow, 1 );
409 
410  curRow = std::max( 0, curRow - 1 );
411  m_textItemsGrid->MakeCellVisible( curRow, m_textItemsGrid->GetGridCursorCol() );
412  m_textItemsGrid->SetGridCursor( curRow, m_textItemsGrid->GetGridCursorCol() );
413 }
414 
415 
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:415
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]
#define _(s)
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:182
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:105
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
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