KiCad PCB EDA Suite
RESCUE_CACHE_CANDIDATE Class Reference
Inheritance diagram for RESCUE_CACHE_CANDIDATE:
RESCUE_CANDIDATE

Public Member Functions

 RESCUE_CACHE_CANDIDATE (const wxString &aRequestedName, const wxString &aNewName, LIB_PART *aCacheCandidate, LIB_PART *aLibCandidate)
 Constructor RESCUE_CACHE_CANDIDATE. More...
 
 RESCUE_CACHE_CANDIDATE ()
 
virtual wxString GetRequestedName () const override
 Function GetRequestedName Get the name that was originally requested in the schematic. More...
 
virtual wxString GetNewName () const override
 Function GetNewName Get the name we're proposing changing it to. More...
 
virtual LIB_PARTGetCacheCandidate () const override
 Function GetCacheCandidate Get the part that can be loaded from the project cache, if possible, or else NULL. More...
 
virtual LIB_PARTGetLibCandidate () const override
 Function GetLibCandidate Get the part the would be loaded from the libraries, if possible, or else NULL. More...
 
virtual wxString GetActionDescription () const override
 Function GetActionDescription Get a description of the action proposed, for displaying in the UI. More...
 
virtual bool PerformAction (RESCUER *aRescuer) override
 Function PerformAction Perform the actual rescue action. More...
 

Static Public Member Functions

static void FindRescues (RESCUER &aRescuer, boost::ptr_vector< RESCUE_CANDIDATE > &aCandidates)
 Function FindRescues Grab all possible RESCUE_CACHE_CANDIDATEs into a vector. More...
 
static void OpenRescueLibrary ()
 Function OpenRescueLibrary Creates the new rescue library. More...
 
static bool WriteRescueLibrary (SCH_EDIT_FRAME *aEditFrame, PROJECT *aProject)
 Function WriteRescueLibrary Writes out the rescue library. More...
 

Private Attributes

wxString m_requested_name
 
wxString m_new_name
 
LIB_PARTm_cache_candidate
 
LIB_PARTm_lib_candidate
 

Static Private Attributes

static std::unique_ptr< PART_LIBm_rescue_lib
 

Detailed Description

Definition at line 344 of file project_rescue.cpp.

Constructor & Destructor Documentation

RESCUE_CACHE_CANDIDATE::RESCUE_CACHE_CANDIDATE ( const wxString &  aRequestedName,
const wxString &  aNewName,
LIB_PART aCacheCandidate,
LIB_PART aLibCandidate 
)
inline

Constructor RESCUE_CACHE_CANDIDATE.

Parameters
aRequestedName- the name the schematic asks for
aNewName- the name we want to change it to
aCacheCandidate- the part from the cache
aLibCandidate- the part that would be loaded from the library

Definition at line 417 of file project_rescue.cpp.

419  : m_requested_name( aRequestedName ),
420  m_new_name( aNewName ),
421  m_cache_candidate( aCacheCandidate ),
422  m_lib_candidate( aLibCandidate ) { }
RESCUE_CACHE_CANDIDATE::RESCUE_CACHE_CANDIDATE ( )
inline

Definition at line 424 of file project_rescue.cpp.

Referenced by FindRescues().

425  : m_cache_candidate( NULL ), m_lib_candidate( NULL ) {}

Member Function Documentation

static void RESCUE_CACHE_CANDIDATE::FindRescues ( RESCUER aRescuer,
boost::ptr_vector< RESCUE_CANDIDATE > &  aCandidates 
)
inlinestatic

Function FindRescues Grab all possible RESCUE_CACHE_CANDIDATEs into a vector.

Parameters
aRescuer- the working RESCUER instance.
aCandidates- the vector the will hold the candidates.

Definition at line 360 of file project_rescue.cpp.

References find_component(), PART_LIBS::FindLibPart(), FROM_UTF8(), RESCUER::GetComponents(), RESCUER::GetLibs(), RESCUER::GetPartNameSuffix(), LIB_PART::PinsConflictWith(), and RESCUE_CACHE_CANDIDATE().

Referenced by RESCUER::FindCandidates().

361  {
362  typedef std::map<wxString, RESCUE_CACHE_CANDIDATE> candidate_map_t;
363  candidate_map_t candidate_map;
364 
365  // Remember the list of components is sorted by part name.
366  // So a search in libraries is made only once by group
367  LIB_PART* cache_match = nullptr;
368  LIB_PART* lib_match = nullptr;
369  wxString old_part_name;
370 
371  wxString part_name_suffix = aRescuer.GetPartNameSuffix();
372 
373  for( SCH_COMPONENT* each_component : *( aRescuer.GetComponents() ) )
374  {
375  wxString part_name( FROM_UTF8( each_component->GetLibId().GetLibItemName() ) );
376 
377  if( old_part_name != part_name )
378  {
379  // A new part name is found (a new group starts here).
380  // Search the symbol names candidates only once for this group:
381  old_part_name = part_name;
382  cache_match = find_component( part_name, aRescuer.GetLibs(), /* aCached */ true );
383  LIB_ID id( wxEmptyString, part_name );
384  lib_match = aRescuer.GetLibs()->FindLibPart( id );
385 
386  // Test whether there is a conflict
387  if( !cache_match || !lib_match )
388  continue;
389 
390  if( !cache_match->PinsConflictWith( *lib_match, /* aTestNums */ true,
391  /* aTestNames */ true, /* aTestType */ true, /* aTestOrientation */ true,
392  /* aTestLength */ false ))
393  continue;
394 
395  RESCUE_CACHE_CANDIDATE candidate(
396  part_name, part_name + part_name_suffix,
397  cache_match, lib_match );
398 
399  candidate_map[part_name] = candidate;
400  }
401  }
402 
403  // Now, dump the map into aCandidates
404  for( const candidate_map_t::value_type& each_pair : candidate_map )
405  {
406  aCandidates.push_back( new RESCUE_CACHE_CANDIDATE( each_pair.second ) );
407  }
408  }
bool PinsConflictWith(LIB_PART &aOtherPart, bool aTestNums, bool aTestNames, bool aTestType, bool aTestOrientation, bool aTestLength)
Function PinsConflictWith returns true if this part's pins do not match another part's pins...
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
Class LIB_ID.
Definition: lib_id.h:56
static LIB_PART * find_component(wxString aName, PART_LIBS *aLibs, bool aCached)
Function find_component Search the libraries for the first component with a given name...
Class LIB_PART defines a library part object.
LIB_PART * FindLibPart(const LIB_ID &aLibId, const wxString &aLibraryName=wxEmptyString)
Function FindLibPart searches all libraries in the list for a part.
wxString GetPartNameSuffix()
Function GetPartNameSuffix Return the suffix to add to rescued parts.
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:68
std::vector< SCH_COMPONENT * > * GetComponents()
Function GetComponents.
PART_LIBS * GetLibs()
Function GetLibs.
virtual wxString RESCUE_CACHE_CANDIDATE::GetActionDescription ( ) const
inlineoverridevirtual

Function GetActionDescription Get a description of the action proposed, for displaying in the UI.

Implements RESCUE_CANDIDATE.

Definition at line 431 of file project_rescue.cpp.

432  {
433  wxString action;
434  action.Printf( _( "Rescue %s as %s" ), m_requested_name, m_new_name );
435  return action;
436  }
virtual LIB_PART* RESCUE_CACHE_CANDIDATE::GetCacheCandidate ( ) const
inlineoverridevirtual

Function GetCacheCandidate Get the part that can be loaded from the project cache, if possible, or else NULL.

Reimplemented from RESCUE_CANDIDATE.

Definition at line 429 of file project_rescue.cpp.

References m_cache_candidate.

429 { return m_cache_candidate; }
virtual LIB_PART* RESCUE_CACHE_CANDIDATE::GetLibCandidate ( ) const
inlineoverridevirtual

Function GetLibCandidate Get the part the would be loaded from the libraries, if possible, or else NULL.

Reimplemented from RESCUE_CANDIDATE.

Definition at line 430 of file project_rescue.cpp.

References m_lib_candidate.

430 { return m_lib_candidate; }
virtual wxString RESCUE_CACHE_CANDIDATE::GetNewName ( ) const
inlineoverridevirtual

Function GetNewName Get the name we're proposing changing it to.

Implements RESCUE_CANDIDATE.

Definition at line 428 of file project_rescue.cpp.

References m_new_name.

428 { return m_new_name; }
virtual wxString RESCUE_CACHE_CANDIDATE::GetRequestedName ( ) const
inlineoverridevirtual

Function GetRequestedName Get the name that was originally requested in the schematic.

Implements RESCUE_CANDIDATE.

Definition at line 427 of file project_rescue.cpp.

References m_requested_name.

427 { return m_requested_name; }
static void RESCUE_CACHE_CANDIDATE::OpenRescueLibrary ( )
inlinestatic

Function OpenRescueLibrary Creates the new rescue library.

Must be called before calling any PerformAction()s.

Definition at line 442 of file project_rescue.cpp.

References g_RootSheet, SCH_SCREEN::GetFileName(), SCH_SHEET::GetScreen(), LIBRARY_TYPE_EESCHEMA, and SchematicLibraryFileExtension.

Referenced by SCH_EDIT_FRAME::RescueProject().

443  {
444  wxFileName fn( g_RootSheet->GetScreen()->GetFileName() );
445  fn.SetName( fn.GetName() + wxT( "-rescue" ) );
446  fn.SetExt( SchematicLibraryFileExtension );
447 
448  std::unique_ptr<PART_LIB> rescue_lib( new PART_LIB( LIBRARY_TYPE_EESCHEMA,
449  fn.GetFullPath() ) );
450 
451  m_rescue_lib = std::move( rescue_lib );
452  m_rescue_lib->EnableBuffering();
453  }
const wxString & GetFileName() const
SCH_SCREEN * GetScreen()
Definition: sch_sheet.h:286
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:54
const wxString SchematicLibraryFileExtension
static std::unique_ptr< PART_LIB > m_rescue_lib
Class PART_LIB is used to load, save, search, and otherwise manipulate part library files...
virtual bool RESCUE_CACHE_CANDIDATE::PerformAction ( RESCUER aRescuer)
inlineoverridevirtual

Function PerformAction Perform the actual rescue action.

If successful, this must log the rescue using RESCUER::LogRescue to allow it to be reversed.

Returns
True on success.

Implements RESCUE_CANDIDATE.

Definition at line 455 of file project_rescue.cpp.

References RESCUER::GetComponents(), RESCUER::LogRescue(), m_rescue_lib, LIB_ID::SetLibItemName(), LIB_PART::SetName(), and TO_UTF8.

456  {
457  LIB_PART new_part( *m_cache_candidate, m_rescue_lib.get() );
458  new_part.SetName( m_new_name );
459  new_part.RemoveAllAliases();
460  RESCUE_CACHE_CANDIDATE::m_rescue_lib.get()->AddPart( &new_part );
461 
462  for( SCH_COMPONENT* each_component : *aRescuer->GetComponents() )
463  {
464  if( each_component->GetLibId().GetLibItemName() != TO_UTF8( m_requested_name ) )
465  continue;
466 
467  LIB_ID libId;
468 
469  libId.SetLibItemName( TO_UTF8( m_new_name ), false );
470  each_component->SetLibId( libId );
471  each_component->ClearFlags();
472  aRescuer->LogRescue( each_component, m_requested_name, m_new_name );
473  }
474  return true;
475  }
Class LIB_ID.
Definition: lib_id.h:56
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
Class LIB_PART defines a library part object.
int SetLibItemName(const UTF8 &aLibItemName, bool aTestForRev=true)
Function SetLibItemName.
Definition: lib_id.cpp:232
static std::unique_ptr< PART_LIB > m_rescue_lib
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:68
std::vector< SCH_COMPONENT * > * GetComponents()
Function GetComponents.
void LogRescue(SCH_COMPONENT *aComponent, const wxString &aOldName, const wxString &aNewName)
Function LogRescue Used by individual RESCUE_CANDIDATEs to log a rescue for undoing.
virtual void SetName(const wxString &aName)
static bool RESCUE_CACHE_CANDIDATE::WriteRescueLibrary ( SCH_EDIT_FRAME aEditFrame,
PROJECT aProject 
)
inlinestatic

Function WriteRescueLibrary Writes out the rescue library.

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

Returns
True on success.

Definition at line 483 of file project_rescue.cpp.

References insert_library(), and save_library().

Referenced by SCH_EDIT_FRAME::RescueProject().

484  {
485  if( !save_library( m_rescue_lib.get(), aEditFrame ) )
486  return false;
487  return insert_library( aProject, m_rescue_lib.get(), 0 );
488  }
static std::unique_ptr< PART_LIB > m_rescue_lib
static bool insert_library(PROJECT *aProject, PART_LIB *aLibrary, size_t aIndex)
Function insert_library inserts a library into the project and refreshes libraries.
static bool save_library(PART_LIB *aLibrary, SCH_EDIT_FRAME *aEditFrame)
Function save_library writes the library out to disk.

Member Data Documentation

LIB_PART* RESCUE_CACHE_CANDIDATE::m_cache_candidate
private

Definition at line 348 of file project_rescue.cpp.

Referenced by GetCacheCandidate().

LIB_PART* RESCUE_CACHE_CANDIDATE::m_lib_candidate
private

Definition at line 349 of file project_rescue.cpp.

Referenced by GetLibCandidate().

wxString RESCUE_CACHE_CANDIDATE::m_new_name
private

Definition at line 347 of file project_rescue.cpp.

Referenced by GetNewName().

wxString RESCUE_CACHE_CANDIDATE::m_requested_name
private

Definition at line 346 of file project_rescue.cpp.

Referenced by GetRequestedName().

std::unique_ptr< PART_LIB > RESCUE_CACHE_CANDIDATE::m_rescue_lib
staticprivate

Definition at line 351 of file project_rescue.cpp.

Referenced by PerformAction().


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