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
 
int GetLibraryHash (const wxString &aLibrary) const
 Retruns 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...
 
wxArrayString GetAliasNames (const wxString &aLibrary) const
 Returns a set containing all part names for a specific library. More...
 
std::list< LIB_ALIAS * > 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_ALIASGetAlias (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 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 FlushLibrary (const wxString &aLibrary)
 Saves changes to the library copy used by the schematic editor. More...
 
bool SaveLibrary (const wxString &aLibrary, const wxString &aFileName)
 Saves library to a file, including unsaved changes. More...
 
bool FlushAll ()
 Saves all changes to libraries. 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...
 
wxString GetUniqueLibraryName () const
 Returns a library name that is not currently in use. More...
 
wxString GetUniqueComponentName (const wxString &aLibrary) const
 Returns a component name that is not stored in a library. 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)
 Sets the currently modified library name. More...
 
const wxString & GetCurrentPart () const
 Returns the currently modified part name. More...
 
void SetCurrentPart (const wxString &aPart)
 Sets the currently modified part name. More...
 
LIB_ID GetCurrentLibId () const
 Returns the current library and part name as LIB_ID. 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

LIB_EDIT_FRAMEm_frame
 

Parent frame

More...
 
std::map< wxString, LIB_BUFFERm_libs
 

The library buffers

More...
 
int m_syncHash
 

Symbol Lib Table hash value returned during the last synchronization

More...
 
wxString m_currentLib
 

Currently modified part

More...
 
wxString m_currentPart
 

Currently modified library

More...
 
SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR m_adapter
 

Detailed Description

Class to handle modifications to the symbol libraries.

Definition at line 49 of file lib_manager.h.

Constructor & Destructor Documentation

◆ LIB_MANAGER()

LIB_MANAGER::LIB_MANAGER ( LIB_EDIT_FRAME aFrame)

Definition at line 39 of file lib_manager.cpp.

40  : m_frame( aFrame ), m_syncHash( 0 )
41 {
43  m_adapter->ShowUnits( false );
44 }
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR m_adapter
Definition: lib_manager.h:451
int m_syncHash
Symbol Lib Table hash value returned during the last synchronization
Definition: lib_manager.h:443

References SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Create(), and m_adapter.

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 99 of file lib_manager.h.

100  {
101  return addLibrary( aFilePath, false, aTable );
102  }
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::OnExportPart().

◆ 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 630 of file lib_manager.cpp.

631 {
632  wxCHECK( aTable, false );
633  wxString libName = getLibraryName( aFilePath );
634  wxCHECK( !LibraryExists( libName ), false ); // either create or add an existing one
635 
636  // try to use path normalized to an environmental variable or project path
637  wxString relPath = NormalizePath( aFilePath, &Pgm().GetLocalEnvVariables(), &m_frame.Prj() );
638 
639  if( relPath.IsEmpty() )
640  relPath = aFilePath;
641 
642  SYMBOL_LIB_TABLE_ROW* libRow = new SYMBOL_LIB_TABLE_ROW( libName, relPath,
643  SCH_IO_MGR::ShowType( SCH_IO_MGR::SCH_LEGACY ) );
644  aTable->InsertRow( libRow );
645 
646  if( aCreate )
647  {
648  // CreateSymbolLib() fails if the file exists
649  if( wxFileName::Exists( aFilePath ) )
650  {
651  if( !wxRemoveFile( aFilePath ) )
652  return false;
653  }
654 
655  aTable->CreateSymbolLib( libName );
656  }
657 
658  m_frame.SyncLibraries( false );
659 
660  return true;
661 }
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:76
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:66
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
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
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:67

References SYMBOL_LIB_TABLE::CreateSymbolLib(), getLibraryName(), LIB_TABLE::InsertRow(), LibraryExists(), m_frame, NormalizePath(), Pgm(), KIWAY_HOLDER::Prj(), 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 243 of file lib_manager.cpp.

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

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

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

References m_libs.

Referenced by LIB_EDIT_FRAME::OnRevert(), LIB_EDIT_FRAME::OnSave(), 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 91 of file lib_manager.h.

92  {
93  return addLibrary( aFilePath, true, aTable );
94  }
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().

◆ FlushAll()

bool LIB_MANAGER::FlushAll ( )

Saves all changes to libraries.

Returns
True if all changes have been flushed successfully, false otherwise.

Definition at line 113 of file lib_manager.cpp.

