KiCad PCB EDA Suite
LIB_MANAGER Class Reference

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

#include <lib_manager.h>

Classes

class  LIB_BUFFER
 

Class to store a working copy of a library

More...
 
class  PART_BUFFER
 

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

More...
 

Public Member Functions

 LIB_MANAGER (LIB_EDIT_FRAME &aFrame)
 
void Sync (bool aForce=false, std::function< void(int, int, const wxString &)> aProgressCallback=[](int, int, const wxString &){})
 Updates the LIB_MANAGER data to synchronize with Symbol Library Table. More...
 
int GetHash () const
 
bool HasModifications () const
 
int GetLibraryHash (const wxString &aLibrary) const
 Returns a library hash value to determine if it has changed. More...
 
wxArrayString GetLibraryNames () const
 Returns the array of library names. More...
 
SYMBOL_LIB_TABLE_ROWGetLibrary (const wxString &aLibrary) const
 Finds 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)
 Creates an empty library and adds it to the library table. More...
 
bool AddLibrary (const wxString &aFilePath, SYMBOL_LIB_TABLE *aTable)
 Adds an existing library. More...
 
bool UpdatePart (LIB_PART *aPart, const wxString &aLibrary)
 Updates the part buffer with a new version of the part. More...
 
bool UpdatePartAfterRename (LIB_PART *aPart, const wxString &oldAlias, const wxString &aLibrary)
 Updates the part buffer with a new version of the part when the name has changed. More...
 
bool RemovePart (const wxString &aName, const wxString &aLibrary)
 Removes the part from the part buffer. More...
 
LIB_PARTGetAlias (const wxString &aAlias, const wxString &aLibrary) const
 Returns 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)
 Returns the part copy from the buffer. More...
 
SCH_SCREENGetScreen (const wxString &aAlias, const wxString &aLibrary)
 Returns the screen used to edit a specific part. More...
 
bool PartExists (const wxString &aAlias, const wxString &aLibrary) const
 Returns 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
 Returns true if library exists. More...
 
bool IsLibraryLoaded (const wxString &aLibrary) const
 Returns true if the library was successfully loaded. More...
 
bool IsLibraryModified (const wxString &aLibrary) const
 Returns true if library has unsaved modifications. More...
 
bool IsPartModified (const wxString &aAlias, const wxString &aLibrary) const
 Returns true if part has unsaved modifications. More...
 
bool ClearLibraryModified (const wxString &aLibrary) const
 Clears the modified flag for all parts in a library. More...
 
bool ClearPartModified (const wxString &aAlias, const wxString &aLibrary) const
 Clears the modified flag for a part. More...
 
bool IsLibraryReadOnly (const wxString &aLibrary) const
 Returns true if the library is stored in a read-only file. More...
 
bool FlushPart (const wxString &aAlias, const wxString &aLibrary)
 Saves 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)
 Saves library to a file, including unsaved changes. More...
 
LIB_ID RevertPart (const wxString &aAlias, const wxString &aLibrary)
 Reverts unsaved changes for a particular part. More...
 
bool RevertLibrary (const wxString &aLibrary)
 Reverts unsaved changes for a particular library. More...
 
bool RevertAll ()
 Revert all pending changes. More...
 
wxString GetUniqueLibraryName () const
 Returns a library name that is not currently in use. More...
 
LIB_TREE_MODEL_ADAPTER::PTRGetAdapter ()
 Returns the adapter object that provides the stored data. More...
 
const wxString & GetCurrentLib () const
 Returns the currently modified library name. More...
 
void SetCurrentLib (const wxString &aLibrary)
 
const wxString & GetCurrentPart () const
 Returns the currently modified part name. More...
 
void SetCurrentPart (const wxString &aPart)
 
LIB_ID GetCurrentLibId () const
 Returns the current library and part name as LIB_ID. 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...
 

Private Member Functions

bool addLibrary (const wxString &aFilePath, bool aCreate, SYMBOL_LIB_TABLE *aTable)
 

Helper function to add either existing or create new library

More...
 
SYMBOL_LIB_TABLEsymTable () const
 

Returns the current Symbol Library Table

More...
 
std::set< LIB_PART * > getOriginalParts (const wxString &aLibrary)
 

Returns a set of LIB_PART objects belonging to the original library

More...
 
LIB_BUFFERgetLibraryBuffer (const wxString &aLibrary)
 

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

More...
 
SYMBOL_TREE_SYNCHRONIZING_ADAPTERgetAdapter ()
 

Static Private Member Functions

static wxString getLibraryName (const wxString &aFilePath)
 

Extracts library name basing on the file name

More...
 

Private Attributes

std::map< wxString, LIB_BUFFERm_libs
 

The library buffers

More...
 
LIB_EDIT_FRAMEm_frame
 
LIB_LOGGER m_logger
 
int m_syncHash
 
wxString m_currentLib
 
wxString m_currentPart
 
SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR m_adapter
 

Detailed Description

Class to handle modifications to the symbol libraries.

Definition at line 99 of file lib_manager.h.

Constructor & Destructor Documentation

◆ LIB_MANAGER()

LIB_MANAGER::LIB_MANAGER ( LIB_EDIT_FRAME aFrame)

Definition at line 40 of file lib_manager.cpp.

40  :
41  m_frame( aFrame ),
42  m_syncHash( 0 )
43 {
45  m_adapter->ShowUnits( false );
46 }
static PTR Create(LIB_EDIT_FRAME *aParent, LIB_MANAGER *aLibs)
LIB_EDIT_FRAME & m_frame
Definition: lib_manager.h:467
SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR m_adapter
Definition: lib_manager.h:474

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

Member Function Documentation

◆ AddLibrary()

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

Adds an existing library.

The library is added to the library table as well.

Definition at line 146 of file lib_manager.h.

147  {
148  return addLibrary( aFilePath, false, aTable );
149  }
bool addLibrary(const wxString &aFilePath, bool aCreate, SYMBOL_LIB_TABLE *aTable)
Helper function to add either existing or create new library

References addLibrary().

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

◆ addLibrary()

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

Helper function to add either existing or create new library

Definition at line 650 of file lib_manager.cpp.

651 {
652  wxCHECK( aTable, false );
653  wxString libName = getLibraryName( aFilePath );
654  wxCHECK( !LibraryExists( libName ), false ); // either create or add an existing one
655 
656  // try to use path normalized to an environmental variable or project path
657  wxString relPath = NormalizePath( aFilePath, &Pgm().GetLocalEnvVariables(), &m_frame.Prj() );
658 
659  if( relPath.IsEmpty() )
660  relPath = aFilePath;
661 
662  SCH_IO_MGR::SCH_FILE_T schFileType = SCH_IO_MGR::GuessPluginTypeFromLibPath( aFilePath );
663  wxString typeName = SCH_IO_MGR::ShowType( schFileType );
664  SYMBOL_LIB_TABLE_ROW* libRow = new SYMBOL_LIB_TABLE_ROW( libName, relPath, typeName );
665  aTable->InsertRow( libRow );
666 
667  if( aCreate )
668  {
669  wxCHECK( schFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY, false );
670 
671  try
672  {
673  aTable->CreateSymbolLib( libName );
674  }
675  catch( const IO_ERROR& )
676  {
677  aTable->RemoveRow( libRow );
678  return false;
679  }
680  }
681 
682  m_frame.SyncLibraries( false );
683 
684  return true;
685 }
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:169
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:87
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:102
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
LIB_EDIT_FRAME & m_frame
Definition: lib_manager.h:467
bool RemoveRow(LIB_TABLE_ROW *aRow)
Removes a row from the table.
void SyncLibraries(bool aShowProgress)
Synchronize the library manager to the symbol library table, and then the symbol tree to the library ...
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
static wxString getLibraryName(const wxString &aFilePath)
Extracts library name basing on the file name
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
wxString NormalizePath(const wxFileName &aFilePath, const ENV_VAR_MAP *aEnvVars, const wxString &aProjectPath)
Normalizes a file path to an environmental variable, if possible.
Definition: env_paths.cpp:68

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 LIB_EDIT_FRAME::SyncLibraries().

Referenced by AddLibrary(), and CreateLibrary().

◆ ClearLibraryModified()

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

Clears the modified flag for all parts in a library.

Definition at line 247 of file lib_manager.cpp.

248 {
249  auto libIt = m_libs.find( aLibrary );
250 
251  if( libIt == m_libs.end() )
252  return false;
253 
254  for( auto& partBuf : libIt->second.GetBuffers() )
255  {
256  SCH_SCREEN* screen = partBuf->GetScreen();
257 
258  if( screen )
259  screen->ClrModify();
260  }
261 
262  return true;
263 }
void ClrModify()
Definition: base_screen.h:124
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465

References BASE_SCREEN::ClrModify(), and m_libs.

Referenced by LIB_EDIT_FRAME::saveLibrary().

◆ ClearPartModified()

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

Clears the modified flag for a part.

Definition at line 266 of file lib_manager.cpp.

267 {
268  auto libI = m_libs.find( aLibrary );
269 
270  if( libI == m_libs.end() )
271  return false;
272 
273  auto partBuf = libI->second.GetBuffer( aAlias );
274  wxCHECK( partBuf, false );
275 
276  partBuf->GetScreen()->ClrModify();
277  return true;
278 }
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465

References m_libs.

Referenced by LIB_EDIT_FRAME::Revert(), LIB_EDIT_FRAME::Save(), and LIB_EDIT_FRAME::saveCurrentPart().

◆ CreateLibrary()

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

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

The library file is created.

Definition at line 138 of file lib_manager.h.

139  {
140  return addLibrary( aFilePath, true, aTable );
141  }
bool addLibrary(const wxString &aFilePath, bool aCreate, SYMBOL_LIB_TABLE *aTable)
Helper function to add either existing or create new library

References addLibrary().

Referenced by LIB_EDIT_FRAME::AddLibraryFile().

◆ FlushPart()

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

Saves 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 448 of file lib_manager.cpp.

449 {
450  auto it = m_libs.find( aLibrary );
451 
452  if( it == m_libs.end() ) // no items to flush
453  return true;
454 
455  auto partBuf = it->second.GetBuffer( aAlias );
456  wxCHECK( partBuf, false );
457 
458  return it->second.SaveBuffer( partBuf, symTable() );
459 }
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465

References m_libs, and symTable().

Referenced by LIB_EDIT_FRAME::Save(), and LIB_EDIT_FRAME::saveCurrentPart().

◆ GetAdapter()

LIB_TREE_MODEL_ADAPTER::PTR& LIB_MANAGER::GetAdapter ( )
inline

Returns the adapter object that provides the stored data.

Definition at line 276 of file lib_manager.h.

276 { return m_adapter; }
SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR m_adapter
Definition: lib_manager.h:474

References m_adapter.

Referenced by LIB_EDIT_FRAME::FreezeSearchTree(), SYMBOL_TREE_PANE::SYMBOL_TREE_PANE(), LIB_EDIT_FRAME::SyncLibraries(), and LIB_EDIT_FRAME::ThawSearchTree().

◆ getAdapter()

SYMBOL_TREE_SYNCHRONIZING_ADAPTER* LIB_MANAGER::getAdapter ( )
inlineprivate

Definition at line 475 of file lib_manager.h.

476  {
477  return static_cast<SYMBOL_TREE_SYNCHRONIZING_ADAPTER*>( m_adapter.get() );
478  }
SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR m_adapter
Definition: lib_manager.h:474

References m_adapter.

Referenced by Sync().

◆ GetAlias()

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

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

Definition at line 542 of file lib_manager.cpp.

543 {
544  // Try the library buffers first
545  auto libIt = m_libs.find( aLibrary );
546 
547  if( libIt != m_libs.end() )
548  {
549  LIB_PART* part = libIt->second.GetPart( aAlias );
550 
551  if( part )
552  return part;
553  }
554 
555  // Get the original part
556  LIB_PART* alias = nullptr;
557 
558  try
559  {
560  alias = symTable()->LoadSymbol( aLibrary, aAlias );
561  }
562  catch( const IO_ERROR& e )
563  {
564  wxLogMessage( _( "Cannot load symbol \"%s\" from library \"%s\" (%s)" ),
565  aAlias, aLibrary, e.What() );
566  }
567 
568  return alias;
569 }
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.
#define _(s)
Definition: 3d_actions.cpp:33
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76

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

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

◆ GetAliases()

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

Definition at line 297 of file lib_manager.cpp.

298 {
299  std::list<LIB_PART*> ret;
300  wxCHECK( LibraryExists( aLibrary ), ret );
301 
302  auto libIt = m_libs.find( aLibrary );
303 
304  if( libIt != m_libs.end() )
305  {
306  for( auto& partBuf : libIt->second.GetBuffers() )
307  {
308  ret.push_back( partBuf->GetPart() );
309  }
310  }
311  else
312  {
313  std::vector<LIB_PART*> aliases;
314 
315  try
316  {
317  symTable()->LoadSymbolLib( aliases, aLibrary );
318  }
319  catch( const IO_ERROR& e )
320  {
321  wxLogWarning( e.Problem() );
322  }
323 
324  std::copy( aliases.begin(), aliases.end(), std::back_inserter( ret ) );
325  }
326 
327  return ret;
328 }
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)
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76

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

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::updateLibrary().

◆ GetBufferedPart()

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

Returns the part copy from the buffer.

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

Definition at line 331 of file lib_manager.cpp.

332 {
333  wxCHECK( LibraryExists( aLibrary ), nullptr );
334 
335  // try the library buffers first
336  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
337  LIB_PART* bufferedPart = libBuf.GetPart( aAlias );
338 
339  if( !bufferedPart ) // no buffer part found
340  {
341  // create a copy of the part
342  try
343  {
344  LIB_PART* part = symTable()->LoadSymbol( aLibrary, aAlias );
345 
346  if( part == nullptr )
347  THROW_IO_ERROR( _( "Symbol not found." ) );
348 
349  LIB_PART* bufferedParent = nullptr;
350 
351  // Create parent symbols on demand so parent symbol can be set.
352  if( part->IsAlias() )
353  {
354  std::shared_ptr< LIB_PART > parent = part->GetParent().lock();
355  wxCHECK_MSG( parent, nullptr,
356  wxString::Format( "Derived symbol '%s' found with undefined parent.",
357  part->GetName() ) );
358 
359  // Check if the parent symbol buffer has already be created.
360  bufferedParent = libBuf.GetPart( parent->GetName() );
361 
362  if( !bufferedParent )
363  {
364  bufferedParent = new LIB_PART( *parent.get() );
365  libBuf.CreateBuffer( bufferedParent, new SCH_SCREEN );
366  }
367  }
368 
369  bufferedPart = new LIB_PART( *part );
370 
371  if( bufferedParent )
372  bufferedPart->SetParent( bufferedParent );
373 
374  libBuf.CreateBuffer( bufferedPart, new SCH_SCREEN );
375  }
376  catch( const IO_ERROR& e )
377  {
378  wxLogMessage( _( "Error loading symbol \"%s\" from library \"%s\". (%s)" ),
379  aAlias, aLibrary, e.What() );
380  bufferedPart = nullptr;
381  }
382  }
383 
384  return bufferedPart;
385 }
wxString GetName() const override
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
void SetParent(LIB_PART *aParent=nullptr)
PART_REF & GetParent()
Define a library symbol object.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Returns an existing library buffer or creates one to using Symbol Library Table to get the original d...
#define _(s)
Definition: 3d_actions.cpp:33
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
bool IsAlias() const
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38

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

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

◆ GetCurrentLib()

const wxString& LIB_MANAGER::GetCurrentLib ( ) const
inline

Returns the currently modified library name.

Definition at line 281 of file lib_manager.h.

281 { return m_currentLib; }
wxString m_currentLib
Definition: lib_manager.h:471

References m_currentLib.

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr().

◆ GetCurrentLibId()

LIB_ID LIB_MANAGER::GetCurrentLibId ( ) const
inline

Returns the current library and part name as LIB_ID.

Definition at line 293 of file lib_manager.h.

294  {
295  return LIB_ID( m_currentLib, m_currentPart );
296  }
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
wxString m_currentLib
Definition: lib_manager.h:471
wxString m_currentPart
Definition: lib_manager.h:472

References m_currentLib, and m_currentPart.

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr().

◆ GetCurrentPart()

const wxString& LIB_MANAGER::GetCurrentPart ( ) const
inline

Returns the currently modified part name.

Definition at line 287 of file lib_manager.h.

287 { return m_currentPart; }
wxString m_currentPart
Definition: lib_manager.h:472

References m_currentPart.

◆ GetHash()

int LIB_MANAGER::GetHash ( ) const

Definition at line 78 of file lib_manager.cpp.

79 {
80  int hash = symTable()->GetModifyHash();
81 
82  for( const auto& lib : m_libs )
83  hash += lib.second.GetHash();
84 
85  return hash;
86 }
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465

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

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

◆ GetLibrary()

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

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

Definition at line 115 of file lib_manager.cpp.

116 {
117  SYMBOL_LIB_TABLE_ROW* row = nullptr;
118 
119  try
120  {
121  row = symTable()->FindRow( aLibrary );
122  }
123  catch( const IO_ERROR& e )
124  {
125  wxLogMessage( _( "Cannot find library \"%s\" in the Symbol Library Table (%s)" ),
126  aLibrary, e.What() );
127  }
128 
129  return row;
130 }
SYMBOL_LIB_TABLE_ROW * FindRow(const wxString &aNickName)
Return an SYMBOL_LIB_TABLE_ROW if aNickName is found in this table or in any chained fallBack table f...
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
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 * symTable() const
Returns the current Symbol Library Table
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76

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

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

◆ getLibraryBuffer()

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

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

Definition at line 719 of file lib_manager.cpp.

720 {
721  auto it = m_libs.find( aLibrary );
722 
723  if( it != m_libs.end() )
724  return it->second;
725 
726  // The requested buffer does not exist yet, so create one
727  auto ret = m_libs.emplace( aLibrary, LIB_BUFFER( aLibrary ) );
728  LIB_BUFFER& buf = ret.first->second;
729 
730  for( auto part : getOriginalParts( aLibrary ) )
731  {
732  LIB_PART* newSymbol;
733 
734  if( part->IsAlias() )
735  {
736  std::shared_ptr< LIB_PART > oldParent = part->GetParent().lock();
737 
738  wxCHECK_MSG( oldParent, buf,
739  wxString::Format( "Derived symbol '%s' found with undefined parent.",
740  part->GetName() ) );
741 
742  LIB_PART* libParent = buf.GetPart( oldParent->GetName() );
743 
744  if( !libParent )
745  {
746  libParent = new LIB_PART( *oldParent.get() );
747  buf.CreateBuffer( libParent, new SCH_SCREEN );
748  }
749 
750  newSymbol = new LIB_PART( *part );
751  newSymbol->SetParent( libParent );
752  buf.CreateBuffer( newSymbol, new SCH_SCREEN );
753  }
754  else if( !buf.GetPart( part->GetName() ) )
755  {
756  buf.CreateBuffer( new LIB_PART( *part ), new SCH_SCREEN );
757  }
758  }
759 
760  return buf;
761 }
void SetParent(LIB_PART *aParent=nullptr)
PART_REF & GetParent()
Define a library symbol object.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
std::set< LIB_PART * > getOriginalParts(const wxString &aLibrary)
Returns a set of LIB_PART objects belonging to the original library
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465

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

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

◆ GetLibraryHash()

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

Returns 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 89 of file lib_manager.cpp.

90 {
91  const auto libBufIt = m_libs.find( aLibrary );
92 
93  if( libBufIt != m_libs.end() )
94  return libBufIt->second.GetHash();
95 
96  auto row = GetLibrary( aLibrary );
97 
98  // return -1 if library does not exist or 0 if not modified
99  return row ? std::hash<std::string>{}( aLibrary.ToStdString() +
100  row->GetFullURI( true ).ToStdString() ) : -1;
101 }
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Finds a single library within the (aggregate) library table.
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465

References GetLibrary(), and m_libs.

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

◆ getLibraryName()

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

Extracts library name basing on the file name

Definition at line 643 of file lib_manager.cpp.

644 {
645  wxFileName fn( aFilePath );
646  return fn.GetName();
647 }

Referenced by addLibrary().

◆ GetLibraryNames()

wxArrayString LIB_MANAGER::GetLibraryNames ( ) const

Returns the array of library names.

Definition at line 104 of file lib_manager.cpp.

105 {
106  wxArrayString res;
107 
108  for( const auto& libName : symTable()->GetLogicalLibs() )
109  res.Add( libName );
110 
111  return res;
112 }
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table

References symTable().

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

◆ getOriginalParts()

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

Returns a set of LIB_PART objects belonging to the original library

Definition at line 694 of file lib_manager.cpp.

695 {
696  std::set<LIB_PART*> parts;
697  wxCHECK( LibraryExists( aLibrary ), parts );
698 
699  try
700  {
701  wxArrayString aliases;
702  symTable()->EnumerateSymbolLib( aLibrary, aliases );
703 
704  for( const auto& aliasName : aliases )
705  {
706  LIB_PART* alias = symTable()->LoadSymbol( aLibrary, aliasName );
707  parts.insert( alias );
708  }
709  }
710  catch( const IO_ERROR& e )
711  {
712  wxLogMessage( _( "Cannot enumerate library \"%s\" (%s)" ), aLibrary, e.What() );
713  }
714 
715  return parts;
716 }
void EnumerateSymbolLib(const wxString &aNickname, wxArrayString &aAliasNames, bool aPowerSymbolsOnly=false)
Return a list of symbol alias names contained within the library given by aNickname.
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.
#define _(s)
Definition: 3d_actions.cpp:33
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76

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

Referenced by getLibraryBuffer(), and SaveLibrary().

◆ GetRootSymbolNames()

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

Definition at line 625 of file lib_manager.cpp.

627 {
628  LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
629 
630  libBuf.GetRootSymbolNames( aRootSymbolNames );
631 }
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Returns an existing library buffer or creates one to using Symbol Library Table to get the original d...

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

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

◆ GetScreen()

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

Returns the screen used to edit a specific part.

LIB_MANAGER retains the ownership.

Definition at line 388 of file lib_manager.cpp.

389 {
390  wxCHECK( LibraryExists( aLibrary ), nullptr );
391  wxCHECK( !aAlias.IsEmpty(), nullptr );
392  auto it = m_libs.find( aLibrary );
393  wxCHECK( it != m_libs.end(), nullptr );
394 
395  LIB_BUFFER& buf = it->second;
396  auto partBuf = buf.GetBuffer( aAlias );
397  return partBuf ? partBuf->GetScreen() : nullptr;
398 }
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465

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

Referenced by LIB_EDIT_FRAME::LoadOneLibraryPartAux().

◆ GetUniqueLibraryName()

wxString LIB_MANAGER::GetUniqueLibraryName ( ) const

Returns a library name that is not currently in use.

Used for generating names for new libraries.

Definition at line 605 of file lib_manager.cpp.

606 {
607  wxString name = "New_Library";
608 
609  if( !LibraryExists( name ) )
610  return name;
611 
612  name += "_";
613 
614  for( unsigned int i = 0; i < std::numeric_limits<unsigned int>::max(); ++i )
615  {
616  if( !LibraryExists( name + wxString::Format( "%u", i ) ) )
617  return name + wxString::Format( "%u", i );
618  }
619 
620  wxFAIL;
621  return wxEmptyString;
622 }
const char * name
Definition: DXF_plotter.cpp:59
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.

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

Referenced by LIB_EDIT_FRAME::AddLibraryFile().

◆ HasDerivedSymbols()

bool LIB_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 634 of file lib_manager.cpp.

636 {
637  LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
638 
639  return libBuf.HasDerivedSymbols( aSymbolName );
640 }
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Returns an existing library buffer or creates one to using Symbol Library Table to get the original d...

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

Referenced by LIB_MANAGER::LIB_BUFFER::DeleteBuffer(), and LIB_EDIT_FRAME::DeletePartFromLibrary().

◆ HasModifications()

bool LIB_MANAGER::HasModifications ( ) const

Definition at line 66 of file lib_manager.cpp.

67 {
68  for( const auto& lib : m_libs )
69  {
70  if( lib.second.IsModified() && !IsLibraryReadOnly( lib.first ) )
71  return true;
72  }
73 
74  return false;
75 }
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465
bool IsLibraryReadOnly(const wxString &aLibrary) const
Returns true if the library is stored in a read-only file.

References IsLibraryReadOnly(), and m_libs.

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

◆ IsLibraryLoaded()

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

Returns true if the library was successfully loaded.

Definition at line 289 of file lib_manager.cpp.

290 {
291  wxCHECK( LibraryExists( aLibrary ), false );
292 
293  return symTable()->IsSymbolLibLoaded( aLibrary );
294 }
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
Returns true if library exists.
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table

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

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

◆ IsLibraryModified()

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

Returns true if library has unsaved modifications.

Definition at line 227 of file lib_manager.cpp.

228 {
229  auto it = m_libs.find( aLibrary );
230  return it != m_libs.end() ? it->second.IsModified() : false;
231 }
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465

References m_libs.

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

◆ IsLibraryReadOnly()

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

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

Returns
True on success, false otherwise.

Definition at line 281 of file lib_manager.cpp.

282 {
283  wxCHECK( LibraryExists( aLibrary ), true );
284 
285  return !symTable()->IsSymbolLibWritable( aLibrary );
286 }
bool IsSymbolLibWritable(const wxString &aNickname)
Return true if the library given by aNickname is writable.
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table

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

Referenced by HasModifications(), LIB_EDIT_FRAME::IsContentModified(), LIB_EDIT_FRAME::Save(), LIB_EDIT_FRAME::saveAllLibraries(), LIB_EDIT_FRAME::saveLibrary(), LIB_EDIT_FRAME::setupUIConditions(), and LIB_EDIT_FRAME::updateTitle().

◆ IsPartModified()

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

Returns true if part has unsaved modifications.

Definition at line 234 of file lib_manager.cpp.

235 {
236  auto libIt = m_libs.find( aLibrary );
237 
238  if( libIt == m_libs.end() )
239  return false;
240 
241  const LIB_BUFFER& buf = libIt->second;
242  auto partBuf = buf.GetBuffer( aAlias );
243  return partBuf ? partBuf->IsModified() : false;
244 }
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465

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

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

◆ LibraryExists()

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

Returns true if library exists.

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

Definition at line 593 of file lib_manager.cpp.

594 {
595  if( aLibrary.IsEmpty() )
596  return false;
597 
598  if( m_libs.count( aLibrary ) > 0 )
599  return true;
600 
601  return symTable()->HasLibrary( aLibrary, aCheckEnabled );
602 }
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465

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

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

◆ PartExists()

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

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

Definition at line 572 of file lib_manager.cpp.

573 {
574  auto libBufIt = m_libs.find( aLibrary );
575  LIB_PART* alias = nullptr;
576 
577  if( libBufIt != m_libs.end() )
578  return !!libBufIt->second.GetBuffer( aAlias );
579 
580  try
581  {
582  alias = symTable()->LoadSymbol( aLibrary, aAlias );
583  }
584  catch( IO_ERROR& )
585  {
586  // checking if certain symbol exists, so its absence is perfectly fine
587  }
588 
589  return alias != nullptr;
590 }
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.
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76

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

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

◆ RemovePart()

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

Removes 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 527 of file lib_manager.cpp.

528 {
529  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
530  auto partBuf = libBuf.GetBuffer( aAlias );
531  wxCHECK( partBuf, false );
532 
533  bool retv = true;
534 
535  retv &= libBuf.DeleteBuffer( partBuf );
536  m_frame.SyncLibraries( false );
537 
538  return retv;
539 }
LIB_EDIT_FRAME & m_frame
Definition: lib_manager.h:467
void SyncLibraries(bool aShowProgress)
Synchronize the library manager to the symbol library table, and then the symbol tree to the library ...
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Returns an existing library buffer or creates one to using Symbol Library Table to get the original d...

References LIB_MANAGER::LIB_BUFFER::DeleteBuffer(), LIB_MANAGER::LIB_BUFFER::GetBuffer(), getLibraryBuffer(), m_frame, and LIB_EDIT_FRAME::SyncLibraries().

Referenced by LIB_EDIT_FRAME::DeletePartFromLibrary().

◆ RevertAll()

bool LIB_MANAGER::RevertAll ( )

Revert all pending changes.

Returns
True if all changes successfully reverted.

Definition at line 501 of file lib_manager.cpp.

502 {
503  bool retv = true;
504 
505  // Nothing to revert.
506  if( GetHash() == 0 )
507  return true;
508 
509  for( const auto& lib : m_libs )
510  {
511  if( !lib.second.IsModified() )
512  continue;
513 
514  for( const auto& buffer : lib.second.GetBuffers() )
515  {
516  if( !buffer->IsModified() )
517  continue;
518 
519  RevertPart( lib.first, buffer->GetOriginal()->GetName() );
520  }
521  }
522 
523  return retv;
524 }
LIB_ID RevertPart(const wxString &aAlias, const wxString &aLibrary)
Reverts unsaved changes for a particular part.
int GetHash() const
Definition: lib_manager.cpp:78
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465

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

Referenced by LIB_EDIT_FRAME::RevertAll().

◆ RevertLibrary()

bool LIB_MANAGER::RevertLibrary ( const wxString &  aLibrary)

Reverts unsaved changes for a particular library.

Returns
True on success, false otherwise.

Definition at line 487 of file lib_manager.cpp.

488 {
489  auto it = m_libs.find( aLibrary );
490 
491  if( it == m_libs.end() ) // nothing to reverse
492  return false;
493 
494  m_libs.erase( it );
495  m_frame.SyncLibraries( false );
496 
497  return true;
498 }
LIB_EDIT_FRAME & m_frame
Definition: lib_manager.h:467
void SyncLibraries(bool aShowProgress)
Synchronize the library manager to the symbol library table, and then the symbol tree to the library ...
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465

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

Referenced by LIB_EDIT_FRAME::Revert().

◆ RevertPart()

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

Reverts 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 462 of file lib_manager.cpp.

463 {
464  auto it = m_libs.find( aLibrary );
465 
466  if( it == m_libs.end() ) // no items to flush
467  return LIB_ID( aLibrary, aAlias );
468 
469  auto partBuf = it->second.GetBuffer( aAlias );
470  wxCHECK( partBuf, LIB_ID( aLibrary, aAlias ) );
471  LIB_PART original( *partBuf->GetOriginal() );
472 
473  if( original.GetName() != aAlias )
474  {
475  UpdatePartAfterRename( &original, aAlias, aLibrary );
476  }
477  else
478  {
479  partBuf->SetPart( new LIB_PART( original ) );
480  m_frame.SyncLibraries( false );
481  }
482 
483  return LIB_ID( aLibrary, original.GetName() );
484 }
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
LIB_EDIT_FRAME & m_frame
Definition: lib_manager.h:467
Define a library symbol object.
void SyncLibraries(bool aShowProgress)
Synchronize the library manager to the symbol library table, and then the symbol tree to the library ...
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465
bool UpdatePartAfterRename(LIB_PART *aPart, const wxString &oldAlias, const wxString &aLibrary)
Updates the part buffer with a new version of the part when the name has changed.

References LIB_PART::LIB_PART(), m_frame, m_libs, LIB_EDIT_FRAME::SyncLibraries(), and UpdatePartAfterRename().

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

◆ SaveLibrary()

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

Saves 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 133 of file lib_manager.cpp.

135 {
136  wxCHECK( aFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY && LibraryExists( aLibrary ), false );
137  wxFileName fn( aFileName );
138  wxCHECK( !fn.FileExists() || fn.IsFileWritable(), false );
139  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( aFileType ) );
140  bool res = true; // assume all libraries are successfully saved
141 
142  auto it = m_libs.find( aLibrary );
143 
144  if( it != m_libs.end() )
145  {
146  // Handle buffered library
147  LIB_BUFFER& libBuf = it->second;
148 
149  const auto& partBuffers = libBuf.GetBuffers();
150 
151  for( const auto& partBuf : partBuffers )
152  {
153  if( !libBuf.SaveBuffer( partBuf, &*pi, true ) )
154  {
155  // Something went wrong, but try to save other libraries
156  res = false;
157  }
158  }
159 
160  // clear the deleted parts buffer only if data is saved to the original file
161  wxFileName original, destination( aFileName );
162  auto row = GetLibrary( aLibrary );
163 
164  if( row )
165  {
166  original = row->GetFullURI( true );
167  original.Normalize();
168  }
169 
170  destination.Normalize();
171 
172  if( res && original == destination )
173  libBuf.ClearDeletedBuffer();
174  }
175  else
176  {
177  // Handle original library
178  PROPERTIES properties;
179  properties.emplace( SCH_LEGACY_PLUGIN::PropBuffering, "" );
180 
181  for( auto part : getOriginalParts( aLibrary ) )
182  {
183  LIB_PART* newSymbol;
184 
185  if( part->IsAlias() )
186  {
187  std::shared_ptr< LIB_PART > oldParent = part->GetParent().lock();
188 
189  wxCHECK_MSG( oldParent, false,
190  wxString::Format( "Derived symbol '%s' found with undefined parent.",
191  part->GetName() ) );
192 
193  LIB_PART* libParent = pi->LoadSymbol( aLibrary, oldParent->GetName(), &properties );
194 
195  if( !libParent )
196  {
197  libParent = new LIB_PART( *oldParent.get() );
198  pi->SaveSymbol( aLibrary, libParent, &properties );
199  }
200 
201  newSymbol = new LIB_PART( *part );
202  newSymbol->SetParent( libParent );
203  pi->SaveSymbol( aLibrary, newSymbol, &properties );
204  }
205  else if( !pi->LoadSymbol( aLibrary, part->GetName(), &properties ) )
206  {
207  pi->SaveSymbol( aLibrary, new LIB_PART( *part ), &properties );
208  }
209  }
210  }
211 
212  try
213  {
214  pi->SaveLibrary( aFileName );
215  }
216  catch( ... )
217  {
218  // return false because something happens.
219  // The library is not successfully saved
220  res = false;
221  }
222 
223  return res;
224 }
PROPERTIES is a name/value tuple with unique names and optional values.
Definition: properties.h:34
void SetParent(LIB_PART *aParent=nullptr)
PART_REF & GetParent()
Define a library symbol object.
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Finds a single library within the (aggregate) library table.
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:470
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
std::set< LIB_PART * > getOriginalParts(const wxString &aLibrary)
Returns a set of LIB_PART objects belonging to the original library
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:465

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

Referenced by LIB_EDIT_FRAME::saveLibrary().

◆ SetCurrentLib()

void LIB_MANAGER::SetCurrentLib ( const wxString &  aLibrary)
inline

Definition at line 282 of file lib_manager.h.

282 { m_currentLib = aLibrary; }
wxString m_currentLib
Definition: lib_manager.h:471

References m_currentLib.

Referenced by LIB_EDIT_FRAME::SetCurLib(), and LIB_EDIT_FRAME::SetCurPart().

◆ SetCurrentPart()

void LIB_MANAGER::SetCurrentPart ( const wxString &  aPart)
inline

Definition at line 288 of file lib_manager.h.

288 { m_currentPart = aPart; }
wxString m_currentPart
Definition: lib_manager.h:472

References m_currentPart.

Referenced by LIB_EDIT_FRAME::SetCurPart(), and UpdatePartAfterRename().

◆ symTable()

SYMBOL_LIB_TABLE * LIB_MANAGER::symTable ( ) const
private

Returns the current Symbol Library Table

Definition at line 688 of file lib_manager.cpp.

689 {
690  return m_frame.Prj().SchSymbolLibTable();
691 }
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
LIB_EDIT_FRAME & m_frame
Definition: lib_manager.h:467

References m_frame, and KIWAY_HOLDER::Prj().

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

◆ Sync()

void LIB_MANAGER::Sync ( bool  aForce = false,
std::function< void(int, int, const wxString &)>  aProgressCallback = [](int, int, const wxString&){} 
)

Updates the LIB_MANAGER data to synchronize with Symbol Library Table.

Definition at line 49 of file lib_manager.cpp.

51 {
53  {
54  int libTableHash = symTable()->GetModifyHash();
55 
56  if( aForce || m_syncHash != libTableHash )
57  {
58  getAdapter()->Sync( aForce, aProgressCallback );
59  m_syncHash = libTableHash;
60  }
61  }
63 }
LIB_LOGGER m_logger
Definition: lib_manager.h:468
void Activate()
Definition: lib_manager.h:60
void Deactivate()
Definition: lib_manager.h:70
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
void Sync(bool aForce=false, std::function< void(int, int, const wxString &)> aProgressCallback=[](int, int, const wxString &){})
SYMBOL_TREE_SYNCHRONIZING_ADAPTER * getAdapter()
Definition: lib_manager.h:475

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 LIB_EDIT_FRAME::SyncLibraries().

◆ UpdatePart()

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

Updates 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 401 of file lib_manager.cpp.

402 {
403  wxCHECK( LibraryExists( aLibrary ), false );
404  wxCHECK( aPart, false );
405  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
406  auto partBuf = libBuf.GetBuffer( aPart->GetName() );
407 
408  if( partBuf ) // Existing symbol.
409  {
410  LIB_PART* bufferedPart = const_cast< LIB_PART* >( partBuf->GetPart() );
411 
412  wxCHECK( bufferedPart, false );
413 
414  *bufferedPart = *aPart;
415  partBuf->GetScreen()->SetModify();
416  }
417  else // New symbol
418  {
419  LIB_PART* partCopy = new LIB_PART( *aPart, nullptr );
420 
421  partCopy->SetLibId( LIB_ID( aLibrary, aPart->GetLibId().GetLibItemName() ) );
422 
423  SCH_SCREEN* screen = new SCH_SCREEN;
424  libBuf.CreateBuffer( partCopy, screen );
425  screen->SetModify();
426  }
427 
428  return true;
429 }
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
LIB_ID GetLibId() const override
wxString GetName() const override
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
Define a library symbol object.
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Returns an existing library buffer or creates one to using Symbol Library Table to get the original d...
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
void SetModify()
Definition: base_screen.h:123
void SetLibId(const LIB_ID &aLibId)

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

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

◆ UpdatePartAfterRename()

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

Updates 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 432 of file lib_manager.cpp.

434 {
435  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
436  auto partBuf = libBuf.GetBuffer( aOldName );
437 
438  wxCHECK( partBuf, false );
439 
440  libBuf.UpdateBuffer( partBuf, aPart );
441  SetCurrentPart( aPart->GetName() );
442  m_frame.SyncLibraries( false );
443 
444  return true;
445 }
wxString GetName() const override
LIB_EDIT_FRAME & m_frame
Definition: lib_manager.h:467
void SetCurrentPart(const wxString &aPart)
Definition: lib_manager.h:288
void SyncLibraries(bool aShowProgress)
Synchronize the library manager to the symbol library table, and then the symbol tree to the library ...
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Returns an existing library buffer or creates one to using Symbol Library Table to get the original d...

References LIB_MANAGER::LIB_BUFFER::GetBuffer(), getLibraryBuffer(), LIB_PART::GetName(), m_frame, SetCurrentPart(), LIB_EDIT_FRAME::SyncLibraries(), and LIB_MANAGER::LIB_BUFFER::UpdateBuffer().

Referenced by LIB_EDIT_FRAME::GetComponentFromRedoList(), LIB_EDIT_FRAME::GetComponentFromUndoList(), RevertPart(), and LIB_EDIT_FRAME::UpdateAfterSymbolProperties().

Member Data Documentation

◆ m_adapter

SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR LIB_MANAGER::m_adapter
private

Definition at line 474 of file lib_manager.h.

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

◆ m_currentLib

wxString LIB_MANAGER::m_currentLib
private

Definition at line 471 of file lib_manager.h.

Referenced by GetCurrentLib(), GetCurrentLibId(), and SetCurrentLib().

◆ m_currentPart

wxString LIB_MANAGER::m_currentPart
private

Definition at line 472 of file lib_manager.h.

Referenced by GetCurrentLibId(), GetCurrentPart(), and SetCurrentPart().

◆ m_frame

LIB_EDIT_FRAME& LIB_MANAGER::m_frame
private

◆ m_libs

◆ m_logger

LIB_LOGGER LIB_MANAGER::m_logger
private

Definition at line 468 of file lib_manager.h.

Referenced by Sync().

◆ m_syncHash

int LIB_MANAGER::m_syncHash
private

Definition at line 469 of file lib_manager.h.

Referenced by Sync().


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