KiCad PCB EDA Suite
panel_fp_lib_table.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) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2013 CERN
6  * Copyright (C) 2012-2019 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 
27 /* TODO:
28 
29 *) After any change to uri, reparse the environment variables.
30 
31 */
32 
33 
34 #include <set>
35 #include <wx/regex.h>
36 #include <wx/grid.h>
37 
38 #include <fctsys.h>
39 #include <project.h>
40 #include <3d_viewer.h> // for KISYS3DMOD
41 #include <panel_fp_lib_table.h>
42 #include <lib_id.h>
43 #include <fp_lib_table.h>
44 #include <lib_table_lexer.h>
45 #include <invoke_pcb_dialog.h>
46 #include <bitmaps.h>
47 #include <grid_tricks.h>
48 #include <widgets/wx_grid.h>
49 #include <confirm.h>
50 #include <lib_table_grid.h>
52 #include <pgm_base.h>
53 #include <pcb_edit_frame.h>
54 #include <env_paths.h>
57 #include <footprint_viewer_frame.h>
58 #include <footprint_edit_frame.h>
59 #include <kiway.h>
62 
63 // Filters for the file picker
64 static constexpr int FILTER_COUNT = 4;
65 static const struct
66 {
67  wxString m_Description;
68  wxString m_Extension;
69  bool m_IsFile;
72 {
73  // wxGenericDirCtrl does not handle regexes in wildcards
74  { "KiCad (folder with .kicad_mod files)", "", false, IO_MGR::KICAD_SEXP },
75  { "Eagle 6.x (*.lbr)", "lbr", true, IO_MGR::EAGLE },
76  { "KiCad legacy (*.mod)", "mod", true, IO_MGR::LEGACY },
77  { "Geda (folder with *.fp files)", "", false, IO_MGR::GEDA_PCB },
78 };
79 
80 
81 // Returns the filter string for the file picker
82 static wxString getFilterString()
83 {
84  wxString filterAll = _( "All supported library formats|" );
85  bool firstFilterAll = true;
86  wxString filter;
87 
88  for( int i = 0; i < FILTER_COUNT; ++i )
89  {
90  if( fileFilters[i].m_IsFile )
91  {
92  // "All supported formats" filter
93  if( firstFilterAll )
94  firstFilterAll = false;
95  else
96  filterAll += ";";
97 
98  wxASSERT( !fileFilters[i].m_Extension.IsEmpty() );
99  filterAll += "*." + fileFilters[i].m_Extension;
100  }
101 
102 
103  // Individual filter strings
104  filter += "|" + fileFilters[i].m_Description +
105  "|" + ( fileFilters[i].m_IsFile ? "*." + fileFilters[i].m_Extension : "" );
106  }
107 
108  return filterAll + filter;
109 }
110 
111 
116 {
117  friend class PANEL_FP_LIB_TABLE;
118  friend class FP_GRID_TRICKS;
119 
120 protected:
121  LIB_TABLE_ROW* at( size_t aIndex ) override { return &rows.at( aIndex ); }
122 
123  size_t size() const override { return rows.size(); }
124 
126  {
127  return dynamic_cast< LIB_TABLE_ROW* >( new FP_LIB_TABLE_ROW );
128  }
129 
130  LIB_TABLE_ROWS_ITER begin() override { return rows.begin(); }
131 
133  {
134  return rows.insert( aIterator, aRow );
135  }
136 
137  void push_back( LIB_TABLE_ROW* aRow ) override { rows.push_back( aRow ); }
138 
140  {
141  return rows.erase( aFirst, aLast );
142  }
143 
144 public:
145 
146  FP_LIB_TABLE_GRID( const FP_LIB_TABLE& aTableToEdit )
147  {
148  rows = aTableToEdit.rows;
149  }
150 };
151 
152 
153 #define MYID_OPTIONS_EDITOR 15151
154 
155 
157 {
158 public:
160  GRID_TRICKS( aGrid ),
161  m_dialog( aParent )
162  { }
163 
164 protected:
166 
167  void optionsEditor( int aRow )
168  {
169  FP_LIB_TABLE_GRID* tbl = (FP_LIB_TABLE_GRID*) m_grid->GetTable();
170 
171  if( tbl->GetNumberRows() > aRow )
172  {
173  LIB_TABLE_ROW* row = tbl->at( (size_t) aRow );
174  const wxString& options = row->GetOptions();
175  wxString result = options;
176 
177  InvokePluginOptionsEditor( m_dialog, row->GetNickName(), row->GetType(), options,
178  &result );
179 
180  if( options != result )
181  {
182  row->SetOptions( result );
183  m_grid->Refresh();
184  }
185  }
186  }
187 
188  bool handleDoubleClick( wxGridEvent& aEvent ) override
189  {
190  if( aEvent.GetCol() == COL_OPTIONS )
191  {
192  optionsEditor( aEvent.GetRow() );
193  return true;
194  }
195 
196  return false;
197  }
198 
199  void showPopupMenu( wxMenu& menu ) override
200  {
201  if( m_grid->GetGridCursorCol() == COL_OPTIONS )
202  {
203  menu.Append( MYID_OPTIONS_EDITOR, _( "Options Editor..." ), _( "Edit options" ) );
204  menu.AppendSeparator();
205  }
206 
208  }
209 
210  void doPopupSelection( wxCommandEvent& event ) override
211  {
212  if( event.GetId() == MYID_OPTIONS_EDITOR )
213  optionsEditor( m_grid->GetGridCursorRow() );
214  else
216  }
217 
220  void paste_text( const wxString& cb_text ) override
221  {
222  FP_LIB_TABLE_GRID* tbl = (FP_LIB_TABLE_GRID*) m_grid->GetTable();
223  size_t ndx = cb_text.find( "(fp_lib_table" );
224 
225  if( ndx != std::string::npos )
226  {
227  // paste the FP_LIB_TABLE_ROWs of s-expression (fp_lib_table), starting
228  // at column 0 regardless of current cursor column.
229 
230  STRING_LINE_READER slr( TO_UTF8( cb_text ), "Clipboard" );
231  LIB_TABLE_LEXER lexer( &slr );
232  FP_LIB_TABLE tmp_tbl;
233  bool parsed = true;
234 
235  try
236  {
237  tmp_tbl.Parse( &lexer );
238  }
239  catch( PARSE_ERROR& pe )
240  {
241  DisplayError( m_dialog, pe.What() );
242  parsed = false;
243  }
244 
245  if( parsed )
246  {
247  // make sure the table is big enough...
248  if( tmp_tbl.GetCount() > (unsigned) tbl->GetNumberRows() )
249  tbl->AppendRows( tmp_tbl.GetCount() - tbl->GetNumberRows() );
250 
251  for( unsigned i = 0; i < tmp_tbl.GetCount(); ++i )
252  tbl->rows.replace( i, tmp_tbl.At( i ).clone() );
253  }
254 
255  m_grid->AutoSizeColumns( false );
256  }
257  else
258  {
259  // paste spreadsheet formatted text.
260  GRID_TRICKS::paste_text( cb_text );
261 
262  m_grid->AutoSizeColumns( false );
263  }
264  }
265 };
266 
267 
269  FP_LIB_TABLE* aGlobal, const wxString& aGlobalTblPath,
270  FP_LIB_TABLE* aProject, const wxString& aProjectTblPath,
271  const wxString& aProjectBasePath ) :
272  PANEL_FP_LIB_TABLE_BASE( aParent ),
273  m_global( aGlobal ),
274  m_project( aProject ),
275  m_projectBasePath( aProjectBasePath ),
276  m_parent( aParent )
277 {
278  // For user info, shows the table filenames:
279  m_GblTableFilename->SetLabel( aGlobalTblPath );
280  m_PrjTableFilename->SetLabel( aProjectTblPath );
281 
282  m_global_grid->SetTable( new FP_LIB_TABLE_GRID( *aGlobal ), true );
283  m_project_grid->SetTable( new FP_LIB_TABLE_GRID( *aProject ), true );
284 
285  // Give a bit more room for wxChoice editors
286  m_global_grid->SetDefaultRowSize( m_global_grid->GetDefaultRowSize() + 4 );
287  m_project_grid->SetDefaultRowSize( m_project_grid->GetDefaultRowSize() + 4 );
288 
289  // add Cut, Copy, and Paste to wxGrids
290  m_global_grid->PushEventHandler( new FP_GRID_TRICKS( m_parent, m_global_grid ) );
291  m_project_grid->PushEventHandler( new FP_GRID_TRICKS( m_parent, m_project_grid ) );
292  m_path_subs_grid->PushEventHandler( new GRID_TRICKS( m_path_subs_grid ) );
293 
294  m_global_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
295  m_project_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
296 
297  m_global_grid->AutoSizeColumns( false );
298  m_project_grid->AutoSizeColumns( false );
299 
300  wxArrayString choices;
301 
302  choices.Add( IO_MGR::ShowType( IO_MGR::KICAD_SEXP ) );
303 #if defined(BUILD_GITHUB_PLUGIN)
304  choices.Add( IO_MGR::ShowType( IO_MGR::GITHUB ) );
305 #endif
306  choices.Add( IO_MGR::ShowType( IO_MGR::LEGACY ) );
307  choices.Add( IO_MGR::ShowType( IO_MGR::EAGLE ) );
308  choices.Add( IO_MGR::ShowType( IO_MGR::GEDA_PCB ) );
309 
310  /* PCAD_PLUGIN does not support Footprint*() functions
311  choices.Add( IO_MGR::ShowType( IO_MGR::PCAD ) );
312  */
313 
315 
316  for( wxGrid* g : { m_global_grid, m_project_grid } )
317  {
318  wxGridCellAttr* attr;
319 
320  attr = new wxGridCellAttr;
321  attr->SetEditor( new GRID_CELL_PATH_EDITOR( m_parent, &m_lastBrowseDir ) );
322  g->SetColAttr( COL_URI, attr );
323 
324  attr = new wxGridCellAttr;
325  attr->SetEditor( new wxGridCellChoiceEditor( choices ) );
326  g->SetColAttr( COL_TYPE, attr );
327 
328  attr = new wxGridCellAttr;
329  attr->SetRenderer( new wxGridCellBoolRenderer() );
330  attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
331  g->SetColAttr( COL_ENABLED, attr );
332 
333  // all but COL_OPTIONS, which is edited with Option Editor anyways.
334  g->AutoSizeColumn( COL_NICKNAME, false );
335  g->AutoSizeColumn( COL_TYPE, false );
336  g->AutoSizeColumn( COL_URI, false );
337  g->AutoSizeColumn( COL_DESCR, false );
338 
339  // would set this to width of title, if it was easily known.
340  g->SetColSize( COL_OPTIONS, 80 );
341  }
342 
343  m_path_subs_grid->SetColLabelValue( 0, _( "Name" ) );
344  m_path_subs_grid->SetColLabelValue( 1, _( "Value" ) );
345 
346  // select the last selected page
347  m_auinotebook->SetSelection( m_pageNdx );
349 
350  // for ALT+A handling, we want the initial focus to be on the first selected grid.
352 
353  // Configure button logos
354  m_append_button->SetBitmap( KiBitmap( small_plus_xpm ) );
355  m_browse_button->SetBitmap( KiBitmap( folder_xpm ) );
356  m_delete_button->SetBitmap( KiBitmap( trash_xpm ) );
357  m_move_up_button->SetBitmap( KiBitmap( small_up_xpm ) );
358  m_move_down_button->SetBitmap( KiBitmap( small_down_xpm ) );
359 
360  // Gives a selection to each grid, mainly for delete button. wxGrid's wake up with
361  // a currentCell which is sometimes not highlighted.
362  if( m_global_grid->GetNumberRows() > 0 )
363  m_global_grid->SelectRow( 0 );
364 
365  if( m_project_grid->GetNumberRows() > 0 )
366  m_project_grid->SelectRow( 0 );
367 }
368 
369 
371 {
372  // When the dialog is closed it will hide the current notebook page first, which will
373  // in turn select the other one. We then end up saving its index as the "current page".
374  // So flip them back again:
375  m_pageNdx = m_pageNdx == 1 ? 0 : 1;
376 
377  // Delete the GRID_TRICKS.
378  // Any additional event handlers should be popped before the window is deleted.
379  m_global_grid->PopEventHandler( true );
380  m_project_grid->PopEventHandler( true );
381  m_path_subs_grid->PopEventHandler( true );
382 }
383 
384 
386 {
387  for( FP_LIB_TABLE_GRID* model : { global_model(), project_model() } )
388  {
389  for( int r = 0; r < model->GetNumberRows(); )
390  {
391  wxString nick = model->GetValue( r, COL_NICKNAME ).Trim( false ).Trim();
392  wxString uri = model->GetValue( r, COL_URI ).Trim( false ).Trim();
393  unsigned illegalCh = 0;
394 
395  if( !nick || !uri )
396  {
397  // Delete the "empty" row, where empty means missing nick or uri.
398  // This also updates the UI which could be slow, but there should only be a few
399  // rows to delete, unless the user fell asleep on the Add Row
400  // button.
401  model->DeleteRows( r, 1 );
402  }
403  else if( ( illegalCh = LIB_ID::FindIllegalLibNicknameChar( nick, LIB_ID::ID_PCB ) ) )
404  {
405  wxString msg = wxString::Format( _( "Illegal character '%c' in Nickname: \"%s\"" ),
406  illegalCh,
407  nick );
408 
409  // show the tabbed panel holding the grid we have flunked:
410  if( model != cur_model() )
411  m_auinotebook->SetSelection( model == global_model() ? 0 : 1 );
412 
413  m_cur_grid->MakeCellVisible( r, 0 );
414  m_cur_grid->SetGridCursor( r, 1 );
415 
416  wxMessageDialog errdlg( this, msg, _( "No Colon in Nicknames" ) );
417  errdlg.ShowModal();
418  return false;
419  }
420  else
421  {
422  // set the trimmed values back into the table so they get saved to disk.
423  model->SetValue( r, COL_NICKNAME, nick );
424  model->SetValue( r, COL_URI, uri );
425  ++r; // this row was OK.
426  }
427  }
428  }
429 
430  // check for duplicate nickNames, separately in each table.
431  for( FP_LIB_TABLE_GRID* model : { global_model(), project_model() } )
432  {
433  for( int r1 = 0; r1 < model->GetNumberRows() - 1; ++r1 )
434  {
435  wxString nick1 = model->GetValue( r1, COL_NICKNAME );
436 
437  for( int r2 = r1 + 1; r2 < model->GetNumberRows(); ++r2 )
438  {
439  wxString nick2 = model->GetValue( r2, COL_NICKNAME );
440 
441  if( nick1 == nick2 )
442  {
443  wxString msg = wxString::Format( _( "Duplicate Nicknames \"%s\"." ), nick1 );
444 
445  // show the tabbed panel holding the grid we have flunked:
446  if( model != cur_model() )
447  m_auinotebook->SetSelection( model == global_model() ? 0 : 1 );
448 
449  // go to the lower of the two rows, it is technically the duplicate:
450  m_cur_grid->MakeCellVisible( r2, 0 );
451  m_cur_grid->SetGridCursor( r2, 1 );
452 
453  wxMessageDialog errdlg( this, msg, _( "Please Delete or Modify One" ) );
454  errdlg.ShowModal();
455  return false;
456  }
457  }
458  }
459  }
460 
461  return true;
462 }
463 
464 
465 //-----<event handlers>----------------------------------
466 
467 void PANEL_FP_LIB_TABLE::pageChangedHandler( wxAuiNotebookEvent& event )
468 {
469  m_pageNdx = (unsigned) std::max( 0, m_auinotebook->GetSelection() );
471 }
472 
473 
474 void PANEL_FP_LIB_TABLE::appendRowHandler( wxCommandEvent& event )
475 {
477  return;
478 
479  if( m_cur_grid->AppendRows( 1 ) )
480  {
481  int last_row = m_cur_grid->GetNumberRows() - 1;
482 
483  // wx documentation is wrong, SetGridCursor does not make visible.
484  m_cur_grid->MakeCellVisible( last_row, 0 );
485  m_cur_grid->SetGridCursor( last_row, 1 );
486  m_cur_grid->EnableCellEditControl( true );
487  m_cur_grid->ShowCellEditControl();
488  }
489 }
490 
491 
492 void PANEL_FP_LIB_TABLE::deleteRowHandler( wxCommandEvent& event )
493 {
495  return;
496 
497  int curRow = m_cur_grid->GetGridCursorRow();
498  int curCol = m_cur_grid->GetGridCursorCol();
499 
500  // In a wxGrid, collect rows that have a selected cell, or are selected
501  // is not so easy: it depend on the way the selection was made.
502  // Here, we collect row selected by clicking on a row label, and
503  // row that contain a cell previously selected.
504  // If no candidate, just delete the row with the grid cursor.
505  wxArrayInt selectedRows = m_cur_grid->GetSelectedRows();
506  wxGridCellCoordsArray cells = m_cur_grid->GetSelectedCells();
507  wxGridCellCoordsArray blockTopLeft = m_cur_grid->GetSelectionBlockTopLeft();
508  wxGridCellCoordsArray blockBotRight = m_cur_grid->GetSelectionBlockBottomRight();
509 
510  // Add all row having cell selected to list:
511  for( unsigned ii = 0; ii < cells.GetCount(); ii++ )
512  selectedRows.Add( cells[ii].GetRow() );
513 
514  // Handle block selection
515  if( !blockTopLeft.IsEmpty() && !blockBotRight.IsEmpty() )
516  {
517  for( int i = blockTopLeft[0].GetRow(); i <= blockBotRight[0].GetRow(); ++i )
518  selectedRows.Add( i );
519  }
520 
521  // Use the row having the grid cursor only if we have no candidate:
522  if( selectedRows.size() == 0 && m_cur_grid->GetGridCursorRow() >= 0 )
523  selectedRows.Add( m_cur_grid->GetGridCursorRow() );
524 
525  if( selectedRows.size() == 0 )
526  {
527  wxBell();
528  return;
529  }
530 
531  std::sort( selectedRows.begin(), selectedRows.end() );
532 
533  // Remove selected rows (note: a row can be stored more than once in list)
534  int last_row = -1;
535  for( int ii = selectedRows.GetCount()-1; ii >= 0; ii-- )
536  {
537  int row = selectedRows[ii];
538 
539  if( row != last_row )
540  {
541  last_row = row;
542  m_cur_grid->DeleteRows( row, 1 );
543  }
544  }
545 
546  m_cur_grid->SetGridCursor( std::min( curRow, m_cur_grid->GetNumberRows() - 1 ), curCol );
547 }
548 
549 
550 void PANEL_FP_LIB_TABLE::moveUpHandler( wxCommandEvent& event )
551 {
553  return;
554 
555  FP_LIB_TABLE_GRID* tbl = cur_model();
556  int curRow = m_cur_grid->GetGridCursorRow();
557 
558  // @todo: add multiple selection moves.
559  if( curRow >= 1 )
560  {
561  boost::ptr_vector< LIB_TABLE_ROW >::auto_type move_me =
562  tbl->rows.release( tbl->rows.begin() + curRow );
563 
564  --curRow;
565  tbl->rows.insert( tbl->rows.begin() + curRow, move_me.release() );
566 
567  if( tbl->GetView() )
568  {
569  // Update the wxGrid
570  wxGridTableMessage msg( tbl, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, curRow, 0 );
571  tbl->GetView()->ProcessTableMessage( msg );
572  }
573 
574  m_cur_grid->MakeCellVisible( curRow, m_cur_grid->GetGridCursorCol() );
575  m_cur_grid->SetGridCursor( curRow, m_cur_grid->GetGridCursorCol() );
576  }
577 }
578 
579 
580 void PANEL_FP_LIB_TABLE::moveDownHandler( wxCommandEvent& event )
581 {
583  return;
584 
585  FP_LIB_TABLE_GRID* tbl = cur_model();
586  int curRow = m_cur_grid->GetGridCursorRow();
587 
588  // @todo: add multiple selection moves.
589  if( unsigned( curRow + 1 ) < tbl->rows.size() )
590  {
591  boost::ptr_vector< LIB_TABLE_ROW >::auto_type move_me =
592  tbl->rows.release( tbl->rows.begin() + curRow );
593 
594  ++curRow;
595  tbl->rows.insert( tbl->rows.begin() + curRow, move_me.release() );
596 
597  if( tbl->GetView() )
598  {
599  // Update the wxGrid
600  wxGridTableMessage msg( tbl, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, curRow - 1, 0 );
601  tbl->GetView()->ProcessTableMessage( msg );
602  }
603 
604  m_cur_grid->MakeCellVisible( curRow, m_cur_grid->GetGridCursorCol() );
605  m_cur_grid->SetGridCursor( curRow, m_cur_grid->GetGridCursorCol() );
606  }
607 }
608 
609 
610 void PANEL_FP_LIB_TABLE::browseLibrariesHandler( wxCommandEvent& event )
611 {
613  return;
614 
615  if( m_lastBrowseDir.IsEmpty() )
617 
618  DIALOG_FILE_DIR_PICKER dlg( this, _( "Select Library" ), m_lastBrowseDir,
620 
621  auto result = dlg.ShowModal();
622 
623  if( result == wxID_CANCEL )
624  return;
625 
627 
628  // Drop the last directory if the path is a .pretty folder
630  m_lastBrowseDir = m_lastBrowseDir.BeforeLast( wxFileName::GetPathSeparator() );
631 
632  const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables();
633  bool addDuplicates = false;
634  bool applyToAll = false;
635  wxString warning = _( "Warning: Duplicate Nickname" );
636  wxString msg = _( "A library nicknamed \"%s\" already exists." );
637  wxArrayString files;
638  dlg.GetFilenames( files );
639 
640  for( const auto& filePath : files )
641  {
642  wxFileName fn( filePath );
643  wxString nickname = LIB_ID::FixIllegalChars( fn.GetName(), LIB_ID::ID_PCB );
644  bool doAdd = true;
645 
646  if( cur_model()->ContainsNickname( nickname ) )
647  {
648  if( !applyToAll )
649  {
650  int ret = YesOrCancelDialog( this, warning, wxString::Format( msg, nickname ),
651  _( "Skip" ), _( "Add Anyway" ), &applyToAll );
652  addDuplicates = (ret == wxID_CANCEL );
653  }
654 
655  doAdd = addDuplicates;
656  }
657 
658  if( doAdd && m_cur_grid->AppendRows( 1 ) )
659  {
660  int last_row = m_cur_grid->GetNumberRows() - 1;
661 
662  m_cur_grid->SetCellValue( last_row, COL_NICKNAME, nickname );
663 
664  auto type = IO_MGR::GuessPluginTypeFromLibPath( filePath );
665  m_cur_grid->SetCellValue( last_row, COL_TYPE, IO_MGR::ShowType( type ) );
666 
667  // try to use path normalized to an environmental variable or project path
668  wxString path = NormalizePath( filePath, &envVars, m_projectBasePath );
669 
670  if( path.IsEmpty() )
671  path = fn.GetFullPath();
672 
673  m_cur_grid->SetCellValue( last_row, COL_URI, path );
674  }
675  }
676 
677  if( !files.IsEmpty() )
678  {
679  int new_row = m_cur_grid->GetNumberRows() - 1;
680  m_cur_grid->MakeCellVisible( new_row, m_cur_grid->GetGridCursorCol() );
681  m_cur_grid->SetGridCursor( new_row, m_cur_grid->GetGridCursorCol() );
682  }
683 }
684 
686 {
687  // Account for scroll bars
688  aWidth -= ( m_path_subs_grid->GetSize().x - m_path_subs_grid->GetClientSize().x );
689 
690  m_path_subs_grid->AutoSizeColumn( 0 );
691  m_path_subs_grid->SetColSize( 1, aWidth - m_path_subs_grid->GetColSize( 0 ) );
692 }
693 
694 
695 void PANEL_FP_LIB_TABLE::onSizeGrid( wxSizeEvent& event )
696 {
697  adjustPathSubsGridColumns( event.GetSize().GetX() );
698 
699  event.Skip();
700 }
701 
702 
704 {
706  return false;
707 
708  if( verifyTables() )
709  {
710  if( *global_model() != *m_global )
711  {
713 
714  m_global->Clear();
715  m_global->rows.transfer( m_global->rows.end(), global_model()->rows.begin(),
716  global_model()->rows.end(), global_model()->rows );
717  m_global->reindex();
718  }
719 
720  if( *project_model() != *m_project )
721  {
723 
724  m_project->Clear();
725  m_project->rows.transfer( m_project->rows.end(), project_model()->rows.begin(),
726  project_model()->rows.end(), project_model()->rows );
727  m_project->reindex();
728  }
729 
730  return true;
731  }
732 
733  return false;
734 }
735 
736 
740 {
741  wxRegEx re( ".*?(\\$\\{(.+?)\\})|(\\$\\((.+?)\\)).*?", wxRE_ADVANCED );
742  wxASSERT( re.IsValid() ); // wxRE_ADVANCED is required.
743 
744  std::set< wxString > unique;
745 
746  // clear the table
747  m_path_subs_grid->DeleteRows( 0, m_path_subs_grid->GetNumberRows() );
748 
749  for( FP_LIB_TABLE_GRID* tbl : { global_model(), project_model() } )
750  {
751  for( int row = 0; row < tbl->GetNumberRows(); ++row )
752  {
753  wxString uri = tbl->GetValue( row, COL_URI );
754 
755  while( re.Matches( uri ) )
756  {
757  wxString envvar = re.GetMatch( uri, 2 );
758 
759  // if not ${...} form then must be $(...)
760  if( envvar.IsEmpty() )
761  envvar = re.GetMatch( uri, 4 );
762 
763  // ignore duplicates
764  unique.insert( envvar );
765 
766  // delete the last match and search again
767  uri.Replace( re.GetMatch( uri, 0 ), wxEmptyString );
768  }
769  }
770  }
771 
772  // Make sure this special environment variable shows up even if it was
773  // not used yet. It is automatically set by KiCad to the directory holding
774  // the current project.
775  unique.insert( PROJECT_VAR_NAME );
776  unique.insert( FP_LIB_TABLE::GlobalPathEnvVariableName() );
777  // This special environment variable is used to locate 3d shapes
778  unique.insert( KISYS3DMOD );
779 
780  for( wxString evName : unique )
781  {
782  int row = m_path_subs_grid->GetNumberRows();
783  m_path_subs_grid->AppendRows( 1 );
784 
785  m_path_subs_grid->SetCellValue( row, 0, wxT( "${" ) + evName + wxT( "}" ) );
786  m_path_subs_grid->SetCellEditor( row, 0, new GRID_CELL_READONLY_TEXT_EDITOR() );
787 
788  wxString evValue;
789  wxGetEnv( evName, &evValue );
790  m_path_subs_grid->SetCellValue( row, 1, evValue );
791  m_path_subs_grid->SetCellEditor( row, 1, new GRID_CELL_READONLY_TEXT_EDITOR() );
792  }
793 
794  // No combobox editors here, but it looks better if its consistent with the other
795  // grids in the dialog.
796  m_path_subs_grid->SetDefaultRowSize( m_path_subs_grid->GetDefaultRowSize() + 2 );
797 
798  adjustPathSubsGridColumns( m_path_subs_grid->GetRect().GetWidth() );
799 }
800 
801 //-----</event handlers>---------------------------------
802 
803 
804 
806 
808 
809 
810 void InvokePcbLibTableEditor( KIWAY* aKiway, wxWindow* aCaller )
811 {
812  FP_LIB_TABLE* globalTable = &GFootprintTable;
813  wxString globalTablePath = FP_LIB_TABLE::GetGlobalTableFileName();
814  FP_LIB_TABLE* projectTable = aKiway->Prj().PcbFootprintLibs();
815  wxString projectTablePath = aKiway->Prj().FootprintLibTblName();
816  wxString msg;
817 
818  DIALOG_EDIT_LIBRARY_TABLES dlg( aCaller, _( "Footprint Libraries" ) );
819  dlg.SetKiway( &dlg, aKiway );
820 
821  dlg.InstallPanel( new PANEL_FP_LIB_TABLE( &dlg, globalTable, globalTablePath,
822  projectTable, projectTablePath,
823  aKiway->Prj().GetProjectPath() ) );
824 
825  if( dlg.ShowModal() == wxID_CANCEL )
826  return;
827 
828  if( dlg.m_GlobalTableChanged )
829  {
830  try
831  {
832  globalTable->Save( globalTablePath );
833  }
834  catch( const IO_ERROR& ioe )
835  {
836  msg.Printf( _( "Error saving global library table:\n\n%s" ), ioe.What() );
837  wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
838  }
839  }
840 
841  if( dlg.m_ProjectTableChanged )
842  {
843  try
844  {
845  projectTable->Save( projectTablePath );
846  }
847  catch( const IO_ERROR& ioe )
848  {
849  msg.Printf( _( "Error saving project-specific library table:\n\n%s" ), ioe.What() );
850  wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
851  }
852  }
853 
854  auto editor = (FOOTPRINT_EDIT_FRAME*) aKiway->Player( FRAME_PCB_MODULE_EDITOR, false );
855 
856  if( editor )
857  editor->SyncLibraryTree( true );
858 
859  auto viewer = (FOOTPRINT_VIEWER_FRAME*) aKiway->Player( FRAME_PCB_MODULE_VIEWER, false );
860 
861  if( viewer )
862  viewer->ReCreateLibraryList();
863 }
FP_GRID_TRICKS(DIALOG_EDIT_LIBRARY_TABLES *aParent, WX_GRID *aGrid)
void SetKiway(wxWindow *aDest, KIWAY *aKiway)
Function SetKiway.
void push_back(LIB_TABLE_ROW *aRow) override
const wxString & GetOptions() const
Return the options string, which may hold a password or anything else needed to instantiate the under...
void moveDownHandler(wxCommandEvent &event) override
void InvokePluginOptionsEditor(wxWindow *aCaller, const wxString &aNickname, const wxString &aPluginType, const wxString &aOptions, wxString *aResult)
Function InvokePluginOptionsEditor calls DIALOG_FP_PLUGIN_OPTIONS dialog so that plugin options set c...
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
void Clear()
Delete all rows.
Class FP_LIB_TABLE_ROW.
Definition: fp_lib_table.h:42
This file is part of the common library.
void showPopupMenu(wxMenu &menu) override
void reindex()
FP_LIB_TABLE * m_project
bool ContainsNickname(const wxString &aNickname)
VTBL_ENTRY PROJECT & Prj() const
Function Prj returns the PROJECT associated with this KIWAY.
Definition: kiway.cpp:163
unsigned GetCount() const
Get the number of rows contained in the table.
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:37
Component library viewer main window.
void appendRowHandler(wxCommandEvent &event) override
Class GRID_TRICKS is used to add mouse and command handling (such as cut, copy, and paste) to a WX_GR...
Definition: grid_tricks.h:51
void adjustPathSubsGridColumns(int aWidth)
static unsigned FindIllegalLibNicknameChar(const UTF8 &aNickname, LIB_ID_TYPE aType)
Looks for characters that are illegal in library nicknames.
Definition: lib_id.cpp:398
void browseLibrariesHandler(wxCommandEvent &event) override
#define MYID_OPTIONS_EDITOR
static wxString m_lastBrowseDir
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:66
LIB_TABLE_ROWS rows
int YesOrCancelDialog(wxWindow *aParent, const wxString &aWarning, const wxString &aMessage, const wxString &aOKLabel, const wxString &aCancelLabel, bool *aApplyToAll)
Function YesOrCancelDialog displays a warning dialog with aMessage and returns the user response.
Definition: confirm.cpp:225
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:57
wxString m_Description
Description shown in the file picker dialog.
static const wxString ShowType(PCB_FILE_T aFileType)
Function ShowType returns a brief name for a plugin, given aFileType enum.
Definition: io_mgr.cpp:77
virtual const wxString GetType() const =0
Return the type of library represented by this row.
size_t GetFilenames(wxArrayString &aFilepaths)
int GetNumberRows() override
VTBL_ENTRY const wxString GetProjectPath() const
Function GetProjectPath returns the full path of the project.
Definition: project.cpp:102
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:116
Geda PCB file formats.
Definition: io_mgr.h:60
static PCB_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath)
Function GuessPluginTypeFromLibPath returns a plugin type given a footprint library's libPath.
Definition: io_mgr.cpp:124
size_t size() const override
FP_LIB_TABLE GFootprintTable
The global footprint library table.
Definition: pcbnew.cpp:326
This abstract base class mixes any object derived from LIB_TABLE into wxGridTableBase so the result c...
LIB_TABLE_ROWS_ITER insert(LIB_TABLE_ROWS_ITER aIterator, LIB_TABLE_ROW *aRow) override
void InvokePcbLibTableEditor(KIWAY *aKiway, wxWindow *aCaller)
Function InvokePcbLibTableEditor shows the modal DIALOG_FP_LIB_TABLE for purposes of editing the glob...
WX_GRID * m_grid
I don't own the grid, but he owns me.
Definition: grid_tricks.h:58
virtual void paste_text(const wxString &cb_text)
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:47
static size_t m_pageNdx
void moveUpHandler(wxCommandEvent &event) override
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
const wxString & GetNickName() const
static const wxString GlobalPathEnvVariableName()
Function GlobalPathEnvVarVariableName.
LIB_TABLE_ROW * clone() const
virtual void Parse(LIB_TABLE_LEXER *aLexer) override
Parse the #LIB_TABLE_LEXER s-expression library table format into the appropriate LIB_TABLE_ROW objec...
PANEL_FP_LIB_TABLE(DIALOG_EDIT_LIBRARY_TABLES *aParent, FP_LIB_TABLE *aGlobal, const wxString &aGlobalTblPath, FP_LIB_TABLE *aProject, const wxString &aProjectTblPath, const wxString &aProjectBasePath)
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:321
static constexpr int FILTER_COUNT
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
Definition: pgm_base.h:132
FP_LIB_TABLE_GRID * global_model() const
static wxString getFilterString()
DIALOG_EDIT_LIBRARY_TABLES * m_dialog
FP_LIB_TABLE_GRID * project_model() const
#define KISYS3DMOD
A variable name whose value holds the path of 3D shape files.
Definition: 3d_viewer.h:38
FP_LIB_TABLE_GRID * cur_model() const
void paste_text(const wxString &cb_text) override
handle specialized clipboard text, with leading "(fp_lib_table", OR spreadsheet formatted text.
wxString m_Extension
In case of folders it stands for extensions of files stored inside.
Definition of file extensions used in Kicad.
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:258
void deleteRowHandler(wxCommandEvent &event) override
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:167
bool AppendRows(size_t aNumRows=1) override
void optionsEditor(int aRow)
VTBL_ENTRY const ENV_VAR_MAP & GetLocalEnvVariables() const
Definition: pgm_base.h:315
bool verifyTables()
Trim important fields, removes blank row entries, and checks for duplicates.
Class PANEL_FP_LIB_TABLE_BASE.
IO_MGR::PCB_FILE_T m_Plugin
LIB_TABLE_ROWS_ITER begin() override
void pageChangedHandler(wxAuiNotebookEvent &event) override
VTBL_ENTRY FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
bool TransferDataFromWindow() override
Struct PARSE_ERROR contains a filename or source description, a problem input line,...
Definition: ki_exception.h:123
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:56
Dialog that can select both files and directories.
see class PGM_BASE
virtual void doPopupSelection(wxCommandEvent &event)
LIB_TABLE_ROW & At(unsigned aIndex)
Get the 'n'th LIB_TABLE_ROW object.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
LIB_TABLE_ROW * makeNewRow() override
virtual void showPopupMenu(wxMenu &menu)
#define max(a, b)
Definition: auxiliary.h:86
FP_LIB_TABLE_GRID(const FP_LIB_TABLE &aTableToEdit)
size_t i
Definition: json11.cpp:597
LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
LIB_TABLE_ROWS_ITER erase(LIB_TABLE_ROWS_ITER aFirst, LIB_TABLE_ROWS_ITER aLast) override
This class builds a wxGridTableBase by wrapping an FP_LIB_TABLE object.
bool m_IsFile
Whether the library is a folder or a file.
DIALOG_EDIT_LIBRARY_TABLES * m_parent
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, LIB_ID_TYPE aType, bool aLib=false)
Replace illegal LIB_ID item name characters with underscores '_'.
Definition: lib_id.cpp:352
void SetOptions(const wxString &aOptions)
Change the library options strings.
void onSizeGrid(wxSizeEvent &event) override
FP_LIB_TABLE * m_global
Dialog to show and edit symbol library tables.
static const struct @9 fileFilters[FILTER_COUNT]
PCB_FILE_T
Enum PCB_FILE_T is a set of file types that the IO_MGR knows about, and for which there has been a pl...
Definition: io_mgr.h:54
Class STRING_LINE_READER is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:254
bool handleDoubleClick(wxGridEvent &aEvent) override
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:244
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
const std::string KiCadFootprintLibPathExtension
void doPopupSelection(wxCommandEvent &event) override
LIB_TABLE_ROW * at(size_t aIndex) override
VTBL_ENTRY const wxString FootprintLibTblName() const
Function FootprintLibTblName returns the path and filename of this project's fp-lib-table,...
Definition: project.cpp:120
wxString NormalizePath(const wxFileName &aFilePath, const ENV_VAR_MAP *aEnvVars, const wxString &aProjectPath)
Normalizes a file path to an environmental variable, if possible.
Definition: env_paths.cpp:67
Declaration of the eda_3d_viewer class.
static wxString GetGlobalTableFileName()
Function GetGlobalTableFileName.
void populateEnvironReadOnlyTable()
Populate the readonly environment variable table with names and values by examining all the full_uri ...
#define min(a, b)
Definition: auxiliary.h:85
S-expression Pcbnew file format.
Definition: io_mgr.h:57