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  SYMBOL_BUFFER
 Store a working copy of a library. More...
 

Public Member Functions

 SYMBOL_LIBRARY_MANAGER (SYMBOL_EDIT_FRAME &aFrame)
 
 ~SYMBOL_LIBRARY_MANAGER ()
 
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_SYMBOL * > 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 UpdateSymbol (LIB_SYMBOL *aSymbol, const wxString &aLibrary)
 Update the symbol buffer with a new version of the symbol. More...
 
bool UpdateSymbolAfterRename (LIB_SYMBOL *aSymbol, const wxString &oldAlias, const wxString &aLibrary)
 Update the symbol buffer with a new version of the symbol when the name has changed. More...
 
bool RemoveSymbol (const wxString &aName, const wxString &aLibrary)
 Remove the symbol from the symbol buffer. More...
 
LIB_SYMBOLGetAlias (const wxString &aAlias, const wxString &aLibrary) const
 Return either an alias of a working LIB_SYMBOL copy, or alias of the original symbol if there is no working copy. More...
 
LIB_SYMBOLGetBufferedSymbol (const wxString &aAlias, const wxString &aLibrary)
 Return the symbol copy from the buffer. More...
 
SCH_SCREENGetScreen (const wxString &aAlias, const wxString &aLibrary)
 Return the screen used to edit a specific symbol. More...
 
bool SymbolExists (const wxString &aAlias, const wxString &aLibrary) const
 Return true if symbol 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 IsSymbolModified (const wxString &aAlias, const wxString &aLibrary) const
 Return true if symbol has unsaved modifications. More...
 
bool ClearLibraryModified (const wxString &aLibrary) const
 Clear the modified flag for all symbols in a library. More...
 
bool ClearSymbolModified (const wxString &aAlias, const wxString &aLibrary) const
 Clear the modified flag for a symbol. More...
 
bool IsLibraryReadOnly (const wxString &aLibrary) const
 Return true if the library is stored in a read-only file. More...
 
bool FlushSymbol (const wxString &aAlias, const wxString &aLibrary)
 Save symbol 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 RevertSymbol (const wxString &aAlias, const wxString &aLibrary)
 Revert unsaved changes for a symbolicular symbol. More...
 
bool RevertLibrary (const wxString &aLibrary)
 Revert unsaved changes for a symbolicular 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_SYMBOL object and editor context. More...
 
std::set< LIB_SYMBOL * > getOriginalSymbols (const wxString &aLibrary)
 Return a set of LIB_SYMBOL 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_LOGGERm_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 53 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 46 of file symbol_library_manager.cpp.

46  :
47  m_frame( aFrame ),
48  m_syncHash( 0 )
49 {
51  m_adapter->ShowUnits( false );
52  m_logger = new LIB_LOGGER();
53 }
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, m_frame, and m_logger.

◆ ~SYMBOL_LIBRARY_MANAGER()

SYMBOL_LIBRARY_MANAGER::~SYMBOL_LIBRARY_MANAGER ( )

Definition at line 56 of file symbol_library_manager.cpp.

57 {
58  delete m_logger;
59 }

References m_logger.

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 108 of file symbol_library_manager.h.

109  {
110  return addLibrary( aFilePath, false, aTable );
111  }
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::ExportSymbol().

◆ 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 712 of file symbol_library_manager.cpp.

714 {
715  wxCHECK( aTable, false );
716  wxString libName = getLibraryName( aFilePath );
717  wxCHECK( !LibraryExists( libName ), false ); // either create or add an existing one
718 
719  // try to use path normalized to an environmental variable or project path
720  wxString relPath = NormalizePath( aFilePath, &Pgm().GetLocalEnvVariables(), &m_frame.Prj() );
721 
722  SCH_IO_MGR::SCH_FILE_T schFileType = SCH_IO_MGR::GuessPluginTypeFromLibPath( aFilePath );
723  wxString typeName = SCH_IO_MGR::ShowType( schFileType );
724  SYMBOL_LIB_TABLE_ROW* libRow = new SYMBOL_LIB_TABLE_ROW( libName, relPath, typeName );
725  aTable->InsertRow( libRow );
726 
727  if( aCreate )
728  {
729  wxCHECK( schFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY, false );
730 
731  try
732  {
733  aTable->CreateSymbolLib( libName );
734  }
735  catch( const IO_ERROR& )
736  {
737  aTable->RemoveRow( libRow );
738  return false;
739  }
740  }
741 
742  m_frame.SyncLibraries( false );
743 
744  return true;
745 }
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:69

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 symbols in a library.

Definition at line 300 of file symbol_library_manager.cpp.

301 {
302  auto libIt = m_libs.find( aLibrary );
303 
304  if( libIt == m_libs.end() )
305  return false;
306 
307  for( auto& symbolBuf : libIt->second.GetBuffers() )
308  {
309  SCH_SCREEN* screen = symbolBuf->GetScreen();
310 
311  if( screen )
312  screen->SetContentModified( false );
313  }
314 
315  return true;
316 }
std::map< wxString, LIB_BUFFER > m_libs
void SetContentModified(bool aModified=true)
Definition: base_screen.h:59

References m_libs, and BASE_SCREEN::SetContentModified().

Referenced by SYMBOL_EDIT_FRAME::saveLibrary().

◆ ClearSymbolModified()

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

Clear the modified flag for a symbol.

Definition at line 319 of file symbol_library_manager.cpp.

321 {
322  auto libI = m_libs.find( aLibrary );
323 
324  if( libI == m_libs.end() )
325  return false;
326 
327  auto symbolBuf = libI->second.GetBuffer( aAlias );
328  wxCHECK( symbolBuf, false );
329 
330  symbolBuf->GetScreen()->SetContentModified( false );
331  return true;
332 }
std::map< wxString, LIB_BUFFER > m_libs

References m_libs.

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

◆ 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 100 of file symbol_library_manager.h.

101  {
102  return addLibrary( aFilePath, true, aTable );
103  }
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().

◆ FlushSymbol()

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

Save symbol 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 502 of file symbol_library_manager.cpp.

503 {
504  auto it = m_libs.find( aLibrary );
505 
506  if( it == m_libs.end() ) // no items to flush
507  return true;
508 
509  auto symbolBuf = it->second.GetBuffer( aAlias );
510  wxCHECK( symbolBuf, false );
511 
512  return it->second.SaveBuffer( symbolBuf, symTable() );
513 }
SYMBOL_LIB_TABLE * symTable() const
std::map< wxString, LIB_BUFFER > m_libs

References m_libs, and symTable().

Referenced by SYMBOL_EDIT_FRAME::saveCurrentSymbol().

◆ GetAdapter()

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

Return the adapter object that provides the stored data.

Definition at line 244 of file symbol_library_manager.h.

244 { 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_SYMBOL object and editor context.

Definition at line 268 of file symbol_library_manager.h.

References m_adapter.

Referenced by Sync().

◆ GetAlias()

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

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

Definition at line 596 of file symbol_library_manager.cpp.

598 {
599  // Try the library buffers first
600  auto libIt = m_libs.find( aLibrary );
601 
602  if( libIt != m_libs.end() )
603  {
604  LIB_SYMBOL* symbol = libIt->second.GetSymbol( aAlias );
605 
606  if( symbol )
607  return symbol;
608  }
609 
610  // Get the original symbol
611  LIB_SYMBOL* alias = nullptr;
612 
613  try
614  {
615  alias = symTable()->LoadSymbol( aLibrary, aAlias );
616  }
617  catch( const IO_ERROR& e )
618  {
619  wxLogMessage( _( "Cannot load symbol '%s' from library '%s'." ) + e.What(),
620  aAlias,
621  aLibrary );
622  }
623 
624  return alias;
625 }
SYMBOL_LIB_TABLE * symTable() const
Define a library symbol object.
Definition: lib_symbol.h:96
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
std::map< wxString, LIB_BUFFER > m_libs
#define _(s)
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
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::CreateNewSymbol(), SYMBOL_EDIT_FRAME::getTargetSymbol(), and DIALOG_LIB_SYMBOL_PROPERTIES::TransferDataFromWindow().

◆ GetAliases()

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

Definition at line 351 of file symbol_library_manager.cpp.

352 {
353  std::list<LIB_SYMBOL*> ret;
354  wxCHECK( LibraryExists( aLibrary ), ret );
355 
356  auto libIt = m_libs.find( aLibrary );
357 
358  if( libIt != m_libs.end() )
359  {
360  for( auto& symbolBuf : libIt->second.GetBuffers() )
361  {
362  ret.push_back( symbolBuf->GetSymbol() );
363  }
364  }
365  else
366  {
367  std::vector<LIB_SYMBOL*> aliases;
368 
369  try
370  {
371  symTable()->LoadSymbolLib( aliases, aLibrary );
372  }
373  catch( const IO_ERROR& e )
374  {
375  wxLogWarning( e.Problem() );
376  }
377 
378  std::copy( aliases.begin(), aliases.end(), std::back_inserter( ret ) );
379  }
380 
381  return ret;
382 }
SYMBOL_LIB_TABLE * symTable() const
virtual const wxString Problem() const
what was the problem?
Definition: exceptions.cpp:46
std::map< wxString, LIB_BUFFER > m_libs
void LoadSymbolLib(std::vector< LIB_SYMBOL * > &aAliasList, const wxString &aNickname, bool aPowerSymbolsOnly=false)
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().

◆ GetBufferedSymbol()

LIB_SYMBOL * SYMBOL_LIBRARY_MANAGER::GetBufferedSymbol ( const wxString &  aAlias,
const wxString &  aLibrary 
)

Return the symbol copy from the buffer.

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

Definition at line 385 of file symbol_library_manager.cpp.

387 {
388  wxCHECK( LibraryExists( aLibrary ), nullptr );
389 
390  // try the library buffers first
391  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
392  LIB_SYMBOL* bufferedSymbol = libBuf.GetSymbol( aAlias );
393 
394  if( !bufferedSymbol ) // no buffer symbol found
395  {
396  // create a copy of the symbol
397  try
398  {
399  LIB_SYMBOL* symbol = symTable()->LoadSymbol( aLibrary, aAlias );
400 
401  if( symbol == nullptr )
402  THROW_IO_ERROR( _( "Symbol not found." ) );
403 
404  LIB_SYMBOL* bufferedParent = nullptr;
405 
406  // Create parent symbols on demand so parent symbol can be set.
407  if( symbol->IsAlias() )
408  {
409  std::shared_ptr< LIB_SYMBOL > parent = symbol->GetParent().lock();
410  wxCHECK_MSG( parent, nullptr,
411  wxString::Format( "Derived symbol '%s' found with undefined parent.",
412  symbol->GetName() ) );
413 
414  // Check if the parent symbol buffer has already be created.
415  bufferedParent = libBuf.GetSymbol( parent->GetName() );
416 
417  if( !bufferedParent )
418  {
419  bufferedParent = new LIB_SYMBOL( *parent.get() );
420  libBuf.CreateBuffer( bufferedParent, new SCH_SCREEN );
421  }
422  }
423 
424  bufferedSymbol = new LIB_SYMBOL( *symbol );
425 
426  if( bufferedParent )
427  bufferedSymbol->SetParent( bufferedParent );
428 
429  libBuf.CreateBuffer( bufferedSymbol, new SCH_SCREEN );
430  }
431  catch( const IO_ERROR& e )
432  {
433  wxLogMessage( _( "Error loading symbol %s from library '%s'. (%s)" ),
434  aAlias, aLibrary, e.What() );
435  bufferedSymbol = nullptr;
436  }
437  }
438 
439  return bufferedSymbol;
440 }
LIB_SYMBOL_REF & GetParent()
Definition: lib_symbol.h:124
wxString GetName() const override
Definition: lib_symbol.h:133
bool IsAlias() const
Definition: lib_symbol.h:172
SYMBOL_LIB_TABLE * symTable() const
Define a library symbol object.
Definition: lib_symbol.h:96
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:30
#define _(s)
void SetParent(LIB_SYMBOL *aParent=nullptr)
Definition: lib_symbol.cpp:322
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.
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
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_SYMBOL::GetName(), LIB_SYMBOL::GetParent(), SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::GetSymbol(), LIB_SYMBOL::IsAlias(), LibraryExists(), SYMBOL_LIB_TABLE::LoadSymbol(), LIB_SYMBOL::SetParent(), symTable(), THROW_IO_ERROR, and IO_ERROR::What().

Referenced by SYMBOL_EDIT_FRAME::CopySymbolToClipboard(), SYMBOL_EDIT_FRAME::DuplicateSymbol(), SYMBOL_EDIT_FRAME::isCurrentSymbol(), SYMBOL_EDIT_FRAME::LoadOneLibrarySymbolAux(), and SYMBOL_EDIT_FRAME::LoadSymbol().

◆ GetHash()

int SYMBOL_LIBRARY_MANAGER::GetHash ( ) const

Definition at line 129 of file symbol_library_manager.cpp.

130 {
131  int hash = symTable()->GetModifyHash();
132 
133  for( const auto& lib : m_libs )
134  hash += lib.second.GetHash();
135 
136  return hash;
137 }
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 166 of file symbol_library_manager.cpp.

167 {
168  SYMBOL_LIB_TABLE_ROW* row = nullptr;
169 
170  try
171  {
172  row = symTable()->FindRow( aLibrary, true );
173  }
174  catch( const IO_ERROR& e )
175  {
176  wxLogMessage( _( "Library '%s' not found in the Symbol Library Table." ) + e.What(),
177  aLibrary );
178  }
179 
180  return row;
181 }
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:30
#define _(s)
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 779 of file symbol_library_manager.cpp.

781 {
782  auto it = m_libs.find( aLibrary );
783 
784  if( it != m_libs.end() )
785  return it->second;
786 
787  // The requested buffer does not exist yet, so create one
788  auto ret = m_libs.emplace( aLibrary, LIB_BUFFER( aLibrary ) );
789  LIB_BUFFER& buf = ret.first->second;
790 
791  for( auto symbol : getOriginalSymbols( aLibrary ) )
792  {
793  LIB_SYMBOL* newSymbol;
794 
795  if( symbol->IsAlias() )
796  {
797  std::shared_ptr< LIB_SYMBOL > oldParent = symbol->GetParent().lock();
798 
799  wxCHECK_MSG( oldParent, buf,
800  wxString::Format( "Derived symbol '%s' found with undefined parent.",
801  symbol->GetName() ) );
802 
803  LIB_SYMBOL* libParent = buf.GetSymbol( oldParent->GetName() );
804 
805  if( !libParent )
806  {
807  libParent = new LIB_SYMBOL( *oldParent.get() );
808  buf.CreateBuffer( libParent, new SCH_SCREEN );
809  }
810 
811  newSymbol = new LIB_SYMBOL( *symbol );
812  newSymbol->SetParent( libParent );
813  buf.CreateBuffer( newSymbol, new SCH_SCREEN );
814  }
815  else if( !buf.GetSymbol( symbol->GetName() ) )
816  {
817  buf.CreateBuffer( new LIB_SYMBOL( *symbol ), new SCH_SCREEN );
818  }
819  }
820 
821  return buf;
822 }
LIB_SYMBOL_REF & GetParent()
Definition: lib_symbol.h:124
Define a library symbol object.
Definition: lib_symbol.h:96
std::map< wxString, LIB_BUFFER > m_libs
void SetParent(LIB_SYMBOL *aParent=nullptr)
Definition: lib_symbol.cpp:322
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_SYMBOL * > getOriginalSymbols(const wxString &aLibrary)
Return a set of LIB_SYMBOL objects belonging to the original library.

References SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::CreateBuffer(), Format(), getOriginalSymbols(), LIB_SYMBOL::GetParent(), SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::GetSymbol(), m_libs, and LIB_SYMBOL::SetParent().

Referenced by GetBufferedSymbol(), GetRootSymbolNames(), HasDerivedSymbols(), RemoveSymbol(), UpdateSymbol(), and UpdateSymbolAfterRename().

◆ GetLibraryCount()

size_t SYMBOL_LIBRARY_MANAGER::GetLibraryCount ( ) const

Definition at line 699 of file symbol_library_manager.cpp.

700 {
701  return symTable()->GetLogicalLibs().size();
702 }
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 140 of file symbol_library_manager.cpp.

141 {
142  const auto libBufIt = m_libs.find( aLibrary );
143 
144  if( libBufIt != m_libs.end() )
145  return libBufIt->second.GetHash();
146 
147  auto row = GetLibrary( aLibrary );
148 
149  // return -1 if library does not exist or 0 if not modified
150  return row ? std::hash<std::string>{}( aLibrary.ToStdString() +
151  row->GetFullURI( true ).ToStdString() ) : -1;
152 }
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 705 of file symbol_library_manager.cpp.

706 {
707  wxFileName fn( aFilePath );
708  return fn.GetName();
709 }

Referenced by addLibrary().

◆ GetLibraryNames()

wxArrayString SYMBOL_LIBRARY_MANAGER::GetLibraryNames ( ) const

Return the array of library names.

Definition at line 155 of file symbol_library_manager.cpp.

156 {
157  wxArrayString res;
158 
159  for( const auto& libName : symTable()->GetLogicalLibs() )
160  res.Add( libName );
161 
162  return res;
163 }
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().

◆ getOriginalSymbols()

std::set< LIB_SYMBOL * > SYMBOL_LIBRARY_MANAGER::getOriginalSymbols ( const wxString &  aLibrary)
private

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

Definition at line 754 of file symbol_library_manager.cpp.

755 {
756  std::set<LIB_SYMBOL*> symbols;
757  wxCHECK( LibraryExists( aLibrary ), symbols );
758 
759  try
760  {
761  wxArrayString aliases;
762  symTable()->EnumerateSymbolLib( aLibrary, aliases );
763 
764  for( const auto& aliasName : aliases )
765  {
766  LIB_SYMBOL* alias = symTable()->LoadSymbol( aLibrary, aliasName );
767  symbols.insert( alias );
768  }
769  }
770  catch( const IO_ERROR& e )
771  {
772  wxLogMessage( _( "Cannot enumerate library '%s'." ) + e.What(), aLibrary );
773  }
774 
775  return symbols;
776 }
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
Define a library symbol object.
Definition: lib_symbol.h:96
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
#define _(s)
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
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 681 of file symbol_library_manager.cpp.

683 {
684  LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
685 
686  libBuf.GetRootSymbolNames( aRootSymbolNames );
687 }
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::CreateNewSymbol(), 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 symbol.

SYMBOL_LIBRARY_MANAGER retains the ownership.

Definition at line 443 of file symbol_library_manager.cpp.

444 {
445  wxCHECK( LibraryExists( aLibrary ), nullptr );
446  wxCHECK( !aAlias.IsEmpty(), nullptr );
447  auto it = m_libs.find( aLibrary );
448  wxCHECK( it != m_libs.end(), nullptr );
449 
450  LIB_BUFFER& buf = it->second;
451  auto symbolBuf = buf.GetBuffer( aAlias );
452  return symbolBuf ? symbolBuf->GetScreen() : nullptr;
453 }
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::LoadOneLibrarySymbolAux().

◆ 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 661 of file symbol_library_manager.cpp.

662 {
663  wxString name = "New_Library";
664 
665  if( !LibraryExists( name ) )
666  return name;
667 
668  name += "_";
669 
670  for( unsigned int i = 0; i < std::numeric_limits<unsigned int>::max(); ++i )
671  {
672  if( !LibraryExists( name + wxString::Format( "%u", i ) ) )
673  return name + wxString::Format( "%u", i );
674  }
675 
676  wxFAIL;
677  return wxEmptyString;
678 }
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:56

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 690 of file symbol_library_manager.cpp.

692 {
693  LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
694 
695  return libBuf.HasDerivedSymbols( aSymbolName );
696 }
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::DeleteSymbolFromLibrary().

◆ HasModifications()

bool SYMBOL_LIBRARY_MANAGER::HasModifications ( ) const

Definition at line 117 of file symbol_library_manager.cpp.

118 {
119  for( const auto& lib : m_libs )
120  {
121  if( lib.second.IsModified() )
122  return true;
123  }
124 
125  return false;
126 }
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 343 of file symbol_library_manager.cpp.

344 {
345  wxCHECK( LibraryExists( aLibrary ), false );
346 
347  return symTable()->IsSymbolLibLoaded( aLibrary );
348 }
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 279 of file symbol_library_manager.cpp.

280 {
281  auto it = m_libs.find( aLibrary );
282  return it != m_libs.end() ? it->second.IsModified() : false;
283 }
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 335 of file symbol_library_manager.cpp.

336 {
337  wxCHECK( LibraryExists( aLibrary ), true );
338 
339  return !symTable()->IsSymbolLibWritable( aLibrary );
340 }
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().

◆ IsSymbolModified()

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

Return true if symbol has unsaved modifications.

Definition at line 286 of file symbol_library_manager.cpp.

288 {
289  auto libIt = m_libs.find( aLibrary );
290 
291  if( libIt == m_libs.end() )
292  return false;
293 
294  const LIB_BUFFER& buf = libIt->second;
295  auto symbolBuf = buf.GetBuffer( aAlias );
296  return symbolBuf ? symbolBuf->IsModified() : false;
297 }
std::map< wxString, LIB_BUFFER > m_libs

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

Referenced by SYMBOL_EDIT_FRAME::DeleteSymbolFromLibrary(), 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 649 of file symbol_library_manager.cpp.

650 {
651  if( aLibrary.IsEmpty() )
652  return false;
653 
654  if( m_libs.count( aLibrary ) > 0 )
655  return true;
656 
657  return symTable()->HasLibrary( aLibrary, aCheckEnabled );
658 }
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::CreateNewSymbol(), SYMBOL_EDIT_FRAME::DuplicateSymbol(), GetAliases(), GetBufferedSymbol(), getOriginalSymbols(), GetScreen(), GetUniqueLibraryName(), SYMBOL_EDIT_FRAME::ImportSymbol(), IsLibraryLoaded(), IsLibraryReadOnly(), SaveLibrary(), SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync(), and UpdateSymbol().

◆ 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 75 of file symbol_library_manager.cpp.

76 {
77  const int progressIntervalMillis = 60;
78 
79  SYMBOL_ASYNC_LOADER loader( symTable()->GetLogicalLibs(), symTable(), false, nullptr,
80  &aReporter );
81 
82  LOCALE_IO toggle;
83 
84  loader.Start();
85 
86  while( !loader.Done() )
87  {
88  aReporter.KeepRefreshing();
89 
90  wxMilliSleep( progressIntervalMillis );
91  }
92 
93  if( aReporter.IsCancelled() )
94  {
95  loader.Abort();
96  }
97  else
98  {
99  loader.Join();
100  }
101 
102  if( !loader.GetErrors().IsEmpty() )
103  {
104  HTML_MESSAGE_BOX dlg( &m_frame, _( "Load Error" ) );
105 
106  dlg.MessageSet( _( "Errors loading symbols:" ) );
107 
108  wxString msg = loader.GetErrors();
109  msg.Replace( "\n", "<BR>" );
110 
111  dlg.AddHTML_Text( msg );
112  dlg.ShowModal();
113  }
114 }
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.
#define _(s)
virtual bool KeepRefreshing(bool aWait=false)=0
Update the UI (if any).
virtual bool IsCancelled() const =0

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

◆ RemoveSymbol()

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

Remove the symbol from the symbol buffer.

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

Definition at line 581 of file symbol_library_manager.cpp.

582 {
583  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
584  auto symbolBuf = libBuf.GetBuffer( aAlias );
585  wxCHECK( symbolBuf, false );
586 
587  bool retv = true;
588 
589  retv &= libBuf.DeleteBuffer( symbolBuf );
590  m_frame.SyncLibraries( false );
591 
592  return retv;
593 }
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::DeleteSymbolFromLibrary().

◆ RevertAll()

bool SYMBOL_LIBRARY_MANAGER::RevertAll ( )

Revert all pending changes.

Returns
True if all changes successfully reverted.

Definition at line 555 of file symbol_library_manager.cpp.

556 {
557  bool retv = true;
558 
559  // Nothing to revert.
560  if( GetHash() == 0 )
561  return true;
562 
563  for( const auto& lib : m_libs )
564  {
565  if( !lib.second.IsModified() )
566  continue;
567 
568  for( const auto& buffer : lib.second.GetBuffers() )
569  {
570  if( !buffer->IsModified() )
571  continue;
572 
573  RevertSymbol( lib.first, buffer->GetOriginal()->GetName() );
574  }
575  }
576 
577  return retv;
578 }
LIB_ID RevertSymbol(const wxString &aAlias, const wxString &aLibrary)
Revert unsaved changes for a symbolicular symbol.
std::map< wxString, LIB_BUFFER > m_libs

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

Referenced by SYMBOL_EDIT_FRAME::RevertAll().

◆ RevertLibrary()

bool SYMBOL_LIBRARY_MANAGER::RevertLibrary ( const wxString &  aLibrary)

Revert unsaved changes for a symbolicular library.

Returns
True on success, false otherwise.

Definition at line 541 of file symbol_library_manager.cpp.

542 {
543  auto it = m_libs.find( aLibrary );
544 
545  if( it == m_libs.end() ) // nothing to reverse
546  return false;
547 
548  m_libs.erase( it );
549  m_frame.SyncLibraries( false );
550 
551  return true;
552 }
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().

◆ RevertSymbol()

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

Revert unsaved changes for a symbolicular symbol.

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

Definition at line 516 of file symbol_library_manager.cpp.

517 {
518  auto it = m_libs.find( aLibrary );
519 
520  if( it == m_libs.end() ) // no items to flush
521  return LIB_ID( aLibrary, aAlias );
522 
523  auto symbolBuf = it->second.GetBuffer( aAlias );
524  wxCHECK( symbolBuf, LIB_ID( aLibrary, aAlias ) );
525  LIB_SYMBOL original( *symbolBuf->GetOriginal() );
526 
527  if( original.GetName() != aAlias )
528  {
529  UpdateSymbolAfterRename( &original, aAlias, aLibrary );
530  }
531  else
532  {
533  symbolBuf->SetSymbol( new LIB_SYMBOL( original ) );
534  m_frame.SyncLibraries( false );
535  }
536 
537  return LIB_ID( aLibrary, original.GetName() );
538 }
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:96
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 ...
bool UpdateSymbolAfterRename(LIB_SYMBOL *aSymbol, const wxString &oldAlias, const wxString &aLibrary)
Update the symbol buffer with a new version of the symbol when the name has changed.

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

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 184 of file symbol_library_manager.cpp.

186 {
187  wxCHECK( aFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY && LibraryExists( aLibrary ), false );
188  wxFileName fn( aFileName );
189  wxCHECK( !fn.FileExists() || fn.IsFileWritable(), false );
190  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( aFileType ) );
191  bool res = true; // assume all libraries are successfully saved
192 
193  PROPERTIES properties;
194  properties.emplace( SCH_LEGACY_PLUGIN::PropBuffering, "" );
195 
196  auto it = m_libs.find( aLibrary );
197 
198  if( it != m_libs.end() )
199  {
200  // Handle buffered library
201  LIB_BUFFER& libBuf = it->second;
202 
203  const auto& symbolBuffers = libBuf.GetBuffers();
204 
205  for( const auto& symbolBuf : symbolBuffers )
206  {
207  if( !libBuf.SaveBuffer( symbolBuf, aFileName, &*pi, true ) )
208  {
209  // Something went wrong, but try to save other libraries
210  res = false;
211  }
212  }
213 
214  // clear the deleted symbols buffer only if data is saved to the original file
215  wxFileName original, destination( aFileName );
216  auto row = GetLibrary( aLibrary );
217 
218  if( row )
219  {
220  original = row->GetFullURI( true );
221  original.Normalize();
222  }
223 
224  destination.Normalize();
225 
226  if( res && original == destination )
227  libBuf.ClearDeletedBuffer();
228  }
229  else
230  {
231  // Handle original library
232  for( LIB_SYMBOL* symbol : getOriginalSymbols( aLibrary ) )
233  {
234  LIB_SYMBOL* newSymbol;
235 
236  if( symbol->IsAlias() )
237  {
238  std::shared_ptr< LIB_SYMBOL > oldParent = symbol->GetParent().lock();
239 
240  wxCHECK_MSG( oldParent, false,
241  wxString::Format( "Derived symbol '%s' found with undefined parent.",
242  symbol->GetName() ) );
243 
244  LIB_SYMBOL* libParent = pi->LoadSymbol( aLibrary, oldParent->GetName(),
245  &properties );
246 
247  if( !libParent )
248  {
249  libParent = new LIB_SYMBOL( *oldParent.get() );
250  pi->SaveSymbol( aLibrary, libParent, &properties );
251  }
252 
253  newSymbol = new LIB_SYMBOL( *symbol );
254  newSymbol->SetParent( libParent );
255  pi->SaveSymbol( aLibrary, newSymbol, &properties );
256  }
257  else if( !pi->LoadSymbol( aLibrary, symbol->GetName(), &properties ) )
258  {
259  pi->SaveSymbol( aLibrary, new LIB_SYMBOL( *symbol ), &properties );
260  }
261  }
262  }
263 
264  try
265  {
266  pi->SaveLibrary( aFileName );
267  }
268  catch( ... )
269  {
270  // return false because something happens.
271  // The library is not successfully saved
272  res = false;
273  }
274 
275  return res;
276 }
LIB_SYMBOL_REF & GetParent()
Definition: lib_symbol.h:124
A name/value tuple with unique names and optional values.
Definition: properties.h:33
Define a library symbol object.
Definition: lib_symbol.h:96
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
void SetParent(LIB_SYMBOL *aParent=nullptr)
Definition: lib_symbol.cpp:322
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:479
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.
std::set< LIB_SYMBOL * > getOriginalSymbols(const wxString &aLibrary)
Return a set of LIB_SYMBOL objects belonging to the original library.
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(), getOriginalSymbols(), LIB_SYMBOL::GetParent(), LibraryExists(), m_libs, SCH_LEGACY_PLUGIN::PropBuffering, SYMBOL_LIBRARY_MANAGER::LIB_BUFFER::SaveBuffer(), and LIB_SYMBOL::SetParent().

Referenced by SYMBOL_EDIT_FRAME::saveLibrary().

◆ SymbolExists()

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

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

Definition at line 628 of file symbol_library_manager.cpp.

629 {
630  auto libBufIt = m_libs.find( aLibrary );
631  LIB_SYMBOL* alias = nullptr;
632 
633  if( libBufIt != m_libs.end() )
634  return !!libBufIt->second.GetBuffer( aAlias );
635 
636  try
637  {
638  alias = symTable()->LoadSymbol( aLibrary, aAlias );
639  }
640  catch( IO_ERROR& )
641  {
642  // checking if certain symbol exists, so its absence is perfectly fine
643  }
644 
645  return alias != nullptr;
646 }
SYMBOL_LIB_TABLE * symTable() const
Define a library symbol object.
Definition: lib_symbol.h:96
std::map< wxString, LIB_BUFFER > m_libs
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
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::CreateNewSymbol(), SYMBOL_EDIT_FRAME::ensureUniqueName(), SYMBOL_EDIT_FRAME::ImportSymbol(), SYMBOL_EDIT_FRAME::Revert(), SYMBOL_EDIT_FRAME::saveSymbolAs(), DIALOG_LIB_SYMBOL_PROPERTIES::TransferDataFromWindow(), and SYMBOL_EDIT_FRAME::UpdateAfterSymbolProperties().

◆ symTable()

SYMBOL_LIB_TABLE * SYMBOL_LIBRARY_MANAGER::symTable ( ) const
private

Definition at line 748 of file symbol_library_manager.cpp.

749 {
750  return m_frame.Prj().SchSymbolLibTable();
751 }
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 FlushSymbol(), GetAlias(), GetAliases(), GetBufferedSymbol(), GetHash(), GetLibrary(), GetLibraryCount(), GetLibraryNames(), getOriginalSymbols(), IsLibraryLoaded(), IsLibraryReadOnly(), LibraryExists(), Preload(), SymbolExists(), 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 62 of file symbol_library_manager.cpp.

65 {
66  m_logger->Activate();
67  {
68  getAdapter()->Sync( aForceRefresh, aProgressCallback );
70  }
72 }
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_SYMBOL object and editor context.
void Activate()
Definition: lib_logger.cpp:39
void Deactivate()
Definition: lib_logger.cpp:49
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().

◆ UpdateSymbol()

bool SYMBOL_LIBRARY_MANAGER::UpdateSymbol ( LIB_SYMBOL aSymbol,
const wxString &  aLibrary 
)

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

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

Definition at line 456 of file symbol_library_manager.cpp.

457 {
458  wxCHECK( LibraryExists( aLibrary ), false );
459  wxCHECK( aSymbol, false );
460  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
461  auto symbolBuf = libBuf.GetBuffer( aSymbol->GetName() );
462 
463  if( symbolBuf ) // Existing symbol.
464  {
465  LIB_SYMBOL* bufferedSymbol = const_cast< LIB_SYMBOL* >( symbolBuf->GetSymbol() );
466 
467  wxCHECK( bufferedSymbol, false );
468 
469  *bufferedSymbol = *aSymbol;
470  symbolBuf->GetScreen()->SetContentModified();
471  }
472  else // New symbol
473  {
474  LIB_SYMBOL* symbolCopy = new LIB_SYMBOL( *aSymbol, nullptr );
475 
476  symbolCopy->SetLibId( LIB_ID( aLibrary, aSymbol->GetLibId().GetLibItemName() ) );
477 
478  SCH_SCREEN* screen = new SCH_SCREEN;
479  libBuf.CreateBuffer( symbolCopy, screen );
480  screen->SetContentModified();
481  }
482 
483  return true;
484 }
const UTF8 & GetLibItemName() const
Definition: lib_id.h:104
wxString GetName() const override
Definition: lib_symbol.h:133
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
Define a library symbol object.
Definition: lib_symbol.h:96
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Return an existing library buffer or creates one to using Symbol Library Table to get the original da...
LIB_ID GetLibId() const override
Definition: lib_symbol.h:135
void SetLibId(const LIB_ID &aLibId)
Definition: lib_symbol.h:136
void SetContentModified(bool aModified=true)
Definition: base_screen.h:59
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.

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

Referenced by SYMBOL_EDIT_FRAME::CreateNewSymbol(), SYMBOL_EDIT_FRAME::DuplicateSymbol(), SYMBOL_EDIT_FRAME::ImportSymbol(), SYMBOL_EDIT_FRAME::saveSymbolAs(), and SYMBOL_EDIT_FRAME::storeCurrentSymbol().

◆ UpdateSymbolAfterRename()

bool SYMBOL_LIBRARY_MANAGER::UpdateSymbolAfterRename ( LIB_SYMBOL aSymbol,
const wxString &  oldAlias,
const wxString &  aLibrary 
)

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

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

Definition at line 487 of file symbol_library_manager.cpp.

489 {
490  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
491  auto symbolBuf = libBuf.GetBuffer( aOldName );
492 
493  wxCHECK( symbolBuf, false );
494 
495  libBuf.UpdateBuffer( symbolBuf, aSymbol );
496  m_frame.SyncLibraries( false );
497 
498  return true;
499 }
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(), RevertSymbol(), and SYMBOL_EDIT_FRAME::UpdateAfterSymbolProperties().

Member Data Documentation

◆ m_adapter

wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER> SYMBOL_LIBRARY_MANAGER::m_adapter
private

Definition at line 428 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 425 of file symbol_library_manager.h.

Referenced by SYMBOL_LIBRARY_MANAGER(), Sync(), and ~SYMBOL_LIBRARY_MANAGER().

◆ m_syncHash

int SYMBOL_LIBRARY_MANAGER::m_syncHash
private

Symbol lib table hash value from last synchronization.

Definition at line 426 of file symbol_library_manager.h.

Referenced by Sync().


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