114 {
115  bool result = true;
116 
117  for( auto& libBuf : m_libs )
118  result &= FlushLibrary( libBuf.first );
119 
120  return result;
121 }
bool FlushLibrary(const wxString &aLibrary)
Saves changes to the library copy used by the schematic editor.
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:440

References FlushLibrary(), and m_libs.

◆ FlushLibrary()

bool LIB_MANAGER::FlushLibrary ( const wxString &  aLibrary)

Saves changes to the library copy used by the schematic editor.

Note it is not necessarily saved to the file.

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

Definition at line 124 of file lib_manager.cpp.

125 {
126  auto it = m_libs.find( aLibrary );
127 
128  if( it == m_libs.end() ) // no changes to flush
129  return true;
130 
131  LIB_BUFFER& libBuf = it->second;
132  wxArrayString aliases;
133 
134  try
135  {
136  symTable()->EnumerateSymbolLib( aLibrary, aliases );
137 
138  // TODO probably this could be implemented more efficiently
139  for( const auto& alias : aliases )
140  symTable()->DeleteAlias( aLibrary, alias );
141  }
142  catch( const IO_ERROR& e )
143  {
144  DisplayErrorMessage( &m_frame, wxString::Format( _( "Cannot flush "
145  "library changes (\"%s\")" ), aLibrary ), e.What() );
146  }
147 
148  // Assume all libraries are successfully saved
149  bool res = true;
150 
151  for( const auto& partBuf : libBuf.GetBuffers() )
152  {
153  if( !libBuf.SaveBuffer( partBuf, symTable() ) )
154  {
155  // Something went wrong but try to save other libraries
156  res = false;
157  }
158  }
159 
160  if( res )
161  libBuf.ClearDeletedBuffer();
162 
163  return res;
164 }
void EnumerateSymbolLib(const wxString &aNickname, wxArrayString &aAliasNames, bool aPowerSymbolsOnly=false)
Return a list of symbol alias names contained within the library given by aNickname.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:257
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
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:205
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:440
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
void DeleteAlias(const wxString &aNickname, const wxString &aAliasName)
Delete aAliasName from the library at aLibraryPath.

References LIB_MANAGER::LIB_BUFFER::ClearDeletedBuffer(), SYMBOL_LIB_TABLE::DeleteAlias(), DisplayErrorMessage(), SYMBOL_LIB_TABLE::EnumerateSymbolLib(), Format(), LIB_MANAGER::LIB_BUFFER::GetBuffers(), m_frame, m_libs, LIB_MANAGER::LIB_BUFFER::SaveBuffer(), symTable(), and IO_ERROR::What().

Referenced by FlushAll().

◆ 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 456 of file lib_manager.cpp.

457 {
458  auto it = m_libs.find( aLibrary );
459 
460  if( it == m_libs.end() ) // no items to flush
461  return true;
462 
463  auto partBuf = it->second.GetBuffer( aAlias );
464  wxCHECK( partBuf, false );
465 
466  return it->second.SaveBuffer( partBuf, symTable() );
467 }
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:440

References m_libs, and symTable().

Referenced by LIB_EDIT_FRAME::OnSave(), 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 235 of file lib_manager.h.

235 { return m_adapter; }
SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR m_adapter
Definition: lib_manager.h:451

References m_adapter.

Referenced by LIB_EDIT_FRAME::OnEditSymbolLibTable(), SYMBOL_TREE_PANE::SYMBOL_TREE_PANE(), and LIB_EDIT_FRAME::SyncLibraries().

◆ getAdapter()

SYMBOL_TREE_SYNCHRONIZING_ADAPTER* LIB_MANAGER::getAdapter ( )
inlineprivate

Definition at line 452 of file lib_manager.h.

452 { return static_cast<SYMBOL_TREE_SYNCHRONIZING_ADAPTER*>( m_adapter.get() ); }
SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR m_adapter
Definition: lib_manager.h:451

References m_adapter.

Referenced by Sync().

◆ GetAlias()

LIB_ALIAS * 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 522 of file lib_manager.cpp.

