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-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 #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>
61 
62 // Filters for the file picker
63 static constexpr int FILTER_COUNT = 4;
64 static const struct
65 {
66  wxString m_Description;
67  wxString m_Extension;
68  bool m_IsFile;
71 {
72  // wxGenericDirCtrl does not handle regexes in wildcards
73  { "KiCad (folder with .kicad_mod files)", "", false, IO_MGR::KICAD_SEXP },
74  { "Eagle 6.x (*.lbr)", "lbr", true, IO_MGR::EAGLE },
75  { "KiCad legacy (*.mod)", "mod", true, IO_MGR::LEGACY },
76  { "Geda (folder with *.fp files)", "", false, IO_MGR::GEDA_PCB },
77 };
78 
79 
80 // Returns the filter string for the file picker
81 static wxString getFilterString()
82 {
83  wxString filterAll = _( "All supported library formats|" );
84  bool firstFilterAll = true;
85  wxString filter;
86 
87  for( int i = 0; i < FILTER_COUNT; ++i )
88  {
89  if( fileFilters[i].m_IsFile )
90  {
91  // "All supported formats" filter
92  if( firstFilterAll )
93  firstFilterAll = false;
94  else
95  filterAll += ";";
96 
97  wxASSERT( !fileFilters[i].m_Extension.IsEmpty() );
98  filterAll += "*." + fileFilters[i].m_Extension;
99  }
100 
101 
102  // Individual filter strings
103  filter += "|" + fileFilters[i].m_Description +
104  "|" + ( fileFilters[i].m_IsFile ? "*." + fileFilters[i].m_Extension : "" );
105  }
106 
107  return filterAll + filter;
108 }
109 
110 
115 {
116  friend class PANEL_FP_LIB_TABLE;
117  friend class FP_GRID_TRICKS;
118 
119 protected:
120  LIB_TABLE_ROW* at( size_t aIndex ) override { return &rows.at( aIndex ); }
121 
122  size_t size() const override { return rows.size(); }
123 
125  {
126  return dynamic_cast< LIB_TABLE_ROW* >( new FP_LIB_TABLE_ROW );
127  }
128 
129  LIB_TABLE_ROWS_ITER begin() override { return rows.begin(); }
130 
132  {
133  return rows.insert( aIterator, aRow );
134  }
135 
136  void push_back( LIB_TABLE_ROW* aRow ) override { rows.push_back( aRow ); }
137 
139  {
140  return rows.erase( aFirst, aLast );
141  }
142 
143 public:
144 
145  FP_LIB_TABLE_GRID( const FP_LIB_TABLE& aTableToEdit )
146  {
147  rows = aTableToEdit.rows;
148  }
149 };
150 
151 
152 #define MYID_OPTIONS_EDITOR 15151
153 
154 
156 {
157 public:
158  FP_GRID_TRICKS( DIALOG_EDIT_LIBRARY_TABLES* aParent, wxGrid* aGrid ) :
159  GRID_TRICKS( aGrid ),
160  m_dialog( aParent )
161  { }
162 
163 protected:
165 
166  void optionsEditor( int aRow )
167  {
168  FP_LIB_TABLE_GRID* tbl = (FP_LIB_TABLE_GRID*) m_grid->GetTable();
169 
170  if( tbl->GetNumberRows() > aRow )
171  {
172  LIB_TABLE_ROW* row = tbl->at( (size_t) aRow );
173  const wxString& options = row->GetOptions();
174  wxString result = options;
175 
176  InvokePluginOptionsEditor( m_dialog, row->GetNickName(), row->GetType(), options,
177  &result );
178 
179  if( options != result )
180  {
181  row->SetOptions( result );
182  m_grid->Refresh();
183  }
184  }
185  }
186 
187  bool handleDoubleClick( wxGridEvent& aEvent ) override
188  {
189  if( aEvent.GetCol() == COL_OPTIONS )
190  {
191  optionsEditor( aEvent.GetRow() );
192  return true;
193  }
194 
195  return false;
196  }
197 
198  void showPopupMenu( wxMenu& menu ) override
199  {
200  if( m_grid->GetGridCursorCol() == COL_OPTIONS )
201  {
202  menu.Append( MYID_OPTIONS_EDITOR, _( "Options Editor..." ), _( "Edit options" ) );
203  menu.AppendSeparator();
204  }
205 
207  }
208 
209  void doPopupSelection( wxCommandEvent& event ) override
210  {
211  if( event.GetId() == MYID_OPTIONS_EDITOR )
212  optionsEditor( m_grid->GetGridCursorRow() );
213  else
215  }
216 
219  void paste_text( const wxString& cb_text ) override
220  {
221  FP_LIB_TABLE_GRID* tbl = (FP_LIB_TABLE_GRID*) m_grid->GetTable();
222  size_t ndx = cb_text.find( "(fp_lib_table" );
223 
224  if( ndx != std::string::npos )
225  {
226  // paste the FP_LIB_TABLE_ROWs of s-expression (fp_lib_table), starting
227  // at column 0 regardless of current cursor column.
228 
229  STRING_LINE_READER slr( TO_UTF8( cb_text ), "Clipboard" );
230  LIB_TABLE_LEXER lexer( &slr );
231  FP_LIB_TABLE tmp_tbl;
232  bool parsed = true;
233 
234  try
235  {
236  tmp_tbl.Parse( &lexer );
237  }
238  catch( PARSE_ERROR& pe )
239  {
240  DisplayError( m_dialog, pe.What() );
241  parsed = false;
242  }
243 
244  if( parsed )
245  {
246  // make sure the table is big enough...
247  if( tmp_tbl.GetCount() > tbl->GetNumberRows() )
248  tbl->AppendRows( tmp_tbl.GetCount() - tbl->GetNumberRows() );
249 
250  for( int i = 0; i < tmp_tbl.GetCount(); ++i )
251  tbl->rows.replace( i, tmp_tbl.At( i ) );
252  }
253 
254  m_grid->AutoSizeColumns( false );
255  }
256  else
257  {
258  // paste spreadsheet formatted text.
259  GRID_TRICKS::paste_text( cb_text );
260 
261  m_grid->AutoSizeColumns( false );
262  }
263  }
264 };
265 
266 
268  FP_LIB_TABLE* aGlobal, const wxString& aGlobalTblPath,
269  FP_LIB_TABLE* aProject, const wxString& aProjectTblPath,
270  const wxString& aProjectBasePath ) :
271  PANEL_FP_LIB_TABLE_BASE( aParent ),
272  m_global( aGlobal ),
273  m_project( aProject ),
274  m_projectBasePath( aProjectBasePath ),
275  m_parent( aParent )
276 {
277  // For user info, shows the table filenames:
278  m_GblTableFilename->SetLabel( aGlobalTblPath );
279  m_PrjTableFilename->SetLabel( aProjectTblPath );
280 
281  m_global_grid->SetTable( new FP_LIB_TABLE_GRID( *aGlobal ), true );
282  m_project_grid->SetTable( new FP_LIB_TABLE_GRID( *aProject ), true );
283 
284  // Give a bit more room for wxChoice editors
285  m_global_grid->SetDefaultRowSize( m_global_grid->GetDefaultRowSize() + 2 );
286  m_project_grid->SetDefaultRowSize( m_project_grid->GetDefaultRowSize() + 2 );
287 
288  // add Cut, Copy, and Paste to wxGrids
289  m_global_grid->PushEventHandler( new FP_GRID_TRICKS( m_parent, m_global_grid ) );
290  m_project_grid->PushEventHandler( new FP_GRID_TRICKS( m_parent, m_project_grid ) );
291  m_path_subs_grid->PushEventHandler( new GRID_TRICKS( m_path_subs_grid ) );
292 
293  m_global_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
294  m_project_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
295 
296  m_global_grid->AutoSizeColumns( false );
297  m_project_grid->AutoSizeColumns( false );
298 
299  wxArrayString choices;
300 
301  choices.Add( IO_MGR::ShowType( IO_MGR::KICAD_SEXP ) );
302  choices.Add( IO_MGR::ShowType( IO_MGR::GITHUB ) );
303  choices.Add( IO_MGR::ShowType( IO_MGR::LEGACY ) );
304  choices.Add( IO_MGR::ShowType( IO_MGR::EAGLE ) );
305  choices.Add( IO_MGR::ShowType( IO_MGR::GEDA_PCB ) );
306 
307  /* PCAD_PLUGIN does not support Footprint*() functions
308  choices.Add( IO_MGR::ShowType( IO_MGR::PCAD ) );
309  */
310 
312 
313  for( wxGrid* g : { m_global_grid, m_project_grid } )
314  {
315  wxGridCellAttr* attr;
316 
317  attr = new wxGridCellAttr;
318  attr->SetEditor( new wxGridCellChoiceEditor( choices ) );
319  g->SetColAttr( COL_TYPE, attr );
320 
321  attr = new wxGridCellAttr;
322  attr->SetRenderer( new wxGridCellBoolRenderer() );
323  attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
324  g->SetColAttr( COL_ENABLED, attr );
325 
326  // all but COL_OPTIONS, which is edited with Option Editor anyways.
327  g->AutoSizeColumn( COL_NICKNAME, false );
328  g->AutoSizeColumn( COL_TYPE, false );
329  g->AutoSizeColumn( COL_URI, false );
330  g->AutoSizeColumn( COL_DESCR, false );
331 
332  // would set this to width of title, if it was easily known.
333  g->SetColSize( COL_OPTIONS, 80 );
334  }
335 
336  // select the last selected page
337  m_auinotebook->SetSelection( m_pageNdx );
339 
340  // for ALT+A handling, we want the initial focus to be on the first selected grid.
342 
343  // Configure button logos
344  m_append_button->SetBitmap( KiBitmap( small_plus_xpm ) );
345  m_browse_button->SetBitmap( KiBitmap( folder_xpm ) );
346  m_delete_button->SetBitmap( KiBitmap( trash_xpm ) );
347  m_move_up_button->SetBitmap( KiBitmap( small_up_xpm ) );
348  m_move_down_button->SetBitmap( KiBitmap( small_down_xpm ) );
349 
350  // Gives a selection to each grid, mainly for delete button. wxGrid's wake up with
351  // a currentCell which is sometimes not highlighted.
352  if( m_global_grid->GetNumberRows() > 0 )
353  m_global_grid->SelectRow( 0 );
354 
355  if( m_project_grid->GetNumberRows() > 0 )
356  m_project_grid->SelectRow( 0 );
357 }
358 
359 
361 {
362  // When the dialog is closed it will hide the current notebook page first, which will
363  // in turn select the other one. We then end up saving its index as the "current page".
364  // So flip them back again:
365  m_pageNdx = m_pageNdx == 1 ? 0 : 1;
366 
367  // Delete the GRID_TRICKS.
368  // Any additional event handlers should be popped before the window is deleted.
369  m_global_grid->PopEventHandler( true );
370  m_project_grid->PopEventHandler( true );
371  m_path_subs_grid->PopEventHandler( true );
372 }
373 
374 
376 {
377  for( FP_LIB_TABLE_GRID* model : { global_model(), project_model() } )
378  {
379  for( int r = 0; r < model->GetNumberRows(); )
380  {
381  wxString nick = model->GetValue( r, COL_NICKNAME ).Trim( false ).Trim();
382  wxString uri = model->GetValue( r, COL_URI ).Trim( false ).Trim();
383  unsigned illegalCh = 0;
384 
385  if( !nick || !uri )
386  {
387  // Delete the "empty" row, where empty means missing nick or uri.
388  // This also updates the UI which could be slow, but there should only be a few
389  // rows to delete, unless the user fell asleep on the Add Row
390  // button.
391  model->DeleteRows( r, 1 );
392  }
393  else if( ( illegalCh = LIB_ID::FindIllegalLibNicknameChar( nick, LIB_ID::ID_PCB ) ) )
394  {
395  wxString msg = wxString::Format( _( "Illegal character '%c' in Nickname: \"%s\"" ),
396  illegalCh,
397  nick );
398 
399  // show the tabbed panel holding the grid we have flunked:
400  if( model != cur_model() )
401  m_auinotebook->SetSelection( model == global_model() ? 0 : 1 );
402 
403  m_cur_grid->MakeCellVisible( r, 0 );
404  m_cur_grid->SetGridCursor( r, 1 );
405 
406  wxMessageDialog errdlg( this, msg, _( "No Colon in Nicknames" ) );
407  errdlg.ShowModal();
408  return false;
409  }
410  else
411  {
412  // set the trimmed values back into the table so they get saved to disk.
413  model->SetValue( r, COL_NICKNAME, nick );
414  model->SetValue( r, COL_URI, uri );
415  ++r; // this row was OK.
416  }
417  }
418  }
419 
420  // check for duplicate nickNames, separately in each table.
421  for( FP_LIB_TABLE_GRID* model : { global_model(), project_model() } )
422  {
423  for( int r1 = 0; r1 < model->GetNumberRows() - 1; ++r1 )
424  {
425  wxString nick1 = model->GetValue( r1, COL_NICKNAME );
426 
427  for( int r2 = r1 + 1; r2 < model->GetNumberRows(); ++r2 )
428  {
429  wxString nick2 = model->GetValue( r2, COL_NICKNAME );
430 
431  if( nick1 == nick2 )
432  {
433  wxString msg = wxString::Format( _( "Duplicate Nicknames \"%s\"." ), nick1 );
434 
435  // show the tabbed panel holding the grid we have flunked:
436  if( model != cur_model() )
437  m_auinotebook->SetSelection( model == global_model() ? 0 : 1 );
438 
439  // go to the lower of the two rows, it is technically the duplicate:
440  m_cur_grid->MakeCellVisible( r2, 0 );
441  m_cur_grid->SetGridCursor( r2, 1 );
442 
443  wxMessageDialog errdlg( this, msg, _( "Please Delete or Modify One" ) );
444  errdlg.ShowModal();
445  return false;
446  }
447  }
448  }
449  }
450 
451  return true;
452 }
453 
454 
455 //-----<event handlers>----------------------------------
456 
457 void PANEL_FP_LIB_TABLE::pageChangedHandler( wxAuiNotebookEvent& event )
458 {
459  m_pageNdx = (unsigned) std::max( 0, m_auinotebook->GetSelection() );
461 }
462 
463 
464 void PANEL_FP_LIB_TABLE::appendRowHandler( wxCommandEvent& event )
465 {
467  return;
468 
469  if( m_cur_grid->AppendRows( 1 ) )
470  {
471  int last_row = m_cur_grid->GetNumberRows() - 1;
472 
473  // wx documentation is wrong, SetGridCursor does not make visible.
474  m_cur_grid->MakeCellVisible( last_row, 0 );
475  m_cur_grid->SetGridCursor( last_row, 1 );
476  m_cur_grid->EnableCellEditControl( true );
477  m_cur_grid->ShowCellEditControl();
478  }
479 }
480 
481 
482 void PANEL_FP_LIB_TABLE::deleteRowHandler( wxCommandEvent& event )
483 {
485  return;
486 
487  int curRow = m_cur_grid->GetGridCursorRow();
488  int curCol = m_cur_grid->GetGridCursorCol();
489 
490  // In a wxGrid, collect rows that have a selected cell, or are selected
491  // is not so easy: it depend on the way the selection was made.
492  // Here, we collect row selected by clicking on a row label, and
493  // row that contain a cell previously selected.
494  // If no candidate, just delete the row with the grid cursor.
495  wxArrayInt selectedRows = m_cur_grid->GetSelectedRows();
496  wxGridCellCoordsArray cells = m_cur_grid->GetSelectedCells();
497  wxGridCellCoordsArray blockTopLeft = m_cur_grid->GetSelectionBlockTopLeft();
498  wxGridCellCoordsArray blockBotRight = m_cur_grid->GetSelectionBlockBottomRight();
499 
500  // Add all row having cell selected to list:
501  for( unsigned ii = 0; ii < cells.GetCount(); ii++ )
502  selectedRows.Add( cells[ii].GetRow() );
503 
504  // Handle block selection
505  if( !blockTopLeft.IsEmpty() && !blockBotRight.IsEmpty() )
506  {
507  for( int i = blockTopLeft[0].GetRow(); i <= blockBotRight[0].GetRow(); ++i )
508  selectedRows.Add( i );
509  }
510 
511  // Use the row having the grid cursor only if we have no candidate:
512  if( selectedRows.size() == 0 && m_cur_grid->GetGridCursorRow() >= 0 )
513  selectedRows.Add( m_cur_grid->GetGridCursorRow() );
514 
515  if( selectedRows.size() == 0 )
516  {
517  wxBell();
518  return;
519  }
520 
521  std::sort( selectedRows.begin(), selectedRows.end() );
522 
523  // Remove selected rows (note: a row can be stored more than once in list)
524  int last_row = -1;
525  for( int ii = selectedRows.GetCount()-1; ii >= 0; ii-- )
526  {
527  int row = selectedRows[ii];
528 
529  if( row != last_row )
530  {
531  last_row = row;
532  m_cur_grid->DeleteRows( row, 1 );
533  }
534  }
535 
536  m_cur_grid->SetGridCursor( std::min( curRow, m_cur_grid->GetNumberRows() - 1 ), curCol );
537 }
538 
539 
540 void PANEL_FP_LIB_TABLE::moveUpHandler( wxCommandEvent& event )
541 {
543  return;
544 
545  FP_LIB_TABLE_GRID* tbl = cur_model();
546  int curRow = m_cur_grid->GetGridCursorRow();
547 
548  // @todo: add multiple selection moves.
549  if( curRow >= 1 )
550  {
551  boost::ptr_vector< LIB_TABLE_ROW >::auto_type move_me =
552  tbl->rows.release( tbl->rows.begin() + curRow );
553 
554  --curRow;
555  tbl->rows.insert( tbl->rows.begin() + curRow, move_me.release() );
556 
557  if( tbl->GetView() )
558  {
559  // Update the wxGrid
560  wxGridTableMessage msg( tbl, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, curRow, 0 );
561  tbl->GetView()->ProcessTableMessage( msg );
562  }
563 
564  m_cur_grid->MakeCellVisible( curRow, m_cur_grid->GetGridCursorCol() );
565  m_cur_grid->SetGridCursor( curRow, m_cur_grid->GetGridCursorCol() );
566  }
567 }
568 
569 
570 void PANEL_FP_LIB_TABLE::moveDownHandler( wxCommandEvent& event )
571 {
573  return;
574 
575  FP_LIB_TABLE_GRID* tbl = cur_model();
576  int curRow = m_cur_grid->GetGridCursorRow();
577 
578  // @todo: add multiple selection moves.
579  if( unsigned( curRow + 1 ) < tbl->rows.size() )
580  {
581  boost::ptr_vector< LIB_TABLE_ROW >::auto_type move_me =
582  tbl->rows.release( tbl->rows.begin() + curRow );
583 
584  ++curRow;
585  tbl->rows.insert( tbl->rows.begin() + curRow, move_me.release() );
586 
587  if( tbl->GetView() )
588  {
589  // Update the wxGrid
590  wxGridTableMessage msg( tbl, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, curRow - 1, 0 );
591  tbl->GetView()->ProcessTableMessage( msg );
592  }
593 
594  m_cur_grid->MakeCellVisible( curRow, m_cur_grid->GetGridCursorCol() );
595  m_cur_grid->SetGridCursor( curRow, m_cur_grid->GetGridCursorCol() );
596  }
597 }
598 
599 
600 void PANEL_FP_LIB_TABLE::browseLibrariesHandler( wxCommandEvent& event )
601 {
603  return;
604 
605  if( m_lastBrowseDir.IsEmpty() )
607 
608  DIALOG_FILE_DIR_PICKER dlg( this, _( "Select Library" ), m_lastBrowseDir,
610 
611  auto result = dlg.ShowModal();
612 
613  if( result == wxID_CANCEL )
614  return;
615 
617 
618  // Drop the last directory if the path is a .pretty folder
620  m_lastBrowseDir = m_lastBrowseDir.BeforeLast( wxFileName::GetPathSeparator() );
621 
622  const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables();
623  bool addDuplicates = false;
624  bool applyToAll = false;
625  wxString warning = _( "Warning: Duplicate Nickname" );
626  wxString msg = _( "A library nicknamed \"%s\" already exists." );
627  wxArrayString files;
628  dlg.GetFilenames( files );
629 
630  for( const auto& filePath : files )
631  {
632  wxFileName fn( filePath );
633  wxString nickname = LIB_ID::FixIllegalChars( fn.GetName(), LIB_ID::ID_PCB );
634  bool doAdd = true;
635 
636  if( cur_model()->ContainsNickname( nickname ) )
637  {
638  if( !applyToAll )
639  {
640  int ret = YesOrCancelDialog( this, warning, wxString::Format( msg, nickname ),
641  _( "Skip" ), _( "Add Anyway" ), &applyToAll );
642  addDuplicates = (ret == wxID_CANCEL );
643  }
644 
645  doAdd = addDuplicates;
646  }
647 
648  if( doAdd && m_cur_grid->AppendRows( 1 ) )
649  {
650  int last_row = m_cur_grid->GetNumberRows() - 1;
651 
652  m_cur_grid->SetCellValue( last_row, COL_NICKNAME, nickname );
653 
654  auto type = IO_MGR::GuessPluginTypeFromLibPath( filePath );
655  m_cur_grid->SetCellValue( last_row, COL_TYPE, IO_MGR::ShowType( type ) );
656 
657  // try to use path normalized to an environmental variable or project path
658  wxString path = NormalizePath( filePath, &envVars, m_projectBasePath );
659 
660  if( path.IsEmpty() )
661  path = fn.GetFullPath();
662 
663  m_cur_grid->SetCellValue( last_row, COL_URI, path );
664  }
665  }
666 
667  if( !files.IsEmpty() )
668  {
669  int new_row = m_cur_grid->GetNumberRows() - 1;
670  m_cur_grid->MakeCellVisible( new_row, m_cur_grid->GetGridCursorCol() );
671  m_cur_grid->SetGridCursor( new_row, m_cur_grid->GetGridCursorCol() );
672  }
673 }
674 
676 {
677  // Account for scroll bars
678  aWidth -= ( m_path_subs_grid->GetSize().x - m_path_subs_grid->GetClientSize().x );
679 
680  m_path_subs_grid->AutoSizeColumn( 0 );
681  m_path_subs_grid->SetColSize( 1, aWidth - m_path_subs_grid->GetColSize( 0 ) );
682 }
683 
684 
685 void PANEL_FP_LIB_TABLE::onSizeGrid( wxSizeEvent& event )
686 {
687  adjustPathSubsGridColumns( event.GetSize().GetX() );
688 
689  event.Skip();
690 }
691 
692 
694 {
696  return false;
697 
698  if( verifyTables() )
699  {
700  if( *global_model() != *m_global )
701  {
703 
704  m_global->Clear();
705  m_global->rows.transfer( m_global->rows.end(), global_model()->rows.begin(),
706  global_model()->rows.end(), global_model()->rows );
707  m_global->reindex();
708  }
709 
710  if( *project_model() != *m_project )
711  {
713 
714  m_project->Clear();
715  m_project->rows.transfer( m_project->rows.end(), project_model()->rows.begin(),
716  project_model()->rows.end(), project_model()->rows );
717  m_project->reindex();
718  }
719 
720  return true;
721  }
722 
723  return false;
724 }
725 
726 
730 {
731  wxRegEx re( ".*?(\\$\\{(.+?)\\})|(\\$\\((.+?)\\)).*?", wxRE_ADVANCED );
732  wxASSERT( re.IsValid() ); // wxRE_ADVANCED is required.
733 
734  std::set< wxString > unique;
735 
736  // clear the table
737  m_path_subs_grid->DeleteRows( 0, m_path_subs_grid->GetNumberRows() );
738 
739  for( FP_LIB_TABLE_GRID* tbl : { global_model(), project_model() } )
740  {
741  for( int row = 0; row < tbl->GetNumberRows(); ++row )
742  {
743  wxString uri = tbl->GetValue( row, COL_URI );
744 
745  while( re.Matches( uri ) )
746  {
747  wxString envvar = re.GetMatch( uri, 2 );
748 
749  // if not ${...} form then must be $(...)
750  if( envvar.IsEmpty() )
751  envvar = re.GetMatch( uri, 4 );
752 
753  // ignore duplicates
754  unique.insert( envvar );
755 
756  // delete the last match and search again
757  uri.Replace( re.GetMatch( uri, 0 ), wxEmptyString );
758  }
759  }
760  }
761 
762  // Make sure this special environment variable shows up even if it was
763  // not used yet. It is automatically set by KiCad to the directory holding
764  // the current project.
765  unique.insert( PROJECT_VAR_NAME );
766  unique.insert( FP_LIB_TABLE::GlobalPathEnvVariableName() );
767  // This special environment variable is used to locate 3d shapes
768  unique.insert( KISYS3DMOD );
769 
770  for( wxString evName : unique )
771  {
772  int row = m_path_subs_grid->GetNumberRows();
773  m_path_subs_grid->AppendRows( 1 );
774 
775  m_path_subs_grid->SetCellValue( row, 0, wxT( "${" ) + evName + wxT( "}" ) );
776  m_path_subs_grid->SetCellEditor( row, 0, new GRID_CELL_READONLY_TEXT_EDITOR() );
777 
778  wxString evValue;
779  wxGetEnv( evName, &evValue );
780  m_path_subs_grid->SetCellValue( row, 1, evValue );
781  m_path_subs_grid->SetCellEditor( row, 1, new GRID_CELL_READONLY_TEXT_EDITOR() );
782  }
783 
784  // No combobox editors here, but it looks better if its consistent with the other
785  // grids in the dialog.
786  m_path_subs_grid->SetDefaultRowSize( m_path_subs_grid->GetDefaultRowSize() + 2 );
787 
788  adjustPathSubsGridColumns( m_path_subs_grid->GetRect().GetWidth() );
789 }
790 
791 //-----</event handlers>---------------------------------
792 
793 
794 
796 
798 
799 
800 void InvokePcbLibTableEditor( KIWAY* aKiway, wxWindow* aCaller )
801 {
802  FP_LIB_TABLE* globalTable = &GFootprintTable;
803  wxString globalTablePath = FP_LIB_TABLE::GetGlobalTableFileName();
804  FP_LIB_TABLE* projectTable = aKiway->Prj().PcbFootprintLibs();
805  wxString projectTablePath = aKiway->Prj().FootprintLibTblName();
806  wxString msg;
807 
808  DIALOG_EDIT_LIBRARY_TABLES dlg( aCaller, _( "Footprint Libraries" ) );
809  dlg.SetKiway( &dlg, aKiway );
810 
811  dlg.InstallPanel( new PANEL_FP_LIB_TABLE( &dlg, globalTable, globalTablePath,
812  projectTable, projectTablePath,
813  aKiway->Prj().GetProjectPath() ) );
814 
815  if( dlg.ShowModal() == wxID_CANCEL )
816  return;
817 
818  if( dlg.m_GlobalTableChanged )
819  {
820  try
821  {
822  globalTable->Save( globalTablePath );
823  }
824  catch( const IO_ERROR& ioe )
825  {
826  msg.Printf( _( "Error saving global library table:\n\n%s" ), ioe.What() );
827  wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
828  }
829  }
830 
831  if( dlg.m_ProjectTableChanged )
832  {
833  try
834  {
835  projectTable->Save( projectTablePath );
836  }
837  catch( const IO_ERROR& ioe )
838  {
839  msg.Printf( _( "Error saving project-specific library table:\n\n%s" ), ioe.What() );
840  wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
841  }
842  }
843 
844  auto editor = (FOOTPRINT_EDIT_FRAME*) aKiway->Player( FRAME_PCB_MODULE_EDITOR, false );
845 
846  if( editor )
847  editor->SyncLibraryTree( true );
848 
849  auto viewer = (FOOTPRINT_VIEWER_FRAME*) aKiway->Player( FRAME_PCB_MODULE_VIEWER, false );
850 
851  if( viewer )
852  viewer->ReCreateLibraryList();
853 }
int GetCount()
void SetKiway(wxWindow *aDest, KIWAY *aKiway)
Function SetKiway.
FP_LIB_TABLE_GRID * global_model() const
void push_back(LIB_TABLE_ROW *aRow) override
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_GRID * cur_model() const
FP_LIB_TABLE * m_project
bool ContainsNickname(const wxString &aNickname)
#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 cut, copy, and paste to an otherwise unmodied wxGrid instance...
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
const wxString & GetOptions() const
Return the options string, which may hold a password or anything else needed to instantiate the under...
#define MYID_OPTIONS_EDITOR
LIB_TABLE_ROW * At(int aIndex)
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:226
void SetTable(wxGridTableBase *table, bool aTakeOwnership=false)
Hide wxGrid&#39;s SetTable() method with one which doesn&#39;t mess up the grid column widths when setting th...
Definition: wx_grid.cpp:46
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
Definition of class FOOTPRINT_EDIT_FRAME.
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:115
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 GFootprintTable
The global footprint library table.
Definition: pcbnew.cpp:333
FP_LIB_TABLE_GRID * project_model() const
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...
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
static const wxString GlobalPathEnvVariableName()
Function GlobalPathEnvVarVariableName.
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 const ENV_VAR_MAP & GetLocalEnvVariables() const
Definition: pgm_base.h:312
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:300
static constexpr int FILTER_COUNT
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
Definition: pgm_base.h:129
static wxString getFilterString()
DIALOG_EDIT_LIBRARY_TABLES * m_dialog
VTBL_ENTRY PROJECT & Prj() const
Function Prj returns the PROJECT associated with this KIWAY.
Definition: kiway.cpp:142
#define KISYS3DMOD
A variable name whose value holds the path of 3D shape files.
Definition: 3d_viewer.h:38
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.
The common library.
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:258
void deleteRowHandler(wxCommandEvent &event) override
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:156
bool AppendRows(size_t aNumRows=1) override
void optionsEditor(int aRow)
VTBL_ENTRY const wxString FootprintLibTblName() const
Function FootprintLibTblName returns the path and filename of this project&#39;s fp-lib-table, i.e.
Definition: project.cpp:120
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
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
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
virtual void doPopupSelection(wxCommandEvent &event)
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
FP_GRID_TRICKS(DIALOG_EDIT_LIBRARY_TABLES *aParent, wxGrid *aGrid)
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
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 &#39;_&#39;.
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:52
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:245
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
Read only http://github.com repo holding pretty footprints.
Definition: io_mgr.h:59
void doPopupSelection(wxCommandEvent &event) override
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 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:55