KiCad PCB EDA Suite
SYMBOL_LIB_TABLE_RESCUER Class Reference

#include <project_rescue.h>

Inheritance diagram for SYMBOL_LIB_TABLE_RESCUER:
RESCUER

Public Member Functions

 SYMBOL_LIB_TABLE_RESCUER (PROJECT &aProject, SCHEMATIC *aSchematic, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType)
 
virtual ~SYMBOL_LIB_TABLE_RESCUER ()
 
virtual void FindCandidates () override
 Populate the RESCUER with all possible candidates. More...
 
virtual void InvokeDialog (wxWindow *aParent, bool aAskShowAgain) override
 Display a dialog to allow the user to select rescues. More...
 
virtual void OpenRescueLibrary () override
 
virtual bool WriteRescueLibrary (wxWindow *aParent) override
 Write the rescue library. More...
 
virtual void AddSymbol (LIB_SYMBOL *aNewSymbol) override
 
void RemoveDuplicates ()
 Filter out duplicately named rescue candidates. More...
 
size_t GetCandidateCount ()
 Return the number of rescue candidates found. More...
 
size_t GetChosenCandidateCount ()
 Get the number of rescue candidates chosen by the user. More...
 
std::vector< SCH_SYMBOL * > * GetSymbols ()
 Get the list of symbols that need rescued. More...
 
PROJECTGetPrj ()
 Return the #SCH_PROJECT object for access to the symbol libraries. More...
 
SCHEMATICSchematic ()
 
void LogRescue (SCH_SYMBOL *aSymbol, const wxString &aOldName, const wxString &aNewName)
 Used by individual RESCUE_CANDIDATE objects to log a rescue for undoing. More...
 
bool DoRescues ()
 Perform all chosen rescue actions, logging them to be undone if necessary. More...
 
void UndoRescues ()
 Reverse the effects of all rescues on the project. More...
 

Static Public Member Functions

static bool RescueProject (wxWindow *aParent, RESCUER &aRescuer, bool aRunningOnDemand)
 

Protected Attributes

std::vector< SCH_SYMBOL * > m_symbols
 
PROJECTm_prj
 
SCHEMATICm_schematic
 
EDA_DRAW_PANEL_GAL::GAL_TYPE m_galBackEndType
 
SCH_SHEET_PATHm_currentSheet
 
boost::ptr_vector< RESCUE_CANDIDATEm_all_candidates
 
std::vector< RESCUE_CANDIDATE * > m_chosen_candidates
 
std::vector< RESCUE_LOGm_rescue_log
 

Private Attributes

SCH_PLUGIN::SCH_PLUGIN_RELEASER m_pi
 
std::unique_ptr< PROPERTIESm_properties
 Library plugin properties. More...
 

Detailed Description

Definition at line 360 of file project_rescue.h.

Constructor & Destructor Documentation

◆ SYMBOL_LIB_TABLE_RESCUER()

SYMBOL_LIB_TABLE_RESCUER::SYMBOL_LIB_TABLE_RESCUER ( PROJECT aProject,
SCHEMATIC aSchematic,
SCH_SHEET_PATH aCurrentSheet,
EDA_DRAW_PANEL_GAL::GAL_TYPE  aGalBackeEndType 
)

Definition at line 771 of file project_rescue.cpp.

773  :
774  RESCUER( aProject, aSchematic, aCurrentSheet, aGalBackEndType )
775 {
776  m_properties = std::make_unique<PROPERTIES>();
777 }
RESCUER(PROJECT &aProject, SCHEMATIC *aSchematic, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType)
std::unique_ptr< PROPERTIES > m_properties
Library plugin properties.

References m_properties.

◆ ~SYMBOL_LIB_TABLE_RESCUER()

virtual SYMBOL_LIB_TABLE_RESCUER::~SYMBOL_LIB_TABLE_RESCUER ( )
inlinevirtual

Definition at line 367 of file project_rescue.h.

368  {
369  }

Member Function Documentation

◆ AddSymbol()

void SYMBOL_LIB_TABLE_RESCUER::AddSymbol ( LIB_SYMBOL aNewSymbol)
overridevirtual

Implements RESCUER.

Definition at line 859 of file project_rescue.cpp.

