KiCad PCB EDA Suite
PANEL_FP_LIB_TABLE Class Reference

Dialog to show and edit symbol library tables. More...

#include <panel_fp_lib_table.h>

Inheritance diagram for PANEL_FP_LIB_TABLE:
PANEL_FP_LIB_TABLE_BASE

Public Member Functions

 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)
 
 ~PANEL_FP_LIB_TABLE () override
 

Protected Attributes

wxNotebook * m_notebook
 
wxPanel * m_global_panel
 
WX_GRIDm_global_grid
 
wxPanel * m_project_panel
 
WX_GRIDm_project_grid
 
wxPanel * m_buttonsPanel
 
wxBitmapButton * m_append_button
 
SPLIT_BUTTONm_browseButton
 
wxBitmapButton * m_move_up_button
 
wxBitmapButton * m_move_down_button
 
wxBitmapButton * m_delete_button
 
WX_GRIDm_path_subs_grid
 

Private Member Functions

bool TransferDataFromWindow () override
 
bool verifyTables ()
 Trim important fields, removes blank row entries, and checks for duplicates. More...
 
void OnUpdateUI (wxUpdateUIEvent &event) override
 
void appendRowHandler (wxCommandEvent &event) override
 
void browseLibrariesHandler (wxCommandEvent &event)
 
void deleteRowHandler (wxCommandEvent &event) override
 
void moveUpHandler (wxCommandEvent &event) override
 
void moveDownHandler (wxCommandEvent &event) override
 
void onSizeGrid (wxSizeEvent &event) override
 
void adjustPathSubsGridColumns (int aWidth)
 
void populateEnvironReadOnlyTable ()
 Populate the readonly environment variable table with names and values by examining all the full_uri columns. More...
 
FP_LIB_TABLE_GRIDglobal_model () const
 
FP_LIB_TABLE_GRIDproject_model () const
 
FP_LIB_TABLE_GRIDcur_model () const
 

Private Attributes

FP_LIB_TABLEm_global
 
FP_LIB_TABLEm_project
 
wxString m_projectBasePath
 
DIALOG_EDIT_LIBRARY_TABLESm_parent
 
WX_GRIDm_cur_grid
 
wxString m_lastProjectLibDir
 

Static Private Attributes

static size_t m_pageNdx = 0
 

Detailed Description

Dialog to show and edit symbol library tables.

Definition at line 33 of file panel_fp_lib_table.h.

Constructor & Destructor Documentation

◆ PANEL_FP_LIB_TABLE()

PANEL_FP_LIB_TABLE::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 
)

Definition at line 353 of file panel_fp_lib_table.cpp.

356  :
357  PANEL_FP_LIB_TABLE_BASE( aParent ),
358  m_global( aGlobal ),
359  m_project( aProject ),
360  m_projectBasePath( aProjectBasePath ),
361  m_parent( aParent )
362 {
363  m_global_grid->SetTable( new FP_LIB_TABLE_GRID( *aGlobal ), true );
364 
365  // add Cut, Copy, and Paste to wxGrids
366  m_path_subs_grid->PushEventHandler( new GRID_TRICKS( m_path_subs_grid ) );
367 
368  wxArrayString choices;
369 
370  choices.Add( IO_MGR::ShowType( IO_MGR::KICAD_SEXP ) );
371  choices.Add( IO_MGR::ShowType( IO_MGR::LEGACY ) );
372  choices.Add( IO_MGR::ShowType( IO_MGR::EAGLE ) );
373  choices.Add( IO_MGR::ShowType( IO_MGR::GEDA_PCB ) );
374 
375  /* PCAD_PLUGIN does not support Footprint*() functions
376  choices.Add( IO_MGR::ShowType( IO_MGR::PCAD ) );
377  */
378  PCBNEW_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<PCBNEW_SETTINGS>();
379 
380  if( cfg->m_lastFootprintLibDir.IsEmpty() )
382 
384 
385  auto setupGrid =
386  [&]( WX_GRID* aGrid )
387  {
388  // Give a bit more room for wxChoice editors
389  aGrid->SetDefaultRowSize( aGrid->GetDefaultRowSize() + 4 );
390 
391  // add Cut, Copy, and Paste to wxGrids
392  aGrid->PushEventHandler( new FP_GRID_TRICKS( m_parent, aGrid ) );
393 
394  aGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
395  aGrid->AutoSizeColumns( false );
396 
397  wxGridCellAttr* attr;
398 
399  attr = new wxGridCellAttr;
400  attr->SetEditor( new GRID_CELL_PATH_EDITOR( m_parent, aGrid,
401  &cfg->m_lastFootprintLibDir,
402  wxEmptyString, true,
403  m_projectBasePath ) );
404  aGrid->SetColAttr( COL_URI, attr );
405 
406  attr = new wxGridCellAttr;
407  attr->SetEditor( new wxGridCellChoiceEditor( choices ) );
408  aGrid->SetColAttr( COL_TYPE, attr );
409 
410  attr = new wxGridCellAttr;
411  attr->SetRenderer( new wxGridCellBoolRenderer() );
412  attr->SetReadOnly(); // not really; we delegate interactivity to GRID_TRICKS
413  aGrid->SetColAttr( COL_ENABLED, attr );
414 
415  // all but COL_OPTIONS, which is edited with Option Editor anyways.
416  aGrid->AutoSizeColumn( COL_NICKNAME, false );
417  aGrid->AutoSizeColumn( COL_TYPE, false );
418  aGrid->AutoSizeColumn( COL_URI, false );
419  aGrid->AutoSizeColumn( COL_DESCR, false );
420 
421  // would set this to width of title, if it was easily known.
422  aGrid->SetColSize( COL_OPTIONS, 80 );
423 
424  // Gives a selection to each grid, mainly for delete button. wxGrid's wake up with
425  // a currentCell which is sometimes not highlighted.
426  if( aGrid->GetNumberRows() > 0 )
427  aGrid->SelectRow( 0 );
428  };
429 
430  setupGrid( m_global_grid );
431 
433 
434  if( aProject )
435  {
436  m_project_grid->SetTable( new FP_LIB_TABLE_GRID( *aProject ), true );
437  setupGrid( m_project_grid );
438  }
439  else
440  {
441  m_pageNdx = 0;
442  m_notebook->DeletePage( 1 );
443  m_project_grid = nullptr;
444  }
445 
446  m_path_subs_grid->SetColLabelValue( 0, _( "Name" ) );
447  m_path_subs_grid->SetColLabelValue( 1, _( "Value" ) );
448 
449  // select the last selected page
450  m_notebook->SetSelection( m_pageNdx );
452 
453  // for ALT+A handling, we want the initial focus to be on the first selected grid.
455 
456  // Configure button logos
462 
463  // For aesthetic reasons, we must set the size of m_browseButton to match the other bitmaps
464  // manually (for instance m_append_button)
465  Layout(); // Needed at least on MSW to compute the actual buttons sizes, after initializing
466  // their bitmaps
467  wxSize buttonSize = m_append_button->GetSize();
468 
470  m_browseButton->SetMinSize( buttonSize );
471 
472  // Populate the browse library options
473  wxMenu* browseMenu = m_browseButton->GetSplitButtonMenu();
474 
475  for( const std::pair<const int, SUPPORTED_FILE_TYPE>& fileType : fileTypes() )
476  {
477  browseMenu->Append( fileType.first, fileType.second.m_Description );
478 
479  browseMenu->Bind( wxEVT_COMMAND_MENU_SELECTED, &PANEL_FP_LIB_TABLE::browseLibrariesHandler,
480  this, fileType.first );
481  }
482 
483  Layout();
484 
485  // Hack to make buttons lay out correctly the first time on Mac
486  wxSize hackSize = m_buttonsPanel->GetSize();
487  m_buttonsPanel->SetSize( wxSize( hackSize.x - 5, hackSize.y ) );
488  Layout();
489 
490  // This is the button only press for the browse button instead of the menu
491  m_browseButton->Bind( wxEVT_BUTTON, &PANEL_FP_LIB_TABLE::browseLibrariesHandler, this );
492 }
void SetBitmap(const wxBitmap &aBmp)
wxMenu * GetSplitButtonMenu()
FP_LIB_TABLE * m_project
void SetMinSize(const wxSize &aSize) override
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:55
static const std::map< int, SUPPORTED_FILE_TYPE > & fileTypes()
Map with event id as the key to supported file types that will be listed for the add a library option...
void SetTable(wxGridTableBase *table, bool aTakeOwnership=false)
Hide wxGrid's SetTable() method with one which doesn't mess up the grid column widths when setting th...
Definition: wx_grid.cpp:72
static const wxString ShowType(PCB_FILE_T aFileType)
Return a brief name for a plugin given aFileType enum.
Definition: io_mgr.cpp:77
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:97
Geda PCB file formats.
Definition: io_mgr.h:64
static size_t m_pageNdx
PANEL_FP_LIB_TABLE_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
#define _(s)
Editor for wxGrid cells that adds a file/folder browser to the grid input field.
FormatType fileType(const char *aFileName)
Definition: loadmodel.cpp:278
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:105
void browseLibrariesHandler(wxCommandEvent &event)
void SetWidthPadding(int aPadding)
static wxString GetDefaultUserFootprintsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:108
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:55
This class builds a wxGridTableBase by wrapping an FP_LIB_TABLE object.
DIALOG_EDIT_LIBRARY_TABLES * m_parent
FP_LIB_TABLE * m_global
wxString m_lastFootprintLibDir
void populateEnvironReadOnlyTable()
Populate the readonly environment variable table with names and values by examining all the full_uri ...
S-expression Pcbnew file format.
Definition: io_mgr.h:56

References _, browseLibrariesHandler(), COL_DESCR, COL_ENABLED, COL_NICKNAME, COL_OPTIONS, COL_TYPE, COL_URI, IO_MGR::EAGLE, fileType(), fileTypes(), IO_MGR::GEDA_PCB, PATHS::GetDefaultUserFootprintsPath(), SPLIT_BUTTON::GetSplitButtonMenu(), KiBitmap(), IO_MGR::KICAD_SEXP, IO_MGR::LEGACY, PANEL_FP_LIB_TABLE_BASE::m_append_button, PANEL_FP_LIB_TABLE_BASE::m_browseButton, PANEL_FP_LIB_TABLE_BASE::m_buttonsPanel, m_cur_grid, PANEL_FP_LIB_TABLE_BASE::m_delete_button, PANEL_FP_LIB_TABLE_BASE::m_global_grid, PCBNEW_SETTINGS::m_lastFootprintLibDir, m_lastProjectLibDir, PANEL_FP_LIB_TABLE_BASE::m_move_down_button, PANEL_FP_LIB_TABLE_BASE::m_move_up_button, PANEL_FP_LIB_TABLE_BASE::m_notebook, m_pageNdx, m_parent, PANEL_FP_LIB_TABLE_BASE::m_path_subs_grid, PANEL_FP_LIB_TABLE_BASE::m_project_grid, m_projectBasePath, Pgm(), populateEnvironReadOnlyTable(), SPLIT_BUTTON::SetBitmap(), DIALOG_SHIM::SetInitialFocus(), SPLIT_BUTTON::SetMinSize(), WX_GRID::SetTable(), SPLIT_BUTTON::SetWidthPadding(), IO_MGR::ShowType(), small_down, small_folder, small_plus, small_trash, and small_up.

◆ ~PANEL_FP_LIB_TABLE()

PANEL_FP_LIB_TABLE::~PANEL_FP_LIB_TABLE ( )
override

Definition at line 495 of file panel_fp_lib_table.cpp.

496 {
497  // When the dialog is closed it will hide the current notebook page first, which will
498  // in turn select the other one. We then end up saving its index as the "current page".
499  // So flip them back again:
500  m_pageNdx = m_pageNdx == 1 ? 0 : 1;
501 
502  // Delete the GRID_TRICKS.
503  // Any additional event handlers should be popped before the window is deleted.
504  m_global_grid->PopEventHandler( true );
505 
506  if( m_project_grid )
507  m_project_grid->PopEventHandler( true );
508 
509  m_path_subs_grid->PopEventHandler( true );
510 }
static size_t m_pageNdx

References PANEL_FP_LIB_TABLE_BASE::m_global_grid, m_pageNdx, PANEL_FP_LIB_TABLE_BASE::m_path_subs_grid, and PANEL_FP_LIB_TABLE_BASE::m_project_grid.

Member Function Documentation

◆ adjustPathSubsGridColumns()

void PANEL_FP_LIB_TABLE::adjustPathSubsGridColumns ( int  aWidth)
private

Definition at line 942 of file panel_fp_lib_table.cpp.

943 {
944  // Account for scroll bars
945  aWidth -= ( m_path_subs_grid->GetSize().x - m_path_subs_grid->GetClientSize().x );
946 
947  m_path_subs_grid->AutoSizeColumn( 0 );
948 
949  if( aWidth - m_path_subs_grid->GetColSize( 0 ) < 60 )
950  m_path_subs_grid->SetColSize( 0, aWidth / 2 );
951 
952  m_path_subs_grid->SetColSize( 1, aWidth - m_path_subs_grid->GetColSize( 0 ) );
953 }

References PANEL_FP_LIB_TABLE_BASE::m_path_subs_grid.

Referenced by onSizeGrid(), and populateEnvironReadOnlyTable().

◆ appendRowHandler()

void PANEL_FP_LIB_TABLE::appendRowHandler ( wxCommandEvent &  event)
overrideprivatevirtual

Reimplemented from PANEL_FP_LIB_TABLE_BASE.

Definition at line 627 of file panel_fp_lib_table.cpp.

628 {
630  return;
631 
632  if( m_cur_grid->AppendRows( 1 ) )
633  {
634  int last_row = m_cur_grid->GetNumberRows() - 1;
635 
636  // wx documentation is wrong, SetGridCursor does not make visible.
637  m_cur_grid->MakeCellVisible( last_row, 0 );
638  m_cur_grid->SetGridCursor( last_row, 1 );
639  m_cur_grid->EnableCellEditControl( true );
640  m_cur_grid->ShowCellEditControl();
641  }
642 }
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:190

References WX_GRID::CommitPendingChanges(), and m_cur_grid.

◆ browseLibrariesHandler()

void PANEL_FP_LIB_TABLE::browseLibrariesHandler ( wxCommandEvent &  event)
private

Definition at line 765 of file panel_fp_lib_table.cpp.

766 {
768  return;
769 
770  std::map<int, SUPPORTED_FILE_TYPE>::const_iterator fileTypeIt;
771 
772  // We are bound both to the menu and button with this one handler
773  // So we must set the file type based on it
774  if( event.GetEventType() == wxEVT_BUTTON )
775  {
776  // Let's default to adding a kicad footprint file for just the footprint
777  fileTypeIt = fileTypes().find( ID_PANEL_FPLIB_ADD_KICADMOD );
778  }
779  else
780  {
781  fileTypeIt = fileTypes().find( event.GetId() );
782  }
783 
784  if( fileTypeIt == fileTypes().end() )
785  {
786  wxLogWarning( "File type selection event received but could not find the file type in the table" );
787  return;
788  }
789 
790  SUPPORTED_FILE_TYPE fileType = fileTypeIt->second;
791 
792  PCBNEW_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<PCBNEW_SETTINGS>();
793 
794  wxArrayString files;
795 
796  wxString title;
797 
798  title.Printf( _( "Select %s Library" ), fileType.m_Description );
799 
800  wxString openDir = cfg->m_lastFootprintLibDir;
801 
802  if( m_cur_grid == m_project_grid )
803  openDir = m_lastProjectLibDir;
804 
805  if( fileType.m_IsFile )
806  {
807  wxFileDialog dlg( this, title, openDir, wxEmptyString, fileType.m_FileFilter,
808  wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE );
809 
810  int result = dlg.ShowModal();
811 
812  if( result == wxID_CANCEL )
813  return;
814 
815  dlg.GetPaths( files );
816 
817  if( m_cur_grid == m_global_grid )
818  cfg->m_lastFootprintLibDir = dlg.GetDirectory();
819  else
820  m_lastProjectLibDir = dlg.GetDirectory();
821  }
822  else
823  {
824 #if wxCHECK_VERSION( 3, 1, 4 ) // 3.1.4 required for wxDD_MULTIPLE
825  wxDirDialog dlg( nullptr, title, openDir,
826  wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST | wxDD_MULTIPLE );
827 
828  int result = dlg.ShowModal();
829 
830  if( result == wxID_CANCEL )
831  return;
832 
833  dlg.GetPaths( files );
834 #else
835  wxDirDialog dlg( nullptr, title, openDir, wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
836 
837  int result = dlg.ShowModal();
838 
839  if( result == wxID_CANCEL )
840  return;
841 
842  // is there a file extension configured to hunt out their containing folders?
843  if( fileType.m_FolderSearchExtension != "" )
844  {
845  wxDir rootDir( dlg.GetPath() );
846 
847  LIBRARY_TRAVERSER traverser( fileType.m_FolderSearchExtension, rootDir.GetName() );
848  rootDir.Traverse( traverser );
849 
850  traverser.GetPaths( files );
851 
852  if( traverser.HasDirectoryOpenFailures() )
853  {
854  wxArrayString failedDirs;
855  traverser.GetPaths( failedDirs );
856  wxString detailedMsg = _( "The following directories could not be opened: \n" );
857 
858  for( const wxString& path : failedDirs )
859  detailedMsg << path << "\n";
860 
861  DisplayErrorMessage( this, _( "Failed to open directories to look for libraries" ),
862  detailedMsg );
863  }
864  }
865  else
866  {
867  files.Add( dlg.GetPath() );
868  }
869 #endif
870 
871  if( !files.IsEmpty() )
872  {
873  wxFileName first( files.front() );
874 
875  if( m_cur_grid == m_global_grid )
876  cfg->m_lastFootprintLibDir = first.GetPath();
877  else
878  m_lastProjectLibDir = first.GetPath();
879  }
880  }
881 
882  // Drop the last directory if the path is a .pretty folder
884  cfg->m_lastFootprintLibDir = cfg->m_lastFootprintLibDir.BeforeLast( wxFileName::GetPathSeparator() );
885 
886  const ENV_VAR_MAP& envVars = Pgm().GetLocalEnvVariables();
887  bool addDuplicates = false;
888  bool applyToAll = false;
889  wxString warning = _( "Warning: Duplicate Nicknames" );
890  wxString msg = _( "A library nicknamed '%s' already exists." );
891  wxString detailedMsg = _( "One of the nicknames will need to be changed after "
892  "adding this library." );
893 
894  for( const wxString& filePath : files )
895  {
896  wxFileName fn( filePath );
897  wxString nickname = LIB_ID::FixIllegalChars( fn.GetName(), true );
898  bool doAdd = true;
899 
900  if( cur_model()->ContainsNickname( nickname ) )
901  {
902  if( !applyToAll )
903  {
904  // The cancel button adds the library to the table anyway
905  addDuplicates = OKOrCancelDialog( this, warning, wxString::Format( msg, nickname ),
906  detailedMsg, _( "Skip" ), _( "Add Anyway" ),
907  &applyToAll ) == wxID_CANCEL;
908  }
909 
910  doAdd = addDuplicates;
911  }
912 
913  if( doAdd && m_cur_grid->AppendRows( 1 ) )
914  {
915  int last_row = m_cur_grid->GetNumberRows() - 1;
916 
917  m_cur_grid->SetCellValue( last_row, COL_NICKNAME, nickname );
918 
919  m_cur_grid->SetCellValue( last_row, COL_TYPE, IO_MGR::ShowType( fileType.m_Plugin ) );
920 
921  // try to use path normalized to an environmental variable or project path
922  wxString path = NormalizePath( filePath, &envVars, m_projectBasePath );
923 
924  // Do not use the project path in the global library table. This will almost
925  // assuredly be wrong for a different project.
926  if( m_pageNdx == 0 && path.Contains( "${KIPRJMOD}" ) )
927  path = fn.GetFullPath();
928 
929  m_cur_grid->SetCellValue( last_row, COL_URI, path );
930  }
931  }
932 
933  if( !files.IsEmpty() )
934  {
935  int new_row = m_cur_grid->GetNumberRows() - 1;
936  m_cur_grid->MakeCellVisible( new_row, m_cur_grid->GetGridCursorCol() );
937  m_cur_grid->SetGridCursor( new_row, m_cur_grid->GetGridCursorCol() );
938  }
939 }
Container that describes file type info for the add a library options.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:292
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
Definition: lib_id.cpp:189
bool ContainsNickname(const wxString &aNickname)
static const std::map< int, SUPPORTED_FILE_TYPE > & fileTypes()
Map with event id as the key to supported file types that will be listed for the add a library option...
static const wxString ShowType(PCB_FILE_T aFileType)
Return a brief name for a plugin given aFileType enum.
Definition: io_mgr.cpp:77
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
static size_t m_pageNdx
FP_LIB_TABLE_GRID * cur_model() const
#define _(s)
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:190
FormatType fileType(const char *aFileName)
Definition: loadmodel.cpp:278
int OKOrCancelDialog(wxWindow *aParent, const wxString &aWarning, const wxString &aMessage, const wxString &aDetailedMessage, const wxString &aOKLabel, const wxString &aCancelLabel, bool *aApplyToAll)
Display a warning dialog with aMessage and returns the user response.
Definition: confirm.cpp:252
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
void GetPaths(wxArrayString &aPathArray)
Traverser implementation that looks to find any and all "folder" libraries by looking for files with ...
wxString m_lastFootprintLibDir
const std::string KiCadFootprintLibPathExtension
wxString NormalizePath(const wxFileName &aFilePath, const ENV_VAR_MAP *aEnvVars, const wxString &aProjectPath)
Normalize a file path to an environmental variable, if possible.
Definition: env_paths.cpp:69

References _, COL_NICKNAME, COL_TYPE, COL_URI, WX_GRID::CommitPendingChanges(), LIB_TABLE_GRID::ContainsNickname(), cur_model(), DisplayErrorMessage(), fileType(), fileTypes(), LIB_ID::FixIllegalChars(), Format(), LIBRARY_TRAVERSER::GetPaths(), ID_PANEL_FPLIB_ADD_KICADMOD, KiCadFootprintLibPathExtension, m_cur_grid, PANEL_FP_LIB_TABLE_BASE::m_global_grid, PCBNEW_SETTINGS::m_lastFootprintLibDir, m_lastProjectLibDir, m_pageNdx, PANEL_FP_LIB_TABLE_BASE::m_project_grid, m_projectBasePath, NormalizePath(), OKOrCancelDialog(), path, Pgm(), and IO_MGR::ShowType().

Referenced by PANEL_FP_LIB_TABLE().

◆ cur_model()

FP_LIB_TABLE_GRID* PANEL_FP_LIB_TABLE::cur_model ( ) const
inlineprivate

Definition at line 77 of file panel_fp_lib_table.h.

78  {
79  return (FP_LIB_TABLE_GRID*) m_cur_grid->GetTable();
80  }
This class builds a wxGridTableBase by wrapping an FP_LIB_TABLE object.

References m_cur_grid.

Referenced by browseLibrariesHandler(), moveDownHandler(), moveUpHandler(), and verifyTables().

◆ deleteRowHandler()

void PANEL_FP_LIB_TABLE::deleteRowHandler ( wxCommandEvent &  event)
overrideprivatevirtual

Reimplemented from PANEL_FP_LIB_TABLE_BASE.

Definition at line 645 of file panel_fp_lib_table.cpp.

646 {
648  return;
649 
650  int curRow = m_cur_grid->GetGridCursorRow();
651  int curCol = m_cur_grid->GetGridCursorCol();
652 
653  // In a wxGrid, collect rows that have a selected cell, or are selected
654  // It is not so easy: it depends on the way the selection was made.
655  // Here, we collect rows selected by clicking on a row label, and rows that contain any
656  // previously-selected cells.
657  // If no candidate, just delete the row with the grid cursor.
658  wxArrayInt selectedRows = m_cur_grid->GetSelectedRows();
659  wxGridCellCoordsArray cells = m_cur_grid->GetSelectedCells();
660  wxGridCellCoordsArray blockTopLeft = m_cur_grid->GetSelectionBlockTopLeft();
661  wxGridCellCoordsArray blockBotRight = m_cur_grid->GetSelectionBlockBottomRight();
662 
663  // Add all row having cell selected to list:
664  for( unsigned ii = 0; ii < cells.GetCount(); ii++ )
665  selectedRows.Add( cells[ii].GetRow() );
666 
667  // Handle block selection
668  if( !blockTopLeft.IsEmpty() && !blockBotRight.IsEmpty() )
669  {
670  for( int i = blockTopLeft[0].GetRow(); i <= blockBotRight[0].GetRow(); ++i )
671  selectedRows.Add( i );
672  }
673 
674  // Use the row having the grid cursor only if we have no candidate:
675  if( selectedRows.size() == 0 && m_cur_grid->GetGridCursorRow() >= 0 )
676  selectedRows.Add( m_cur_grid->GetGridCursorRow() );
677 
678  if( selectedRows.size() == 0 )
679  {
680  wxBell();
681  return;
682  }
683 
684  std::sort( selectedRows.begin(), selectedRows.end() );
685 
686  // Remove selected rows (note: a row can be stored more than once in list)
687  int last_row = -1;
688 
689  for( int ii = selectedRows.GetCount()-1; ii >= 0; ii-- )
690  {
691  int row = selectedRows[ii];
692 
693  if( row != last_row )
694  {
695  last_row = row;
696  m_cur_grid->DeleteRows( row, 1 );
697  }
698  }
699 
700  if( m_cur_grid->GetNumberRows() > 0 && curRow >= 0 )
701  m_cur_grid->SetGridCursor( std::min( curRow, m_cur_grid->GetNumberRows() - 1 ), curCol );
702 }
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:190

References WX_GRID::CommitPendingChanges(), and m_cur_grid.

◆ global_model()

FP_LIB_TABLE_GRID* PANEL_FP_LIB_TABLE::global_model ( ) const
inlineprivate

Definition at line 67 of file panel_fp_lib_table.h.

68  {
69  return (FP_LIB_TABLE_GRID*) m_global_grid->GetTable();
70  }
This class builds a wxGridTableBase by wrapping an FP_LIB_TABLE object.

References PANEL_FP_LIB_TABLE_BASE::m_global_grid.

Referenced by populateEnvironReadOnlyTable(), TransferDataFromWindow(), and verifyTables().

◆ moveDownHandler()

void PANEL_FP_LIB_TABLE::moveDownHandler ( wxCommandEvent &  event)
overrideprivatevirtual

Reimplemented from PANEL_FP_LIB_TABLE_BASE.

Definition at line 735 of file panel_fp_lib_table.cpp.

736 {
738  return;
739 
740  FP_LIB_TABLE_GRID* tbl = cur_model();
741  int curRow = m_cur_grid->GetGridCursorRow();
742 
743  // @todo: add multiple selection moves.
744  if( unsigned( curRow + 1 ) < tbl->rows.size() )
745  {
746  boost::ptr_vector< LIB_TABLE_ROW >::auto_type move_me =
747  tbl->rows.release( tbl->rows.begin() + curRow );
748 
749  ++curRow;
750  tbl->rows.insert( tbl->rows.begin() + curRow, move_me.release() );
751 
752  if( tbl->GetView() )
753  {
754  // Update the wxGrid
755  wxGridTableMessage msg( tbl, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, curRow - 1, 0 );
756  tbl->GetView()->ProcessTableMessage( msg );
757  }
758 
759  m_cur_grid->MakeCellVisible( curRow, m_cur_grid->GetGridCursorCol() );
760  m_cur_grid->SetGridCursor( curRow, m_cur_grid->GetGridCursorCol() );
761  }
762 }
LIB_TABLE_ROWS rows
FP_LIB_TABLE_GRID * cur_model() const
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:190
This class builds a wxGridTableBase by wrapping an FP_LIB_TABLE object.

References WX_GRID::CommitPendingChanges(), cur_model(), m_cur_grid, and LIB_TABLE::rows.

◆ moveUpHandler()

void PANEL_FP_LIB_TABLE::moveUpHandler ( wxCommandEvent &  event)
overrideprivatevirtual

Reimplemented from PANEL_FP_LIB_TABLE_BASE.

Definition at line 705 of file panel_fp_lib_table.cpp.

706 {
708  return;
709 
710  FP_LIB_TABLE_GRID* tbl = cur_model();
711  int curRow = m_cur_grid->GetGridCursorRow();
712 
713  // @todo: add multiple selection moves.
714  if( curRow >= 1 )
715  {
716  boost::ptr_vector< LIB_TABLE_ROW >::auto_type move_me =
717  tbl->rows.release( tbl->rows.begin() + curRow );
718 
719  --curRow;
720  tbl->rows.insert( tbl->rows.begin() + curRow, move_me.release() );
721 
722  if( tbl->GetView() )
723  {
724  // Update the wxGrid
725  wxGridTableMessage msg( tbl, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, curRow, 0 );
726  tbl->GetView()->ProcessTableMessage( msg );
727  }
728 
729  m_cur_grid->MakeCellVisible( curRow, m_cur_grid->GetGridCursorCol() );
730  m_cur_grid->SetGridCursor( curRow, m_cur_grid->GetGridCursorCol() );
731  }
732 }
LIB_TABLE_ROWS rows
FP_LIB_TABLE_GRID * cur_model() const
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:190
This class builds a wxGridTableBase by wrapping an FP_LIB_TABLE object.

References WX_GRID::CommitPendingChanges(), cur_model(), m_cur_grid, and LIB_TABLE::rows.

◆ onSizeGrid()

void PANEL_FP_LIB_TABLE::onSizeGrid ( wxSizeEvent &  event)
overrideprivatevirtual

Reimplemented from PANEL_FP_LIB_TABLE_BASE.

Definition at line 956 of file panel_fp_lib_table.cpp.

957 {
958  adjustPathSubsGridColumns( event.GetSize().GetX() );
959 
960  event.Skip();
961 }
void adjustPathSubsGridColumns(int aWidth)

References adjustPathSubsGridColumns().

◆ OnUpdateUI()

void PANEL_FP_LIB_TABLE::OnUpdateUI ( wxUpdateUIEvent &  event)
overrideprivatevirtual

◆ populateEnvironReadOnlyTable()

void PANEL_FP_LIB_TABLE::populateEnvironReadOnlyTable ( )
private

Populate the readonly environment variable table with names and values by examining all the full_uri columns.

Definition at line 1000 of file panel_fp_lib_table.cpp.

1001 {
1002  wxRegEx re( ".*?(\\$\\{(.+?)\\})|(\\$\\((.+?)\\)).*?", wxRE_ADVANCED );
1003  wxASSERT( re.IsValid() ); // wxRE_ADVANCED is required.
1004 
1005  std::set< wxString > unique;
1006 
1007  // clear the table
1009 
1010  for( FP_LIB_TABLE_GRID* tbl : { global_model(), project_model() } )
1011  {
1012  if( !tbl )
1013  continue;
1014 
1015  for( int row = 0; row < tbl->GetNumberRows(); ++row )
1016  {
1017  wxString uri = tbl->GetValue( row, COL_URI );
1018 
1019  while( re.Matches( uri ) )
1020  {
1021  wxString envvar = re.GetMatch( uri, 2 );
1022 
1023  // if not ${...} form then must be $(...)
1024  if( envvar.IsEmpty() )
1025  envvar = re.GetMatch( uri, 4 );
1026 
1027  // ignore duplicates
1028  unique.insert( envvar );
1029 
1030  // delete the last match and search again
1031  uri.Replace( re.GetMatch( uri, 0 ), wxEmptyString );
1032  }
1033  }
1034  }
1035 
1036  // Make sure this special environment variable shows up even if it was
1037  // not used yet. It is automatically set by KiCad to the directory holding
1038  // the current project.
1039  unique.insert( PROJECT_VAR_NAME );
1040  unique.insert( FP_LIB_TABLE::GlobalPathEnvVariableName() );
1041  // This special environment variable is used to locate 3d shapes
1042  unique.insert( KICAD6_3DMODEL_DIR );
1043 
1044  for( const wxString& evName : unique )
1045  {
1046  int row = m_path_subs_grid->GetNumberRows();
1047  m_path_subs_grid->AppendRows( 1 );
1048 
1049  m_path_subs_grid->SetCellValue( row, 0, wxT( "${" ) + evName + wxT( "}" ) );
1050  m_path_subs_grid->SetCellEditor( row, 0, new GRID_CELL_READONLY_TEXT_EDITOR() );
1051 
1052  wxString evValue;
1053  wxGetEnv( evName, &evValue );
1054  m_path_subs_grid->SetCellValue( row, 1, evValue );
1055  m_path_subs_grid->SetCellEditor( row, 1, new GRID_CELL_READONLY_TEXT_EDITOR() );
1056  }
1057 
1058  // No combobox editors here, but it looks better if its consistent with the other
1059  // grids in the dialog.
1060  m_path_subs_grid->SetDefaultRowSize( m_path_subs_grid->GetDefaultRowSize() + 2 );
1061 
1062  adjustPathSubsGridColumns( m_path_subs_grid->GetRect().GetWidth() );
1063 }
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:38
void adjustPathSubsGridColumns(int aWidth)
static const wxString GlobalPathEnvVariableName()
Return the name of the environment variable used to hold the directory of locally installed "KiCad sp...
FP_LIB_TABLE_GRID * global_model() const
FP_LIB_TABLE_GRID * project_model() const
#define KICAD6_3DMODEL_DIR
A variable name whose value holds the path of 3D shape files.
This class builds a wxGridTableBase by wrapping an FP_LIB_TABLE object.
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:106

References adjustPathSubsGridColumns(), WX_GRID::ClearRows(), COL_URI, global_model(), FP_LIB_TABLE::GlobalPathEnvVariableName(), KICAD6_3DMODEL_DIR, PANEL_FP_LIB_TABLE_BASE::m_path_subs_grid, project_model(), and PROJECT_VAR_NAME.

Referenced by PANEL_FP_LIB_TABLE().

◆ project_model()

FP_LIB_TABLE_GRID* PANEL_FP_LIB_TABLE::project_model ( ) const
inlineprivate

Definition at line 72 of file panel_fp_lib_table.h.

73  {
74  return m_project_grid ? (FP_LIB_TABLE_GRID*) m_project_grid->GetTable() : nullptr;
75  }
This class builds a wxGridTableBase by wrapping an FP_LIB_TABLE object.

References PANEL_FP_LIB_TABLE_BASE::m_project_grid.

Referenced by populateEnvironReadOnlyTable(), TransferDataFromWindow(), and verifyTables().

◆ TransferDataFromWindow()

bool PANEL_FP_LIB_TABLE::TransferDataFromWindow ( )
overrideprivate

Definition at line 964 of file panel_fp_lib_table.cpp.

965 {
967  return false;
968 
969  if( verifyTables() )
970  {
971  if( *global_model() != *m_global )
972  {
974 
975  m_global->Clear();
976  m_global->rows.transfer( m_global->rows.end(), global_model()->rows.begin(),
977  global_model()->rows.end(), global_model()->rows );
978  m_global->reindex();
979  }
980 
981  if( project_model() && *project_model() != *m_project )
982  {
984 
985  m_project->Clear();
986  m_project->rows.transfer( m_project->rows.end(), project_model()->rows.begin(),
987  project_model()->rows.end(), project_model()->rows );
988  m_project->reindex();
989  }
990 
991  return true;
992  }
993 
994  return false;
995 }
void Clear()
Delete all rows.
void reindex()
FP_LIB_TABLE * m_project
LIB_TABLE_ROWS rows
FP_LIB_TABLE_GRID * global_model() const
FP_LIB_TABLE_GRID * project_model() const
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:190
bool verifyTables()
Trim important fields, removes blank row entries, and checks for duplicates.
DIALOG_EDIT_LIBRARY_TABLES * m_parent
FP_LIB_TABLE * m_global

References LIB_TABLE::Clear(), WX_GRID::CommitPendingChanges(), global_model(), m_cur_grid, m_global, DIALOG_EDIT_LIBRARY_TABLES::m_GlobalTableChanged, m_parent, m_project, DIALOG_EDIT_LIBRARY_TABLES::m_ProjectTableChanged, project_model(), LIB_TABLE::reindex(), LIB_TABLE::rows, and verifyTables().

◆ verifyTables()

bool PANEL_FP_LIB_TABLE::verifyTables ( )
private

Trim important fields, removes blank row entries, and checks for duplicates.

Returns
bool - true if tables are OK, else false.

Definition at line 513 of file panel_fp_lib_table.cpp.

514 {
515  wxString msg;
516 
517  for( FP_LIB_TABLE_GRID* model : { global_model(), project_model() } )
518  {
519  if( !model )
520  continue;
521 
522  for( int r = 0; r < model->GetNumberRows(); )
523  {
524  wxString nick = model->GetValue( r, COL_NICKNAME ).Trim( false ).Trim();
525  wxString uri = model->GetValue( r, COL_URI ).Trim( false ).Trim();
526  unsigned illegalCh = 0;
527 
528  if( !nick || !uri )
529  {
530  if( !nick && !uri )
531  msg = _( "A library table row nickname and path cells are empty." );
532  else if( !nick )
533  msg = _( "A library table row nickname cell is empty." );
534  else
535  msg = _( "A library table row path cell is empty." );
536 
537  wxMessageDialog badCellDlg( this, msg, _( "Invalid Row Definition" ),
538  wxYES_NO | wxCENTER | wxICON_QUESTION | wxYES_DEFAULT );
539  badCellDlg.SetExtendedMessage( _( "Empty cells will result in all rows that are "
540  "invalid to be removed from the table." ) );
541  badCellDlg.SetYesNoLabels( wxMessageDialog::ButtonLabel( "Remove Invalid Cells" ),
542  wxMessageDialog::ButtonLabel( "Cancel Table Update" ) );
543 
544  if( badCellDlg.ShowModal() == wxID_NO )
545  return false;
546 
547  // Delete the "empty" row, where empty means missing nick or uri.
548  // This also updates the UI which could be slow, but there should only be a few
549  // rows to delete, unless the user fell asleep on the Add Row
550  // button.
551  model->DeleteRows( r, 1 );
552  }
553  else if( ( illegalCh = LIB_ID::FindIllegalLibraryNameChar( nick ) ) )
554  {
555  msg = wxString::Format( _( "Illegal character '%c' in nickname '%s'." ),
556  illegalCh,
557  nick );
558 
559  // show the tabbed panel holding the grid we have flunked:
560  if( model != cur_model() )
561  m_notebook->SetSelection( model == global_model() ? 0 : 1 );
562 
563  m_cur_grid->MakeCellVisible( r, 0 );
564  m_cur_grid->SetGridCursor( r, 1 );
565 
566  wxMessageDialog errdlg( this, msg, _( "Library Nickname Error" ) );
567  errdlg.ShowModal();
568  return false;
569  }
570  else
571  {
572  // set the trimmed values back into the table so they get saved to disk.
573  model->SetValue( r, COL_NICKNAME, nick );
574  model->SetValue( r, COL_URI, uri );
575  ++r; // this row was OK.
576  }
577  }
578  }
579 
580  // check for duplicate nickNames, separately in each table.
581  for( FP_LIB_TABLE_GRID* model : { global_model(), project_model() } )
582  {
583  if( !model )
584  continue;
585 
586  for( int r1 = 0; r1 < model->GetNumberRows() - 1; ++r1 )
587  {
588  wxString nick1 = model->GetValue( r1, COL_NICKNAME );
589 
590  for( int r2 = r1 + 1; r2 < model->GetNumberRows(); ++r2 )
591  {
592  wxString nick2 = model->GetValue( r2, COL_NICKNAME );
593 
594  if( nick1 == nick2 )
595  {
596  msg = wxString::Format( _( "Multiple libraries cannot share the same "
597  "nickname ('%s')." ),
598  nick1 );
599 
600  // show the tabbed panel holding the grid we have flunked:
601  if( model != cur_model() )
602  m_notebook->SetSelection( model == global_model() ? 0 : 1 );
603 
604  // go to the lower of the two rows, it is technically the duplicate:
605  m_cur_grid->MakeCellVisible( r2, 0 );
606  m_cur_grid->SetGridCursor( r2, 1 );
607 
608  wxMessageDialog errdlg( this, msg, _( "Library Nickname Error" ) );
609  errdlg.ShowModal();
610  return false;
611  }
612  }
613  }
614  }
615 
616  return true;
617 }
FP_LIB_TABLE_GRID * global_model() const
FP_LIB_TABLE_GRID * project_model() const
FP_LIB_TABLE_GRID * cur_model() const
#define _(s)
E_SERIE r
Definition: eserie.cpp:41
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
This class builds a wxGridTableBase by wrapping an FP_LIB_TABLE object.
static unsigned FindIllegalLibraryNameChar(const UTF8 &aLibraryName)
Looks for characters that are illegal in library nicknames.
Definition: lib_id.cpp:240

References _, COL_NICKNAME, COL_URI, cur_model(), LIB_ID::FindIllegalLibraryNameChar(), Format(), global_model(), m_cur_grid, PANEL_FP_LIB_TABLE_BASE::m_notebook, project_model(), and r.

Referenced by TransferDataFromWindow().

Member Data Documentation

◆ m_append_button

wxBitmapButton* PANEL_FP_LIB_TABLE_BASE::m_append_button
protectedinherited

◆ m_browseButton

SPLIT_BUTTON* PANEL_FP_LIB_TABLE_BASE::m_browseButton
protectedinherited

◆ m_buttonsPanel

wxPanel* PANEL_FP_LIB_TABLE_BASE::m_buttonsPanel
protectedinherited

◆ m_cur_grid

◆ m_delete_button

wxBitmapButton* PANEL_FP_LIB_TABLE_BASE::m_delete_button
protectedinherited

◆ m_global

FP_LIB_TABLE* PANEL_FP_LIB_TABLE::m_global
private

Definition at line 83 of file panel_fp_lib_table.h.

Referenced by TransferDataFromWindow().

◆ m_global_grid

WX_GRID* PANEL_FP_LIB_TABLE_BASE::m_global_grid
protectedinherited

◆ m_global_panel

wxPanel* PANEL_FP_LIB_TABLE_BASE::m_global_panel
protectedinherited

◆ m_lastProjectLibDir

wxString PANEL_FP_LIB_TABLE::m_lastProjectLibDir
private

Definition at line 93 of file panel_fp_lib_table.h.

Referenced by browseLibrariesHandler(), and PANEL_FP_LIB_TABLE().

◆ m_move_down_button

wxBitmapButton* PANEL_FP_LIB_TABLE_BASE::m_move_down_button
protectedinherited

◆ m_move_up_button

wxBitmapButton* PANEL_FP_LIB_TABLE_BASE::m_move_up_button
protectedinherited

◆ m_notebook

wxNotebook* PANEL_FP_LIB_TABLE_BASE::m_notebook
protectedinherited

◆ m_pageNdx

size_t PANEL_FP_LIB_TABLE::m_pageNdx = 0
staticprivate

◆ m_parent

DIALOG_EDIT_LIBRARY_TABLES* PANEL_FP_LIB_TABLE::m_parent
private

Definition at line 87 of file panel_fp_lib_table.h.

Referenced by PANEL_FP_LIB_TABLE(), and TransferDataFromWindow().

◆ m_path_subs_grid

◆ m_project

FP_LIB_TABLE* PANEL_FP_LIB_TABLE::m_project
private

Definition at line 84 of file panel_fp_lib_table.h.

Referenced by TransferDataFromWindow().

◆ m_project_grid

WX_GRID* PANEL_FP_LIB_TABLE_BASE::m_project_grid
protectedinherited

◆ m_project_panel

wxPanel* PANEL_FP_LIB_TABLE_BASE::m_project_panel
protectedinherited

◆ m_projectBasePath

wxString PANEL_FP_LIB_TABLE::m_projectBasePath
private

Definition at line 85 of file panel_fp_lib_table.h.

Referenced by browseLibrariesHandler(), and PANEL_FP_LIB_TABLE().


The documentation for this class was generated from the following files: