KiCad PCB EDA Suite
dialog_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-2018 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 
37 #include <fctsys.h>
38 #include <project.h>
39 #include <3d_viewer.h> // for KISYS3DMOD
41 #include <lib_id.h>
42 #include <fp_lib_table.h>
43 #include <lib_table_lexer.h>
44 #include <invoke_pcb_dialog.h>
45 #include <grid_tricks.h>
46 #include <confirm.h>
47 #include <lib_table_grid.h>
49 #include <pgm_base.h>
50 #include <env_paths.h>
52 
53 // Filters for the file picker
54 static constexpr int FILTER_COUNT = 4;
55 static const struct
56 {
57  wxString m_Description;
58  wxString m_Extension;
59  bool m_IsFile;
62 {
63  // wxGenericDirCtrl does not handle regexes in wildcards
64  { "KiCad (folder with .kicad_mod files)", "", false, IO_MGR::KICAD_SEXP },
65  { "Eagle 6.x (*.lbr)", "lbr", true, IO_MGR::EAGLE },
66  { "KiCad legacy (*.mod)", "mod", true, IO_MGR::LEGACY },
67  { "Geda (folder with *.fp files)", "", false, IO_MGR::GEDA_PCB },
68 };
69 
70 
71 // Returns the filter string for the file picker
72 static wxString getFilterString()
73 {
74  wxString filterAll = _( "All supported library formats|" );
75  bool firstFilterAll = true;
76  wxString filter;
77 
78  for( int i = 0; i < FILTER_COUNT; ++i )
79  {
80  if( fileFilters[i].m_IsFile )
81  {
82  // "All supported formats" filter
83  if( firstFilterAll )
84  firstFilterAll = false;
85  else
86  filterAll += ";";
87 
88  wxASSERT( !fileFilters[i].m_Extension.IsEmpty() );
89  filterAll += "*." + fileFilters[i].m_Extension;
90  }
91 
92 
93  // Individual filter strings
94  filter += "|" + fileFilters[i].m_Description +
95  "|" + ( fileFilters[i].m_IsFile ? "*." + fileFilters[i].m_Extension : "" );
96  }
97 
98  return filterAll + filter;
99 }
100 
101 
106 {
107  friend class FP_GRID_TRICKS;
108 
109 protected:
110  LIB_TABLE_ROW* at( size_t aIndex ) override { return &rows.at( aIndex ); }
111 
112  size_t size() const override { return rows.size(); }
113 
115  {
116  return dynamic_cast< LIB_TABLE_ROW* >( new FP_LIB_TABLE_ROW );
117  }
118 
119  LIB_TABLE_ROWS_ITER begin() override { return rows.begin(); }
120 
122  {
123  return rows.insert( aIterator, aRow );
124  }
125 
126  void push_back( LIB_TABLE_ROW* aRow ) override { rows.push_back( aRow ); }
127 
129  {
130  return rows.erase( aFirst, aLast );
131  }
132 
133 public:
134 
135  FP_LIB_TABLE_GRID( const FP_LIB_TABLE& aTableToEdit )
136  {
137  rows = aTableToEdit.rows;
138  }
139 };
140 
141 
143 {
144 public:
145  FP_GRID_TRICKS( wxGrid* aGrid ) :
146  GRID_TRICKS( aGrid )
147  {
148  }
149 
150 protected:
151 
154  virtual void paste_text( const wxString& cb_text ) override
155  {
156  FP_LIB_TABLE_GRID* tbl = (FP_LIB_TABLE_GRID*) m_grid->GetTable();
157 
158  size_t ndx = cb_text.find( "(fp_lib_table" );
159 
160  if( ndx != std::string::npos )
161  {
162  // paste the FP_LIB_TABLE_ROWs of s-expression (fp_lib_table), starting
163  // at column 0 regardless of current cursor column.
164 
165  STRING_LINE_READER slr( TO_UTF8( cb_text ), "Clipboard" );
166  LIB_TABLE_LEXER lexer( &slr );
167  FP_LIB_TABLE tmp_tbl;
168  bool parsed = true;
169 
170  try
171  {
172  tmp_tbl.Parse( &lexer );
173  }
174  catch( PARSE_ERROR& pe )
175  {
176  DisplayError( NULL, pe.What() );
177  parsed = false;
178  }
179 
180  if( parsed )
181  {
182  const int cur_row = std::max( getCursorRow(), 0 );
183 
184  // if clipboard rows would extend past end of current table size...
185  if( tmp_tbl.GetCount() > tbl->GetNumberRows() - cur_row )
186  {
187  int newRowsNeeded = tmp_tbl.GetCount() - ( tbl->GetNumberRows() - cur_row );
188  tbl->AppendRows( newRowsNeeded );
189  }
190 
191  for( int i = 0; i < tmp_tbl.GetCount(); ++i )
192  {
193  tbl->rows.replace( cur_row+i, tmp_tbl.At( i ) );
194  }
195  }
196 
197  m_grid->AutoSizeColumns( false );
198  }
199  else
200  {
201  // paste spreadsheet formatted text.
202  GRID_TRICKS::paste_text( cb_text );
203  }
204  }
205 };
206 
207 
214 {
215 
216 public:
217  DIALOG_FP_LIB_TABLE( wxTopLevelWindow* aParent, FP_LIB_TABLE* aGlobal, FP_LIB_TABLE* aProject ) :
218  DIALOG_FP_LIB_TABLE_BASE( aParent ),
219  m_global( aGlobal ),
220  m_project( aProject )
221  {
222  // For user info, shows the table filenames:
223  m_PrjTableFilename->SetLabel( Prj().FootprintLibTblName() );
224  m_GblTableFilename->SetLabel( FP_LIB_TABLE::GetGlobalTableFileName() );
225 
226  // wxGrid only supports user owned tables if they exist past end of ~wxGrid(),
227  // so make it a grid owned table.
228  m_global_grid->SetTable( new FP_LIB_TABLE_GRID( *aGlobal ), true );
229  m_project_grid->SetTable( new FP_LIB_TABLE_GRID( *aProject ), true );
230 
231  // add Cut, Copy, and Paste to wxGrids
232  m_global_grid->PushEventHandler( new FP_GRID_TRICKS( m_global_grid ) );
233  m_project_grid->PushEventHandler( new FP_GRID_TRICKS( m_project_grid ) );
234 
235  m_global_grid->AutoSizeColumns( false );
236  m_project_grid->AutoSizeColumns( false );
237 
238  wxArrayString choices;
239 
240  choices.Add( IO_MGR::ShowType( IO_MGR::KICAD_SEXP ) );
241  choices.Add( IO_MGR::ShowType( IO_MGR::GITHUB ) );
242  choices.Add( IO_MGR::ShowType( IO_MGR::LEGACY ) );
243  choices.Add( IO_MGR::ShowType( IO_MGR::EAGLE ) );
244  choices.Add( IO_MGR::ShowType( IO_MGR::GEDA_PCB ) );
245 
246  /* PCAD_PLUGIN does not support Footprint*() functions
247  choices.Add( IO_MGR::ShowType( IO_MGR::GITHUB ) );
248  */
249 
250  populateEnvironReadOnlyTable();
251 
252  for( int i=0; i<2; ++i )
253  {
254  wxGrid* g = i==0 ? m_global_grid : m_project_grid;
255 
256  wxGridCellAttr* attr;
257 
258  attr = new wxGridCellAttr;
259  attr->SetEditor( new wxGridCellChoiceEditor( choices ) );
260  g->SetColAttr( COL_TYPE, attr );
261 
262  attr = new wxGridCellAttr;
263  attr->SetRenderer( new wxGridCellBoolRenderer() );
264  attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
265  g->SetColAttr( COL_ENABLED, attr );
266 
267  // all but COL_OPTIONS, which is edited with Option Editor anyways.
268  g->AutoSizeColumn( COL_NICKNAME, false );
269  g->AutoSizeColumn( COL_TYPE, false );
270  g->AutoSizeColumn( COL_URI, false );
271  g->AutoSizeColumn( COL_DESCR, false );
272 
273  // would set this to width of title, if it was easily known.
274  g->SetColSize( COL_OPTIONS, 80 );
275  }
276 
277  // This scrunches the dialog hideously, probably due to wxAUI container.
278  // Fit();
279  // We derive from DIALOG_SHIM so prior size will be used anyways.
280 
281  // select the last selected page
282  m_auinotebook->SetSelection( m_pageNdx );
283 
284  // fire pageChangedHandler() so m_cur_grid gets set
285  // m_auinotebook->SetSelection will generate a pageChangedHandler()
286  // event call later, but too late.
287  wxAuiNotebookEvent uneventful;
288  pageChangedHandler( uneventful );
289 
290  // Gives a selection for each grid, mainly for delete lib button.
291  // Without that, we do not see what lib will be deleted
292  m_global_grid->SelectRow( 0 );
293  m_project_grid->SelectRow( 0 );
294 
295  // for ALT+A handling, we want the initial focus to be on the first selected grid.
296  m_cur_grid->SetFocus();
297 
298  SetSizeInDU( 450, 380 );
299  Center();
300 
301  // On some windows manager (Unity, XFCE), this dialog is
302  // not always raised, depending on this dialog is run.
303  // Force it to be raised
304  Raise();
305  }
306 
308  {
309  // Delete the GRID_TRICKS.
310  // Any additional event handlers should be popped before the window is deleted.
311  m_global_grid->PopEventHandler( true );
312  m_project_grid->PopEventHandler( true );
313  }
314 
315 
316 private:
319  int getCursorCol() const
320  {
321  return m_cur_grid->GetGridCursorCol();
322  }
323 
326  int getCursorRow() const
327  {
328  return m_cur_grid->GetGridCursorRow();
329  }
330 
337  {
338  for( int t=0; t<2; ++t )
339  {
340  FP_LIB_TABLE_GRID& model = t==0 ? *global_model() : *project_model();
341 
342  for( int r = 0; r < model.GetNumberRows(); )
343  {
344  wxString nick = model.GetValue( r, COL_NICKNAME ).Trim( false ).Trim();
345  wxString uri = model.GetValue( r, COL_URI ).Trim( false ).Trim();
346  unsigned illegalCh = 0;
347 
348  if( !nick || !uri )
349  {
350  // Delete the "empty" row, where empty means missing nick or uri.
351  // This also updates the UI which could be slow, but there should only be a few
352  // rows to delete, unless the user fell asleep on the Add Row
353  // button.
354  model.DeleteRows( r, 1 );
355  }
356  else if( ( illegalCh = LIB_ID::FindIllegalLibNicknameChar( nick, LIB_ID::ID_PCB ) ) )
357  {
358  wxString msg = wxString::Format(
359  _( "Illegal character \"%c\" found in Nickname: \"%s\" in row %d" ),
360  illegalCh, GetChars( nick ), r );
361 
362  // show the tabbed panel holding the grid we have flunked:
363  if( &model != cur_model() )
364  {
365  m_auinotebook->SetSelection( &model == global_model() ? 0 : 1 );
366  }
367 
368  // go to the problematic row
369  m_cur_grid->SetGridCursor( r, 0 );
370  m_cur_grid->SelectBlock( r, 0, r, 0 );
371  m_cur_grid->MakeCellVisible( r, 0 );
372 
373  wxMessageDialog errdlg( this, msg, _( "No Colon in Nicknames" ) );
374  errdlg.ShowModal();
375  return false;
376  }
377  else
378  {
379  // set the trimmed values back into the table so they get saved to disk.
380  model.SetValue( r, COL_NICKNAME, nick );
381  model.SetValue( r, COL_URI, uri );
382  ++r; // this row was OK.
383  }
384  }
385  }
386 
387  // check for duplicate nickNames, separately in each table.
388  for( int t=0; t<2; ++t )
389  {
390  FP_LIB_TABLE_GRID& model = t==0 ? *global_model() : *project_model();
391 
392  for( int r1 = 0; r1 < model.GetNumberRows() - 1; ++r1 )
393  {
394  wxString nick1 = model.GetValue( r1, COL_NICKNAME );
395 
396  for( int r2=r1+1; r2 < model.GetNumberRows(); ++r2 )
397  {
398  wxString nick2 = model.GetValue( r2, COL_NICKNAME );
399 
400  if( nick1 == nick2 )
401  {
402  wxString msg = wxString::Format(
403  _( "Duplicate Nickname: \"%s\" in rows %d and %d" ),
404  GetChars( nick1 ), r1+1, r2+1
405  );
406 
407  // show the tabbed panel holding the grid we have flunked:
408  if( &model != cur_model() )
409  {
410  m_auinotebook->SetSelection( &model == global_model() ? 0 : 1 );
411  }
412 
413  // go to the lower of the two rows, it is technically the duplicate:
414  m_cur_grid->SetGridCursor( r2, 0 );
415  m_cur_grid->SelectBlock( r2, 0, r2, 0 );
416  m_cur_grid->MakeCellVisible( r2, 0 );
417 
418  wxMessageDialog errdlg( this, msg, _( "Please Delete or Modify One" ) );
419  errdlg.ShowModal();
420  return false;
421  }
422  }
423  }
424  }
425 
426  return true;
427  }
428 
429  //-----<event handlers>----------------------------------
430 
431  void onKeyDown( wxKeyEvent& ev ) override
432  {
433 #if 0
434  // send the key to the current grid
435  ((wxEvtHandler*)m_cur_grid)->ProcessEvent( ev );
436 #else
437  // or no:
438  // m_cur_grid has the focus most of the time anyways, so above not needed.
439  ev.Skip();
440 #endif
441  }
442 
443  void pageChangedHandler( wxAuiNotebookEvent& event ) override
444  {
445  m_pageNdx = m_auinotebook->GetSelection();
446  m_cur_grid = ( m_pageNdx == 0 ) ? m_global_grid : m_project_grid;
447  }
448 
449  void appendRowHandler( wxCommandEvent& event ) override
450  {
451  if( m_cur_grid->AppendRows( 1 ) )
452  {
453  int last_row = m_cur_grid->GetNumberRows() - 1;
454 
455  // wx documentation is wrong, SetGridCursor does not make visible.
456  m_cur_grid->MakeCellVisible( last_row, 0 );
457  m_cur_grid->SetGridCursor( last_row, 0 );
458  m_cur_grid->SelectRow( m_cur_grid->GetGridCursorRow() );
459  }
460  }
461 
462  void deleteRowHandler( wxCommandEvent& event ) override
463  {
464  int currRow = getCursorRow();
465 
466  // In a wxGrid, collect rows that have a selected cell, or are selected
467  // is not so easy: it depend on the way the selection was made.
468  // Here, we collect row selected by clicking on a row label, and
469  // row that contain a cell previously selected.
470  // If no candidate, just delete the row with the grid cursor.
471  wxArrayInt selectedRows = m_cur_grid->GetSelectedRows();
472  wxGridCellCoordsArray cells = m_cur_grid->GetSelectedCells();
473  wxGridCellCoordsArray blockTopLeft = m_cur_grid->GetSelectionBlockTopLeft();
474  wxGridCellCoordsArray blockBotRight = m_cur_grid->GetSelectionBlockBottomRight();
475 
476  // Add all row having cell selected to list:
477  for( unsigned ii = 0; ii < cells.GetCount(); ii++ )
478  selectedRows.Add( cells[ii].GetRow() );
479 
480  // Handle block selection
481  if( !blockTopLeft.IsEmpty() && !blockBotRight.IsEmpty() )
482  {
483  for( int i = blockTopLeft[0].GetRow(); i <= blockBotRight[0].GetRow(); ++i )
484  selectedRows.Add( i );
485  }
486 
487  // Use the row having the grid cursor only if we have no candidate:
488  if( selectedRows.size() == 0 && getCursorRow() >= 0 )
489  selectedRows.Add( getCursorRow() );
490 
491  std::sort( selectedRows.begin(), selectedRows.end() );
492 
493  // Remove selected rows (note: a row can be stored more than once in list)
494  int last_row = -1;
495  for( int ii = selectedRows.GetCount()-1; ii >= 0; ii-- )
496  {
497  int row = selectedRows[ii];
498 
499  if( row != last_row )
500  {
501  last_row = row;
502  m_cur_grid->DeleteRows( row, 1 );
503  }
504  }
505 
506  if( currRow >= m_cur_grid->GetNumberRows() )
507  m_cur_grid->SetGridCursor(m_cur_grid->GetNumberRows()-1, getCursorCol() );
508 
509  m_cur_grid->SelectRow( m_cur_grid->GetGridCursorRow() );
510  }
511 
512  void moveUpHandler( wxCommandEvent& event ) override
513  {
514  wxArrayInt rowsSelected = m_cur_grid->GetSelectedRows();
515 
516  if( rowsSelected.GetCount() == 0 )
517  return;
518 
519  // @todo: add multiple selection moves.
520  int curRow = rowsSelected[0];
521 
522  if( curRow >= 1 )
523  {
524  int curCol = getCursorCol();
525 
526  FP_LIB_TABLE_GRID* tbl = cur_model();
527 
528  boost::ptr_vector< LIB_TABLE_ROW >::auto_type move_me =
529  tbl->rows.release( tbl->rows.begin() + curRow );
530 
531  --curRow;
532  tbl->rows.insert( tbl->rows.begin() + curRow, move_me.release() );
533 
534  if( tbl->GetView() )
535  {
536  // fire a msg to cause redrawing
537  wxGridTableMessage msg( tbl,
538  wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
539  curRow,
540  0 );
541 
542  tbl->GetView()->ProcessTableMessage( msg );
543  }
544 
545  m_cur_grid->MakeCellVisible( curRow, curCol );
546  m_cur_grid->SetGridCursor( curRow, curCol );
547  m_cur_grid->SelectRow( getCursorRow() );
548  }
549  }
550 
551  void moveDownHandler( wxCommandEvent& event ) override
552  {
553  wxArrayInt rowsSelected = m_cur_grid->GetSelectedRows();
554 
555  if( rowsSelected.GetCount() == 0 )
556  return;
557 
558  FP_LIB_TABLE_GRID* tbl = cur_model();
559 
560  // @todo: add multiple selection moves.
561  int curRow = rowsSelected[0];
562 
563  if( unsigned( curRow + 1 ) < tbl->rows.size() )
564  {
565  int curCol = getCursorCol();
566 
567  boost::ptr_vector< LIB_TABLE_ROW >::auto_type move_me =
568  tbl->rows.release( tbl->rows.begin() + curRow );
569 
570  ++curRow;
571  tbl->rows.insert( tbl->rows.begin() + curRow, move_me.release() );
572 
573  if( tbl->GetView() )
574  {
575  // fire a msg to cause redrawing
576  wxGridTableMessage msg( tbl,
577  wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
578  curRow - 1,
579  0 );
580 
581  tbl->GetView()->ProcessTableMessage( msg );
582  }
583 
584  m_cur_grid->MakeCellVisible( curRow, curCol );
585  m_cur_grid->SetGridCursor( curRow, curCol );
586  m_cur_grid->SelectRow( getCursorRow() );
587  }
588  }
589 
590  void optionsEditor( wxCommandEvent& event ) override
591  {
592  FP_LIB_TABLE_GRID* tbl = cur_model();
593 
594  if( tbl->GetNumberRows() )
595  {
596  int curRow = getCursorRow();
597  LIB_TABLE_ROW* row = &tbl->rows[curRow];
598 
599  wxString result;
600  const wxString& options = row->GetOptions();
601 
602  InvokePluginOptionsEditor( this, row->GetNickName(), row->GetType(), options, &result );
603 
604  if( options != result )
605  {
606  row->SetOptions( result );
607 
608  // all but options:
609  m_cur_grid->AutoSizeColumn( COL_NICKNAME, false );
610  m_cur_grid->AutoSizeColumn( COL_URI, false );
611  m_cur_grid->AutoSizeColumn( COL_TYPE, false );
612 
613  // On Windows, the grid is not refresh,
614  // so force resfresh after a change
615 #ifdef __WINDOWS__
616  Refresh();
617 #endif
618  }
619  }
620  }
621 
622  void browseLibrariesHandler( wxCommandEvent& event ) override
623  {
624  if( m_lastBrowseDir.IsEmpty() )
625  m_lastBrowseDir = Prj().GetProjectPath();
626 
627  DIALOG_FILE_DIR_PICKER dlg( this, _( "Select Library" ), m_lastBrowseDir,
629 
630  auto result = dlg.ShowModal();
631 
632  if( result == wxID_CANCEL )
633  return;
634 
635  m_lastBrowseDir = dlg.GetDirectory();
636 
637  // Drop the last directory if the path is a .pretty folder
638  if( m_lastBrowseDir.EndsWith( KiCadFootprintLibPathExtension ) )
639  m_lastBrowseDir = m_lastBrowseDir.BeforeLast( wxFileName::GetPathSeparator() );
640 
641  bool skipRemainingDuplicates = false;
642  wxArrayString files;
643  dlg.GetFilenames( files );
644 
645  for( const auto& filePath : files )
646  {
647  wxFileName fn( filePath );
648  wxString nickname = LIB_ID::FixIllegalChars( fn.GetName(), LIB_ID::ID_PCB );
649 
650  if( cur_model()->ContainsNickname( nickname ) )
651  {
652  if( skipRemainingDuplicates )
653  continue;
654 
655  int ret = YesNoCancelDialog( this,
656  _( "Warning: Duplicate Nickname" ),
657  wxString::Format( _( "A library nicknamed \"%s\" already exists." ), nickname ),
658  _( "Skip" ),
659  _( "Skip All Remaining Duplicates" ),
660  _( "Add Anyway" ) );
661 
662  if( ret == wxID_YES )
663  continue;
664  else if ( ret == wxID_NO )
665  {
666  skipRemainingDuplicates = true;
667  continue;
668  }
669  }
670 
671  if( m_cur_grid->AppendRows( 1 ) )
672  {
673  int last_row = m_cur_grid->GetNumberRows() - 1;
674 
675  m_cur_grid->SetCellValue( last_row, COL_NICKNAME, nickname );
676 
677  auto type = IO_MGR::GuessPluginTypeFromLibPath( filePath );
678  m_cur_grid->SetCellValue( last_row, COL_TYPE, IO_MGR::ShowType( type ) );
679 
680  // try to use path normalized to an environmental variable or project path
681  wxString normalizedPath = NormalizePath( filePath, &Pgm().GetLocalEnvVariables(), &Prj() );
682  m_cur_grid->SetCellValue( last_row, COL_URI,
683  normalizedPath.IsEmpty() ? fn.GetFullPath() : normalizedPath );
684  }
685  }
686 
687  if( !files.IsEmpty() )
688  scrollToRow( m_cur_grid->GetNumberRows() - 1 ); // scroll to the new libraries
689  }
690 
691  void onCancelButtonClick( wxCommandEvent& event ) override
692  {
693  EndModal( 0 );
694  }
695 
696  void onCancelCaptionButtonClick( wxCloseEvent& event ) override
697  {
698  EndModal( 0 );
699  }
700 
701  void onOKButtonClick( wxCommandEvent& event ) override
702  {
703  int dialogRet = 0;
704 
705  // stuff any pending cell editor text into the table.
706  m_cur_grid->DisableCellEditControl();
707 
708  if( verifyTables() )
709  {
710  if( *global_model() != *m_global )
711  {
712  dialogRet |= 1;
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  {
722  dialogRet |= 2;
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  EndModal( dialogRet );
731  }
732  }
733 
737  {
738  wxRegEx re( ".*?(\\$\\{(.+?)\\})|(\\$\\((.+?)\\)).*?", wxRE_ADVANCED );
739  wxASSERT( re.IsValid() ); // wxRE_ADVANCED is required.
740 
741  std::set< wxString > unique;
742 
743  // clear the table
744  m_path_subs_grid->DeleteRows( 0, m_path_subs_grid->GetNumberRows() );
745 
746  for( int i = 0; i < 2; ++i )
747  {
748  FP_LIB_TABLE_GRID* tbl = i == 0 ? global_model() : project_model();
749 
750  for( int row = 0; row < tbl->GetNumberRows(); ++row )
751  {
752  wxString uri = tbl->GetValue( row, COL_URI );
753 
754  while( re.Matches( uri ) )
755  {
756  wxString envvar = re.GetMatch( uri, 2 );
757 
758  // if not ${...} form then must be $(...)
759  if( envvar.IsEmpty() )
760  envvar = re.GetMatch( uri, 4 );
761 
762  // ignore duplicates
763  unique.insert( envvar );
764 
765  // delete the last match and search again
766  uri.Replace( re.GetMatch( uri, 0 ), wxEmptyString );
767  }
768  }
769  }
770 
771  // Make sure this special environment variable shows up even if it was
772  // not used yet. It is automatically set by KiCad to the directory holding
773  // the current project.
774  unique.insert( PROJECT_VAR_NAME );
775  unique.insert( FP_LIB_TABLE::GlobalPathEnvVariableName() );
776  // This special environment variable is used to locate 3d shapes
777  unique.insert( KISYS3DMOD );
778 
779  m_path_subs_grid->AppendRows( unique.size() );
780 
781  int row = 0;
782 
783  for( auto it = unique.begin(); it != unique.end(); ++it, ++row )
784  {
785  wxString evName = *it;
786  wxString evValue;
787 
788  m_path_subs_grid->SetCellValue( row, 0, evName );
789 
790  if( wxGetEnv( evName, &evValue ) )
791  m_path_subs_grid->SetCellValue( row, 1, evValue );
792  }
793 
794  m_path_subs_grid->AutoSizeColumns();
795  }
796 
798  void scrollToRow( int aRowNumber )
799  {
800  // wx documentation is wrong, SetGridCursor does not make visible.
801  m_cur_grid->MakeCellVisible( aRowNumber, 0 );
802  m_cur_grid->SetGridCursor( aRowNumber, 0 );
803  m_cur_grid->SelectRow( m_cur_grid->GetGridCursorRow() );
804  }
805 
806  //-----</event handlers>---------------------------------
807 
808  // caller's tables are modified only on OK button and successful verification.
811 
813  {
814  return (FP_LIB_TABLE_GRID*) m_global_grid->GetTable();
815  }
816 
818  {
819  return (FP_LIB_TABLE_GRID*) m_project_grid->GetTable();
820  }
821 
823  {
824  return (FP_LIB_TABLE_GRID*) m_cur_grid->GetTable();
825  }
826 
827  wxGrid* m_cur_grid;
828  static int m_pageNdx;
829 
830  wxString m_lastBrowseDir;
831 };
832 
833 
835 
836 
837 int InvokePcbLibTableEditor( wxTopLevelWindow* aCaller, FP_LIB_TABLE* aGlobal,
838  FP_LIB_TABLE* aProject )
839 {
840  DIALOG_FP_LIB_TABLE dlg( aCaller, aGlobal, aProject );
841 
842  int dialogRet = dlg.ShowModal(); // returns value passed to EndModal() above
843 
844  return dialogRet;
845 }
int GetCount()
FP_LIB_TABLE_GRID * project_model() const
int getCursorCol() const
If the cursor is not on a valid cell, because there are no rows at all, return -1, else return a 0 based column index.
wxString m_lastBrowseDir
last browsed directory
void push_back(LIB_TABLE_ROW *aRow) override
int getCursorRow() const
If the cursor is not on a valid cell, because there are no rows at all, return -1, else return a 0 based row index.
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE...
void appendRowHandler(wxCommandEvent &event) override
Class FP_LIB_TABLE_ROW.
Definition: fp_lib_table.h:42
This file is part of the common library.
PROJECT & Prj()
Definition: kicad.cpp:287
void onCancelCaptionButtonClick(wxCloseEvent &event) override
void InvokePluginOptionsEditor(wxTopLevelWindow *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...
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:36
Class GRID_TRICKS is used to add cut, copy, and paste to an otherwise unmodied wxGrid instance...
Definition: grid_tricks.h:34
void deleteRowHandler(wxCommandEvent &event) override
static unsigned FindIllegalLibNicknameChar(const UTF8 &aNickname, LIB_ID_TYPE aType)
Looks for characters that are illegal in library nicknames.
Definition: lib_id.cpp:413
bool DeleteRows(size_t aPos, size_t aNumRows) override
static constexpr int FILTER_COUNT
Class DIALOG_FP_LIB_TABLE shows and edits the PCB library tables.
const wxString & GetOptions() const
Return the options string, which may hold a password or anything else needed to instantiate the under...
bool m_IsFile
Whether the library is a folder or a file.
IO_MGR::PCB_FILE_T m_Plugin
LIB_TABLE_ROW * At(int aIndex)
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:66
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, LIB_ID_TYPE aType)
Replace illegal LIB_ID item name characters with underscores &#39;_&#39;.
Definition: lib_id.cpp:374
LIB_TABLE_ROWS rows
void moveUpHandler(wxCommandEvent &event) override
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
void browseLibrariesHandler(wxCommandEvent &event) override
void moveDownHandler(wxCommandEvent &event) override
Geda PCB file formats.
Definition: io_mgr.h:58
static PCB_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath)
Function GuessPluginTypeFromLibPath returns a plugin type given a footprint library&#39;s libPath...
Definition: io_mgr.cpp:124
size_t size() const override
FP_LIB_TABLE_GRID * global_model() const
void populateEnvironReadOnlyTable()
Populate the readonly environment variable table with names and values by examining all the full_uri ...
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
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 const wxString GlobalPathEnvVariableName()
Function GlobalPathEnvVarVariableName.
static wxString getFilterString()
virtual void paste_text(const wxString &cb_text) override
handle specialized clipboard text, with leading "(fp_lib_table", OR spreadsheet formatted text...
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...
wxString m_Extension
In case of folders it stands for extensions of files stored inside.
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
#define KISYS3DMOD
A variable name whose value holds the path of 3D shape files.
Definition: 3d_viewer.h:38
int InvokePcbLibTableEditor(wxTopLevelWindow *aCaller, FP_LIB_TABLE *aGlobal, FP_LIB_TABLE *aProject)
Function InvokePcbLibTableEditor shows the modal DIALOG_FP_LIB_TABLE for purposes of editing two lib ...
const wxString KiCadFootprintLibPathExtension
The common library.
void pageChangedHandler(wxAuiNotebookEvent &event) override
bool AppendRows(size_t aNumRows=1) override
FP_LIB_TABLE_GRID * cur_model() const
static const struct @6 fileFilters[FILTER_COUNT]
void scrollToRow(int aRowNumber)
Makes a specific row visible.
DIALOG_FP_LIB_TABLE(wxTopLevelWindow *aParent, FP_LIB_TABLE *aGlobal, FP_LIB_TABLE *aProject)
void onKeyDown(wxKeyEvent &ev) override
LIB_TABLE_ROWS_ITER begin() override
Class DIALOG_FP_LIB_TABLE_BASE.
void SetValue(int aRow, int aCol, const wxString &aValue) override
wxString GetValue(int aRow, int aCol) override
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
const wxString & GetNickName() const
Struct PARSE_ERROR contains a filename or source description, a problem input line, a line number, a byte offset, and an error message which contains the the caller&#39;s report and his call site information: CPP source file, function, and line number.
Definition: ki_exception.h:123
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:54
Dialog that can select both files and directories.
see class PGM_BASE
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
#define max(a, b)
Definition: auxiliary.h:86
FP_LIB_TABLE_GRID(const FP_LIB_TABLE &aTableToEdit)
size_t i
Definition: json11.cpp:597
static int m_pageNdx
Remember the last notebook page selected during a session.
LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER
VTBL_ENTRY const wxString GetProjectPath() const
Function GetProjectPath returns the full path of the project.
Definition: project.cpp:102
LIB_TABLE_ROWS_ITER erase(LIB_TABLE_ROWS_ITER aFirst, LIB_TABLE_ROWS_ITER aLast) override
FP_GRID_TRICKS(wxGrid *aGrid)
wxGrid * m_cur_grid
changed based on tab choice
This class builds a wxGridTableBase by wrapping an FP_LIB_TABLE object.
void SetOptions(const wxString &aOptions)
Change the library options strings.
void onOKButtonClick(wxCommandEvent &event) override
void onCancelButtonClick(wxCommandEvent &event) override
bool verifyTables()
Function verifyTables trims important fields, removes blank row entries, and checks for duplicates...
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:52
Class STRING_LINE_READER is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:254
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:185
wxString m_Description
Description shown in the file picker dialog.
int YesNoCancelDialog(wxWindow *aParent, const wxString &aPrimaryMessage, const wxString &aSecondaryMessage, const wxString &aYesButtonText, const wxString &aNoButtonText, const wxString &aCancelButtonText)
Function YesNoCancelDialog displays a yes/no/cancel dialog with aMessage and returns the user respons...
Definition: confirm.cpp:270
Read only http://github.com repo holding pretty footprints.
Definition: io_mgr.h:59
LIB_TABLE_ROW * at(size_t aIndex) override
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 optionsEditor(wxCommandEvent &event) override
S-expression Pcbnew file format.
Definition: io_mgr.h:55