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 <fctsys.h>
25 #include <widgets/paged_dialog.h>
26 #include <footprint_edit_frame.h>
28 #include <widgets/wx_grid.h>
29 #include <grid_tricks.h>
30 
31 #include <panel_modedit_defaults.h>
32 #include <grid_layer_box_helpers.h>
33 #include <bitmaps.h>
34 
35 class TEXT_ITEMS_GRID_TABLE : public wxGridTableBase
36 {
37  std::vector<TEXT_ITEM_INFO> m_items;
38 
39 public:
41  { }
42 
43  int GetNumberRows() override { return m_items.size(); }
44  int GetNumberCols() override { return 3; }
45 
46  wxString GetColLabelValue( int aCol ) override
47  {
48  switch( aCol )
49  {
50  case 0: return _( "Text Items" );
51  case 1: return _( "Show" );
52  case 2: return _( "Layer" );
53  default: return wxEmptyString;
54  }
55  }
56 
57  wxString GetRowLabelValue( int aRow ) override
58  {
59  switch( aRow )
60  {
61  case 0: return _( "Reference designator" );
62  case 1: return _( "Value" );
63  default: return wxEmptyString;
64  }
65  }
66 
67  bool CanGetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
68  {
69  switch( aCol )
70  {
71  case 0: return aTypeName == wxGRID_VALUE_STRING;
72  case 1: return aTypeName == wxGRID_VALUE_BOOL;
73  case 2: return aTypeName == wxGRID_VALUE_NUMBER;
74  default: wxFAIL; return false;
75  }
76  }
77 
78  bool CanSetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
79  {
80  return CanGetValueAs( aRow, aCol, aTypeName );
81  }
82 
83  wxString GetValue( int row, int col ) override
84  {
85  return m_items[row].m_Text;
86  }
87  void SetValue( int row, int col, const wxString& value ) override
88  {
89  if( col == 0 )
90  m_items[row].m_Text = value;
91  }
92 
93  bool GetValueAsBool( int row, int col ) override
94  {
95  return m_items[row].m_Visible;
96  }
97  void SetValueAsBool( int row, int col, bool value ) override
98  {
99  if( col == 1 )
100  m_items[row].m_Visible = value;
101  }
102 
103  long GetValueAsLong( int row, int col ) override
104  {
105  return m_items[row].m_Layer;
106  }
107  void SetValueAsLong( int row, int col, long value ) override
108  {
109  if( col == 2 )
110  m_items[row].m_Layer = (int) value;
111  }
112 
113  bool AppendRows( size_t aNumRows = 1 ) override
114  {
115  for( size_t i = 0; i < aNumRows; ++i )
116  m_items.emplace_back( wxT( "" ), true, F_SilkS );
117 
118  if( GetView() )
119  {
120  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, aNumRows );
121  GetView()->ProcessTableMessage( msg );
122  }
123 
124  return true;
125  }
126 
127  bool DeleteRows( size_t aPos, size_t aNumRows ) override
128  {
129  // aPos may be a large positive, e.g. size_t(-1), and the sum of
130  // aPos+aNumRows may wrap here, so both ends of the range are tested.
131  if( aPos < m_items.size() && aPos + aNumRows <= m_items.size() )
132  {
133  m_items.erase( m_items.begin() + aPos, m_items.begin() + aPos + aNumRows );
134 
135  if( GetView() )
136  {
137  wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aPos, aNumRows );
138  GetView()->ProcessTableMessage( msg );
139  }
140  return true;
141  }
142 
143  return false;
144  }
145 };
146 
147 
148 // Columns of layer classes grid
149 enum
150 {
156 };
157 
158 enum
159 {
160  ROW_SILK = 0,
166 
168 };
169 
170 
172  PANEL_MODEDIT_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, LSET::ForbiddenTextLayers() ) );
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, 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_BOTTOM );
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( small_plus_xpm ) );
288  m_bpDelete->SetBitmap( KiBitmap( trash_xpm ) );
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_MODEDIT_DEFAULTS::getGridValue( int aRow, int aCol )
302 {
304  m_layerClassesGrid->GetCellValue( aRow, aCol ), true );
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  cfg->m_DesignSettings = m_brdSettings;
367 
368  return true;
369 }
370 
371 
372 void PANEL_MODEDIT_DEFAULTS::OnAddTextItem( wxCommandEvent& event )
373 {
375  return;
376 
377  wxGridTableBase* table = m_textItemsGrid->GetTable();
378 
379  int newRow = m_textItemsGrid->GetNumberRows();
380  table->AppendRows( 1 );
381  table->SetValueAsBool( newRow, 1, table->GetValueAsBool( newRow - 1, 1 ) );
382  table->SetValueAsLong( newRow, 2, table->GetValueAsLong( newRow - 1, 2 ) );
383 
384  m_textItemsGrid->MakeCellVisible( newRow, 0 );
385  m_textItemsGrid->SetGridCursor( newRow, 0 );
386 
387  m_textItemsGrid->EnableCellEditControl( true );
388  m_textItemsGrid->ShowCellEditControl();
389 }
390 
391 
392 void PANEL_MODEDIT_DEFAULTS::OnDeleteTextItem( wxCommandEvent& event )
393 {
395  return;
396 
397  int curRow = m_textItemsGrid->GetGridCursorRow();
398 
399  if( curRow < 2 ) // First two rows are required
400  return;
401 
402  m_textItemsGrid->GetTable()->DeleteRows( curRow, 1 );
403 
404  curRow = std::max( 0, curRow - 1 );
405  m_textItemsGrid->MakeCellVisible( curRow, m_textItemsGrid->GetGridCursorCol() );
406  m_textItemsGrid->SetGridCursor( curRow, m_textItemsGrid->GetGridCursorCol() );
407 }
408 
409 
const BITMAP_OPAQUE trash_xpm[1]
Definition: trash.cpp:46
std::vector< TEXT_ITEM_INFO > m_DefaultFPTextItems
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:51
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:59
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:230
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:80
void SetValue(int row, int col, const wxString &value) override
int m_TextThickness[LAYER_CLASS_COUNT]
bool Show(bool aShow) override
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:174
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
static LSET ForbiddenTextLayers()
Function ForbiddenTextLayers Layers which are now allowed to have text on them.
Definition: lset.cpp:832
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.
long long int ValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, bool aUseMils, EDA_DATA_TYPE aType)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application.
Definition: base_units.cpp:471
wxString GetValue(int row, int col) override
FOOTPRINT_EDIT_FRAME * m_frame
#define DISABLE_CELL(row, col)