860 {
861  wxCHECK_RET( aNewSymbol, "Invalid LIB_SYMBOL pointer." );
862 
863  wxFileName fn = GetRescueLibraryFileName( m_schematic );
864 
865  try
866  {
867  if( !m_prj->SchSymbolLibTable()->HasLibrary( fn.GetName() ) )
868  m_pi->SaveSymbol( fn.GetFullPath(), new LIB_SYMBOL( *aNewSymbol ), m_properties.get() );
869  else
870  m_prj->SchSymbolLibTable()->SaveSymbol( fn.GetName(), new LIB_SYMBOL( *aNewSymbol ) );
871  }
872  catch( ... /* IO_ERROR */ )
873  {
874  }
875 }
static wxFileName GetRescueLibraryFileName(SCHEMATIC *aSchematic)
Define a library symbol object.
Definition: lib_symbol.h:96
SCH_PLUGIN::SCH_PLUGIN_RELEASER m_pi
std::unique_ptr< PROPERTIES > m_properties
Library plugin properties.
PROJECT * m_prj
SCHEMATIC * m_schematic

References GetRescueLibraryFileName(), m_pi, RESCUER::m_prj, m_properties, and RESCUER::m_schematic.

◆ DoRescues()

bool RESCUER::DoRescues ( )
inherited

Perform all chosen rescue actions, logging them to be undone if necessary.

Returns
True on success

Definition at line 535 of file project_rescue.cpp.

536 {
537  for( RESCUE_CANDIDATE* each_candidate : m_chosen_candidates )
538  {
539  if( ! each_candidate->PerformAction( this ) )
540  return false;
541  }
542 
543  return true;
544 }
std::vector< RESCUE_CANDIDATE * > m_chosen_candidates

References RESCUER::m_chosen_candidates.

Referenced by RESCUER::RescueProject().

◆ FindCandidates()

void SYMBOL_LIB_TABLE_RESCUER::FindCandidates ( )
overridevirtual

Populate the RESCUER with all possible candidates.

Implements RESCUER.

Definition at line 780 of file project_rescue.cpp.

781 {
783 }
boost::ptr_vector< RESCUE_CANDIDATE > m_all_candidates
static void FindRescues(RESCUER &aRescuer, boost::ptr_vector< RESCUE_CANDIDATE > &aCandidates)
Grab all possible RESCUE_SYMBOL_LIB_TABLE_CANDIDATE objects into a vector.

References RESCUE_SYMBOL_LIB_TABLE_CANDIDATE::FindRescues(), and RESCUER::m_all_candidates.

◆ GetCandidateCount()

size_t RESCUER::GetCandidateCount ( )
inlineinherited

Return the number of rescue candidates found.

Definition at line 278 of file project_rescue.h.

278 { return m_all_candidates.size(); }
boost::ptr_vector< RESCUE_CANDIDATE > m_all_candidates

References RESCUER::m_all_candidates.

Referenced by SCH_EDITOR_CONTROL::rescueProject(), RESCUER::RescueProject(), and DIALOG_RESCUE_EACH::TransferDataFromWindow().

◆ GetChosenCandidateCount()

size_t RESCUER::GetChosenCandidateCount ( )
inlineinherited

Get the number of rescue candidates chosen by the user.

Definition at line 283 of file project_rescue.h.

283 { return m_chosen_candidates.size(); }
std::vector< RESCUE_CANDIDATE * > m_chosen_candidates

References RESCUER::m_chosen_candidates.

Referenced by RESCUER::RescueProject().

◆ GetPrj()

PROJECT* RESCUER::GetPrj ( )
inlineinherited

Return the #SCH_PROJECT object for access to the symbol libraries.

Definition at line 293 of file project_rescue.h.

293 { return m_prj; }
PROJECT * m_prj

References RESCUER::m_prj.

Referenced by RESCUE_CASE_CANDIDATE::FindRescues(), RESCUE_CACHE_CANDIDATE::FindRescues(), and RESCUE_SYMBOL_LIB_TABLE_CANDIDATE::FindRescues().

◆ GetSymbols()

◆ InvokeDialog()

void SYMBOL_LIB_TABLE_RESCUER::InvokeDialog ( wxWindow *  aParent,
bool  aAskShowAgain 
)
overridevirtual

Display a dialog to allow the user to select rescues.

Parameters
aAskShowAgain- whether the "Never Show Again" button should be visible

Implements RESCUER.

Definition at line 786 of file project_rescue.cpp.

787 {
788  InvokeDialogRescueEach( aParent, static_cast< RESCUER& >( *this ), m_currentSheet,
789  m_galBackEndType, aAskShowAgain );
790 }
EDA_DRAW_PANEL_GAL::GAL_TYPE m_galBackEndType
int InvokeDialogRescueEach(wxWindow *aParent, RESCUER &aRescuer, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackEndType, bool aAskShowAgain)
This dialog asks the user which rescuable, cached parts he wants to rescue.
SCH_SHEET_PATH * m_currentSheet

References InvokeDialogRescueEach(), RESCUER::m_currentSheet, and RESCUER::m_galBackEndType.

◆ LogRescue()

void RESCUER::LogRescue ( SCH_SYMBOL aSymbol,
const wxString &  aOldName,
const wxString &  aNewName 
)
inherited

Used by individual RESCUE_CANDIDATE objects to log a rescue for undoing.

Definition at line 524 of file project_rescue.cpp.

526 {
527  RESCUE_LOG logitem;
528  logitem.symbol = aSymbol;
529  logitem.old_name = aOldName;
530  logitem.new_name = aNewName;
531  m_rescue_log.push_back( logitem );
532 }
SCH_SYMBOL * symbol
std::vector< RESCUE_LOG > m_rescue_log
wxString old_name
wxString new_name

References RESCUER::m_rescue_log, RESCUE_LOG::new_name, RESCUE_LOG::old_name, and RESCUE_LOG::symbol.

Referenced by RESCUE_CASE_CANDIDATE::PerformAction(), RESCUE_CACHE_CANDIDATE::PerformAction(), and RESCUE_SYMBOL_LIB_TABLE_CANDIDATE::PerformAction().

◆ OpenRescueLibrary()

void SYMBOL_LIB_TABLE_RESCUER::OpenRescueLibrary ( )
overridevirtual

Implements RESCUER.

Definition at line 793 of file project_rescue.cpp.

794 {
795  m_pi.set( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
796  (*m_properties)[ SCH_LEGACY_PLUGIN::PropBuffering ] = "";
797 }
void set(SCH_PLUGIN *aPlugin)
Definition: sch_io_mgr.h:507
SCH_PLUGIN::SCH_PLUGIN_RELEASER m_pi
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...

References m_pi, SCH_LEGACY_PLUGIN::PropBuffering, and SCH_PLUGIN::SCH_PLUGIN_RELEASER::set().

◆ RemoveDuplicates()

void RESCUER::RemoveDuplicates ( )
inherited

Filter out duplicately named rescue candidates.

Definition at line 606 of file project_rescue.cpp.

607 {
608  std::vector<wxString> names_seen;
609 
610  for( boost::ptr_vector<RESCUE_CANDIDATE>::iterator it = m_all_candidates.begin();
611  it != m_all_candidates.end(); )
612  {
613  bool seen_already = false;
614 
615  for( wxString& name_seen : names_seen )
616  {
617  if( name_seen == it->GetRequestedName() )
618  {
619  seen_already = true;
620  break;
621  }
622  }
623 
624  if( seen_already )
625  {
626  it = m_all_candidates.erase( it );
627  }
628  else
629  {
630  names_seen.push_back( it->GetRequestedName() );
631  ++it;
632  }
633  }
634 }
boost::ptr_vector< RESCUE_CANDIDATE > m_all_candidates

References RESCUER::m_all_candidates.

Referenced by RESCUER::RescueProject().

◆ RescueProject()

bool RESCUER::RescueProject ( wxWindow *  aParent,
RESCUER aRescuer,
bool  aRunningOnDemand 
)
staticinherited

Definition at line 560 of file project_rescue.cpp.

561 {
562  aRescuer.FindCandidates();
563 
564  if( !aRescuer.GetCandidateCount() )
565  {
566  if( aRunningOnDemand )
567  {
568  wxMessageDialog dlg( aParent, _( "This project has nothing to rescue." ),
569  _( "Project Rescue Helper" ) );
570  dlg.ShowModal();
571  }
572 
573  return true;
574  }
575 
576  aRescuer.RemoveDuplicates();
577  aRescuer.InvokeDialog( aParent, !aRunningOnDemand );
578 
579  // If no symbols were rescued, let the user know what's going on. He might
580  // have clicked cancel by mistake, and should have some indication of that.
581  if( !aRescuer.GetChosenCandidateCount() )
582  {
583  wxMessageDialog dlg( aParent, _( "No symbols were rescued." ),
584  _( "Project Rescue Helper" ) );
585  dlg.ShowModal();
586 
587  // Set the modified flag even on Cancel. Many users seem to instinctively want to Save at
588  // this point, due to the reloading of the symbols, so we'll make the save button active.
589  return true;
590  }
591 
592  aRescuer.OpenRescueLibrary();
593 
594  if( !aRescuer.DoRescues() )
595  {
596  aRescuer.UndoRescues();
597  return false;
598  }
599 
600  aRescuer.WriteRescueLibrary( aParent );
601 
602  return true;
603 }
virtual bool WriteRescueLibrary(wxWindow *aParent)=0
Write the rescue library.
virtual void FindCandidates()=0
Populate the RESCUER with all possible candidates.
size_t GetChosenCandidateCount()
Get the number of rescue candidates chosen by the user.
virtual void InvokeDialog(wxWindow *aParent, bool aAskShowAgain)=0
Display a dialog to allow the user to select rescues.
void RemoveDuplicates()
Filter out duplicately named rescue candidates.
size_t GetCandidateCount()
Return the number of rescue candidates found.
#define _(s)
virtual void OpenRescueLibrary()=0
bool DoRescues()
Perform all chosen rescue actions, logging them to be undone if necessary.
void UndoRescues()
Reverse the effects of all rescues on the project.

References _, RESCUER::DoRescues(), RESCUER::FindCandidates(), RESCUER::GetCandidateCount(), RESCUER::GetChosenCandidateCount(), RESCUER::InvokeDialog(), RESCUER::OpenRescueLibrary(), RESCUER::RemoveDuplicates(), RESCUER::UndoRescues(), and RESCUER::WriteRescueLibrary().

Referenced by DIALOG_SYMBOL_REMAP::OnRemapSymbols(), and SCH_EDITOR_CONTROL::rescueProject().

◆ Schematic()

SCHEMATIC* RESCUER::Schematic ( )
inlineinherited

Definition at line 295 of file project_rescue.h.

295 { return m_schematic; }
SCHEMATIC * m_schematic

References RESCUER::m_schematic.

Referenced by RESCUE_SYMBOL_LIB_TABLE_CANDIDATE::FindRescues().

◆ UndoRescues()

void RESCUER::UndoRescues ( )
inherited

Reverse the effects of all rescues on the project.

Definition at line 547 of file project_rescue.cpp.

548 {
549  for( RESCUE_LOG& each_logitem : m_rescue_log )
550  {
551  LIB_ID libId;
552 
553  libId.SetLibItemName( each_logitem.old_name );
554  each_logitem.symbol->SetLibId( libId );
555  each_logitem.symbol->ClearFlags();
556  }
557 }
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition: lib_id.cpp:108
std::vector< RESCUE_LOG > m_rescue_log

References RESCUER::m_rescue_log, and LIB_ID::SetLibItemName().

Referenced by RESCUER::RescueProject().

◆ WriteRescueLibrary()

bool SYMBOL_LIB_TABLE_RESCUER::WriteRescueLibrary ( wxWindow *  aParent)
overridevirtual

Write the rescue library.

Called after successful PerformAction()s. If this fails, undo the actions.

Returns
True on success.

Implements RESCUER.

Definition at line 800 of file project_rescue.cpp.

801 {
802  wxString msg;
803  wxFileName fn = GetRescueLibraryFileName( m_schematic );
804 
805  // If the rescue library already exists in the symbol library table no need save it to add
806  // it to the table.
807  if( !m_prj->SchSymbolLibTable()->HasLibrary( fn.GetName() ) )
808  {
809  try
810  {
811  m_pi->SaveLibrary( fn.GetFullPath() );
812  }
813  catch( const IO_ERROR& ioe )
814  {
815  msg.Printf( _( "Failed to save rescue library %s." ), fn.GetFullPath() );
816  DisplayErrorMessage( aParent, msg, ioe.What() );
817  return false;
818  }
819 
820  wxString uri = "${KIPRJMOD}/" + fn.GetFullName();
821  wxString libNickname = fn.GetName();
822 
823  // Spaces in the file name will break the symbol name because they are not
824  // quoted in the symbol library file format.
825  libNickname.Replace( " ", "-" );
826 
827  SYMBOL_LIB_TABLE_ROW* row = new SYMBOL_LIB_TABLE_ROW( libNickname, uri,
828  wxString( "Legacy" ) );
829  m_prj->SchSymbolLibTable()->InsertRow( row );
830 
832 
833  try
834  {
835  m_prj->SchSymbolLibTable()->Save( fn.GetFullPath() );
836  }
837  catch( const IO_ERROR& ioe )
838  {
839  msg.Printf( _( "Error occurred saving project specific symbol library table." ) );
840  DisplayErrorMessage( aParent, msg, ioe.What() );
841  return false;
842  }
843  }
844 
845  // Relaod the symbol library table.
847 
848  // This can only happen if the symbol library table file was corrupted on write.
849  if( !m_prj->SchSymbolLibTable() )
850  return false;
851 
852  // Update the schematic symbol library links since the library list has changed.
853  SCH_SCREENS schematic( m_schematic->Root() );
854  schematic.UpdateSymbolLinks();
855  return true;
856 }
static const wxString & GetSymbolLibTableFileName()
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:284
static wxFileName GetRescueLibraryFileName(SCHEMATIC *aSchematic)
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:122
virtual void SetElem(ELEM_T aIndex, _ELEM *aElem)
Definition: project.cpp:258
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
SCH_PLUGIN::SCH_PLUGIN_RELEASER m_pi
PROJECT * m_prj
#define _(s)
SCH_SHEET & Root() const
Definition: schematic.h:92
SCHEMATIC * m_schematic
void UpdateSymbolLinks(REPORTER *aReporter=nullptr)
Initialize the LIB_SYMBOL reference for each SCH_SYMBOL found in the full schematic.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:557

References _, DisplayErrorMessage(), PROJECT::ELEM_SYMBOL_LIB_TABLE, PROJECT::GetProjectPath(), GetRescueLibraryFileName(), SYMBOL_LIB_TABLE::GetSymbolLibTableFileName(), m_pi, RESCUER::m_prj, RESCUER::m_schematic, SCHEMATIC::Root(), PROJECT::SetElem(), SCH_SCREENS::UpdateSymbolLinks(), and IO_ERROR::What().

Member Data Documentation

◆ m_all_candidates

◆ m_chosen_candidates

◆ m_currentSheet

SCH_SHEET_PATH* RESCUER::m_currentSheet
protectedinherited

Definition at line 323 of file project_rescue.h.

Referenced by LEGACY_RESCUER::InvokeDialog(), InvokeDialog(), and RESCUER::RESCUER().

◆ m_galBackEndType

EDA_DRAW_PANEL_GAL::GAL_TYPE RESCUER::m_galBackEndType
protectedinherited

Definition at line 322 of file project_rescue.h.

Referenced by LEGACY_RESCUER::InvokeDialog(), InvokeDialog(), and RESCUER::RESCUER().

◆ m_pi

SCH_PLUGIN::SCH_PLUGIN_RELEASER SYMBOL_LIB_TABLE_RESCUER::m_pi
private

Definition at line 382 of file project_rescue.h.

Referenced by AddSymbol(), OpenRescueLibrary(), and WriteRescueLibrary().

◆ m_prj

◆ m_properties

std::unique_ptr< PROPERTIES > SYMBOL_LIB_TABLE_RESCUER::m_properties
private

Library plugin properties.

Definition at line 384 of file project_rescue.h.

Referenced by AddSymbol(), and SYMBOL_LIB_TABLE_RESCUER().

◆ m_rescue_log

std::vector<RESCUE_LOG> RESCUER::m_rescue_log
protectedinherited

Definition at line 328 of file project_rescue.h.

Referenced by RESCUER::LogRescue(), and RESCUER::UndoRescues().

◆ m_schematic

◆ m_symbols

std::vector<SCH_SYMBOL*> RESCUER::m_symbols
protectedinherited

Definition at line 319 of file project_rescue.h.

Referenced by RESCUER::GetSymbols(), and RESCUER::RESCUER().


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