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  m_staticTextInfo->SetFont( KIUI::GetInfoFont( this ).Italic() );
203 }
204 
205 
207 {
208  // destroy GRID_TRICKS before m_graphicsGrid.
209  m_graphicsGrid->PopEventHandler( true );
210 }
211 
212 
214 {
215  wxColour disabledColour = wxSystemSettings::GetColour( wxSYS_COLOUR_BACKGROUND );
216 
217 #define SET_MILS_CELL( row, col, val ) \
218  m_graphicsGrid->SetCellValue( row, col, StringFromValue( m_frame->GetUserUnits(), val, true ) )
219 
220 #define DISABLE_CELL( row, col ) \
221  m_graphicsGrid->SetReadOnly( row, col ); \
222  m_graphicsGrid->SetCellBackgroundColour( row, col, disabledColour );
223 
224  for( int i = 0; i < ROW_COUNT; ++i )
225  {
227 
228  if( i == ROW_EDGES || i == ROW_COURTYARD )
229  {
234  }
235  else
236  {
240  m_graphicsGrid->SetCellValue( i, COL_TEXT_ITALIC, m_brdSettings.m_TextItalic[ i ] ? "1" : "" );
241 
242  auto attr = new wxGridCellAttr;
243  attr->SetRenderer( new wxGridCellBoolRenderer() );
244  attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
245  attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
246  m_graphicsGrid->SetAttr( i, COL_TEXT_ITALIC, attr );
247  }
248  }
249 
250  // Footprint defaults
251  m_textItemsGrid->GetTable()->AppendRows( m_brdSettings.m_DefaultFPTextItems.size() );
252 
253  for( size_t i = 0; i < m_brdSettings.m_DefaultFPTextItems.size(); ++i )
254  {
256 
257  m_textItemsGrid->GetTable()->SetValue( i, 0, item.m_Text );
258  m_textItemsGrid->GetTable()->SetValueAsBool( i, 1, item.m_Visible );
259  m_textItemsGrid->GetTable()->SetValueAsLong( i, 2, item.m_Layer );
260  }
261 
262  for( int col = 0; col < m_graphicsGrid->GetNumberCols(); col++ )
263  {
264  // Set the minimal width to the column label size.
265  m_graphicsGrid->SetColMinimalWidth( col, m_graphicsGrid->GetVisibleWidth( col, true, false, false ) );
266 
267  // Set the width to see the full contents
268  if( m_graphicsGrid->IsColShown( col ) )
269  m_graphicsGrid->SetColSize( col, m_graphicsGrid->GetVisibleWidth( col, true, true, true ) );
270  }
271 
272  m_graphicsGrid->SetRowLabelSize( m_graphicsGrid->GetVisibleWidth( -1, true, true, true ) );
273 
274  Layout();
275 
276  return true;
277 }
278 
279 
281 {
282  bool retVal = wxPanel::Show( aShow );
283 
284  if( aShow )
285  {
286  // These *should* work in the constructor, and indeed they do if this panel is the
287  // first displayed. However, on OSX 3.0.5 (at least), if another panel is displayed
288  // first then the icons will be blank unless they're set here.
289  m_bpAdd->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
290  m_bpDelete->SetBitmap( KiBitmap( BITMAPS::small_trash ) );
291  }
292 
293  if( aShow && m_firstShow )
294  {
295  m_graphicsGrid->SetColSize( 0, m_graphicsGrid->GetColSize( 0 ) + 1 );
296  m_firstShow = false;
297  }
298 
299  return retVal;
300 }
301 
302 
303 int PANEL_FP_EDITOR_DEFAULTS::getGridValue( int aRow, int aCol )
304 {
306  m_graphicsGrid->GetCellValue( aRow, aCol ) );
307 }
308 
309 
311 {
313  return false;
314 
315  // Test text parameters.
316  for( int row : { ROW_SILK, ROW_COPPER, ROW_FAB, ROW_OTHERS } )
317  {
318  int textSize = std::min( getGridValue( row, COL_TEXT_WIDTH ),
319  getGridValue( row, COL_TEXT_HEIGHT ) );
320 
321  if( getGridValue( row, COL_TEXT_THICKNESS ) > textSize / 4 )
322  {
323  wxString msg = _( "Text will not be readable with a thickness greater than\n"
324  "1/4 its width or height." );
325  m_parent->SetError( msg, this, m_graphicsGrid, row, COL_TEXT_THICKNESS );
326  return false;
327  }
328  }
329 
330  return true;
331 }
332 
333 
335 {
336  if( !validateData() )
337  return false;
338 
339  for( int i = 0; i < ROW_COUNT; ++i )
340  {
342 
343  if( i == ROW_EDGES || i == ROW_COURTYARD )
344  continue;
345 
349 
350  wxString msg = m_graphicsGrid->GetCellValue( i, COL_TEXT_ITALIC );
351  m_brdSettings.m_TextItalic[ i ] = wxGridCellBoolEditor::IsTrueValue( msg );
352  }
353 
354  // Footprint defaults
355  wxGridTableBase* table = m_textItemsGrid->GetTable();
357 
358  for( int i = 0; i < m_textItemsGrid->GetNumberRows(); ++i )
359  {
360  wxString text = table->GetValue( i, 0 );
361  bool visible = table->GetValueAsBool( i, 1 );
362  int layer = (int) table->GetValueAsLong( i, 2 );
363 
364  m_brdSettings.m_DefaultFPTextItems.emplace_back( text, visible, layer );
365  }
366 
368 
370  cfg->m_DesignSettings = m_brdSettings;
371 
372  return true;
373 }
374 
375 
376 void PANEL_FP_EDITOR_DEFAULTS::OnAddTextItem( wxCommandEvent& event )
377 {
379  return;
380 
381  wxGridTableBase* table = m_textItemsGrid->GetTable();
382 
383  int newRow = m_textItemsGrid->GetNumberRows();
384  table->AppendRows( 1 );
385  table->SetValueAsBool( newRow, 1, table->GetValueAsBool( newRow - 1, 1 ) );
386  table->SetValueAsLong( newRow, 2, table->GetValueAsLong( newRow - 1, 2 ) );
387 
388  m_textItemsGrid->MakeCellVisible( newRow, 0 );
389  m_textItemsGrid->SetGridCursor( newRow, 0 );
390 
391  m_textItemsGrid->EnableCellEditControl( true );
392  m_textItemsGrid->ShowCellEditControl();
393 }
394 
395 
396 void PANEL_FP_EDITOR_DEFAULTS::OnDeleteTextItem( wxCommandEvent& event )
397 {
399  return;
400 
401  int curRow = m_textItemsGrid->GetGridCursorRow();
402 
403  if( curRow < 2 ) // First two rows are required
404  return;
405 
406  m_textItemsGrid->GetTable()->DeleteRows( curRow, 1 );
407 
408  curRow = std::max( 0, curRow - 1 );
409  m_textItemsGrid->MakeCellVisible( curRow, m_textItemsGrid->GetGridCursorCol() );
410  m_textItemsGrid->SetGridCursor( curRow, m_textItemsGrid->GetGridCursorCol() );
411 }
412 
413 
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
wxFont GetInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:141
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