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, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType)
 
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
 Writes out the rescue library. More...
 
virtual void AddPart (LIB_PART *aNewPart) override
 
void RemoveDuplicates ()
 Filter out duplicately named rescue candidates. More...
 
size_t GetCandidateCount ()
 Returen the number of rescue candidates found. More...
 
size_t GetChosenCandidateCount ()
 Get the number of resuce candidates chosen by the user. More...
 
std::vector< SCH_COMPONENT * > * GetComponents ()
 Get the list of symbols that need rescued. More...
 
PROJECTGetPrj ()
 Return the #SCH_PROJECT object for access to the symbol libraries. More...
 
void LogRescue (SCH_COMPONENT *aComponent, 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_COMPONENT * > m_components
 
PROJECTm_prj
 
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 331 of file project_rescue.h.

Constructor & Destructor Documentation

◆ SYMBOL_LIB_TABLE_RESCUER()

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

Definition at line 785 of file project_rescue.cpp.

787  :
788  RESCUER( aProject, aCurrentSheet, aGalBackEndType )
789 {
790  m_properties = std::make_unique<PROPERTIES>();
791 }
RESCUER(PROJECT &aProject, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType)
std::unique_ptr< PROPERTIES > m_properties
Library plugin properties.

References m_properties.

Member Function Documentation

◆ AddPart()

void SYMBOL_LIB_TABLE_RESCUER::AddPart ( LIB_PART aNewPart)
overridevirtual

Implements RESCUER.

Definition at line 874 of file project_rescue.cpp.

875 {
876  wxCHECK_RET( aNewPart, "Invalid LIB_PART pointer." );
877 
878  wxFileName fn = GetRescueLibraryFileName();
879 
880  try
881  {
882  if( !m_prj->SchSymbolLibTable()->HasLibrary( fn.GetName() ) )
883  m_pi->SaveSymbol( fn.GetFullPath(), new LIB_PART( *aNewPart ), m_properties.get() );
884  else
885  m_prj->SchSymbolLibTable()->SaveSymbol( fn.GetName(), new LIB_PART( *aNewPart ) );
886  }
887  catch( ... /* IO_ERROR */ )
888  {
889  }
890 }
SCH_PLUGIN::SCH_PLUGIN_RELEASER m_pi
std::unique_ptr< PROPERTIES > m_properties
Library plugin properties.
Define a library symbol object.
PROJECT * m_prj
static wxFileName GetRescueLibraryFileName()

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

◆ DoRescues()

bool RESCUER::DoRescues ( )
inherited

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

Returns
True on success

Definition at line 501 of file project_rescue.cpp.

502 {
503  for( RESCUE_CANDIDATE* each_candidate : m_chosen_candidates )
504  {
505  if( ! each_candidate->PerformAction( this ) )
506  return false;
507  }
508 
509  return true;
510 }
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 794 of file project_rescue.cpp.

795 {
797 }
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

Returen the number of rescue candidates found.

Definition at line 268 of file project_rescue.h.

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

References RESCUER::m_all_candidates.

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

◆ GetChosenCandidateCount()

size_t RESCUER::GetChosenCandidateCount ( )
inlineinherited

Get the number of resuce candidates chosen by the user.

Definition at line 273 of file project_rescue.h.

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

References RESCUER::m_chosen_candidates.

Referenced by RESCUER::RescueProject().

◆ GetComponents()

◆ GetPrj()

PROJECT* RESCUER::GetPrj ( )
inlineinherited

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

Definition at line 283 of file project_rescue.h.

283 { 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().

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

801 {
802  InvokeDialogRescueEach( aParent, static_cast< RESCUER& >( *this ), m_currentSheet,
803  m_galBackEndType, aAskShowAgain );
804 }
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_COMPONENT aComponent,
const wxString &  aOldName,
const wxString &  aNewName 
)
inherited

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

Definition at line 490 of file project_rescue.cpp.

492 {
493  RESCUE_LOG logitem;
494  logitem.component = aComponent;
495  logitem.old_name = aOldName;
496  logitem.new_name = aNewName;
497  m_rescue_log.push_back( logitem );
498 }
SCH_COMPONENT * component
std::vector< RESCUE_LOG > m_rescue_log
wxString old_name
wxString new_name

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

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

808 {
809  m_pi.set( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
810  (*m_properties)[ SCH_LEGACY_PLUGIN::PropBuffering ] = "";
811 }
void set(SCH_PLUGIN *aPlugin)
Definition: sch_io_mgr.h:525
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 619 of file project_rescue.cpp.

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

574 {
575  aRescuer.FindCandidates();
576 
577  if( !aRescuer.GetCandidateCount() )
578  {
579  if( aRunningOnDemand )
580  {
581  wxMessageDialog dlg( aParent, _( "This project has nothing to rescue." ),
582  _( "Project Rescue Helper" ) );
583  dlg.ShowModal();
584  }
585 
586  return true;
587  }
588 
589  aRescuer.RemoveDuplicates();
590  aRescuer.InvokeDialog( aParent, !aRunningOnDemand );
591 
592  // If no symbols were rescued, let the user know what's going on. He might
593  // have clicked cancel by mistake, and should have some indication of that.
594  if( !aRescuer.GetChosenCandidateCount() )
595  {
596  wxMessageDialog dlg( aParent, _( "No symbols were rescued." ),
597  _( "Project Rescue Helper" ) );
598  dlg.ShowModal();
599 
600  // Set the modified flag even on Cancel. Many users seem to instinctively want to Save at
601  // this point, due to the reloading of the symbols, so we'll make the save button active.
602  return true;
603  }
604 
605  aRescuer.OpenRescueLibrary();
606 
607  if( !aRescuer.DoRescues() )
608  {
609  aRescuer.UndoRescues();
610  return false;
611  }
612 
613  aRescuer.WriteRescueLibrary( aParent );
614 
615  return true;
616 }
virtual bool WriteRescueLibrary(wxWindow *aParent)=0
Writes out the rescue library.
virtual void FindCandidates()=0
Populate the RESCUER with all possible candidates.
size_t GetChosenCandidateCount()
Get the number of resuce 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()
Returen the number of rescue candidates found.
virtual void OpenRescueLibrary()=0
bool DoRescues()
Perform all chosen rescue actions, logging them to be undone if necessary.
#define _(s)
Definition: 3d_actions.cpp:31
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_EDIT_FRAME::rescueProject().

◆ UndoRescues()

void RESCUER::UndoRescues ( )
inherited

Reverse the effects of all rescues on the project.

Definition at line 513 of file project_rescue.cpp.

514 {
515  for( RESCUE_LOG& each_logitem : m_rescue_log )
516  {
517  LIB_ID libId;
518 
519  libId.SetLibItemName( each_logitem.old_name, false );
520  each_logitem.component->SetLibId( libId );
521  each_logitem.component->ClearFlags();
522  }
523 }
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
int SetLibItemName(const UTF8 &aLibItemName, bool aTestForRev=true)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition: lib_id.cpp:206
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

Writes out the rescue library.

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

Returns
True on success.

Implements RESCUER.

Definition at line 814 of file project_rescue.cpp.

815 {
816  wxString msg;
817  wxFileName fn = GetRescueLibraryFileName();
818 
819  // If the rescue library already exists in the symbol library table no need save it to add
820  // it to the table.
821  if( !m_prj->SchSymbolLibTable()->HasLibrary( fn.GetName() ) )
822  {
823  try
824  {
825  m_pi->SaveLibrary( fn.GetFullPath() );
826  }
827  catch( const IO_ERROR& ioe )
828  {
829  msg.Printf( _( "Failed to save rescue library %s." ), fn.GetFullPath() );
830  DisplayErrorMessage( aParent, msg, ioe.What() );
831  return false;
832  }
833 
834  wxString uri = "${KIPRJMOD}/" + fn.GetFullName();
835  wxString libNickname = fn.GetName();
836 
837  // Spaces in the file name will break the symbol name because they are not
838  // quoted in the symbol library file format.
839  libNickname.Replace( " ", "-" );
840 
841  SYMBOL_LIB_TABLE_ROW* row = new SYMBOL_LIB_TABLE_ROW( libNickname, uri,
842  wxString( "Legacy" ) );
843  m_prj->SchSymbolLibTable()->InsertRow( row );
844 
846 
847  try
848  {
849  m_prj->SchSymbolLibTable()->Save( fn.GetFullPath() );
850  }
851  catch( const IO_ERROR& ioe )
852  {
853  msg.Printf( _( "Error occurred saving project specific symbol library table." ) );
854  DisplayErrorMessage( aParent, msg, ioe.What() );
855  return false;
856  }
857  }
858 
859  // Relaod the symbol library table.
861 
862  // This can only happen if the symbol library table file was currupted on write.
863  if( !m_prj->SchSymbolLibTable() )
864  return false;
865 
866  // Update the schematic symbol library links since the library list has changed.
867  SCH_SCREENS schematic;
868 
869  schematic.UpdateSymbolLinks( true );
870  return true;
871 }
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:249
VTBL_ENTRY const wxString GetProjectPath() const
Function GetProjectPath returns the full path of the project.
Definition: project.cpp:102
VTBL_ENTRY void SetElem(ELEM_T aIndex, _ELEM *aElem)
Definition: project.cpp:217
#define NULL
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
SCH_PLUGIN::SCH_PLUGIN_RELEASER m_pi
PROJECT * m_prj
static wxFileName GetRescueLibraryFileName()
void UpdateSymbolLinks(bool aForce=false)
Initialize or reinitialize the weak reference to the LIB_PART for each SCH_COMPONENT found in the ful...
#define _(s)
Definition: 3d_actions.cpp:31
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:498

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

Member Data Documentation

◆ m_all_candidates

◆ m_chosen_candidates

◆ m_components

std::vector<SCH_COMPONENT*> RESCUER::m_components
protectedinherited

Definition at line 222 of file project_rescue.h.

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

◆ m_currentSheet

SCH_SHEET_PATH* RESCUER::m_currentSheet
protectedinherited

Definition at line 225 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 224 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 334 of file project_rescue.h.

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

◆ m_prj

◆ m_properties

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

Library plugin properties.

Definition at line 336 of file project_rescue.h.

Referenced by AddPart(), and SYMBOL_LIB_TABLE_RESCUER().

◆ m_rescue_log

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

Definition at line 230 of file project_rescue.h.

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


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