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_EDIT_FRAME aFrame)

Definition at line 39 of file lib_manager.cpp.

References SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Create(), and m_adapter.

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

Member Function Documentation

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.

References addLibrary(), ClearLibraryModified(), ClearPartModified(), FlushAll(), FlushLibrary(), FlushPart(), GetAlias(), GetBufferedPart(), GetScreen(), GetUniqueComponentName(), GetUniqueLibraryName(), IsLibraryModified(), IsLibraryReadOnly(), IsPartModified(), LibraryExists(), PartExists(), RemovePart(), RevertLibrary(), RevertPart(), SaveLibrary(), UpdatePart(), and UpdatePartAfterRename().

Referenced by LIB_EDIT_FRAME::addLibraryFile(), and LIB_EDIT_FRAME::OnExportPart().

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

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

621 {
622  wxCHECK( aTable, false );
623  wxString libName = getLibraryName( aFilePath );
624  wxCHECK( !LibraryExists( libName ), false ); // either create or add an existing one
625 
626  // try to use path normalized to an environmental variable or project path
627  wxString relPath = NormalizePath( aFilePath, &Pgm().GetLocalEnvVariables(), &m_frame.Prj() );
628 
629  if( relPath.IsEmpty() )
630  relPath = aFilePath;
631 
632  SYMBOL_LIB_TABLE_ROW* libRow = new SYMBOL_LIB_TABLE_ROW( libName, relPath,
633  SCH_IO_MGR::ShowType( SCH_IO_MGR::SCH_LEGACY ) );
634  aTable->InsertRow( libRow );
635 
636  if( aCreate )
637  {
638  // CreateSymbolLib() fails if the file exists
639  if( wxFileName::Exists( aFilePath ) )
640  {
641  if( !wxRemoveFile( aFilePath ) )
642  return false;
643  }
644 
645  aTable->CreateSymbolLib( libName );
646  }
647 
648  m_frame.SyncLibraries( false );
649 
650  return true;
651 }
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
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.

References BASE_SCREEN::ClrModify(), and m_libs.

Referenced by AddLibrary(), and LIB_EDIT_FRAME::saveLibrary().

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

References m_libs.

Referenced by AddLibrary(), LIB_EDIT_FRAME::OnRevert(), LIB_EDIT_FRAME::OnSave(), and LIB_EDIT_FRAME::saveCurrentPart().

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

References addLibrary().

Referenced by LIB_EDIT_FRAME::addLibraryFile().

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

References FlushLibrary(), and m_libs.

Referenced by AddLibrary().

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

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 AddLibrary(), and FlushAll().

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:259
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
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
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.
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 446 of file lib_manager.cpp.

References m_libs, and symTable().

Referenced by AddLibrary(), LIB_EDIT_FRAME::OnSave(), and LIB_EDIT_FRAME::saveCurrentPart().

447 {
448  auto it = m_libs.find( aLibrary );
449 
450  if( it == m_libs.end() ) // no items to flush
451  return true;
452 
453  auto partBuf = it->second.GetBuffer( aAlias );
454  wxCHECK( partBuf, false );
455 
456  return it->second.SaveBuffer( partBuf, symTable() );
457 }
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
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.

References m_adapter.

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

235 { return m_adapter; }
SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR m_adapter
Definition: lib_manager.h:451
SYMBOL_TREE_SYNCHRONIZING_ADAPTER* LIB_MANAGER::getAdapter ( )
inlineprivate

Definition at line 452 of file lib_manager.h.

Referenced by Sync().

452 { return static_cast<SYMBOL_TREE_SYNCHRONIZING_ADAPTER*>( m_adapter.get() ); }
SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR m_adapter
Definition: lib_manager.h:451
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 512 of file lib_manager.cpp.

References DisplayErrorMessage(), Format(), LIB_PART::GetAlias(), SYMBOL_LIB_TABLE::LoadSymbol(), m_frame, m_libs, symTable(), and IO_ERROR::What().

Referenced by AddLibrary(), and LIB_EDIT_FRAME::getTargetPart().

513 {
514  // Try the library buffers first
515  auto libIt = m_libs.find( aLibrary );
516 
517  if( libIt != m_libs.end() )
518  {
519  LIB_PART* part = libIt->second.GetPart( aAlias );
520 
521  if( part )
522  return part->GetAlias( aAlias );
523  }
524 
525  // Get the original part
526  LIB_ALIAS* alias = nullptr;
527 
528  try
529  {
530  alias = symTable()->LoadSymbol( aLibrary, aAlias );
531  }
532  catch( const IO_ERROR& e )
533  {
534  DisplayErrorMessage( &m_frame, wxString::Format( _( "Cannot load "
535  "symbol \"%s\" from library \"%s\"" ), aAlias, aLibrary ), e.What() );
536  }
537 
538  return alias;
539 }
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:259
LIB_ALIAS * GetAlias(size_t aIndex)
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
Define a library symbol object.
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
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
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
std::list< LIB_ALIAS * > LIB_MANAGER::GetAliases ( const wxString &  aLibrary) const

Definition at line 313 of file lib_manager.cpp.

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

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:259
void LoadSymbolLib(std::vector< LIB_ALIAS * > &aAliasList, const wxString &aNickname, bool aPowerSymbolsOnly=false)
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
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
size_t i
Definition: json11.cpp:597
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
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
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.

References DisplayErrorMessage(), SYMBOL_LIB_TABLE::EnumerateSymbolLib(), Format(), LibraryExists(), m_frame, m_libs, symTable(), and IO_ERROR::What().

Referenced by LIB_MANAGER::LIB_BUFFER::GetHash().

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:259
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
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.
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
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.

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(), and IO_ERROR::What().

Referenced by AddLibrary(), 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().

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  wxCHECK( alias, nullptr );
364  bufferedPart = new LIB_PART( *alias->GetPart(), nullptr );
365  libBuf.CreateBuffer( bufferedPart, new SCH_SCREEN( &m_frame.Kiway() ) );
366  }
367  catch( const IO_ERROR& e )
368  {
369  DisplayErrorMessage( &m_frame, wxString::Format( _( "Cannot load "
370  "symbol \"%s\" from library \"%s\"" ), aAlias, aLibrary ), e.What() );
371  bufferedPart = nullptr;
372  }
373  }
374 
375  return bufferedPart;
376 }
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:259
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
Define a library symbol object.
LIB_PART * GetPart() const
Get the shared LIB_PART.
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
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.
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
const wxString& LIB_MANAGER::GetCurrentLib ( ) const
inline

Returns the currently modified library name.

Definition at line 240 of file lib_manager.h.

References m_currentLib.

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr().

241  {
242  return m_currentLib;
243  }
wxString m_currentLib
Currently modified part
Definition: lib_manager.h:446
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.

References m_currentLib, and m_currentPart.

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr().

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
const wxString& LIB_MANAGER::GetCurrentPart ( ) const
inline

Returns the currently modified part name.

Definition at line 256 of file lib_manager.h.

References m_currentPart.

257  {
258  return m_currentPart;
259  }
wxString m_currentPart
Currently modified library
Definition: lib_manager.h:449
int LIB_MANAGER::GetHash ( ) const

Definition at line 59 of file lib_manager.cpp.

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

Referenced by SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync().

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

References DisplayErrorMessage(), SYMBOL_LIB_TABLE::FindRow(), Format(), m_frame, symTable(), and IO_ERROR::What().

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

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:259
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
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
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
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 685 of file lib_manager.cpp.

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

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

686 {
687  auto it = m_libs.find( aLibrary );
688 
689  if( it != m_libs.end() )
690  return it->second;
691 
692  // The requested buffer does not exist yet, so create one
693  auto ret = m_libs.emplace( aLibrary, LIB_BUFFER( aLibrary ) );
694  LIB_BUFFER& buf = ret.first->second;
695 
696  for( auto part : getOriginalParts( aLibrary ) )
697  buf.CreateBuffer( new LIB_PART( *part, nullptr ), new SCH_SCREEN( &m_frame.Kiway() ) );
698 
699  return buf;
700 }
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
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.

References GetLibrary(), and m_libs.

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

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
wxString LIB_MANAGER::getLibraryName ( const wxString &  aFilePath)
staticprivate

Extracts library name basing on the file name

Definition at line 613 of file lib_manager.cpp.

Referenced by addLibrary().

614 {
615  wxFileName fn( aFilePath );
616  return fn.GetName();
617 }
wxArrayString LIB_MANAGER::GetLibraryNames ( ) const

Returns the array of library names.

Definition at line 84 of file lib_manager.cpp.

References symTable().

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

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

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

661 {
662  std::set<LIB_PART*> parts;
663  wxCHECK( LibraryExists( aLibrary ), parts );
664 
665  try
666  {
667  wxArrayString aliases;
668  symTable()->EnumerateSymbolLib( aLibrary, aliases );
669 
670  for( const auto& aliasName : aliases )
671  {
672  LIB_ALIAS* alias = symTable()->LoadSymbol( aLibrary, aliasName );
673  parts.insert( alias->GetPart() );
674  }
675  } catch( const IO_ERROR& e )
676  {
677  DisplayErrorMessage( &m_frame, wxString::Format( _( "Cannot enumerate "
678  "library \"%s\"" ), aLibrary ), e.What() );
679  }
680 
681  return parts;
682 }
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:259
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
LIB_PART * GetPart() const
Get the shared LIB_PART.
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
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.
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
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 379 of file lib_manager.cpp.

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

Referenced by AddLibrary(), and LIB_EDIT_FRAME::LoadOneLibraryPartAux().

380 {
381  wxCHECK( LibraryExists( aLibrary ), nullptr );
382  wxCHECK( !aAlias.IsEmpty(), nullptr );
383  auto it = m_libs.find( aLibrary );
384  wxCHECK( it != m_libs.end(), nullptr );
385 
386  LIB_BUFFER& buf = it->second;
387  auto partBuf = buf.GetBuffer( aAlias );
388  return partBuf ? partBuf->GetScreen() : nullptr;
389 }
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
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 593 of file lib_manager.cpp.

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

Referenced by AddLibrary().

594 {
595  wxString name = "New_Component";
596 
597  if( !PartExists( name, aLibrary ) )
598  return name;
599 
600  name += "_";
601 
602  for( unsigned int i = 0; i < std::numeric_limits<unsigned int>::max(); ++i )
603  {
604  if( !PartExists( name + wxString::Format( "%u", i ), aLibrary ) )
605  return name + wxString::Format( "%u", i );
606  }
607 
608  wxFAIL;
609  return wxEmptyString;
610 }
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
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 573 of file lib_manager.cpp.

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

Referenced by AddLibrary(), and LIB_EDIT_FRAME::addLibraryFile().

574 {
575  wxString name = "New_Library";
576 
577  if( !LibraryExists( name ) )
578  return name;
579 
580  name += "_";
581 
582  for( unsigned int i = 0; i < std::numeric_limits<unsigned int>::max(); ++i )
583  {
584  if( !LibraryExists( name + wxString::Format( "%u", i ) ) )
585  return name + wxString::Format( "%u", i );
586  }
587 
588  wxFAIL;
589  return wxEmptyString;
590 }
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
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
bool LIB_MANAGER::IsLibraryModified ( const wxString &  aLibrary) const

Returns true if library has unsaved modifications.

Definition at line 223 of file lib_manager.cpp.

References m_libs.

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

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

References LibraryExists(), and symTable().

Referenced by AddLibrary(), and LIB_EDIT_FRAME::OnUpdateSave().

278 {
279  wxCHECK( LibraryExists( aLibrary ), true );
280  wxFileName fn( symTable()->GetFullURI( aLibrary ) );
281  return ( fn.FileExists() && !fn.IsFileWritable() ) || !fn.IsDirWritable();
282 }
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
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.

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

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

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

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

Referenced by AddLibrary(), 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().

563 {
564  if( aLibrary.IsEmpty() )
565  return false;
566 
567  if( m_libs.count( aLibrary ) > 0 )
568  return true;
569 
570  return symTable()->HasLibrary( aLibrary, aCheckEnabled );
571 }
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
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 542 of file lib_manager.cpp.

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

Referenced by AddLibrary(), 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().

543 {
544  auto libBufIt = m_libs.find( aLibrary );
545  LIB_ALIAS* alias = nullptr;
546 
547  if( libBufIt != m_libs.end() )
548  return !!libBufIt->second.GetBuffer( aAlias );
549 
550  try
551  {
552  alias = symTable()->LoadSymbol( aLibrary, aAlias );
553  } catch( IO_ERROR& )
554  {
555  // checking if certain symbol exists, so its absence is perfectly fine
556  }
557 
558  return alias != nullptr;
559 }
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
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 499 of file lib_manager.cpp.

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

Referenced by AddLibrary(), and LIB_EDIT_FRAME::OnRemovePart().

500 {
501  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
502  auto partBuf = libBuf.GetBuffer( aAlias );
503  wxCHECK( partBuf, false );
504 
505  bool res = libBuf.DeleteBuffer( partBuf );
506  m_frame.SyncLibraries( false );
507 
508  return res;
509 }
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...
bool LIB_MANAGER::RevertLibrary ( const wxString &  aLibrary)

Reverts unsaved changes for a particular library.

Returns
True on success, false otherwise.

Definition at line 485 of file lib_manager.cpp.

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

Referenced by AddLibrary(), and LIB_EDIT_FRAME::OnRevert().

486 {
487  auto it = m_libs.find( aLibrary );
488 
489  if( it == m_libs.end() ) // nothing to reverse
490  return false;
491 
492  m_libs.erase( it );
493  m_frame.SyncLibraries( false );
494 
495  return true;
496 }
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
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 460 of file lib_manager.cpp.

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

Referenced by AddLibrary(), and LIB_EDIT_FRAME::OnRevert().

461 {
462  auto it = m_libs.find( aLibrary );
463 
464  if( it == m_libs.end() ) // no items to flush
465  return LIB_ID( aLibrary, aAlias );
466 
467  auto partBuf = it->second.GetBuffer( aAlias );
468  wxCHECK( partBuf, LIB_ID( aLibrary, aAlias ) );
469  LIB_PART original( *partBuf->GetOriginal() );
470 
471  if( original.GetName() != aAlias )
472  {
473  UpdatePartAfterRename( &original, aAlias, aLibrary );
474  }
475  else
476  {
477  partBuf->SetPart( new LIB_PART( original ) );
478  m_frame.SyncLibraries( false );
479  }
480 
481  return LIB_ID( aLibrary, original.GetName() );
482 }
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. ...
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.

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 AddLibrary(), and LIB_EDIT_FRAME::saveLibrary().

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
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Finds a single library within the (aggregate) library table.
Definition: lib_manager.cpp:95
Define a library symbol object.
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
void LIB_MANAGER::SetCurrentLib ( const wxString &  aLibrary)
inline

Sets the currently modified library name.

Definition at line 248 of file lib_manager.h.

References m_currentLib.

Referenced by LIB_EDIT_FRAME::SetCurLib().

249  {
250  m_currentLib = aLibrary;
251  }
wxString m_currentLib
Currently modified part
Definition: lib_manager.h:446
void LIB_MANAGER::SetCurrentPart ( const wxString &  aPart)
inline

Sets the currently modified part name.

Definition at line 264 of file lib_manager.h.

References m_currentPart.

Referenced by LIB_EDIT_FRAME::SetCurPart().

265  {
266  m_currentPart = aPart;
267  }
wxString m_currentPart
Currently modified library
Definition: lib_manager.h:449
SYMBOL_LIB_TABLE * LIB_MANAGER::symTable ( ) const
private

Returns the current Symbol Library Table

Definition at line 654 of file lib_manager.cpp.

References m_frame, and KIWAY_HOLDER::Prj().

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

655 {
656  return m_frame.Prj().SchSymbolLibTable();
657 }
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 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.

References getAdapter(), SYMBOL_LIB_TABLE::GetModifyHash(), m_syncHash, symTable(), and SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync().

Referenced by LIB_EDIT_FRAME::SyncLibraries().

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

References LIB_MANAGER::LIB_BUFFER::CreateBuffer(), LIB_MANAGER::LIB_BUFFER::GetBuffer(), getLibraryBuffer(), LIB_PART::GetName(), KIWAY_HOLDER::Kiway(), LibraryExists(), m_frame, BASE_SCREEN::SetModify(), and LIB_MANAGER::LIB_BUFFER::UpdateBuffer().

Referenced by AddLibrary(), 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().

393 {
394  wxCHECK( LibraryExists( aLibrary ), false );
395  wxCHECK( aPart, false );
396  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
397  auto partBuf = libBuf.GetBuffer( aPart->GetName() );
398  LIB_PART* partCopy = new LIB_PART( *aPart, nullptr );
399 
400  if( partBuf )
401  {
402  libBuf.UpdateBuffer( partBuf, partCopy );
403  }
404  else // New entry
405  {
406  SCH_SCREEN* screen = new SCH_SCREEN( &m_frame.Kiway() );
407  libBuf.CreateBuffer( partCopy, screen );
408  screen->SetModify();
409  }
410 
411  return true;
412 }
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.
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.
const wxString & GetName() const
void SetModify()
Definition: base_screen.h:324
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 415 of file lib_manager.cpp.

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

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

417 {
418  // This is essentially a delete/update, but we have to make a copy of the "original"
419  // LIB_PART from the old buffer to give to the new one.
420 
421  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
422  auto partBuf = libBuf.GetBuffer( oldAlias );
423  wxCHECK( partBuf, false );
424 
425  std::unique_ptr<LIB_PART> original( new LIB_PART( *partBuf->GetOriginal() ) );
426  // Save the screen object, so it is transferred to the new buffer
427  std::unique_ptr<SCH_SCREEN> screen = partBuf->RemoveScreen();
428 
429  if( !libBuf.DeleteBuffer( partBuf ) )
430  return false;
431 
432  if( !UpdatePart( aPart, aLibrary ) )
433  return false;
434 
435  partBuf = libBuf.GetBuffer( aPart->GetName() );
436  partBuf->SetScreen( std::move( screen ) );
437  wxCHECK( partBuf, false );
438  partBuf->SetOriginal( original.release() ); // part buffer takes ownership of pointer
439 
440  m_frame.SyncLibraries( false );
441 
442  return true;
443 }
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
Define a library symbol object.
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

Member Data Documentation

SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR LIB_MANAGER::m_adapter
private

Definition at line 451 of file lib_manager.h.

Referenced by GetAdapter(), and LIB_MANAGER().

wxString LIB_MANAGER::m_currentLib
private

Currently modified part

Definition at line 446 of file lib_manager.h.

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

wxString LIB_MANAGER::m_currentPart
private

Currently modified library

Definition at line 449 of file lib_manager.h.

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

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: