KiCad PCB EDA Suite
panel_sym_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) 2017 Wayne Stambaugh <stambaughw@gmail.com>
5  * Copyright (C) 2017-2018 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <set>
22 #include <wx/regex.h>
23 
24 #include <fctsys.h>
25 #include <project.h>
26 #include <panel_sym_lib_table.h>
27 #include <lib_id.h>
28 #include <symbol_lib_table.h>
29 #include <lib_table_lexer.h>
30 #include <grid_tricks.h>
31 #include <widgets/wx_grid.h>
32 #include <confirm.h>
33 #include <bitmaps.h>
34 #include <lib_table_grid.h>
36 #include <env_paths.h>
37 #include <lib_edit_frame.h>
38 #include <viewlib_frame.h>
39 #include <kiway.h>
42 
47 {
48  friend class SYMBOL_GRID_TRICKS;
49 
50 protected:
51  LIB_TABLE_ROW* at( size_t aIndex ) override { return &rows.at( aIndex ); }
52 
53  size_t size() const override { return rows.size(); }
54 
56  {
57  return dynamic_cast< LIB_TABLE_ROW* >( new SYMBOL_LIB_TABLE_ROW );
58  }
59 
60  LIB_TABLE_ROWS_ITER begin() override { return rows.begin(); }
61 
63  {
64  return rows.insert( aIterator, aRow );
65  }
66 
67  void push_back( LIB_TABLE_ROW* aRow ) override { rows.push_back( aRow ); }
68 
70  {
71  return rows.erase( aFirst, aLast );
72  }
73 
74 public:
75 
76  SYMBOL_LIB_TABLE_GRID( const SYMBOL_LIB_TABLE& aTableToEdit )
77  {
78  rows = aTableToEdit.rows;
79  }
80 };
81 
82 
84 {
85 public:
86  SYMBOL_GRID_TRICKS( DIALOG_EDIT_LIBRARY_TABLES* aParent, wxGrid* aGrid ) :
87  GRID_TRICKS( aGrid ),
88  m_dialog( aParent )
89  {
90  }
91 
92 protected:
94 
97  virtual void paste_text( const wxString& cb_text ) override
98  {
99  SYMBOL_LIB_TABLE_GRID* tbl = (SYMBOL_LIB_TABLE_GRID*) m_grid->GetTable();
100  size_t ndx = cb_text.find( "(sym_lib_table" );
101 
102  if( ndx != std::string::npos )
103  {
104  // paste the SYMBOL_LIB_TABLE_ROWs of s-expression (sym_lib_table), starting
105  // at column 0 regardless of current cursor column.
106 
107  STRING_LINE_READER slr( TO_UTF8( cb_text ), "Clipboard" );
108  LIB_TABLE_LEXER lexer( &slr );
109  SYMBOL_LIB_TABLE tmp_tbl;
110  bool parsed = true;
111 
112  try
113  {
114  tmp_tbl.Parse( &lexer );
115  }
116  catch( PARSE_ERROR& pe )
117  {
118  DisplayError( m_dialog, pe.What() );
119  parsed = false;
120  }
121 
122  if( parsed )
123  {
124  // make sure the table is big enough...
125  if( tmp_tbl.GetCount() > tbl->GetNumberRows() )
126  tbl->AppendRows( tmp_tbl.GetCount() - tbl->GetNumberRows() );
127 
128  for( int i = 0; i < tmp_tbl.GetCount(); ++i )
129  tbl->rows.replace( i, tmp_tbl.At( i ) );
130  }
131 
132  m_grid->AutoSizeColumns( false );
133  }
134  else
135  {
136  // paste spreadsheet formatted text.
137  GRID_TRICKS::paste_text( cb_text );
138 
139  m_grid->AutoSizeColumns( false );
140  }
141  }
142 };
143 
144 
146  SYMBOL_LIB_TABLE* aGlobal,
147  const wxString& aGlobalTablePath,
148  SYMBOL_LIB_TABLE* aProject,
149  const wxString& aProjectTablePath,
150  const wxString& aProjectBasePath ) :
151  PANEL_SYM_LIB_TABLE_BASE( aParent ),
152  m_globalTable( aGlobal ),
153  m_projectTable( aProject ),
154  m_projectBasePath( aProjectBasePath ),
155  m_parent( aParent ),
156  m_lastBrowseDir( aProjectBasePath )
157 {
158  // For user info, shows the table filenames:
159  m_GblTableFilename->SetLabel( aGlobalTablePath );
160  m_PrjTableFilename->SetLabel( aProjectTablePath );
161 
162  // wxGrid only supports user owned tables if they exist past end of ~wxGrid(),
163  // so make it a grid owned table.
164  m_global_grid->SetTable( new SYMBOL_LIB_TABLE_GRID( *aGlobal ), true );
165  m_project_grid->SetTable( new SYMBOL_LIB_TABLE_GRID( *aProject ), true );
166 
167  // Give a bit more room for combobox editors
168  m_global_grid->SetDefaultRowSize( m_global_grid->GetDefaultRowSize() + 4 );
169  m_project_grid->SetDefaultRowSize( m_project_grid->GetDefaultRowSize() + 4 );
170 
171  // add Cut, Copy, and Paste to wxGrids
172  m_global_grid->PushEventHandler( new SYMBOL_GRID_TRICKS( m_parent, m_global_grid ) );
173  m_project_grid->PushEventHandler( new SYMBOL_GRID_TRICKS( m_parent, m_project_grid ) );
174  m_path_subs_grid->PushEventHandler( new GRID_TRICKS( m_path_subs_grid ) );
175 
176  m_global_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
177  m_project_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
178 
179  m_global_grid->AutoSizeColumns( false );
180  m_project_grid->AutoSizeColumns( false );
181 
182  wxArrayString pluginChoices;
183 
184 // pluginChoices.Add( SCH_IO_MGR::ShowType( SCH_IO_MGR::SCH_KICAD ) );
185  pluginChoices.Add( SCH_IO_MGR::ShowType( SCH_IO_MGR::SCH_LEGACY ) );
186 
188 
189  for( wxGrid* g : { m_global_grid, m_project_grid } )
190  {
191  // Set special attributes
192  wxGridCellAttr* attr;
193 
194  attr = new wxGridCellAttr;
195  attr->SetEditor( new GRID_CELL_PATH_EDITOR( m_parent, &m_lastBrowseDir ) );
196  g->SetColAttr( COL_URI, attr );
197 
198  attr = new wxGridCellAttr;
199  attr->SetEditor( new wxGridCellChoiceEditor( pluginChoices ) );
200  g->SetColAttr( COL_TYPE, attr );
201 
202  attr = new wxGridCellAttr;
203  attr->SetRenderer( new wxGridCellBoolRenderer() );
204  attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
205  g->SetColAttr( COL_ENABLED, attr );
206 
207  // all but COL_OPTIONS, which is edited with Option Editor anyways.
208  g->AutoSizeColumn( COL_NICKNAME, false );
209  g->AutoSizeColumn( COL_TYPE, false );
210  g->AutoSizeColumn( COL_URI, false );
211  g->AutoSizeColumn( COL_DESCR, false );
212  g->AutoSizeColumn( COL_ENABLED, false );
213 
214  // would set this to width of title, if it was easily known.
215  g->SetColSize( COL_OPTIONS, 80 );
216  }
217 
218  // select the last selected page
219  m_auinotebook->SetSelection( m_pageNdx );
221 
222  // for ALT+A handling, we want the initial focus to be on the first selected grid.
224 
225  // Configure button logos
226  m_append_button->SetBitmap( KiBitmap( small_plus_xpm ) );
227  m_delete_button->SetBitmap( KiBitmap( trash_xpm ) );
228  m_move_up_button->SetBitmap( KiBitmap( small_up_xpm ) );
229  m_move_down_button->SetBitmap( KiBitmap( small_down_xpm ) );
230  m_browse_button->SetBitmap( KiBitmap( folder_xpm ) );
231 
232  // Gives a selection to each grid, mainly for delete button. wxGrid's wake up with
233  // a currentCell which is sometimes not highlighted.
234  if( m_global_grid->GetNumberRows() > 0 )
235  m_global_grid->SelectRow( 0 );
236 
237  if( m_project_grid->GetNumberRows() > 0 )
238  m_project_grid->SelectRow( 0 );
239 }
240 
241 
243 {
244  // When the dialog is closed it will hide the current notebook page first, which will
245  // in turn select the other one. We then end up saving its index as the "current page".
246  // So flip them back again:
247  m_pageNdx = m_pageNdx == 1 ? 0 : 1;
248 
249  // Delete the GRID_TRICKS.
250  // Any additional event handlers should be popped before the window is deleted.
251  m_global_grid->PopEventHandler( true );
252  m_project_grid->PopEventHandler( true );
253  m_path_subs_grid->PopEventHandler( true );
254 }
255 
256 
258 {
259  for( SYMBOL_LIB_TABLE_GRID* model : { global_model(), project_model() } )
260  {
261  for( int r = 0; r < model->GetNumberRows(); )
262  {
263  wxString nick = model->GetValue( r, COL_NICKNAME ).Trim( false ).Trim();
264  wxString uri = model->GetValue( r, COL_URI ).Trim( false ).Trim();
265  unsigned illegalCh = 0;
266 
267  if( !nick || !uri )
268  {
269  // Delete the "empty" row, where empty means missing nick or uri.
270  // This also updates the UI which could be slow, but there should only be a few
271  // rows to delete, unless the user fell asleep on the Add Row
272  // button.
273  model->DeleteRows( r, 1 );
274  }
275  else if( ( illegalCh = LIB_ID::FindIllegalLibNicknameChar( nick, LIB_ID::ID_SCH ) ) )
276  {
277  wxString msg = wxString::Format( _( "Illegal character '%c' in Nickname: \"%s\"" ),
278  illegalCh,
279  nick );
280 
281  // show the tabbed panel holding the grid we have flunked:
282  if( model != cur_model() )
283  m_auinotebook->SetSelection( model == global_model() ? 0 : 1 );
284 
285  m_cur_grid->MakeCellVisible( r, 0 );
286  m_cur_grid->SetGridCursor( r, 1 );
287 
288  wxMessageDialog errdlg( this, msg, _( "No Colon in Nicknames" ) );
289  errdlg.ShowModal();
290  return false;
291  }
292  else
293  {
294  // set the trimmed values back into the table so they get saved to disk.
295  model->SetValue( r, COL_NICKNAME, nick );
296  model->SetValue( r, COL_URI, uri );
297  ++r; // this row was OK.
298  }
299  }
300  }
301 
302  // check for duplicate nickNames, separately in each table.
303  for( SYMBOL_LIB_TABLE_GRID* model : { global_model(), project_model() } )
304  {
305  for( int r1 = 0; r1 < model->GetNumberRows() - 1; ++r1 )
306  {
307  wxString nick1 = model->GetValue( r1, COL_NICKNAME );
308 
309  for( int r2=r1+1; r2 < model->GetNumberRows(); ++r2 )
310  {
311  wxString nick2 = model->GetValue( r2, COL_NICKNAME );
312 
313  if( nick1 == nick2 )
314  {
315  wxString msg = wxString::Format( _( "Duplicate Nickname: \"%s\"." ), nick1 );
316 
317  // show the tabbed panel holding the grid we have flunked:
318  if( model != cur_model() )
319  m_auinotebook->SetSelection( model == global_model() ? 0 : 1 );
320 
321  // go to the lower of the two rows, it is technically the duplicate:
322  m_cur_grid->MakeCellVisible( r2, 0 );
323  m_cur_grid->SetGridCursor( r2, 1 );
324 
325  wxMessageDialog errdlg( this, msg, _( "Please Delete or Modify One" ) );
326  errdlg.ShowModal();
327 
328  return false;
329  }
330  }
331  }
332  }
333 
334  return true;
335 }
336 
337 
338 void PANEL_SYM_LIB_TABLE::pageChangedHandler( wxAuiNotebookEvent& event )
339 {
340  m_pageNdx = (unsigned) std::max( 0, m_auinotebook->GetSelection() );
342 }
343 
344 
345 void PANEL_SYM_LIB_TABLE::browseLibrariesHandler( wxCommandEvent& event )
346 {
347  wxFileDialog dlg( this, _( "Select Library" ), m_lastBrowseDir,
348  wxEmptyString, SchematicLibraryFileWildcard(),
349  wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE );
350 
351  auto result = dlg.ShowModal();
352 
353  if( result == wxID_CANCEL )
354  return;
355 
356  m_lastBrowseDir = dlg.GetDirectory();
357 
358  const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables();
359  bool addDuplicates = false;
360  bool applyToAll = false;
361  wxString warning = _( "Warning: Duplicate Nickname" );
362  wxString msg = _( "A library nicknamed \"%s\" already exists." );
363  wxArrayString files;
364  dlg.GetFilenames( files );
365 
366  for( const auto& file : files )
367  {
368  wxString filePath = dlg.GetDirectory() + wxFileName::GetPathSeparator() + file;
369  wxFileName fn( filePath );
370  wxString nickname = LIB_ID::FixIllegalChars( fn.GetName(), LIB_ID::ID_SCH );
371  bool doAdd = true;
372 
373  if( cur_model()->ContainsNickname( nickname ) )
374  {
375  if( !applyToAll )
376  {
377  int ret = YesOrCancelDialog( this, warning, wxString::Format( msg, nickname ),
378  _( "Skip" ), _( "Add Anyway" ), &applyToAll );
379  addDuplicates = (ret == wxID_CANCEL );
380  }
381 
382  doAdd = addDuplicates;
383  }
384 
385  if( doAdd && m_cur_grid->AppendRows( 1 ) )
386  {
387  int last_row = m_cur_grid->GetNumberRows() - 1;
388 
389  m_cur_grid->SetCellValue( last_row, COL_NICKNAME, nickname );
390 
391  // TODO the following code can detect only schematic types, not libs
392  // SCH_IO_MGR needs to provide file extension information for libraries too
393 
394  // auto detect the plugin type
395  /*for( auto pluginType : SCH_IO_MGR::SCH_FILE_T_vector )
396  {
397  if( SCH_IO_MGR::GetFileExtension( pluginType ).Lower() == fn.GetExt().Lower() )
398  {
399  m_cur_grid->SetCellValue( last_row, COL_TYPE,
400  SCH_IO_MGR::ShowType( pluginType ) );
401  break;
402  }
403  }*/
404  m_cur_grid->SetCellValue( last_row, COL_TYPE,
405  SCH_IO_MGR::ShowType( SCH_IO_MGR::SCH_LEGACY ) );
406 
407  // try to use path normalized to an environmental variable or project path
408  wxString path = NormalizePath( filePath, &envVars, m_projectBasePath );
409 
410  if( path.IsEmpty() )
411  path = fn.GetFullPath();
412 
413  m_cur_grid->SetCellValue( last_row, COL_URI, path );
414  }
415  }
416 
417  if( !files.IsEmpty() )
418  {
419  m_cur_grid->MakeCellVisible( m_cur_grid->GetNumberRows() - 1, 0 );
420  m_cur_grid->SetGridCursor( m_cur_grid->GetNumberRows() - 1, 1 );
421  }
422 }
423 
424 
425 void PANEL_SYM_LIB_TABLE::appendRowHandler( wxCommandEvent& event )
426 {
428  return;
429 
430  if( m_cur_grid->AppendRows( 1 ) )
431  {
432  int row = m_cur_grid->GetNumberRows() - 1;
433  // Gives a default type (currently, only one type exists):
434  m_cur_grid->SetCellValue( row, COL_TYPE, SCH_IO_MGR::ShowType( SCH_IO_MGR::SCH_LEGACY ) );
435 
436  // wx documentation is wrong, SetGridCursor does not make visible.
437  m_cur_grid->MakeCellVisible( row, 0 );
438  m_cur_grid->SetGridCursor( row, 1 );
439 
440  m_cur_grid->EnableCellEditControl( true );
441  m_cur_grid->ShowCellEditControl();
442  }
443 }
444 
445 
446 void PANEL_SYM_LIB_TABLE::deleteRowHandler( wxCommandEvent& event )
447 {
449  return;
450 
451  int curRow = m_cur_grid->GetGridCursorRow();
452  int curCol = m_cur_grid->GetGridCursorCol();
453 
454  // In a wxGrid, collect rows that have a selected cell, or are selected
455  // is not so easy: it depend on the way the selection was made.
456  // Here, we collect row selected by clicking on a row label, and
457  // row that contain a cell previously selected.
458  // If no candidate, just delete the row with the grid cursor.
459  wxArrayInt selectedRows = m_cur_grid->GetSelectedRows();
460  wxGridCellCoordsArray cells = m_cur_grid->GetSelectedCells();
461  wxGridCellCoordsArray blockTopLeft = m_cur_grid->GetSelectionBlockTopLeft();
462  wxGridCellCoordsArray blockBotRight = m_cur_grid->GetSelectionBlockBottomRight();
463 
464  // Add all row having cell selected to list:
465  for( unsigned ii = 0; ii < cells.GetCount(); ii++ )
466  selectedRows.Add( cells[ii].GetRow() );
467 
468  // Handle block selection
469  if( !blockTopLeft.IsEmpty() && !blockBotRight.IsEmpty() )
470  {
471  for( int i = blockTopLeft[0].GetRow(); i <= blockBotRight[0].GetRow(); ++i )
472  selectedRows.Add( i );
473  }
474 
475  // Use the row having the grid cursor only if we have no candidate:
476  if( selectedRows.size() == 0 && m_cur_grid->GetGridCursorRow() >= 0 )
477  selectedRows.Add( m_cur_grid->GetGridCursorRow() );
478 
479  if( selectedRows.size() == 0 )
480  {
481  wxBell();
482  return;
483  }
484 
485  std::sort( selectedRows.begin(), selectedRows.end() );
486 
487  // Remove selected rows (note: a row can be stored more than once in list)
488  int last_row = -1;
489 
490  for( int ii = selectedRows.GetCount()-1; ii >= 0; ii-- )
491  {
492  int row = selectedRows[ii];
493 
494  if( row != last_row )
495  {
496  last_row = row;
497  m_cur_grid->DeleteRows( row, 1 );
498  }
499  }
500 
501  m_cur_grid->SetGridCursor( std::min( curRow, m_cur_grid->GetNumberRows() - 1 ), curCol );
502 }
503 
504 
505 void PANEL_SYM_LIB_TABLE::moveUpHandler( wxCommandEvent& event )
506 {
508  return;
509 
511  int curRow = m_cur_grid->GetGridCursorRow();
512 
513  // @todo: add multiple selection moves.
514  if( curRow >= 1 )
515  {
516  boost::ptr_vector< LIB_TABLE_ROW >::auto_type move_me =
517  tbl->rows.release( tbl->rows.begin() + curRow );
518 
519  --curRow;
520  tbl->rows.insert( tbl->rows.begin() + curRow, move_me.release() );
521 
522  if( tbl->GetView() )
523  {
524  // Update the wxGrid
525  wxGridTableMessage msg( tbl, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, curRow, 0 );
526  tbl->GetView()->ProcessTableMessage( msg );
527  }
528 
529  m_cur_grid->MakeCellVisible( curRow, m_cur_grid->GetGridCursorCol() );
530  m_cur_grid->SetGridCursor( curRow, m_cur_grid->GetGridCursorCol() );
531  }
532 }
533 
534 
535 void PANEL_SYM_LIB_TABLE::moveDownHandler( wxCommandEvent& event )
536 {
538  return;
539 
541  int curRow = m_cur_grid->GetGridCursorRow();
542 
543  // @todo: add multiple selection moves.
544  if( unsigned( curRow + 1 ) < tbl->rows.size() )
545  {
546  boost::ptr_vector< LIB_TABLE_ROW >::auto_type move_me =
547  tbl->rows.release( tbl->rows.begin() + curRow );
548 
549  ++curRow;
550  tbl->rows.insert( tbl->rows.begin() + curRow, move_me.release() );
551 
552  if( tbl->GetView() )
553  {
554  // Update the wxGrid
555  wxGridTableMessage msg( tbl, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, curRow - 1, 0 );
556  tbl->GetView()->ProcessTableMessage( msg );
557  }
558 
559  m_cur_grid->MakeCellVisible( curRow, m_cur_grid->GetGridCursorCol() );
560  m_cur_grid->SetGridCursor( curRow, m_cur_grid->GetGridCursorCol() );
561  }
562 }
563 
564 
566 {
568  return false;
569 
570  if( !verifyTables() )
571  return false;
572 
573  if( *global_model() != *m_globalTable )
574  {
576 
577  m_globalTable->Clear();
578  m_globalTable->rows.transfer( m_globalTable->rows.end(), global_model()->rows.begin(),
579  global_model()->rows.end(), global_model()->rows );
581  }
582 
583  if( *project_model() != *m_projectTable )
584  {
586 
588  m_projectTable->rows.transfer( m_projectTable->rows.end(), project_model()->rows.begin(),
589  project_model()->rows.end(), project_model()->rows );
591  }
592 
593  return true;
594 }
595 
596 
598 {
599  wxRegEx re( ".*?(\\$\\{(.+?)\\})|(\\$\\((.+?)\\)).*?", wxRE_ADVANCED );
600  wxASSERT( re.IsValid() ); // wxRE_ADVANCED is required.
601 
602  std::set< wxString > unique;
603 
604  // clear the table
605  m_path_subs_grid->DeleteRows( 0, m_path_subs_grid->GetNumberRows() );
606 
607  for( SYMBOL_LIB_TABLE_GRID* tbl : { global_model(), project_model() } )
608  {
609  for( int row = 0; row < tbl->GetNumberRows(); ++row )
610  {
611  wxString uri = tbl->GetValue( row, COL_URI );
612 
613  while( re.Matches( uri ) )
614  {
615  wxString envvar = re.GetMatch( uri, 2 );
616 
617  // if not ${...} form then must be $(...)
618  if( envvar.IsEmpty() )
619  envvar = re.GetMatch( uri, 4 );
620 
621  // ignore duplicates
622  unique.insert( envvar );
623 
624  // delete the last match and search again
625  uri.Replace( re.GetMatch( uri, 0 ), wxEmptyString );
626  }
627  }
628  }
629 
630  // Make sure this special environment variable shows up even if it was
631  // not used yet. It is automatically set by KiCad to the directory holding
632  // the current project.
633  unique.insert( PROJECT_VAR_NAME );
635 
636  for( wxString evName : unique )
637  {
638  int row = m_path_subs_grid->GetNumberRows();
639  m_path_subs_grid->AppendRows( 1 );
640 
641  m_path_subs_grid->SetCellValue( row, 0, wxT( "${" ) + evName + wxT( "}" ) );
642  m_path_subs_grid->SetCellEditor( row, 0, new GRID_CELL_READONLY_TEXT_EDITOR() );
643 
644  wxString evValue;
645  wxGetEnv( evName, &evValue );
646  m_path_subs_grid->SetCellValue( row, 1, evValue );
647  m_path_subs_grid->SetCellEditor( row, 1, new GRID_CELL_READONLY_TEXT_EDITOR() );
648  }
649 
650  // No combobox editors here, but it looks better if its consistent with the other
651  // grids in the dialog.
652  m_path_subs_grid->SetDefaultRowSize( m_path_subs_grid->GetDefaultRowSize() + 2 );
653 
654  adjustPathSubsGridColumns( m_path_subs_grid->GetRect().GetWidth() );
655 }
656 
657 
659 {
660  // Account for scroll bars
661  aWidth -= ( m_path_subs_grid->GetSize().x - m_path_subs_grid->GetClientSize().x );
662 
663  m_path_subs_grid->AutoSizeColumn( 0 );
664  m_path_subs_grid->SetColSize( 1, aWidth - m_path_subs_grid->GetColSize( 0 ) );
665 }
666 
667 
668 void PANEL_SYM_LIB_TABLE::onSizeGrid( wxSizeEvent& event )
669 {
670  adjustPathSubsGridColumns( event.GetSize().GetX() );
671 
672  event.Skip();
673 }
674 
675 
677 {
678  return (SYMBOL_LIB_TABLE_GRID*) m_global_grid->GetTable();
679 }
680 
681 
683 {
684  return (SYMBOL_LIB_TABLE_GRID*) m_project_grid->GetTable();
685 }
686 
687 
689 {
690  return (SYMBOL_LIB_TABLE_GRID*) m_cur_grid->GetTable();
691 }
692 
693 
695 
696 
697 void InvokeSchEditSymbolLibTable( KIWAY* aKiway, wxWindow *aParent )
698 {
700  wxString globalTablePath = SYMBOL_LIB_TABLE::GetGlobalTableFileName();
701  SYMBOL_LIB_TABLE* projectTable = aKiway->Prj().SchSymbolLibTable();
702  wxString projectPath = aKiway->Prj().GetProjectPath();
703  wxFileName projectTableFn( projectPath, SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
704  wxString msg;
705 
706  DIALOG_EDIT_LIBRARY_TABLES dlg( aParent, _( "Symbol Libraries" ) );
707 
708  dlg.InstallPanel( new PANEL_SYM_LIB_TABLE( &dlg, globalTable, globalTablePath,
709  projectTable, projectTableFn.GetFullPath(),
710  aKiway->Prj().GetProjectPath() ) );
711 
712  if( dlg.ShowModal() == wxID_CANCEL )
713  return;
714 
715  if( dlg.m_GlobalTableChanged )
716  {
717  try
718  {
719  globalTable->Save( globalTablePath );
720  }
721  catch( const IO_ERROR& ioe )
722  {
723  msg.Printf( _( "Error saving global library table:\n\n%s" ), ioe.What() );
724  wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
725  }
726  }
727 
728  if( dlg.m_ProjectTableChanged )
729  {
730 
731  try
732  {
733  projectTable->Save( projectTableFn.GetFullPath() );
734  }
735  catch( const IO_ERROR& ioe )
736  {
737  msg.Printf( _( "Error saving project-specific library table:\n\n%s" ), ioe.What() );
738  wxMessageBox( msg, _( "File Save Error" ), wxOK | wxICON_ERROR );
739  }
740  }
741 
742  auto editor = (LIB_EDIT_FRAME*) aKiway->Player( FRAME_SCH_LIB_EDITOR, false );
743 
744  if( editor )
745  editor->SyncLibraries( true );
746 
747  LIB_VIEW_FRAME* viewer = (LIB_VIEW_FRAME*) aKiway->Player( FRAME_SCH_VIEWER, false );
748 
749  if( viewer )
750  viewer->ReCreateListLib();
751 }
752 
753 
int GetCount()
static const wxString & GetSymbolLibTableFileName()
static SYMBOL_LIB_TABLE & GetGlobalLibTable()
virtual void paste_text(const wxString &cb_text) override
handle specialized clipboard text, with leading "(sym_lib_table" or spreadsheet formatted text...
void moveUpHandler(wxCommandEvent &event) override
void pageChangedHandler(wxAuiNotebookEvent &event) override
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE...
void Clear()
Delete all rows.
PANEL_SYM_LIB_TABLE(DIALOG_EDIT_LIBRARY_TABLES *aParent, SYMBOL_LIB_TABLE *aGlobal, const wxString &aGlobalTablePath, SYMBOL_LIB_TABLE *aProject, const wxString &aProjectTablePath, const wxString &m_projectBasePath)
This file is part of the common library.
void reindex()
bool ContainsNickname(const wxString &aNickname)
static const wxString ShowType(SCH_FILE_T aFileType)
Return a brief name for a plugin, given aFileType enum.
Definition: sch_io_mgr.cpp:76
LIB_TABLE_ROW * at(size_t aIndex) override
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:37
void InvokeSchEditSymbolLibTable(KIWAY *aKiway, wxWindow *aParent)
WX_GRID * m_cur_grid
changed based on tab choice
Class GRID_TRICKS is used to add cut, copy, and paste to an otherwise unmodied wxGrid instance...
Definition: grid_tricks.h:51
SYMBOL_LIB_TABLE_GRID * cur_model() const
static unsigned FindIllegalLibNicknameChar(const UTF8 &aNickname, LIB_ID_TYPE aType)
Looks for characters that are illegal in library nicknames.
Definition: lib_id.cpp:398
wxString SchematicLibraryFileWildcard()
static const wxString GlobalPathEnvVariableName()
Return the name of the environment variable used to hold the directory of locally installed "KiCad sp...
LIB_TABLE_ROW * At(int aIndex)
static size_t m_pageNdx
Remember the last notebook page selected during a session.
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
SYMBOL_LIB_TABLE_GRID * global_model() const
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
int GetNumberRows() override
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:115
Symbol library viewer main window.
Definition: viewlib_frame.h:44
void adjustPathSubsGridColumns(int aWidth)
This abstract base class mixes any object derived from LIB_TABLE into wxGridTableBase so the result c...
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
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
void populateEnvironReadOnlyTable()
Populate the readonly environment variable table with names and values by examining all the full_uri ...
Build a wxGridTableBase by wrapping an SYMBOL_LIB_TABLE object.
VTBL_ENTRY const ENV_VAR_MAP & GetLocalEnvVariables() const
Definition: pgm_base.h:314
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
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
Definition: pgm_base.h:131
SYMBOL_LIB_TABLE * m_projectTable
VTBL_ENTRY PROJECT & Prj() const
Function Prj returns the PROJECT associated with this KIWAY.
Definition: kiway.cpp:142
bool TransferDataFromWindow() override
Class PANEL_SYM_LIB_TABLE_BASE.
SYMBOL_GRID_TRICKS(DIALOG_EDIT_LIBRARY_TABLES *aParent, wxGrid *aGrid)
Definition of file extensions used in Kicad.
DIALOG_EDIT_LIBRARY_TABLES * m_dialog
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:258
void push_back(LIB_TABLE_ROW *aRow) override
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
void deleteRowHandler(wxCommandEvent &event) override
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:156
bool AppendRows(size_t aNumRows=1) override
void appendRowHandler(wxCommandEvent &event) override
SYMBOL_LIB_TABLE_GRID(const SYMBOL_LIB_TABLE &aTableToEdit)
DIALOG_EDIT_LIBRARY_TABLES * m_parent
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
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
The symbol library editor main window.
wxString m_lastBrowseDir
last browsed directory
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
#define max(a, b)
Definition: auxiliary.h:86
SYMBOL_LIB_TABLE_GRID * project_model() const
LIB_TABLE_ROWS_ITER erase(LIB_TABLE_ROWS_ITER aFirst, LIB_TABLE_ROWS_ITER aLast) override
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...
size_t i
Definition: json11.cpp:597
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
bool verifyTables()
Trim important fields, removes blank row entries, and checks for duplicates.
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
Definition of class LIB_EDIT_FRAME.
void onSizeGrid(wxSizeEvent &event) override
LIB_TABLE_ROWS_ITER insert(LIB_TABLE_ROWS_ITER aIterator, LIB_TABLE_ROW *aRow) override
SYMBOL_LIB_TABLE * m_globalTable
bool ReCreateListLib()
Creates or recreates a sorted list of currently loaded libraries.
LIB_TABLE_ROW * makeNewRow() override
Class STRING_LINE_READER is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:254
LIB_TABLE_ROWS_ITER begin() 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
static wxString GetGlobalTableFileName()
Fetch the global symbol library table file name.
size_t size() const override
void moveDownHandler(wxCommandEvent &event) 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
#define min(a, b)
Definition: auxiliary.h:85
void browseLibrariesHandler(wxCommandEvent &event) override