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

std::vector< std::unique_ptr< LIB_SYMBOL > > m_rescueLibSymbols
 
std::unique_ptr< STRING_UTF8_MAPm_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 798 of file project_rescue.cpp.

800 :
801 RESCUER( aProject, aSchematic, aCurrentSheet, aGalBackEndType )
802{
803 m_properties = std::make_unique<STRING_UTF8_MAP>();
804}
RESCUER(PROJECT &aProject, SCHEMATIC *aSchematic, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType)
std::unique_ptr< STRING_UTF8_MAP > 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 913 of file project_rescue.cpp.

914{
915 wxCHECK_RET( aNewSymbol, wxS( "Invalid LIB_SYMBOL pointer." ) );
916
917 m_rescueLibSymbols.emplace_back( std::make_unique<LIB_SYMBOL>( *aNewSymbol ) );
918}
std::vector< std::unique_ptr< LIB_SYMBOL > > m_rescueLibSymbols

References m_rescueLibSymbols.

◆ DoRescues()

bool RESCUER::DoRescues ( )
inherited

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

Returns
True on success

Definition at line 562 of file project_rescue.cpp.

563{
564 for( RESCUE_CANDIDATE* each_candidate : m_chosen_candidates )
565 {
566 if( ! each_candidate->PerformAction( this ) )
567 return false;
568 }
569
570 return true;
571}
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 807 of file project_rescue.cpp.

808{
810}
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(); }

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(); }

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 813 of file project_rescue.cpp.

814{
815 InvokeDialogRescueEach( aParent, static_cast< RESCUER& >( *this ), m_currentSheet,
816 m_galBackEndType, aAskShowAgain );
817}
SCH_SHEET_PATH * m_currentSheet
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.

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 551 of file project_rescue.cpp.

553{
554 RESCUE_LOG logitem;
555 logitem.symbol = aSymbol;
556 logitem.old_name = aOldName;
557 logitem.new_name = aNewName;
558 m_rescue_log.push_back( logitem );
559}
std::vector< RESCUE_LOG > m_rescue_log
wxString old_name
wxString new_name
SCH_SYMBOL * symbol

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 820 of file project_rescue.cpp.

821{
822 (*m_properties)[ SCH_LEGACY_PLUGIN::PropBuffering ] = "";
823
824 wxFileName fn = GetRescueLibraryFileName( m_schematic );
825
826 SYMBOL_LIB_TABLE_ROW* row = m_prj->SchSymbolLibTable()->FindRow( fn.GetName() );
827
828 // If a rescue library already exists copy the contents of that library so we do not
829 // lose any previous rescues.
830 if( row )
831 {
832 if( SCH_IO_MGR::EnumFromStr( row->GetType() ) == SCH_IO_MGR::SCH_KICAD )
833 fn.SetExt( KiCadSymbolLibFileExtension );
834
835 std::vector<LIB_SYMBOL*> symbols;
836
837 try
838 {
839 m_prj->SchSymbolLibTable()->LoadSymbolLib( symbols, fn.GetName() );
840 }
841 catch( ... /* IO_ERROR */ )
842 {
843 return;
844 }
845
846 for( LIB_SYMBOL* symbol : symbols )
847 m_rescueLibSymbols.emplace_back( std::make_unique<LIB_SYMBOL>( *symbol ) );
848 }
849}
Define a library symbol object.
Definition: lib_symbol.h:99
SCHEMATIC * m_schematic
static SCH_FILE_T EnumFromStr(const wxString &aFileType)
Return the #SCH_FILE_T from the corresponding plugin type name: "kicad", "legacy",...
Definition: sch_io_mgr.cpp:101
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
const wxString GetType() const override
Return the type of symbol library table represented by this row.
const std::string KiCadSymbolLibFileExtension
static wxFileName GetRescueLibraryFileName(SCHEMATIC *aSchematic)

References SCH_IO_MGR::EnumFromStr(), GetRescueLibraryFileName(), SYMBOL_LIB_TABLE_ROW::GetType(), KiCadSymbolLibFileExtension, RESCUER::m_prj, m_rescueLibSymbols, RESCUER::m_schematic, and SCH_LEGACY_PLUGIN::PropBuffering.

◆ RemoveDuplicates()

void RESCUER::RemoveDuplicates ( )
inherited

Filter out duplicately named rescue candidates.

Definition at line 633 of file project_rescue.cpp.

634{
635 std::vector<wxString> names_seen;
636
637 for( boost::ptr_vector<RESCUE_CANDIDATE>::iterator it = m_all_candidates.begin();
638 it != m_all_candidates.end(); )
639 {
640 bool seen_already = false;
641
642 for( wxString& name_seen : names_seen )
643 {
644 if( name_seen == it->GetRequestedName() )
645 {
646 seen_already = true;
647 break;
648 }
649 }
650
651 if( seen_already )
652 {
653 it = m_all_candidates.erase( it );
654 }
655 else
656 {
657 names_seen.push_back( it->GetRequestedName() );
658 ++it;
659 }
660 }
661}

References RESCUER::m_all_candidates.

Referenced by RESCUER::RescueProject().

◆ RescueProject()

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

Definition at line 587 of file project_rescue.cpp.

588{
589 aRescuer.FindCandidates();
590
591 if( !aRescuer.GetCandidateCount() )
592 {
593 if( aRunningOnDemand )
594 {
595 wxMessageDialog dlg( aParent, _( "This project has nothing to rescue." ),
596 _( "Project Rescue Helper" ) );
597 dlg.ShowModal();
598 }
599
600 return true;
601 }
602
603 aRescuer.RemoveDuplicates();
604 aRescuer.InvokeDialog( aParent, !aRunningOnDemand );
605
606 // If no symbols were rescued, let the user know what's going on. He might
607 // have clicked cancel by mistake, and should have some indication of that.
608 if( !aRescuer.GetChosenCandidateCount() )
609 {
610 wxMessageDialog dlg( aParent, _( "No symbols were rescued." ),
611 _( "Project Rescue Helper" ) );
612 dlg.ShowModal();
613
614 // Set the modified flag even on Cancel. Many users seem to instinctively want to Save at
615 // this point, due to the reloading of the symbols, so we'll make the save button active.
616 return true;
617 }
618
619 aRescuer.OpenRescueLibrary();
620
621 if( !aRescuer.DoRescues() )
622 {
623 aRescuer.UndoRescues();
624 return false;
625 }
626
627 aRescuer.WriteRescueLibrary( aParent );
628
629 return true;
630}
virtual bool WriteRescueLibrary(wxWindow *aParent)=0
Write the rescue library.
void UndoRescues()
Reverse the effects of all rescues on the project.
bool DoRescues()
Perform all chosen rescue actions, logging them to be undone if necessary.
size_t GetCandidateCount()
Return the number of rescue candidates found.
virtual void InvokeDialog(wxWindow *aParent, bool aAskShowAgain)=0
Display a dialog to allow the user to select rescues.
virtual void OpenRescueLibrary()=0
size_t GetChosenCandidateCount()
Get the number of rescue candidates chosen by the user.
virtual void FindCandidates()=0
Populate the RESCUER with all possible candidates.
void RemoveDuplicates()
Filter out duplicately named rescue candidates.
#define _(s)

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; }

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 574 of file project_rescue.cpp.

575{
576 for( RESCUE_LOG& each_logitem : m_rescue_log )
577 {
578 LIB_ID libId;
579
580 libId.SetLibItemName( each_logitem.old_name );
581 each_logitem.symbol->SetLibId( libId );
582 each_logitem.symbol->ClearFlags();
583 }
584}
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition: lib_id.cpp:109

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 852 of file project_rescue.cpp.

853{
854 wxString msg;
855 wxFileName fn = GetRescueLibraryFileName( m_schematic );
856 SYMBOL_LIB_TABLE_ROW* row = m_prj->SchSymbolLibTable()->FindRow( fn.GetName() );
857
858 fn.SetExt( KiCadSymbolLibFileExtension );
859
860 try
861 {
862 SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
863
864 for( const std::unique_ptr<LIB_SYMBOL>& symbol : m_rescueLibSymbols )
865 pi->SaveSymbol( fn.GetFullPath(), new LIB_SYMBOL( *symbol.get() ), m_properties.get() );
866
867 pi->SaveLibrary( fn.GetFullPath() );
868 }
869 catch( const IO_ERROR& ioe )
870 {
871 msg.Printf( _( "Failed to save rescue library %s." ), fn.GetFullPath() );
872 DisplayErrorMessage( aParent, msg, ioe.What() );
873 return false;
874 }
875
876 // If the rescue library already exists in the symbol library table no need save it to add
877 // it to the table.
878 if( !row || ( SCH_IO_MGR::EnumFromStr( row->GetType() ) == SCH_IO_MGR::SCH_LEGACY ) )
879 {
880 wxString uri = wxS( "${KIPRJMOD}/" ) + fn.GetFullName();
881 wxString libNickname = fn.GetName();
882
883 row = new SYMBOL_LIB_TABLE_ROW( libNickname, uri, wxT( "KiCad" ) );
884 m_prj->SchSymbolLibTable()->InsertRow( row, true );
885
887
888 try
889 {
890 m_prj->SchSymbolLibTable()->Save( fn.GetFullPath() );
891 }
892 catch( const IO_ERROR& ioe )
893 {
894 msg.Printf( _( "Error occurred saving project specific symbol library table." ) );
895 DisplayErrorMessage( aParent, msg, ioe.What() );
896 return false;
897 }
898 }
899
901
902 // This can only happen if the symbol library table file was corrupted on write.
903 if( !m_prj->SchSymbolLibTable() )
904 return false;
905
906 // Update the schematic symbol library links since the library list has changed.
907 SCH_SCREENS schematic( m_schematic->Root() );
908 schematic.UpdateSymbolLinks();
909 return true;
910}
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:126
virtual void SetElem(ELEM_T aIndex, _ELEM *aElem)
Definition: project.cpp:294
@ ELEM_SYMBOL_LIB_TABLE
Definition: project.h:211
SCH_SHEET & Root() const
Definition: schematic.h:91
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:535
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:662
static const wxString & GetSymbolLibTableFileName()
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:325

References _, DisplayErrorMessage(), PROJECT::ELEM_SYMBOL_LIB_TABLE, SCH_IO_MGR::EnumFromStr(), PROJECT::GetProjectPath(), GetRescueLibraryFileName(), SYMBOL_LIB_TABLE::GetSymbolLibTableFileName(), SYMBOL_LIB_TABLE_ROW::GetType(), KiCadSymbolLibFileExtension, RESCUER::m_prj, m_properties, m_rescueLibSymbols, RESCUER::m_schematic, SCHEMATIC::Root(), SCH_PLUGIN::SaveLibrary(), SCH_PLUGIN::SaveSymbol(), 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_prj

◆ m_properties

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

Library plugin properties.

Definition at line 384 of file project_rescue.h.

Referenced by SYMBOL_LIB_TABLE_RESCUER(), 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_rescueLibSymbols

std::vector<std::unique_ptr<LIB_SYMBOL> > SYMBOL_LIB_TABLE_RESCUER::m_rescueLibSymbols
private

Definition at line 382 of file project_rescue.h.

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

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