KiCad PCB EDA Suite
LEGACY_RESCUER Class Reference

#include <project_rescue.h>

Inheritance diagram for LEGACY_RESCUER:
RESCUER

Public Member Functions

 LEGACY_RESCUER (PROJECT &aProject, SCHEMATIC *aSchematic, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackEndType)
 
virtual ~LEGACY_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

std::unique_ptr< SYMBOL_LIBm_rescue_lib
 

Detailed Description

Definition at line 332 of file project_rescue.h.

Constructor & Destructor Documentation

◆ LEGACY_RESCUER()

LEGACY_RESCUER::LEGACY_RESCUER ( PROJECT aProject,
SCHEMATIC aSchematic,
SCH_SHEET_PATH aCurrentSheet,
EDA_DRAW_PANEL_GAL::GAL_TYPE  aGalBackEndType 
)
inline

Definition at line 335 of file project_rescue.h.

336  :
337  RESCUER( aProject, aSchematic, aCurrentSheet, aGalBackEndType )
338  {
339  }
RESCUER(PROJECT &aProject, SCHEMATIC *aSchematic, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType)

◆ ~LEGACY_RESCUER()

virtual LEGACY_RESCUER::~LEGACY_RESCUER ( )
inlinevirtual

Definition at line 341 of file project_rescue.h.

342  {
343  }

Member Function Documentation

◆ AddSymbol()

void LEGACY_RESCUER::AddSymbol ( LIB_SYMBOL aNewSymbol)
overridevirtual

Implements RESCUER.

Definition at line 778 of file project_rescue.cpp.

779 {
780  wxCHECK_RET( aNewSymbol, wxT( "Invalid LIB_SYMBOL pointer." ) );
781 
782  aNewSymbol->SetLib( m_rescue_lib.get() );
783  m_rescue_lib->AddSymbol( aNewSymbol );
784 }
std::unique_ptr< SYMBOL_LIB > m_rescue_lib
void SetLib(SYMBOL_LIB *aLibrary)
Definition: lib_symbol.h:177

References m_rescue_lib, and LIB_SYMBOL::SetLib().

◆ DoRescues()

bool RESCUER::DoRescues ( )
inherited

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

Returns
True on success

Definition at line 551 of file project_rescue.cpp.

552 {
553  for( RESCUE_CANDIDATE* each_candidate : m_chosen_candidates )
554  {
555  if( ! each_candidate->PerformAction( this ) )
556  return false;
557  }
558 
559  return true;
560 }
std::vector< RESCUE_CANDIDATE * > m_chosen_candidates

References RESCUER::m_chosen_candidates.

Referenced by RESCUER::RescueProject().

◆ FindCandidates()

void LEGACY_RESCUER::FindCandidates ( )
overridevirtual

Populate the RESCUER with all possible candidates.

Implements RESCUER.

Definition at line 653 of file project_rescue.cpp.

654 {
657 }
boost::ptr_vector< RESCUE_CANDIDATE > m_all_candidates
static void FindRescues(RESCUER &aRescuer, boost::ptr_vector< RESCUE_CANDIDATE > &aCandidates)
Grab all possible RESCUE_CACHE_CANDIDATE objects into a vector.
static void FindRescues(RESCUER &aRescuer, boost::ptr_vector< RESCUE_CANDIDATE > &aCandidates)
Grab all possible RESCUE_CASE_CANDIDATE objects into a vector.

References RESCUE_CASE_CANDIDATE::FindRescues(), RESCUE_CACHE_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 LEGACY_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 660 of file project_rescue.cpp.

661 {
662  InvokeDialogRescueEach( aParent, static_cast< RESCUER& >( *this ), m_currentSheet,
663  m_galBackEndType, aAskShowAgain );
664 }
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 540 of file project_rescue.cpp.

542 {
543  RESCUE_LOG logitem;
544  logitem.symbol = aSymbol;
545  logitem.old_name = aOldName;
546  logitem.new_name = aNewName;
547  m_rescue_log.push_back( logitem );
548 }
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 LEGACY_RESCUER::OpenRescueLibrary ( )
overridevirtual

Implements RESCUER.

Definition at line 667 of file project_rescue.cpp.

668 {
669  wxFileName fn = GetRescueLibraryFileName( m_schematic );
670 
671  std::unique_ptr<SYMBOL_LIB> rescue_lib = std::make_unique<SYMBOL_LIB>( SCH_LIB_TYPE::LT_EESCHEMA,
672  fn.GetFullPath() );
673 
674  m_rescue_lib = std::move( rescue_lib );
675  m_rescue_lib->EnableBuffering();
676 
677  // If a rescue library already exists copy the contents of that library so we do not
678  // lose an previous rescues.
679  SYMBOL_LIB* rescueLib = m_prj->SchLibs()->FindLibrary( fn.GetName() );
680 
681  if( rescueLib )
682  {
683  // For items in the rescue library, aliases are the root symbol.
684  std::vector< LIB_SYMBOL* > symbols;
685 
686  rescueLib->GetSymbols( symbols );
687 
688  for( auto symbol : symbols )
689  {
690  // The LIB_SYMBOL copy constructor flattens derived symbols (formerly known as aliases).
691  m_rescue_lib->AddSymbol( new LIB_SYMBOL( *symbol, m_rescue_lib.get() ) );
692  }
693  }
694 }
std::unique_ptr< SYMBOL_LIB > m_rescue_lib
Object used to load, save, search, and otherwise manipulate symbol library files.
static wxFileName GetRescueLibraryFileName(SCHEMATIC *aSchematic)
Define a library symbol object.
Definition: lib_symbol.h:96
PROJECT * m_prj
void GetSymbols(std::vector< LIB_SYMBOL * > &aSymbols) const
Load a vector with all the entries in this library.
SCHEMATIC * m_schematic

References GetRescueLibraryFileName(), SYMBOL_LIB::GetSymbols(), LT_EESCHEMA, RESCUER::m_prj, m_rescue_lib, and RESCUER::m_schematic.

◆ RemoveDuplicates()

void RESCUER::RemoveDuplicates ( )
inherited

Filter out duplicately named rescue candidates.

Definition at line 622 of file project_rescue.cpp.

623 {
624  std::vector<wxString> names_seen;
625 
626  for( boost::ptr_vector<RESCUE_CANDIDATE>::iterator it = m_all_candidates.begin();
627  it != m_all_candidates.end(); )
628  {
629  bool seen_already = false;
630 
631  for( wxString& name_seen : names_seen )
632  {
633  if( name_seen == it->GetRequestedName() )
634  {
635  seen_already = true;
636  break;
637  }
638  }
639 
640  if( seen_already )
641  {
642  it = m_all_candidates.erase( it );
643  }
644  else
645  {
646  names_seen.push_back( it->GetRequestedName() );
647  ++it;
648  }
649  }
650 }
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 576 of file project_rescue.cpp.

577 {
578  aRescuer.FindCandidates();
579 
580  if( !aRescuer.GetCandidateCount() )
581  {
582  if( aRunningOnDemand )
583  {
584  wxMessageDialog dlg( aParent, _( "This project has nothing to rescue." ),
585  _( "Project Rescue Helper" ) );
586  dlg.ShowModal();
587  }
588 
589  return true;
590  }
591 
592  aRescuer.RemoveDuplicates();
593  aRescuer.InvokeDialog( aParent, !aRunningOnDemand );
594 
595  // If no symbols were rescued, let the user know what's going on. He might
596  // have clicked cancel by mistake, and should have some indication of that.
597  if( !aRescuer.GetChosenCandidateCount() )
598  {
599  wxMessageDialog dlg( aParent, _( "No symbols were rescued." ),
600  _( "Project Rescue Helper" ) );
601  dlg.ShowModal();
602 
603  // Set the modified flag even on Cancel. Many users seem to instinctively want to Save at
604  // this point, due to the reloading of the symbols, so we'll make the save button active.
605  return true;
606  }
607 
608  aRescuer.OpenRescueLibrary();
609 
610  if( !aRescuer.DoRescues() )
611  {
612  aRescuer.UndoRescues();
613  return false;
614  }
615 
616  aRescuer.WriteRescueLibrary( aParent );
617 
618  return true;
619 }
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 563 of file project_rescue.cpp.

564 {
565  for( RESCUE_LOG& each_logitem : m_rescue_log )
566  {
567  LIB_ID libId;
568 
569  libId.SetLibItemName( each_logitem.old_name );
570  each_logitem.symbol->SetLibId( libId );
571  each_logitem.symbol->ClearFlags();
572  }
573 }
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 LEGACY_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 697 of file project_rescue.cpp.

698 {
699  try
700  {
701  m_rescue_lib->Save( false );
702  }
703  catch( ... /* IO_ERROR ioe */ )
704  {
705  wxString msg;
706 
707  msg.Printf( _( "Failed to create symbol library file '%s'." ),
708  m_rescue_lib->GetFullFileName() );
709  DisplayError( aParent, msg );
710  return false;
711  }
712 
713  wxArrayString libNames;
714  wxString libPaths;
715 
716  wxString libName = m_rescue_lib->GetName();
717  SYMBOL_LIBS *libs = dynamic_cast<SYMBOL_LIBS*>( m_prj->GetElem( PROJECT::ELEM_SCH_SYMBOL_LIBS ) );
718 
719  if( !libs )
720  {
721  libs = new SYMBOL_LIBS();
723  }
724 
725  try
726  {
727  SYMBOL_LIBS::LibNamesAndPaths( m_prj, false, &libPaths, &libNames );
728 
729  // Make sure the library is not already in the list
730  while( libNames.Index( libName ) != wxNOT_FOUND )
731  libNames.Remove( libName );
732 
733  // Add the library to the top of the list and save.
734  libNames.Insert( libName, 0 );
735  SYMBOL_LIBS::LibNamesAndPaths( m_prj, true, &libPaths, &libNames );
736  }
737  catch( const IO_ERROR& )
738  {
739  // Could not get or save the current libraries.
740  return false;
741  }
742 
743  // Save the old libraries in case there is a problem after clear(). We'll
744  // put them back in.
745  boost::ptr_vector<SYMBOL_LIB> libsSave;
746  libsSave.transfer( libsSave.end(), libs->begin(), libs->end(), *libs );
747 
749 
750  libs = new SYMBOL_LIBS();
751 
752  try
753  {
754  libs->LoadAllLibraries( m_prj );
755  }
756  catch( const PARSE_ERROR& )
757  {
758  // Some libraries were not found. There's no point in showing the error,
759  // because it was already shown. Just don't do anything.
760  }
761  catch( const IO_ERROR& )
762  {
763  // Restore the old list
764  libs->clear();
765  libs->transfer( libs->end(), libsSave.begin(), libsSave.end(), libsSave );
766  return false;
767  }
768 
770 
771  // Update the schematic symbol library links since the library list has changed.
772  SCH_SCREENS schematic( m_schematic->Root() );
773  schematic.UpdateSymbolLinks();
774  return true;
775 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:279
std::unique_ptr< SYMBOL_LIB > m_rescue_lib
static void LibNamesAndPaths(PROJECT *aProject, bool doSave, wxString *aPaths, wxArrayString *aNames=nullptr)
Save or load the names of the currently configured symbol libraries (without paths).
virtual _ELEM * GetElem(ELEM_T aIndex)
Get and set the elements for this project.
Definition: project.cpp:246
void LoadAllLibraries(PROJECT *aProject, bool aShowProgress=true)
Load all of the project's libraries into this container, which should be cleared before calling it.
virtual void SetElem(ELEM_T aIndex, _ELEM *aElem)
Definition: project.cpp:258
PROJECT * m_prj
#define _(s)
A collection of SYMBOL_LIB objects.
SCH_SHEET & Root() const
Definition: schematic.h:92
A filename or source description, a problem input line, a line number, a byte offset,...
Definition: ki_exception.h:118
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:593

References _, DisplayError(), PROJECT::ELEM_SCH_SYMBOL_LIBS, PROJECT::GetElem(), SYMBOL_LIBS::LibNamesAndPaths(), SYMBOL_LIBS::LoadAllLibraries(), RESCUER::m_prj, m_rescue_lib, RESCUER::m_schematic, SCHEMATIC::Root(), PROJECT::SetElem(), and SCH_SCREENS::UpdateSymbolLinks().

Member Data Documentation

◆ m_all_candidates

◆ m_chosen_candidates

◆ m_currentSheet

SCH_SHEET_PATH* RESCUER::m_currentSheet
protectedinherited

◆ m_galBackEndType

EDA_DRAW_PANEL_GAL::GAL_TYPE RESCUER::m_galBackEndType
protectedinherited

◆ m_prj

◆ m_rescue_lib

std::unique_ptr<SYMBOL_LIB> LEGACY_RESCUER::m_rescue_lib
private

Definition at line 356 of file project_rescue.h.

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

◆ 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: