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