523 {
524  // Try the library buffers first
525  auto libIt = m_libs.find( aLibrary );
526 
527  if( libIt != m_libs.end() )
528  {
529  LIB_PART* part = libIt->second.GetPart( aAlias );
530 
531  if( part )
532  return part->GetAlias( aAlias );
533  }
534 
535  // Get the original part
536  LIB_ALIAS* alias = nullptr;
537 
538  try
539  {
540  alias = symTable()->LoadSymbol( aLibrary, aAlias );
541  }
542  catch( const IO_ERROR& e )
543  {
544  DisplayErrorMessage( &m_frame, wxString::Format( _( "Cannot load "
545  "symbol \"%s\" from library \"%s\"" ), aAlias, aLibrary ), e.What() );
546  }
547 
548  return alias;
549 }
LIB_ALIAS * LoadSymbol(const wxString &aNickname, const wxString &aAliasName)
Load a LIB_ALIAS having aAliasName from the library given by aNickname.
Part library alias object definition.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:257
LIB_ALIAS * GetAlias(size_t aIndex)
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
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:205
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:440
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 DisplayErrorMessage(), Format(), LIB_PART::GetAlias(), SYMBOL_LIB_TABLE::LoadSymbol(), m_frame, m_libs, symTable(), and IO_ERROR::What().

Referenced by LIB_EDIT_FRAME::getTargetPart().

◆ GetAliases()

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

Definition at line 313 of file lib_manager.cpp.

314 {
315  std::list<LIB_ALIAS*> ret;
316  wxCHECK( LibraryExists( aLibrary ), ret );
317 
318  auto libIt = m_libs.find( aLibrary );
319 
320  if( libIt != m_libs.end() )
321  {
322  for( auto& partBuf : libIt->second.GetBuffers() )
323  {
324  for( unsigned int i = 0; i < partBuf->GetPart()->GetAliasCount(); ++i )
325  ret.push_back( partBuf->GetPart()->GetAlias( i ) );
326  }
327  }
328  else
329  {
330  std::vector<LIB_ALIAS*> aliases;
331 
332  try
333  {
334  symTable()->LoadSymbolLib( aliases, aLibrary );
335  }
336  catch( const IO_ERROR& e )
337  {
338  DisplayErrorMessage( &m_frame, wxString::Format( _( "Cannot load "
339  "aliases from library \"%s\"" ), aLibrary ), e.What() );
340  }
341 
342  std::copy( aliases.begin(), aliases.end(), std::back_inserter( ret ) );
343  }
344 
345  return ret;
346 }
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:257
void LoadSymbolLib(std::vector< LIB_ALIAS * > &aAliasList, const wxString &aNickname, bool aPowerSymbolsOnly=false)
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
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:205
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
size_t i
Definition: json11.cpp:597
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:440
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 DisplayErrorMessage(), Format(), i, LibraryExists(), SYMBOL_LIB_TABLE::LoadSymbolLib(), m_frame, m_libs, symTable(), and IO_ERROR::What().

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::updateLibrary().

◆ GetAliasNames()

wxArrayString LIB_MANAGER::GetAliasNames ( const wxString &  aLibrary) const

Returns a set containing all part names for a specific library.

Definition at line 285 of file lib_manager.cpp.

286 {
287  wxArrayString names;
288  wxCHECK( LibraryExists( aLibrary ), names );
289 
290  auto it = m_libs.find( aLibrary );
291 
292  if( it == m_libs.end() )
293  {
294  try
295  {
296  symTable()->EnumerateSymbolLib( aLibrary, names );
297  }
298  catch( const IO_ERROR& e )
299  {
300  DisplayErrorMessage( &m_frame, wxString::Format( _( "Cannot enumerate "
301  "library \"%s\"" ), aLibrary ), e.What() );
302  }
303  }
304  else
305  {
306  names = it->second.GetAliasNames();
307  }
308 
309  return names;
310 }
void EnumerateSymbolLib(const wxString &aNickname, wxArrayString &aAliasNames, bool aPowerSymbolsOnly=false)
Return a list of symbol alias names contained within the library given by aNickname.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:257
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
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:205
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:440
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 DisplayErrorMessage(), SYMBOL_LIB_TABLE::EnumerateSymbolLib(), Format(), LibraryExists(), m_frame, m_libs, symTable(), and IO_ERROR::What().

◆ 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 349 of file lib_manager.cpp.

350 {
351  wxCHECK( LibraryExists( aLibrary ), nullptr );
352 
353  // try the library buffers first
354  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
355  LIB_PART* bufferedPart = libBuf.GetPart( aAlias );
356 
357  if( !bufferedPart ) // no buffer part found
358  {
359  // create a copy of the part
360  try
361  {
362  LIB_ALIAS* alias = symTable()->LoadSymbol( aLibrary, aAlias );
363 
364  if( alias == nullptr )
365  THROW_IO_ERROR( _( "Symbol not found." ) );
366 
367  bufferedPart = new LIB_PART( *alias->GetPart(), nullptr );
368  libBuf.CreateBuffer( bufferedPart, new SCH_SCREEN( &m_frame.Kiway() ) );
369  }
370  catch( const IO_ERROR& e )
371  {
372  wxString msg = wxString::Format( _( "Error loading symbol \"%s\" from library \"%s\"." ),
373  aAlias,
374  aLibrary);
375  DisplayErrorMessage( &m_frame, msg, e.What() );
376 
377  bufferedPart = nullptr;
378  }
379  }
380 
381  return bufferedPart;
382 }
LIB_ALIAS * LoadSymbol(const wxString &aNickname, const wxString &aAliasName)
Load a LIB_ALIAS having aAliasName from the library given by aNickname.
Part library alias object definition.
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_player.h:60
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:257
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
Define a library symbol object.
#define THROW_IO_ERROR(msg)
LIB_PART * GetPart() const
Get the shared LIB_PART.
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:205
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.
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 LIB_MANAGER::LIB_BUFFER::CreateBuffer(), DisplayErrorMessage(), Format(), getLibraryBuffer(), LIB_ALIAS::GetPart(), LIB_MANAGER::LIB_BUFFER::GetPart(), KIWAY_HOLDER::Kiway(), LibraryExists(), SYMBOL_LIB_TABLE::LoadSymbol(), m_frame, symTable(), THROW_IO_ERROR, and IO_ERROR::What().

Referenced by LIB_EDIT_FRAME::isCurrentPart(), LIB_EDIT_FRAME::LoadOneLibraryPartAux(), LIB_EDIT_FRAME::loadPart(), LIB_EDIT_FRAME::OnCopyCutPart(), LIB_EDIT_FRAME::OnPasteDuplicatePart(), and LIB_EDIT_FRAME::savePartAs().

◆ GetCurrentLib()

const wxString& LIB_MANAGER::GetCurrentLib ( ) const
inline

Returns the currently modified library name.

Definition at line 240 of file lib_manager.h.

241  {
242  return m_currentLib;
243  }
wxString m_currentLib
Currently modified part
Definition: lib_manager.h:446

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 272 of file lib_manager.h.

273  {
274  return LIB_ID( m_currentLib, m_currentPart );
275  }
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
wxString m_currentLib
Currently modified part
Definition: lib_manager.h:446
wxString m_currentPart
Currently modified library
Definition: lib_manager.h:449

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 256 of file lib_manager.h.

257  {
258  return m_currentPart;
259  }
wxString m_currentPart
Currently modified library
Definition: lib_manager.h:449

References m_currentPart.

◆ GetHash()

int LIB_MANAGER::GetHash ( ) const

Definition at line 59 of file lib_manager.cpp.

60 {
61  int hash = symTable()->GetModifyHash();
62 
63  for( const auto& libBuf : m_libs )
64  hash += libBuf.second.GetHash();
65 
66  return hash;
67 }
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:440

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

Referenced by 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 95 of file lib_manager.cpp.

96 {
97  SYMBOL_LIB_TABLE_ROW* row = nullptr;
98 
99  try
100  {
101  row = symTable()->FindRow( aLibrary );
102  }
103  catch( const IO_ERROR& e )
104  {
105  DisplayErrorMessage( &m_frame, wxString::Format( _( "Cannot find library \"%s\" in "
106  "the Symbol Library Table" ), aLibrary ), e.What() );
107  }
108 
109  return row;
110 }
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...
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:257
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
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:205
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 DisplayErrorMessage(), SYMBOL_LIB_TABLE::FindRow(), Format(), m_frame, 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 695 of file lib_manager.cpp.

696 {
697  auto it = m_libs.find( aLibrary );
698 
699  if( it != m_libs.end() )
700  return it->second;
701 
702  // The requested buffer does not exist yet, so create one
703  auto ret = m_libs.emplace( aLibrary, LIB_BUFFER( aLibrary ) );
704  LIB_BUFFER& buf = ret.first->second;
705 
706  for( auto part : getOriginalParts( aLibrary ) )
707  buf.CreateBuffer( new LIB_PART( *part, nullptr ), new SCH_SCREEN( &m_frame.Kiway() ) );
708 
709  return buf;
710 }
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_player.h:60
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
Define a library symbol object.
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:440

References LIB_MANAGER::LIB_BUFFER::CreateBuffer(), getOriginalParts(), KIWAY_HOLDER::Kiway(), m_frame, and m_libs.

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

◆ GetLibraryHash()

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

Retruns 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 70 of file lib_manager.cpp.

71 {
72  const auto libBufIt = m_libs.find( aLibrary );
73 
74  if( libBufIt != m_libs.end() )
75  return libBufIt->second.GetHash();
76 
77  auto row = GetLibrary( aLibrary );
78 
79  // return -1 if library does not exist or 0 if not modified
80  return row ? std::hash<std::string>{}( aLibrary.ToStdString() + row->GetFullURI( true ).ToStdString() ) : -1;
81 }
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Finds a single library within the (aggregate) library table.
Definition: lib_manager.cpp:95
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:440

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

624 {
625  wxFileName fn( aFilePath );
626  return fn.GetName();
627 }

Referenced by addLibrary().

◆ GetLibraryNames()

wxArrayString LIB_MANAGER::GetLibraryNames ( ) const

Returns the array of library names.

Definition at line 84 of file lib_manager.cpp.

85 {
86  wxArrayString res;
87 
88  for( const auto& libName : symTable()->GetLogicalLibs() )
89  res.Add( libName );
90 
91  return res;
92 }
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table

References symTable().

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetLibrariesCount(), 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 670 of file lib_manager.cpp.

671 {
672  std::set<LIB_PART*> parts;
673  wxCHECK( LibraryExists( aLibrary ), parts );
674 
675  try
676  {
677  wxArrayString aliases;
678  symTable()->EnumerateSymbolLib( aLibrary, aliases );
679 
680  for( const auto& aliasName : aliases )
681  {
682  LIB_ALIAS* alias = symTable()->LoadSymbol( aLibrary, aliasName );
683  parts.insert( alias->GetPart() );
684  }
685  } catch( const IO_ERROR& e )
686  {
687  DisplayErrorMessage( &m_frame, wxString::Format( _( "Cannot enumerate "
688  "library \"%s\"" ), aLibrary ), e.What() );
689  }
690 
691  return parts;
692 }
LIB_ALIAS * LoadSymbol(const wxString &aNickname, const wxString &aAliasName)
Load a LIB_ALIAS having aAliasName from the library given by aNickname.
void EnumerateSymbolLib(const wxString &aNickname, wxArrayString &aAliasNames, bool aPowerSymbolsOnly=false)
Return a list of symbol alias names contained within the library given by aNickname.
Part library alias object definition.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:257
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
LIB_PART * GetPart() const
Get the shared LIB_PART.
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:205
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 DisplayErrorMessage(), SYMBOL_LIB_TABLE::EnumerateSymbolLib(), Format(), LIB_ALIAS::GetPart(), LibraryExists(), SYMBOL_LIB_TABLE::LoadSymbol(), m_frame, symTable(), and IO_ERROR::What().

Referenced by getLibraryBuffer(), and SaveLibrary().

◆ 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 385 of file lib_manager.cpp.

386 {
387  wxCHECK( LibraryExists( aLibrary ), nullptr );
388  wxCHECK( !aAlias.IsEmpty(), nullptr );
389  auto it = m_libs.find( aLibrary );
390  wxCHECK( it != m_libs.end(), nullptr );
391 
392  LIB_BUFFER& buf = it->second;
393  auto partBuf = buf.GetBuffer( aAlias );
394  return partBuf ? partBuf->GetScreen() : nullptr;
395 }
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:440

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

Referenced by LIB_EDIT_FRAME::LoadOneLibraryPartAux().

◆ GetUniqueComponentName()

wxString LIB_MANAGER::GetUniqueComponentName ( const wxString &  aLibrary) const

Returns a component name that is not stored in a library.

Used for generating names for new components.

Definition at line 603 of file lib_manager.cpp.

604 {
605  wxString name = "New_Component";
606 
607  if( !PartExists( name, aLibrary ) )
608  return name;
609 
610  name += "_";
611 
612  for( unsigned int i = 0; i < std::numeric_limits<unsigned int>::max(); ++i )
613  {
614  if( !PartExists( name + wxString::Format( "%u", i ), aLibrary ) )
615  return name + wxString::Format( "%u", i );
616  }
617 
618  wxFAIL;
619  return wxEmptyString;
620 }
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).
const char * name
Definition: DXF_plotter.cpp:61
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:205
#define max(a, b)
Definition: auxiliary.h:86
size_t i
Definition: json11.cpp:597

References Format(), i, max, name, and PartExists().

◆ 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 583 of file lib_manager.cpp.

584 {
585  wxString name = "New_Library";
586 
587  if( !LibraryExists( name ) )
588  return name;
589 
590  name += "_";
591 
592  for( unsigned int i = 0; i < std::numeric_limits<unsigned int>::max(); ++i )
593  {
594  if( !LibraryExists( name + wxString::Format( "%u", i ) ) )
595  return name + wxString::Format( "%u", i );
596  }
597 
598  wxFAIL;
599  return wxEmptyString;
600 }
const char * name
Definition: DXF_plotter.cpp:61
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:205
#define max(a, b)
Definition: auxiliary.h:86
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
size_t i
Definition: json11.cpp:597

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

Referenced by LIB_EDIT_FRAME::addLibraryFile().

◆ IsLibraryModified()

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

Returns true if library has unsaved modifications.

Definition at line 223 of file lib_manager.cpp.

224 {
225  auto it = m_libs.find( aLibrary );
226  return it != m_libs.end() ? it->second.IsModified() : false;
227 }
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:440

References m_libs.

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr(), SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue(), LIB_EDIT_FRAME::OnUpdateRevert(), LIB_EDIT_FRAME::OnUpdateSave(), and LIB_EDIT_FRAME::saveAllLibraries().

◆ 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 277 of file lib_manager.cpp.

278 {
279  wxCHECK( LibraryExists( aLibrary ), true );
280  wxFileName fn( symTable()->GetFullURI( aLibrary ) );
281  return ( fn.FileExists() && !fn.IsFileWritable() ) || !fn.IsDirWritable();
282 }
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 LibraryExists(), and symTable().

Referenced by LIB_EDIT_FRAME::OnUpdateSave().

◆ IsPartModified()

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

Returns true if part has unsaved modifications.

Definition at line 230 of file lib_manager.cpp.

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

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

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr(), SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue(), LIB_EDIT_FRAME::OnRemovePart(), LIB_EDIT_FRAME::OnUpdateRevert(), and LIB_EDIT_FRAME::OnUpdateSave().

◆ 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 572 of file lib_manager.cpp.

573 {
574  if( aLibrary.IsEmpty() )
575  return false;
576 
577  if( m_libs.count( aLibrary ) > 0 )
578  return true;
579 
580  return symTable()->HasLibrary( aLibrary, aCheckEnabled );
581 }
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:440

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

Referenced by addLibrary(), LIB_EDIT_FRAME::addLibraryFile(), GetAliases(), GetAliasNames(), GetBufferedPart(), getOriginalParts(), GetScreen(), GetUniqueLibraryName(), IsLibraryReadOnly(), LIB_EDIT_FRAME::OnCreateNewPart(), LIB_EDIT_FRAME::OnImportPart(), LIB_EDIT_FRAME::OnPasteDuplicatePart(), 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 552 of file lib_manager.cpp.

553 {
554  auto libBufIt = m_libs.find( aLibrary );
555  LIB_ALIAS* alias = nullptr;
556 
557  if( libBufIt != m_libs.end() )
558  return !!libBufIt->second.GetBuffer( aAlias );
559 
560  try
561  {
562  alias = symTable()->LoadSymbol( aLibrary, aAlias );
563  } catch( IO_ERROR& )
564  {
565  // checking if certain symbol exists, so its absence is perfectly fine
566  }
567 
568  return alias != nullptr;
569 }
LIB_ALIAS * LoadSymbol(const wxString &aNickname, const wxString &aAliasName)
Load a LIB_ALIAS having aAliasName from the library given by aNickname.
Part library alias object definition.
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:440
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::EditField(), LIB_EDIT_FRAME::fixDuplicateAliases(), GetUniqueComponentName(), LIB_EDIT_FRAME::loadPart(), LIB_EDIT_FRAME::OnCreateNewPart(), LIB_EDIT_FRAME::OnImportPart(), LIB_EDIT_FRAME::OnRevert(), and LIB_EDIT_FRAME::savePartAs().

◆ 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 509 of file lib_manager.cpp.

510 {
511  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
512  auto partBuf = libBuf.GetBuffer( aAlias );
513  wxCHECK( partBuf, false );
514 
515  bool res = libBuf.DeleteBuffer( partBuf );
516  m_frame.SyncLibraries( false );
517 
518  return res;
519 }
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
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::OnRemovePart().

◆ RevertLibrary()

bool LIB_MANAGER::RevertLibrary ( const wxString &  aLibrary)

Reverts unsaved changes for a particular library.

Returns
True on success, false otherwise.

Definition at line 495 of file lib_manager.cpp.

496 {
497  auto it = m_libs.find( aLibrary );
498 
499  if( it == m_libs.end() ) // nothing to reverse
500  return false;
501 
502  m_libs.erase( it );
503  m_frame.SyncLibraries( false );
504 
505  return true;
506 }
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
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:440

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

Referenced by LIB_EDIT_FRAME::OnRevert().

◆ 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 470 of file lib_manager.cpp.

471 {
472  auto it = m_libs.find( aLibrary );
473 
474  if( it == m_libs.end() ) // no items to flush
475  return LIB_ID( aLibrary, aAlias );
476 
477  auto partBuf = it->second.GetBuffer( aAlias );
478  wxCHECK( partBuf, LIB_ID( aLibrary, aAlias ) );
479  LIB_PART original( *partBuf->GetOriginal() );
480 
481  if( original.GetName() != aAlias )
482  {
483  UpdatePartAfterRename( &original, aAlias, aLibrary );
484  }
485  else
486  {
487  partBuf->SetPart( new LIB_PART( original ) );
488  m_frame.SyncLibraries( false );
489  }
490 
491  return LIB_ID( aLibrary, original.GetName() );
492 }
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
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:440
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 m_frame, m_libs, LIB_EDIT_FRAME::SyncLibraries(), and UpdatePartAfterRename().

Referenced by LIB_EDIT_FRAME::OnRevert().

◆ SaveLibrary()

bool LIB_MANAGER::SaveLibrary ( const wxString &  aLibrary,
const wxString &  aFileName 
)

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

168 {
169  wxCHECK( LibraryExists( aLibrary ), false );
170  wxFileName fn( aFileName );
171  wxCHECK( !fn.FileExists() || fn.IsFileWritable(), false );
172  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
173  bool res = true; // assume all libraries are successfully saved
174 
175  auto it = m_libs.find( aLibrary );
176 
177  if( it != m_libs.end() )
178  {
179  // Handle buffered library
180  LIB_BUFFER& libBuf = it->second;
181 
182  const auto& partBuffers = libBuf.GetBuffers();
183 
184  for( const auto& partBuf : partBuffers )
185  {
186  if( !libBuf.SaveBuffer( partBuf, &*pi, true ) )
187  {
188  // Something went wrong, but try to save other libraries
189  res = false;
190  }
191  }
192 
193  // clear the deleted parts buffer only if data is saved to the original file
194  wxFileName original, destination( aFileName );
195  auto row = GetLibrary( aLibrary );
196 
197  if( row )
198  {
199  original = row->GetFullURI( true );
200  original.Normalize();
201  }
202 
203  destination.Normalize();
204 
205  if( res && original == destination )
206  libBuf.ClearDeletedBuffer();
207  }
208  else
209  {
210  // Handle original library
211  PROPERTIES properties;
212  properties.emplace( SCH_LEGACY_PLUGIN::PropBuffering, "" );
213 
214  for( auto part : getOriginalParts( aLibrary ) )
215  pi->SaveSymbol( aLibrary, new LIB_PART( *part ), &properties );
216  }
217 
218  pi->SaveLibrary( aFileName );
219  return res;
220 }
Class PROPERTIES is a name/value tuple with unique names and optional values.
Definition: properties.h:34
Define a library symbol object.
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Finds a single library within the (aggregate) library table.
Definition: lib_manager.cpp:95
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:523
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
const char* PropBuffering
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:440

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

Referenced by LIB_EDIT_FRAME::saveLibrary().

◆ SetCurrentLib()

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

Sets the currently modified library name.

Definition at line 248 of file lib_manager.h.

249  {
250  m_currentLib = aLibrary;
251  }
wxString m_currentLib
Currently modified part
Definition: lib_manager.h:446

References m_currentLib.

Referenced by LIB_EDIT_FRAME::SetCurLib().

◆ SetCurrentPart()

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

Sets the currently modified part name.

Definition at line 264 of file lib_manager.h.

265  {
266  m_currentPart = aPart;
267  }
wxString m_currentPart
Currently modified library
Definition: lib_manager.h:449

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

665 {
666  return m_frame.Prj().SchSymbolLibTable();
667 }
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279

References m_frame, and KIWAY_HOLDER::Prj().

Referenced by FlushLibrary(), FlushPart(), GetAlias(), GetAliases(), GetAliasNames(), GetBufferedPart(), GetHash(), GetLibrary(), GetLibraryNames(), getOriginalParts(), 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 47 of file lib_manager.cpp.

48 {
49  int libTableHash = symTable()->GetModifyHash();
50 
51  if( aForce || m_syncHash != libTableHash )
52  {
53  getAdapter()->Sync( aForce, aProgressCallback );
54  m_syncHash = libTableHash;
55  }
56 }
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
int m_syncHash
Symbol Lib Table hash value returned during the last synchronization
Definition: lib_manager.h:443
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:452

References getAdapter(), SYMBOL_LIB_TABLE::GetModifyHash(), 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 398 of file lib_manager.cpp.

399 {
400  wxCHECK( LibraryExists( aLibrary ), false );
401  wxCHECK( aPart, false );
402  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
403  auto partBuf = libBuf.GetBuffer( aPart->GetName() );
404  LIB_PART* partCopy = new LIB_PART( *aPart, nullptr );
405 
406  partCopy->SetLibId( LIB_ID( aLibrary, aPart->GetLibId().GetLibItemName() ) );
407 
408  if( partBuf )
409  {
410  libBuf.UpdateBuffer( partBuf, partCopy );
411  }
412  else // New entry
413  {
414  SCH_SCREEN* screen = new SCH_SCREEN( &m_frame.Kiway() );
415  libBuf.CreateBuffer( partCopy, screen );
416  screen->SetModify();
417  }
418 
419  return true;
420 }
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_player.h:60
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
const LIB_ID & GetLibId() const
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...
const wxString & GetName() const
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
void SetModify()
Definition: base_screen.h:324
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(), KIWAY_HOLDER::Kiway(), LibraryExists(), m_frame, LIB_PART::SetLibId(), BASE_SCREEN::SetModify(), and LIB_MANAGER::LIB_BUFFER::UpdateBuffer().

Referenced by LIB_EDIT_FRAME::OnCreateNewPart(), LIB_EDIT_FRAME::OnEditComponentProperties(), LIB_EDIT_FRAME::OnImportPart(), LIB_EDIT_FRAME::OnPasteDuplicatePart(), LIB_EDIT_FRAME::savePartAs(), LIB_EDIT_FRAME::storeCurrentPart(), and UpdatePartAfterRename().

◆ 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 423 of file lib_manager.cpp.

425 {
426  // This is essentially a delete/update.
427 
428  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
429  auto partBuf = libBuf.GetBuffer( oldAlias );
430  wxCHECK( partBuf, false );
431 
432  // Save the original record so it is transferred to the new buffer
433  std::unique_ptr<LIB_PART> original( new LIB_PART( *partBuf->GetOriginal() ) );
434 
435  // Save the screen object, so it is transferred to the new buffer
436  std::unique_ptr<SCH_SCREEN> screen = partBuf->RemoveScreen();
437 
438  if( !libBuf.DeleteBuffer( partBuf ) )
439  return false;
440 
441  if( !UpdatePart( aPart, aLibrary ) )
442  return false;
443 
444  partBuf = libBuf.GetBuffer( aPart->GetName() );
445  partBuf->SetScreen( std::move( screen ) );
446  wxCHECK( partBuf, false );
447  partBuf->SetOriginal( original.release() ); // part buffer takes ownership of pointer
448 
449  SetCurrentPart( aPart->GetName() );
450  m_frame.SyncLibraries( false );
451 
452  return true;
453 }
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
Define a library symbol object.
void SetCurrentPart(const wxString &aPart)
Sets the currently modified part name.
Definition: lib_manager.h:264
bool UpdatePart(LIB_PART *aPart, const wxString &aLibrary)
Updates the part buffer with a new version of the part.
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...
const wxString & GetName() const

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

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

Member Data Documentation

◆ m_adapter

SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR LIB_MANAGER::m_adapter
private

Definition at line 451 of file lib_manager.h.

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

◆ m_currentLib

wxString LIB_MANAGER::m_currentLib
private

Currently modified part

Definition at line 446 of file lib_manager.h.

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

◆ m_currentPart

wxString LIB_MANAGER::m_currentPart
private

Currently modified library

Definition at line 449 of file lib_manager.h.

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

◆ m_frame

◆ m_libs

◆ m_syncHash

int LIB_MANAGER::m_syncHash
private

Symbol Lib Table hash value returned during the last synchronization

Definition at line 443 of file lib_manager.h.

Referenced by Sync().


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