KiCad PCB EDA Suite
SYMBOL_LIBRARY_MANAGER Class Reference

Class to handle modifications to the symbol libraries. More...

#include <symbol_library_manager.h>

Classes

class  LIB_BUFFER
 
class  PART_BUFFER
 Store a working copy of a library. More...
 

Public Member Functions

 SYMBOL_LIBRARY_MANAGER (SYMBOL_EDIT_FRAME &aFrame)
 
void Sync (const wxString &aForceRefresh, std::function< void(int, int, const wxString &)> aProgressCallback)
 Updates the SYMBOL_LIBRARY_MANAGER data to synchronize with Symbol Library Table. More...
 
void Preload (PROGRESS_REPORTER &aReporter)
 Preloads all symbol libraries in the symbol library table using SYMBOL_ASYNC_LOADER. More...
 
int GetHash () const
 
bool HasModifications () const
 
int GetLibraryHash (const wxString &aLibrary) const
 Return a library hash value to determine if it has changed. More...
 
wxArrayString GetLibraryNames () const
 Return the array of library names. More...
 
SYMBOL_LIB_TABLE_ROWGetLibrary (const wxString &aLibrary) const
 Find a single library within the (aggregate) library table. More...
 
std::list< LIB_PART * > GetAliases (const wxString &aLibrary) const
 
bool CreateLibrary (const wxString &aFilePath, SYMBOL_LIB_TABLE *aTable)
 Create an empty library and adds it to the library table. More...
 
bool AddLibrary (const wxString &aFilePath, SYMBOL_LIB_TABLE *aTable)
 Add an existing library. More...
 
bool UpdatePart (LIB_PART *aPart, const wxString &aLibrary)
 Update the part buffer with a new version of the part. More...
 
bool UpdatePartAfterRename (LIB_PART *aPart, const wxString &oldAlias, const wxString &aLibrary)
 Update the part buffer with a new version of the part when the name has changed. More...
 
bool RemovePart (const wxString &aName, const wxString &aLibrary)
 Remove the part from the part buffer. More...
 
LIB_PARTGetAlias (const wxString &aAlias, const wxString &aLibrary) const
 Return either an alias of a working LIB_PART copy, or alias of the original part if there is no working copy. More...
 
LIB_PARTGetBufferedPart (const wxString &aAlias, const wxString &aLibrary)
 Return the part copy from the buffer. More...
 
SCH_SCREENGetScreen (const wxString &aAlias, const wxString &aLibrary)
 Return the screen used to edit a specific part. More...
 
bool PartExists (const wxString &aAlias, const wxString &aLibrary) const
 Return true if part with a specific alias exists in library (either original one or buffered). More...
 
bool LibraryExists (const wxString &aLibrary, bool aCheckEnabled=false) const
 Return true if library exists. More...
 
bool IsLibraryLoaded (const wxString &aLibrary) const
 Return true if the library was successfully loaded. More...
 
bool IsLibraryModified (const wxString &aLibrary) const
 Return true if library has unsaved modifications. More...
 
bool IsPartModified (const wxString &aAlias, const wxString &aLibrary) const
 Return true if part has unsaved modifications. More...
 
bool ClearLibraryModified (const wxString &aLibrary) const
 Clear the modified flag for all parts in a library. More...
 
bool ClearPartModified (const wxString &aAlias, const wxString &aLibrary) const
 Clear the modified flag for a part. More...
 
bool IsLibraryReadOnly (const wxString &aLibrary) const
 Return true if the library is stored in a read-only file. More...
 
bool FlushPart (const wxString &aAlias, const wxString &aLibrary)
 Save part changes to the library copy used by the schematic editor. More...
 
bool SaveLibrary (const wxString &aLibrary, const wxString &aFileName, SCH_IO_MGR::SCH_FILE_T aFileType=SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY)
 Save library to a file, including unsaved changes. More...
 
LIB_ID RevertPart (const wxString &aAlias, const wxString &aLibrary)
 Revert unsaved changes for a particular part. More...
 
bool RevertLibrary (const wxString &aLibrary)
 Revert unsaved changes for a particular library. More...
 
bool RevertAll ()
 Revert all pending changes. More...
 
wxString GetUniqueLibraryName () const
 Return a library name that is not currently in use. More...
 
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > & GetAdapter ()
 Return the adapter object that provides the stored data. More...
 
void GetRootSymbolNames (const wxString &aLibName, wxArrayString &aRootSymbolNames)
 
bool HasDerivedSymbols (const wxString &aSymbolName, const wxString &aLibraryName)
 Check if symbol aSymbolName in library aLibraryName is a root symbol that has derived symbols. More...
 
size_t GetLibraryCount () const
 

Private Member Functions

bool addLibrary (const wxString &aFilePath, bool aCreate, SYMBOL_LIB_TABLE *aTable)
 Return the current Symbol Library Table. More...
 
SYMBOL_LIB_TABLEsymTable () const
 
SYMBOL_TREE_SYNCHRONIZING_ADAPTERgetAdapter ()
 Class to store a working copy of a LIB_PART object and editor context. More...
 
std::set< LIB_PART * > getOriginalParts (const wxString &aLibrary)
 Return a set of LIB_PART objects belonging to the original library. More...
 
LIB_BUFFERgetLibraryBuffer (const wxString &aLibrary)
 Return an existing library buffer or creates one to using Symbol Library Table to get the original data. More...
 

Static Private Member Functions

static wxString getLibraryName (const wxString &aFilePath)
 < Extract library name basing on the file name. More...
 

Private Attributes

std::map< wxString, LIB_BUFFERm_libs
 
SYMBOL_EDIT_FRAMEm_frame
 Parent frame. More...
 
LIB_LOGGER m_logger
 
int m_syncHash
 Symbol lib table hash value from last synchronization. More...
 
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTERm_adapter
 

Detailed Description

Class to handle modifications to the symbol libraries.

Definition at line 101 of file symbol_library_manager.h.

Constructor & Destructor Documentation

◆ SYMBOL_LIBRARY_MANAGER()

SYMBOL_LIBRARY_MANAGER::SYMBOL_LIBRARY_MANAGER ( SYMBOL_EDIT_FRAME aFrame)

Definition at line 43 of file symbol_library_manager.cpp.

43  :
44  m_frame( aFrame ),
45  m_syncHash( 0 )
46 {
48  m_adapter->ShowUnits( false );
49 }
static wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > Create(SYMBOL_EDIT_FRAME *aParent, SYMBOL_LIBRARY_MANAGER *aLibs)
SYMBOL_EDIT_FRAME & m_frame
Parent frame.
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
int m_syncHash
Symbol lib table hash value from last synchronization.

References SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Create(), m_adapter, and m_frame.

Member Function Documentation

◆ AddLibrary()

bool SYMBOL_LIBRARY_MANAGER::AddLibrary ( const wxString &  aFilePath,
SYMBOL_LIB_TABLE aTable 
)
inline

Add an existing library.

The library is added to the library table as well.

Definition at line 155 of file symbol_library_manager.h.

156  {
157  return addLibrary( aFilePath, false, aTable );
158  }
bool addLibrary(const wxString &aFilePath, bool aCreate, SYMBOL_LIB_TABLE *aTable)
Return the current Symbol Library Table.

References addLibrary().

Referenced by SYMBOL_EDIT_FRAME::AddLibraryFile(), and SYMBOL_EDIT_FRAME::ExportPart().

◆ addLibrary()

bool SYMBOL_LIBRARY_MANAGER::addLibrary ( const wxString &  aFilePath,
bool  aCreate,
SYMBOL_LIB_TABLE aTable 
)
private

Return the current Symbol Library Table.

Definition at line 700 of file symbol_library_manager.cpp.

702 {
703  wxCHECK( aTable, false );
704  wxString libName = getLibraryName( aFilePath );
705  wxCHECK( !LibraryExists( libName ), false ); // either create or add an existing one
706 
707  // try to use path normalized to an environmental variable or project path
708  wxString relPath = NormalizePath( aFilePath, &Pgm().GetLocalEnvVariables(), &m_frame.Prj() );
709 
710  if( relPath.IsEmpty() )
711  relPath = aFilePath;
712 
713  SCH_IO_MGR::SCH_FILE_T schFileType = SCH_IO_MGR::GuessPluginTypeFromLibPath( aFilePath );
714  wxString typeName = SCH_IO_MGR::ShowType( schFileType );
715  SYMBOL_LIB_TABLE_ROW* libRow = new SYMBOL_LIB_TABLE_ROW( libName, relPath, typeName );
716  aTable->InsertRow( libRow );
717 
718  if( aCreate )
719  {
720  wxCHECK( schFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY, false );
721 
722  try
723  {
724  aTable->CreateSymbolLib( libName );
725  }
726  catch( const IO_ERROR& )
727  {
728  aTable->RemoveRow( libRow );
729  return false;
730  }
731  }
732 
733  m_frame.SyncLibraries( false );
734 
735  return true;
736 }
static SCH_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath)
Return a plugin type given a symbol library using the file extension of aLibPath.
Definition: sch_io_mgr.cpp:151
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
void CreateSymbolLib(const wxString &aNickname)
bool InsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Adds aRow if it does not already exist or if doReplace is true.
static const wxString ShowType(SCH_FILE_T aFileType)
Return a brief name for a plugin, given aFileType enum.
Definition: sch_io_mgr.cpp:79
static wxString getLibraryName(const wxString &aFilePath)
< Extract library name basing on the file name.
SYMBOL_EDIT_FRAME & m_frame
Parent frame.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
bool RemoveRow(LIB_TABLE_ROW *aRow)
Removes a row from the table.
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.
void SyncLibraries(bool aShowProgress, const wxString &aForceRefresh=wxEmptyString)
Synchronize the library manager to the symbol library table, and then the symbol tree to the library ...
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
wxString NormalizePath(const wxFileName &aFilePath, const ENV_VAR_MAP *aEnvVars, const wxString &aProjectPath)
Normalize a file path to an environmental variable, if possible.
Definition: env_paths.cpp:67

References SYMBOL_LIB_TABLE::CreateSymbolLib(), getLibraryName(), SCH_IO_MGR::GuessPluginTypeFromLibPath(), LIB_TABLE::InsertRow(), LibraryExists(), m_frame, NormalizePath(), Pgm(), KIWAY_HOLDER::Prj(), LIB_TABLE::RemoveRow(), SCH_IO_MGR::ShowType(), and SYMBOL_EDIT_FRAME::SyncLibraries().

Referenced by AddLibrary(), and CreateLibrary().

◆ ClearLibraryModified()

bool SYMBOL_LIBRARY_MANAGER::ClearLibraryModified ( const wxString &  aLibrary) const

Clear the modified flag for all parts in a library.

Definition at line 289 of file symbol_library_manager.cpp.

290 {
291  auto libIt = m_libs.find( aLibrary );
292 
293  if( libIt == m_libs.end() )
294  return false;
295 
296  for( auto& partBuf : libIt->second.GetBuffers() )
297  {
298  SCH_SCREEN* screen = partBuf->GetScreen();
299 
300  if( screen )
301  screen->ClrModify();
302  }
303 
304  return true;
305 }
std::map< wxString, LIB_BUFFER > m_libs
void ClrModify()
Definition: base_screen.h:60

References BASE_SCREEN::ClrModify(), and m_libs.

Referenced by SYMBOL_EDIT_FRAME::saveLibrary().

◆ ClearPartModified()

bool SYMBOL_LIBRARY_MANAGER::ClearPartModified ( const wxString &  aAlias,
const wxString &  aLibrary 
) const

Clear the modified flag for a part.

Definition at line 308 of file symbol_library_manager.cpp.

310 {
311  auto libI = m_libs.find( aLibrary );
312 
313  if( libI == m_libs.end() )
314  return false;
315 
316  auto partBuf = libI->second.GetBuffer( aAlias );
317  wxCHECK( partBuf, false );
318 
319  partBuf->GetScreen()->ClrModify();
320  return true;
321 }
std::map< wxString, LIB_BUFFER > m_libs

References m_libs.

Referenced by SYMBOL_EDIT_FRAME::Revert(), and SYMBOL_EDIT_FRAME::saveCurrentPart().

◆ CreateLibrary()

bool SYMBOL_LIBRARY_MANAGER::CreateLibrary ( const wxString &  aFilePath,
SYMBOL_LIB_TABLE aTable 
)
inline

Create an empty library and adds it to the library table.

The library file is created.

Definition at line 147 of file symbol_library_manager.h.

148  {
149  return addLibrary( aFilePath, true, aTable );
150  }
bool addLibrary(const wxString &aFilePath, bool aCreate, SYMBOL_LIB_TABLE *aTable)
Return the current Symbol Library Table.

References addLibrary().

Referenced by SYMBOL_EDIT_FRAME::AddLibraryFile().

◆ FlushPart()

bool SYMBOL_LIBRARY_MANAGER::FlushPart ( const wxString &  aAlias,
const wxString &  aLibrary 
)

Save part changes to the library copy used by the schematic editor.

Not it is not necessarily saved to the file.

Returns
True on success, false otherwise.

Definition at line 491 of file symbol_library_manager.cpp.

492 {
493  auto it = m_libs.find( aLibrary );
494 
495  if( it == m_libs.end() ) // no items to flush
496  return true;
497 
498  auto partBuf = it->second.GetBuffer( aAlias );
499  wxCHECK( partBuf, false );
500 
501  return it->second.SaveBuffer( partBuf, symTable() );
502 }
SYMBOL_LIB_TABLE * symTable() const
std::map< wxString, LIB_BUFFER > m_libs

References m_libs, and symTable().

Referenced by SYMBOL_EDIT_FRAME::saveCurrentPart().

◆ GetAdapter()

wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>& SYMBOL_LIBRARY_MANAGER::GetAdapter ( )
inline

Return the adapter object that provides the stored data.

Definition at line 291 of file symbol_library_manager.h.

291 { return m_adapter; }
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter

References m_adapter.

Referenced by SYMBOL_EDIT_FRAME::FreezeLibraryTree(), SYMBOL_TREE_PANE::SYMBOL_TREE_PANE(), SYMBOL_EDIT_FRAME::SyncLibraries(), and SYMBOL_EDIT_FRAME::ThawLibraryTree().

◆ getAdapter()

SYMBOL_TREE_SYNCHRONIZING_ADAPTER* SYMBOL_LIBRARY_MANAGER::getAdapter ( )
inlineprivate

Class to store a working copy of a LIB_PART object and editor context.

Definition at line 315 of file symbol_library_manager.h.

References m_adapter.

Referenced by Sync().

◆ GetAlias()

LIB_PART * SYMBOL_LIBRARY_MANAGER::GetAlias ( const wxString &  aAlias,
const wxString &  aLibrary 
) const

Return either an alias of a working LIB_PART copy, or alias of the original part if there is no working copy.

Definition at line 585 of file symbol_library_manager.cpp.

587 {
588  // Try the library buffers first
589  auto libIt = m_libs.find( aLibrary );
590 
591  if( libIt != m_libs.end() )
592  {
593  LIB_PART* part = libIt->second.GetPart( aAlias );
594 
595  if( part )
596  return part;
597  }
598 
599  // Get the original part
600  LIB_PART* alias = nullptr;
601 
602  try
603  {
604  alias = symTable()->LoadSymbol( aLibrary, aAlias );
605  }
606  catch( const IO_ERROR& e )
607  {
608  wxLogMessage( _( "Cannot load symbol \"%s\" from library \"%s\" (%s)" ),
609  aAlias, aLibrary, e.What() );
610  }
611 
612  return alias;
613 }
SYMBOL_LIB_TABLE * symTable() const
LIB_PART * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_PART having aName from the library given by aNickname.
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
Define a library symbol object.
Definition: lib_symbol.h:93
std::map< wxString, LIB_BUFFER > m_libs
#define _(s)
Definition: 3d_actions.cpp:33
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75

References _, SYMBOL_LIB_TABLE::LoadSymbol(), m_libs, symTable(), and IO_ERROR::What().

Referenced by SYMBOL_EDIT_FRAME::CreateNewPart(), SYMBOL_EDIT_FRAME::getTargetPart(), and DIALOG_LIB_SYMBOL_PROPERTIES::TransferDataFromWindow().

◆ GetAliases()

std::list< LIB_PART * > SYMBOL_LIBRARY_MANAGER::GetAliases ( const wxString &  aLibrary) const

Definition at line 340 of file symbol_library_manager.cpp.

341 {
342  std::list<LIB_PART*> ret;
343  wxCHECK( LibraryExists( aLibrary ), ret );
344 
345  auto libIt = m_libs.find( aLibrary );
346 
347  if( libIt != m_libs.end() )
348  {
349  for( auto& partBuf : libIt->second.GetBuffers() )
350  {
351  ret.push_back( partBuf->GetPart() );
352  }
353  }
354  else
355  {
356  std::vector<LIB_PART*> aliases;
357 
358  try
359  {
360  symTable()->LoadSymbolLib( aliases, aLibrary );
361  }
362  catch( const IO_ERROR& e )
363  {
364  wxLogWarning( e.Problem() );
365  }
366 
367  std::copy( aliases.begin(), aliases.end(), std::back_inserter( ret ) );
368  }
369 
370  return ret;
371 }
SYMBOL_LIB_TABLE * symTable() const
virtual const wxString Problem() const
what was the problem?
Definition: exceptions.cpp:45
void LoadSymbolLib(std::vector< LIB_PART * > &aAliasList, const wxString &aNickname, bool aPowerSymbolsOnly=false)
std::map< wxString, LIB_BUFFER > m_libs
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75

References LibraryExists(), SYMBOL_LIB_TABLE::LoadSymbolLib(), m_libs, IO_ERROR::Problem(), and symTable().

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::updateLibrary().

◆ GetBufferedPart()

LIB_PART * SYMBOL_LIBRARY_MANAGER::GetBufferedPart ( const wxString &  aAlias,
const wxString &  aLibrary 
)

Return the part copy from the buffer.

In case it does not exist yet, the copy is created. SYMBOL_LIBRARY_MANAGER retains the ownership.

Definition at line 374 of file symbol_library_manager.cpp.

376 {
377  wxCHECK( LibraryExists( aLibrary ), nullptr );
378 
379  // try the library buffers first
380  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
381  LIB_PART* bufferedPart = libBuf.GetPart( aAlias );
382 
383  if( !bufferedPart ) // no buffer part found
384  {
385  // create a copy of the part
386  try
387  {
388  LIB_PART* part = symTable()->LoadSymbol( aLibrary, aAlias );
389 
390  if( part == nullptr )
391  THROW_IO_ERROR( _( "Symbol not found." ) );
392 
393  LIB_PART* bufferedParent = nullptr;
394 
395  // Create parent symbols on demand so parent symbol can be set.
396  if( part->IsAlias() )
397  {
398  std::shared_ptr< LIB_PART > parent = part->GetParent().lock();
399  wxCHECK_MSG( parent, nullptr,
400  wxString::Format( "Derived symbol '%s' found with undefined parent.",
401  part->GetName() ) );
402 
403  // Check if the parent symbol buffer has already be created.
404  bufferedParent = libBuf.GetPart( parent->GetName() );
405 
406  if( !bufferedParent )
407  {
408  bufferedParent = new LIB_PART( *parent.get() );
409  libBuf.CreateBuffer( bufferedParent, new SCH_SCREEN );
410  }
411  }
412 
413  bufferedPart = new LIB_PART( *part );
414 
415  if( bufferedParent )
416  bufferedPart->SetParent( bufferedParent );
417 
418  libBuf.CreateBuffer( bufferedPart, new SCH_SCREEN );
419  }
420  catch( const IO_ERROR& e )
421  {
422  wxLogMessage( _( "Error loading symbol \"%s\" from library \"%s\". (%s)" ),
423  aAlias, aLibrary, e.What() );
424  bufferedPart = nullptr;
425  }
426  }
427 
428  return bufferedPart;
429 }
wxString GetName() const override
Definition: lib_symbol.h:129
SYMBOL_LIB_TABLE * symTable() const
LIB_PART * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_PART having aName from the library given by aNickname.
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Return an existing library buffer or creates one to using Symbol Library Table to get the original da...
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
void SetParent(LIB_PART *aParent=nullptr)
Definition: lib_symbol.cpp:325
PART_REF & GetParent()
Definition: lib_symbol.h:120
Define a library symbol object.
Definition: lib_symbol.h:93
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.
#define _(s)
Definition: 3d_actions.cpp:33
bool IsAlias() const
Definition: lib_symbol.h:168
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38

References _, SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::CreateBuffer(), Format(), getLibraryBuffer(), LIB_PART::GetName(), LIB_PART::GetParent(), SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::GetPart(), LIB_PART::IsAlias(), LibraryExists(), SYMBOL_LIB_TABLE::LoadSymbol(), LIB_PART::SetParent(), symTable(), THROW_IO_ERROR, and IO_ERROR::What().

Referenced by SYMBOL_EDIT_FRAME::CopyPartToClipboard(), SYMBOL_EDIT_FRAME::DuplicatePart(), SYMBOL_EDIT_FRAME::isCurrentPart(), SYMBOL_EDIT_FRAME::LoadOneLibraryPartAux(), and SYMBOL_EDIT_FRAME::LoadPart().

◆ GetHash()

int SYMBOL_LIBRARY_MANAGER::GetHash ( ) const

Definition at line 119 of file symbol_library_manager.cpp.

120 {
121  int hash = symTable()->GetModifyHash();
122 
123  for( const auto& lib : m_libs )
124  hash += lib.second.GetHash();
125 
126  return hash;
127 }
SYMBOL_LIB_TABLE * symTable() const
std::map< wxString, LIB_BUFFER > m_libs

References SYMBOL_LIB_TABLE::GetModifyHash(), m_libs, and symTable().

Referenced by RevertAll(), and SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync().

◆ GetLibrary()

SYMBOL_LIB_TABLE_ROW * SYMBOL_LIBRARY_MANAGER::GetLibrary ( const wxString &  aLibrary) const

Find a single library within the (aggregate) library table.

Definition at line 156 of file symbol_library_manager.cpp.

157 {
158  SYMBOL_LIB_TABLE_ROW* row = nullptr;
159 
160  try
161  {
162  row = symTable()->FindRow( aLibrary, true );
163  }
164  catch( const IO_ERROR& e )
165  {
166  wxLogMessage( _( "Cannot find library \"%s\" in the Symbol Library Table (%s)" ),
167  aLibrary, e.What() );
168  }
169 
170  return row;
171 }
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
SYMBOL_LIB_TABLE * symTable() const
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
#define _(s)
Definition: 3d_actions.cpp:33
SYMBOL_LIB_TABLE_ROW * FindRow(const wxString &aNickName, bool aCheckIfEnabled=false)
Return an SYMBOL_LIB_TABLE_ROW if aNickName is found in this table or in any chained fallBack table f...
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75

References _, SYMBOL_LIB_TABLE::FindRow(), symTable(), and IO_ERROR::What().

Referenced by GetLibraryHash(), SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue(), SYMBOL_EDIT_FRAME::IsSymbolFromLegacyLibrary(), SaveLibrary(), and SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync().

◆ getLibraryBuffer()

SYMBOL_LIBRARY_MANAGER::LIB_BUFFER & SYMBOL_LIBRARY_MANAGER::getLibraryBuffer ( const wxString &  aLibrary)
private

Return an existing library buffer or creates one to using Symbol Library Table to get the original data.

The library buffers

Definition at line 770 of file symbol_library_manager.cpp.

772 {
773  auto it = m_libs.find( aLibrary );
774 
775  if( it != m_libs.end() )
776  return it->second;
777 
778  // The requested buffer does not exist yet, so create one
779  auto ret = m_libs.emplace( aLibrary, LIB_BUFFER( aLibrary ) );
780  LIB_BUFFER& buf = ret.first->second;
781 
782  for( auto part : getOriginalParts( aLibrary ) )
783  {
784  LIB_PART* newSymbol;
785 
786  if( part->IsAlias() )
787  {
788  std::shared_ptr< LIB_PART > oldParent = part->GetParent().lock();
789 
790  wxCHECK_MSG( oldParent, buf,
791  wxString::Format( "Derived symbol '%s' found with undefined parent.",
792  part->GetName() ) );
793 
794  LIB_PART* libParent = buf.GetPart( oldParent->GetName() );
795 
796  if( !libParent )
797  {
798  libParent = new LIB_PART( *oldParent.get() );
799  buf.CreateBuffer( libParent, new SCH_SCREEN );
800  }
801 
802  newSymbol = new LIB_PART( *part );
803  newSymbol->SetParent( libParent );
804  buf.CreateBuffer( newSymbol, new SCH_SCREEN );
805  }
806  else if( !buf.GetPart( part->GetName() ) )
807  {
808  buf.CreateBuffer( new LIB_PART( *part ), new SCH_SCREEN );
809  }
810  }
811 
812  return buf;
813 }
void SetParent(LIB_PART *aParent=nullptr)
Definition: lib_symbol.cpp:325
PART_REF & GetParent()
Definition: lib_symbol.h:120
Define a library symbol object.
Definition: lib_symbol.h:93
std::map< wxString, LIB_BUFFER > m_libs
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
std::set< LIB_PART * > getOriginalParts(const wxString &aLibrary)
Return a set of LIB_PART objects belonging to the original library.

References SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::CreateBuffer(), Format(), getOriginalParts(), LIB_PART::GetParent(), SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::GetPart(), m_libs, and LIB_PART::SetParent().

Referenced by GetBufferedPart(), GetRootSymbolNames(), HasDerivedSymbols(), RemovePart(), UpdatePart(), and UpdatePartAfterRename().

◆ GetLibraryCount()

size_t SYMBOL_LIBRARY_MANAGER::GetLibraryCount ( ) const

Definition at line 687 of file symbol_library_manager.cpp.

688 {
689  return symTable()->GetLogicalLibs().size();
690 }
SYMBOL_LIB_TABLE * symTable() const
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.

References LIB_TABLE::GetLogicalLibs(), and symTable().

◆ GetLibraryHash()

int SYMBOL_LIBRARY_MANAGER::GetLibraryHash ( const wxString &  aLibrary) const

Return a library hash value to determine if it has changed.

For buffered libraries, it returns a number corresponding to the number of modifications. For original libraries, hash is computed basing on the library URI. Returns -1 when the requested library does not exist.

Definition at line 130 of file symbol_library_manager.cpp.

131 {
132  const auto libBufIt = m_libs.find( aLibrary );
133 
134  if( libBufIt != m_libs.end() )
135  return libBufIt->second.GetHash();
136 
137  auto row = GetLibrary( aLibrary );
138 
139  // return -1 if library does not exist or 0 if not modified
140  return row ? std::hash<std::string>{}( aLibrary.ToStdString() +
141  row->GetFullURI( true ).ToStdString() ) : -1;
142 }
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Find a single library within the (aggregate) library table.
std::map< wxString, LIB_BUFFER > m_libs

References GetLibrary(), and m_libs.

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::updateLibrary().

◆ getLibraryName()

wxString SYMBOL_LIBRARY_MANAGER::getLibraryName ( const wxString &  aFilePath)
staticprivate

< Extract library name basing on the file name.

Helper function to add either existing or create new library

Definition at line 693 of file symbol_library_manager.cpp.

694 {
695  wxFileName fn( aFilePath );
696  return fn.GetName();
697 }

Referenced by addLibrary().

◆ GetLibraryNames()

wxArrayString SYMBOL_LIBRARY_MANAGER::GetLibraryNames ( ) const

Return the array of library names.

Definition at line 145 of file symbol_library_manager.cpp.

146 {
147  wxArrayString res;
148 
149  for( const auto& libName : symTable()->GetLogicalLibs() )
150  res.Add( libName );
151 
152  return res;
153 }
SYMBOL_LIB_TABLE * symTable() const

References symTable().

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetLibrariesCount(), SYMBOL_EDIT_FRAME::IsContentModified(), SYMBOL_EDIT_FRAME::saveAllLibraries(), and SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync().

◆ getOriginalParts()

std::set< LIB_PART * > SYMBOL_LIBRARY_MANAGER::getOriginalParts ( const wxString &  aLibrary)
private

Return a set of LIB_PART objects belonging to the original library.

Definition at line 745 of file symbol_library_manager.cpp.

746 {
747  std::set<LIB_PART*> parts;
748  wxCHECK( LibraryExists( aLibrary ), parts );
749 
750  try
751  {
752  wxArrayString aliases;
753  symTable()->EnumerateSymbolLib( aLibrary, aliases );
754 
755  for( const auto& aliasName : aliases )
756  {
757  LIB_PART* alias = symTable()->LoadSymbol( aLibrary, aliasName );
758  parts.insert( alias );
759  }
760  }
761  catch( const IO_ERROR& e )
762  {
763  wxLogMessage( _( "Cannot enumerate library \"%s\" (%s)" ), aLibrary, e.What() );
764  }
765 
766  return parts;
767 }
void EnumerateSymbolLib(const wxString &aNickname, wxArrayString &aAliasNames, bool aPowerSymbolsOnly=false)
Return a list of symbol alias names contained within the library given by aNickname.
SYMBOL_LIB_TABLE * symTable() const
LIB_PART * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_PART having aName from the library given by aNickname.
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
Define a library symbol object.
Definition: lib_symbol.h:93
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.
#define _(s)
Definition: 3d_actions.cpp:33
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75

References _, SYMBOL_LIB_TABLE::EnumerateSymbolLib(), LibraryExists(), SYMBOL_LIB_TABLE::LoadSymbol(), symTable(), and IO_ERROR::What().

Referenced by getLibraryBuffer(), and SaveLibrary().

◆ GetRootSymbolNames()

void SYMBOL_LIBRARY_MANAGER::GetRootSymbolNames ( const wxString &  aLibName,
wxArrayString &  aRootSymbolNames 
)

Definition at line 669 of file symbol_library_manager.cpp.

671 {
672  LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
673 
674  libBuf.GetRootSymbolNames( aRootSymbolNames );
675 }
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Return an existing library buffer or creates one to using Symbol Library Table to get the original da...

References getLibraryBuffer(), and SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::GetRootSymbolNames().

Referenced by SYMBOL_EDIT_FRAME::CreateNewPart(), and DIALOG_LIB_SYMBOL_PROPERTIES::TransferDataToWindow().

◆ GetScreen()

SCH_SCREEN * SYMBOL_LIBRARY_MANAGER::GetScreen ( const wxString &  aAlias,
const wxString &  aLibrary 
)

Return the screen used to edit a specific part.

SYMBOL_LIBRARY_MANAGER retains the ownership.

Definition at line 432 of file symbol_library_manager.cpp.

433 {
434  wxCHECK( LibraryExists( aLibrary ), nullptr );
435  wxCHECK( !aAlias.IsEmpty(), nullptr );
436  auto it = m_libs.find( aLibrary );
437  wxCHECK( it != m_libs.end(), nullptr );
438 
439  LIB_BUFFER& buf = it->second;
440  auto partBuf = buf.GetBuffer( aAlias );
441  return partBuf ? partBuf->GetScreen() : nullptr;
442 }
std::map< wxString, LIB_BUFFER > m_libs
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.

References SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::GetBuffer(), LibraryExists(), and m_libs.

Referenced by SYMBOL_EDIT_FRAME::LoadOneLibraryPartAux().

◆ GetUniqueLibraryName()

wxString SYMBOL_LIBRARY_MANAGER::GetUniqueLibraryName ( ) const

Return a library name that is not currently in use.

Used for generating names for new libraries.

Definition at line 649 of file symbol_library_manager.cpp.

650 {
651  wxString name = "New_Library";
652 
653  if( !LibraryExists( name ) )
654  return name;
655 
656  name += "_";
657 
658  for( unsigned int i = 0; i < std::numeric_limits<unsigned int>::max(); ++i )
659  {
660  if( !LibraryExists( name + wxString::Format( "%u", i ) ) )
661  return name + wxString::Format( "%u", i );
662  }
663 
664  wxFAIL;
665  return wxEmptyString;
666 }
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.
const char * name
Definition: DXF_plotter.cpp:59

References Format(), LibraryExists(), and name.

Referenced by SYMBOL_EDIT_FRAME::AddLibraryFile().

◆ HasDerivedSymbols()

bool SYMBOL_LIBRARY_MANAGER::HasDerivedSymbols ( const wxString &  aSymbolName,
const wxString &  aLibraryName 
)

Check if symbol aSymbolName in library aLibraryName is a root symbol that has derived symbols.

Returns
true if \aSymbolName in aLibraryName has derived symbols.

Definition at line 678 of file symbol_library_manager.cpp.

680 {
681  LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
682 
683  return libBuf.HasDerivedSymbols( aSymbolName );
684 }
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Return an existing library buffer or creates one to using Symbol Library Table to get the original da...

References getLibraryBuffer(), and SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::HasDerivedSymbols().

Referenced by SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::DeleteBuffer(), and SYMBOL_EDIT_FRAME::DeletePartFromLibrary().

◆ HasModifications()

bool SYMBOL_LIBRARY_MANAGER::HasModifications ( ) const

Definition at line 107 of file symbol_library_manager.cpp.

108 {
109  for( const auto& lib : m_libs )
110  {
111  if( lib.second.IsModified() )
112  return true;
113  }
114 
115  return false;
116 }
std::map< wxString, LIB_BUFFER > m_libs

References m_libs.

Referenced by SYMBOL_EDIT_FRAME::HasLibModifications(), and SYMBOL_EDIT_FRAME::setupUIConditions().

◆ IsLibraryLoaded()

bool SYMBOL_LIBRARY_MANAGER::IsLibraryLoaded ( const wxString &  aLibrary) const

Return true if the library was successfully loaded.

Definition at line 332 of file symbol_library_manager.cpp.

333 {
334  wxCHECK( LibraryExists( aLibrary ), false );
335 
336  return symTable()->IsSymbolLibLoaded( aLibrary );
337 }
SYMBOL_LIB_TABLE * symTable() const
bool IsSymbolLibLoaded(const wxString &aNickname)
Return true if the library given by aNickname was successfully loaded.
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.

References SYMBOL_LIB_TABLE::IsSymbolLibLoaded(), LibraryExists(), and symTable().

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr(), and SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue().

◆ IsLibraryModified()

bool SYMBOL_LIBRARY_MANAGER::IsLibraryModified ( const wxString &  aLibrary) const

Return true if library has unsaved modifications.

Definition at line 268 of file symbol_library_manager.cpp.

269 {
270  auto it = m_libs.find( aLibrary );
271  return it != m_libs.end() ? it->second.IsModified() : false;
272 }
std::map< wxString, LIB_BUFFER > m_libs

References m_libs.

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr(), SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue(), SYMBOL_EDIT_FRAME::IsContentModified(), and SYMBOL_EDIT_FRAME::saveAllLibraries().

◆ IsLibraryReadOnly()

bool SYMBOL_LIBRARY_MANAGER::IsLibraryReadOnly ( const wxString &  aLibrary) const

Return true if the library is stored in a read-only file.

Returns
True on success, false otherwise.

Definition at line 324 of file symbol_library_manager.cpp.

325 {
326  wxCHECK( LibraryExists( aLibrary ), true );
327 
328  return !symTable()->IsSymbolLibWritable( aLibrary );
329 }
SYMBOL_LIB_TABLE * symTable() const
bool IsSymbolLibWritable(const wxString &aNickname)
Return true if the library given by aNickname is writable.
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.

References SYMBOL_LIB_TABLE::IsSymbolLibWritable(), LibraryExists(), and symTable().

Referenced by SYMBOL_EDITOR_CONTROL::AddSymbol(), SYMBOL_EDITOR_CONTROL::CutCopyDelete(), SYMBOL_EDITOR_CONTROL::DuplicateSymbol(), SYMBOL_EDIT_FRAME::IsContentModified(), SYMBOL_EDIT_FRAME::Save(), SYMBOL_EDIT_FRAME::saveAllLibraries(), SYMBOL_EDIT_FRAME::saveLibrary(), SYMBOL_EDIT_FRAME::SelectLibraryFromList(), and SYMBOL_EDIT_FRAME::updateTitle().

◆ IsPartModified()

bool SYMBOL_LIBRARY_MANAGER::IsPartModified ( const wxString &  aAlias,
const wxString &  aLibrary 
) const

Return true if part has unsaved modifications.

Definition at line 275 of file symbol_library_manager.cpp.

277 {
278  auto libIt = m_libs.find( aLibrary );
279 
280  if( libIt == m_libs.end() )
281  return false;
282 
283  const LIB_BUFFER& buf = libIt->second;
284  auto partBuf = buf.GetBuffer( aAlias );
285  return partBuf ? partBuf->IsModified() : false;
286 }
std::map< wxString, LIB_BUFFER > m_libs

References SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::GetBuffer(), and m_libs.

Referenced by SYMBOL_EDIT_FRAME::DeletePartFromLibrary(), SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr(), and SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue().

◆ LibraryExists()

bool SYMBOL_LIBRARY_MANAGER::LibraryExists ( const wxString &  aLibrary,
bool  aCheckEnabled = false 
) const

Return true if library exists.

If aCheckEnabled is set, then the library must also be enabled in the library table.

Definition at line 637 of file symbol_library_manager.cpp.

638 {
639  if( aLibrary.IsEmpty() )
640  return false;
641 
642  if( m_libs.count( aLibrary ) > 0 )
643  return true;
644 
645  return symTable()->HasLibrary( aLibrary, aCheckEnabled );
646 }
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
SYMBOL_LIB_TABLE * symTable() const
std::map< wxString, LIB_BUFFER > m_libs

References LIB_TABLE::HasLibrary(), m_libs, and symTable().

Referenced by addLibrary(), SYMBOL_EDIT_FRAME::AddLibraryFile(), SYMBOL_EDIT_FRAME::CreateNewPart(), SYMBOL_EDIT_FRAME::DuplicatePart(), GetAliases(), GetBufferedPart(), getOriginalParts(), GetScreen(), GetUniqueLibraryName(), SYMBOL_EDIT_FRAME::ImportPart(), IsLibraryLoaded(), IsLibraryReadOnly(), SaveLibrary(), SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync(), and UpdatePart().

◆ PartExists()

bool SYMBOL_LIBRARY_MANAGER::PartExists ( const wxString &  aAlias,
const wxString &  aLibrary 
) const

Return true if part with a specific alias exists in library (either original one or buffered).

Definition at line 616 of file symbol_library_manager.cpp.

617 {
618  auto libBufIt = m_libs.find( aLibrary );
619  LIB_PART* alias = nullptr;
620 
621  if( libBufIt != m_libs.end() )
622  return !!libBufIt->second.GetBuffer( aAlias );
623 
624  try
625  {
626  alias = symTable()->LoadSymbol( aLibrary, aAlias );
627  }
628  catch( IO_ERROR& )
629  {
630  // checking if certain symbol exists, so its absence is perfectly fine
631  }
632 
633  return alias != nullptr;
634 }
SYMBOL_LIB_TABLE * symTable() const
LIB_PART * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_PART having aName from the library given by aNickname.
Define a library symbol object.
Definition: lib_symbol.h:93
std::map< wxString, LIB_BUFFER > m_libs
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75

References SYMBOL_LIB_TABLE::LoadSymbol(), m_libs, and symTable().

Referenced by SYMBOL_EDIT_FRAME::CreateNewPart(), SYMBOL_EDIT_FRAME::ensureUniqueName(), SYMBOL_EDIT_FRAME::ImportPart(), SYMBOL_EDIT_FRAME::Revert(), SYMBOL_EDIT_FRAME::savePartAs(), DIALOG_LIB_SYMBOL_PROPERTIES::TransferDataFromWindow(), and SYMBOL_EDIT_FRAME::UpdateAfterSymbolProperties().

◆ Preload()

void SYMBOL_LIBRARY_MANAGER::Preload ( PROGRESS_REPORTER aReporter)

Preloads all symbol libraries in the symbol library table using SYMBOL_ASYNC_LOADER.

Call before the first call to Sync() to get better performance.

Parameters
aReporteris used to report progress of the load

Definition at line 65 of file symbol_library_manager.cpp.

66 {
67  const int progressIntervalMillis = 60;
68 
69  SYMBOL_ASYNC_LOADER loader( symTable()->GetLogicalLibs(), symTable(), false, nullptr,
70  &aReporter );
71 
72  LOCALE_IO toggle;
73 
74  loader.Start();
75 
76  while( !loader.Done() )
77  {
78  aReporter.KeepRefreshing();
79 
80  wxMilliSleep( progressIntervalMillis );
81  }
82 
83  if( aReporter.IsCancelled() )
84  {
85  loader.Abort();
86  }
87  else
88  {
89  loader.Join();
90  }
91 
92  if( !loader.GetErrors().IsEmpty() )
93  {
94  HTML_MESSAGE_BOX dlg( &m_frame, _( "Load Error" ) );
95 
96  dlg.MessageSet( _( "Errors were encountered loading symbols:" ) );
97 
98  wxString msg = loader.GetErrors();
99  msg.Replace( "\n", "<BR>" );
100 
101  dlg.AddHTML_Text( msg );
102  dlg.ShowModal();
103  }
104 }
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
SYMBOL_LIB_TABLE * symTable() const
SYMBOL_EDIT_FRAME & m_frame
Parent frame.
bool IsCancelled() const
HTML_MESSAGE_BOX.
#define _(s)
Definition: 3d_actions.cpp:33
bool KeepRefreshing(bool aWait=false)
Update the UI dialog.

References _, SYMBOL_ASYNC_LOADER::Abort(), HTML_MESSAGE_BOX::AddHTML_Text(), SYMBOL_ASYNC_LOADER::Done(), SYMBOL_ASYNC_LOADER::GetErrors(), PROGRESS_REPORTER::IsCancelled(), SYMBOL_ASYNC_LOADER::Join(), PROGRESS_REPORTER::KeepRefreshing(), m_frame, HTML_MESSAGE_BOX::MessageSet(), SYMBOL_ASYNC_LOADER::Start(), and symTable().

◆ RemovePart()

bool SYMBOL_LIBRARY_MANAGER::RemovePart ( const wxString &  aName,
const wxString &  aLibrary 
)

Remove the part from the part buffer.

It is required to save the library to have the part removed in the schematic editor.

Definition at line 570 of file symbol_library_manager.cpp.

571 {
572  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
573  auto partBuf = libBuf.GetBuffer( aAlias );
574  wxCHECK( partBuf, false );
575 
576  bool retv = true;
577 
578  retv &= libBuf.DeleteBuffer( partBuf );
579  m_frame.SyncLibraries( false );
580 
581  return retv;
582 }
SYMBOL_EDIT_FRAME & m_frame
Parent frame.
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Return an existing library buffer or creates one to using Symbol Library Table to get the original da...
void SyncLibraries(bool aShowProgress, const wxString &aForceRefresh=wxEmptyString)
Synchronize the library manager to the symbol library table, and then the symbol tree to the library ...

References SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::DeleteBuffer(), SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::GetBuffer(), getLibraryBuffer(), m_frame, and SYMBOL_EDIT_FRAME::SyncLibraries().

Referenced by SYMBOL_EDIT_FRAME::DeletePartFromLibrary().

◆ RevertAll()

bool SYMBOL_LIBRARY_MANAGER::RevertAll ( )

Revert all pending changes.

Returns
True if all changes successfully reverted.

Definition at line 544 of file symbol_library_manager.cpp.

545 {
546  bool retv = true;
547 
548  // Nothing to revert.
549  if( GetHash() == 0 )
550  return true;
551 
552  for( const auto& lib : m_libs )
553  {
554  if( !lib.second.IsModified() )
555  continue;
556 
557  for( const auto& buffer : lib.second.GetBuffers() )
558  {
559  if( !buffer->IsModified() )
560  continue;
561 
562  RevertPart( lib.first, buffer->GetOriginal()->GetName() );
563  }
564  }
565 
566  return retv;
567 }
std::map< wxString, LIB_BUFFER > m_libs
LIB_ID RevertPart(const wxString &aAlias, const wxString &aLibrary)
Revert unsaved changes for a particular part.

References GetHash(), m_libs, and RevertPart().

Referenced by SYMBOL_EDIT_FRAME::RevertAll().

◆ RevertLibrary()

bool SYMBOL_LIBRARY_MANAGER::RevertLibrary ( const wxString &  aLibrary)

Revert unsaved changes for a particular library.

Returns
True on success, false otherwise.

Definition at line 530 of file symbol_library_manager.cpp.

531 {
532  auto it = m_libs.find( aLibrary );
533 
534  if( it == m_libs.end() ) // nothing to reverse
535  return false;
536 
537  m_libs.erase( it );
538  m_frame.SyncLibraries( false );
539 
540  return true;
541 }
SYMBOL_EDIT_FRAME & m_frame
Parent frame.
std::map< wxString, LIB_BUFFER > m_libs
void SyncLibraries(bool aShowProgress, const wxString &aForceRefresh=wxEmptyString)
Synchronize the library manager to the symbol library table, and then the symbol tree to the library ...

References m_frame, m_libs, and SYMBOL_EDIT_FRAME::SyncLibraries().

Referenced by SYMBOL_EDIT_FRAME::Revert().

◆ RevertPart()

LIB_ID SYMBOL_LIBRARY_MANAGER::RevertPart ( const wxString &  aAlias,
const wxString &  aLibrary 
)

Revert unsaved changes for a particular part.

Returns
The LIB_ID of the reverted part (which may be different in the case of a rename)

Definition at line 505 of file symbol_library_manager.cpp.

506 {
507  auto it = m_libs.find( aLibrary );
508 
509  if( it == m_libs.end() ) // no items to flush
510  return LIB_ID( aLibrary, aAlias );
511 
512  auto partBuf = it->second.GetBuffer( aAlias );
513  wxCHECK( partBuf, LIB_ID( aLibrary, aAlias ) );
514  LIB_PART original( *partBuf->GetOriginal() );
515 
516  if( original.GetName() != aAlias )
517  {
518  UpdatePartAfterRename( &original, aAlias, aLibrary );
519  }
520  else
521  {
522  partBuf->SetPart( new LIB_PART( original ) );
523  m_frame.SyncLibraries( false );
524  }
525 
526  return LIB_ID( aLibrary, original.GetName() );
527 }
bool UpdatePartAfterRename(LIB_PART *aPart, const wxString &oldAlias, const wxString &aLibrary)
Update the part buffer with a new version of the part when the name has changed.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
SYMBOL_EDIT_FRAME & m_frame
Parent frame.
Define a library symbol object.
Definition: lib_symbol.h:93
std::map< wxString, LIB_BUFFER > m_libs
void SyncLibraries(bool aShowProgress, const wxString &aForceRefresh=wxEmptyString)
Synchronize the library manager to the symbol library table, and then the symbol tree to the library ...

References m_frame, m_libs, SYMBOL_EDIT_FRAME::SyncLibraries(), and UpdatePartAfterRename().

Referenced by SYMBOL_EDIT_FRAME::Revert(), and RevertAll().

◆ SaveLibrary()

bool SYMBOL_LIBRARY_MANAGER::SaveLibrary ( const wxString &  aLibrary,
const wxString &  aFileName,
SCH_IO_MGR::SCH_FILE_T  aFileType = SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY 
)

Save library to a file, including unsaved changes.

Parameters
aLibraryis the library name.
aFileNameis the target file name.
Returns
True on success, false otherwise.

Definition at line 174 of file symbol_library_manager.cpp.

176 {
177  wxCHECK( aFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY && LibraryExists( aLibrary ), false );
178  wxFileName fn( aFileName );
179  wxCHECK( !fn.FileExists() || fn.IsFileWritable(), false );
180  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( aFileType ) );
181  bool res = true; // assume all libraries are successfully saved
182 
183  PROPERTIES properties;
184  properties.emplace( SCH_LEGACY_PLUGIN::PropBuffering, "" );
185 
186  auto it = m_libs.find( aLibrary );
187 
188  if( it != m_libs.end() )
189  {
190  // Handle buffered library
191  LIB_BUFFER& libBuf = it->second;
192 
193  const auto& partBuffers = libBuf.GetBuffers();
194 
195  for( const auto& partBuf : partBuffers )
196  {
197  if( !libBuf.SaveBuffer( partBuf, aFileName, &*pi, true ) )
198  {
199  // Something went wrong, but try to save other libraries
200  res = false;
201  }
202  }
203 
204  // clear the deleted parts buffer only if data is saved to the original file
205  wxFileName original, destination( aFileName );
206  auto row = GetLibrary( aLibrary );
207 
208  if( row )
209  {
210  original = row->GetFullURI( true );
211  original.Normalize();
212  }
213 
214  destination.Normalize();
215 
216  if( res && original == destination )
217  libBuf.ClearDeletedBuffer();
218  }
219  else
220  {
221  // Handle original library
222  for( LIB_PART* part : getOriginalParts( aLibrary ) )
223  {
224  LIB_PART* newSymbol;
225 
226  if( part->IsAlias() )
227  {
228  std::shared_ptr< LIB_PART > oldParent = part->GetParent().lock();
229 
230  wxCHECK_MSG( oldParent, false,
231  wxString::Format( "Derived symbol '%s' found with undefined parent.",
232  part->GetName() ) );
233 
234  LIB_PART* libParent = pi->LoadSymbol( aLibrary, oldParent->GetName(), &properties );
235 
236  if( !libParent )
237  {
238  libParent = new LIB_PART( *oldParent.get() );
239  pi->SaveSymbol( aLibrary, libParent, &properties );
240  }
241 
242  newSymbol = new LIB_PART( *part );
243  newSymbol->SetParent( libParent );
244  pi->SaveSymbol( aLibrary, newSymbol, &properties );
245  }
246  else if( !pi->LoadSymbol( aLibrary, part->GetName(), &properties ) )
247  {
248  pi->SaveSymbol( aLibrary, new LIB_PART( *part ), &properties );
249  }
250  }
251  }
252 
253  try
254  {
255  pi->SaveLibrary( aFileName );
256  }
257  catch( ... )
258  {
259  // return false because something happens.
260  // The library is not successfully saved
261  res = false;
262  }
263 
264  return res;
265 }
A name/value tuple with unique names and optional values.
Definition: properties.h:33
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Find a single library within the (aggregate) library table.
void SetParent(LIB_PART *aParent=nullptr)
Definition: lib_symbol.cpp:325
PART_REF & GetParent()
Definition: lib_symbol.h:120
Define a library symbol object.
Definition: lib_symbol.h:93
std::map< wxString, LIB_BUFFER > m_libs
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:473
std::set< LIB_PART * > getOriginalParts(const wxString &aLibrary)
Return a set of LIB_PART objects belonging to the original library.
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...

References SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::ClearDeletedBuffer(), Format(), SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::GetBuffers(), GetLibrary(), getOriginalParts(), LIB_PART::GetParent(), LibraryExists(), m_libs, SCH_LEGACY_PLUGIN::PropBuffering, SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::SaveBuffer(), and LIB_PART::SetParent().

Referenced by SYMBOL_EDIT_FRAME::saveLibrary().

◆ symTable()

SYMBOL_LIB_TABLE * SYMBOL_LIBRARY_MANAGER::symTable ( ) const
private

Definition at line 739 of file symbol_library_manager.cpp.

740 {
741  return m_frame.Prj().SchSymbolLibTable();
742 }
SYMBOL_EDIT_FRAME & m_frame
Parent frame.
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.

References m_frame, and KIWAY_HOLDER::Prj().

Referenced by FlushPart(), GetAlias(), GetAliases(), GetBufferedPart(), GetHash(), GetLibrary(), GetLibraryCount(), GetLibraryNames(), getOriginalParts(), IsLibraryLoaded(), IsLibraryReadOnly(), LibraryExists(), PartExists(), Preload(), and Sync().

◆ Sync()

void SYMBOL_LIBRARY_MANAGER::Sync ( const wxString &  aForceRefresh,
std::function< void(int, int, const wxString &)>  aProgressCallback 
)

Updates the SYMBOL_LIBRARY_MANAGER data to synchronize with Symbol Library Table.

Definition at line 52 of file symbol_library_manager.cpp.

55 {
57  {
58  getAdapter()->Sync( aForceRefresh, aProgressCallback );
60  }
62 }
SYMBOL_LIB_TABLE * symTable() const
void Sync(const wxString &aForceRefresh, std::function< void(int, int, const wxString &)> aProgressCallback)
SYMBOL_TREE_SYNCHRONIZING_ADAPTER * getAdapter()
Class to store a working copy of a LIB_PART object and editor context.
int m_syncHash
Symbol lib table hash value from last synchronization.

References LIB_LOGGER::Activate(), LIB_LOGGER::Deactivate(), getAdapter(), SYMBOL_LIB_TABLE::GetModifyHash(), m_logger, m_syncHash, symTable(), and SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync().

Referenced by SYMBOL_EDIT_FRAME::SyncLibraries().

◆ UpdatePart()

bool SYMBOL_LIBRARY_MANAGER::UpdatePart ( LIB_PART aPart,
const wxString &  aLibrary 
)

Update the part buffer with a new version of the part.

The library buffer creates a copy of the part. It is required to save the library to use the updated part in the schematic editor.

Definition at line 445 of file symbol_library_manager.cpp.

446 {
447  wxCHECK( LibraryExists( aLibrary ), false );
448  wxCHECK( aPart, false );
449  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
450  auto partBuf = libBuf.GetBuffer( aPart->GetName() );
451 
452  if( partBuf ) // Existing symbol.
453  {
454  LIB_PART* bufferedPart = const_cast< LIB_PART* >( partBuf->GetPart() );
455 
456  wxCHECK( bufferedPart, false );
457 
458  *bufferedPart = *aPart;
459  partBuf->GetScreen()->SetModify();
460  }
461  else // New symbol
462  {
463  LIB_PART* partCopy = new LIB_PART( *aPart, nullptr );
464 
465  partCopy->SetLibId( LIB_ID( aLibrary, aPart->GetLibId().GetLibItemName() ) );
466 
467  SCH_SCREEN* screen = new SCH_SCREEN;
468  libBuf.CreateBuffer( partCopy, screen );
469  screen->SetModify();
470  }
471 
472  return true;
473 }
const UTF8 & GetLibItemName() const
Definition: lib_id.h:106
LIB_ID GetLibId() const override
Definition: lib_symbol.h:131
wxString GetName() const override
Definition: lib_symbol.h:129
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Return an existing library buffer or creates one to using Symbol Library Table to get the original da...
Define a library symbol object.
Definition: lib_symbol.h:93
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.
void SetModify()
Definition: base_screen.h:59
void SetLibId(const LIB_ID &aLibId)
Definition: lib_symbol.h:132

References SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::CreateBuffer(), SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::GetBuffer(), LIB_PART::GetLibId(), LIB_ID::GetLibItemName(), getLibraryBuffer(), LIB_PART::GetName(), LibraryExists(), LIB_PART::SetLibId(), and BASE_SCREEN::SetModify().

Referenced by SYMBOL_EDIT_FRAME::CreateNewPart(), SYMBOL_EDIT_FRAME::DuplicatePart(), SYMBOL_EDIT_FRAME::ImportPart(), SYMBOL_EDIT_FRAME::savePartAs(), and SYMBOL_EDIT_FRAME::storeCurrentPart().

◆ UpdatePartAfterRename()

bool SYMBOL_LIBRARY_MANAGER::UpdatePartAfterRename ( LIB_PART aPart,
const wxString &  oldAlias,
const wxString &  aLibrary 
)

Update the part buffer with a new version of the part when the name has changed.

The old library buffer will be deleted and a new one created with the new name.

Definition at line 476 of file symbol_library_manager.cpp.

478 {
479  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
480  auto partBuf = libBuf.GetBuffer( aOldName );
481 
482  wxCHECK( partBuf, false );
483 
484  libBuf.UpdateBuffer( partBuf, aPart );
485  m_frame.SyncLibraries( false );
486 
487  return true;
488 }
SYMBOL_EDIT_FRAME & m_frame
Parent frame.
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Return an existing library buffer or creates one to using Symbol Library Table to get the original da...
void SyncLibraries(bool aShowProgress, const wxString &aForceRefresh=wxEmptyString)
Synchronize the library manager to the symbol library table, and then the symbol tree to the library ...

References SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::GetBuffer(), getLibraryBuffer(), m_frame, SYMBOL_EDIT_FRAME::SyncLibraries(), and SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::UpdateBuffer().

Referenced by SYMBOL_EDIT_FRAME::GetSymbolFromRedoList(), SYMBOL_EDIT_FRAME::GetSymbolFromUndoList(), RevertPart(), and SYMBOL_EDIT_FRAME::UpdateAfterSymbolProperties().

Member Data Documentation

◆ m_adapter

wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER> SYMBOL_LIBRARY_MANAGER::m_adapter
private

Definition at line 475 of file symbol_library_manager.h.

Referenced by GetAdapter(), getAdapter(), and SYMBOL_LIBRARY_MANAGER().

◆ m_frame

SYMBOL_EDIT_FRAME& SYMBOL_LIBRARY_MANAGER::m_frame
private

◆ m_libs

◆ m_logger

LIB_LOGGER SYMBOL_LIBRARY_MANAGER::m_logger
private

Definition at line 472 of file symbol_library_manager.h.

Referenced by Sync().

◆ m_syncHash

int SYMBOL_LIBRARY_MANAGER::m_syncHash
private

Symbol lib table hash value from last synchronization.

Definition at line 473 of file symbol_library_manager.h.

Referenced by Sync().


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