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

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

References m_libs, and symTable().

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

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

522 {
523  // Try the library buffers first
524  auto libIt = m_libs.find( aLibrary );
525 
526  if( libIt != m_libs.end() )
527  {
528  LIB_PART* part = libIt->second.GetPart( aAlias );
529 
530  if( part )
531  return part->GetAlias( aAlias );
532  }
533 
534  // Get the original part
535  LIB_ALIAS* alias = nullptr;
536 
537  try
538  {
539  alias = symTable()->LoadSymbol( aLibrary, aAlias );
540  }
541  catch( const IO_ERROR& e )
542  {
543  DisplayErrorMessage( &m_frame, wxString::Format( _( "Cannot load "
544  "symbol \"%s\" from library \"%s\"" ), aAlias, aLibrary ), e.What() );
545  }
546 
547  return alias;
548 }
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(), THROW_IO_ERROR, 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 
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:259
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.
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 694 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().

695 {
696  auto it = m_libs.find( aLibrary );
697 
698  if( it != m_libs.end() )
699  return it->second;
700 
701  // The requested buffer does not exist yet, so create one
702  auto ret = m_libs.emplace( aLibrary, LIB_BUFFER( aLibrary ) );
703  LIB_BUFFER& buf = ret.first->second;
704 
705  for( auto part : getOriginalParts( aLibrary ) )
706  buf.CreateBuffer( new LIB_PART( *part, nullptr ), new SCH_SCREEN( &m_frame.Kiway() ) );
707 
708  return buf;
709 }
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 622 of file lib_manager.cpp.

Referenced by addLibrary().

623 {
624  wxFileName fn( aFilePath );
625  return fn.GetName();
626 }
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 669 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().

670 {
671  std::set<LIB_PART*> parts;
672  wxCHECK( LibraryExists( aLibrary ), parts );
673 
674  try
675  {
676  wxArrayString aliases;
677  symTable()->EnumerateSymbolLib( aLibrary, aliases );
678 
679  for( const auto& aliasName : aliases )
680  {
681  LIB_ALIAS* alias = symTable()->LoadSymbol( aLibrary, aliasName );
682  parts.insert( alias->GetPart() );
683  }
684  } catch( const IO_ERROR& e )
685  {
686  DisplayErrorMessage( &m_frame, wxString::Format( _( "Cannot enumerate "
687  "library \"%s\"" ), aLibrary ), e.What() );
688  }
689 
690  return parts;
691 }
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 385 of file lib_manager.cpp.

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

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

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

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

Referenced by AddLibrary().

603 {
604  wxString name = "New_Component";
605 
606  if( !PartExists( name, aLibrary ) )
607  return name;
608 
609  name += "_";
610 
611  for( unsigned int i = 0; i < std::numeric_limits<unsigned int>::max(); ++i )
612  {
613  if( !PartExists( name + wxString::Format( "%u", i ), aLibrary ) )
614  return name + wxString::Format( "%u", i );
615  }
616 
617  wxFAIL;
618  return wxEmptyString;
619 }
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 582 of file lib_manager.cpp.

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

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

583 {
584  wxString name = "New_Library";
585 
586  if( !LibraryExists( name ) )
587  return name;
588 
589  name += "_";
590 
591  for( unsigned int i = 0; i < std::numeric_limits<unsigned int>::max(); ++i )
592  {
593  if( !LibraryExists( name + wxString::Format( "%u", i ) ) )
594  return name + wxString::Format( "%u", i );
595  }
596 
597  wxFAIL;
598  return wxEmptyString;
599 }
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 571 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().

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

552 {
553  auto libBufIt = m_libs.find( aLibrary );
554  LIB_ALIAS* alias = nullptr;
555 
556  if( libBufIt != m_libs.end() )
557  return !!libBufIt->second.GetBuffer( aAlias );
558 
559  try
560  {
561  alias = symTable()->LoadSymbol( aLibrary, aAlias );
562  } catch( IO_ERROR& )
563  {
564  // checking if certain symbol exists, so its absence is perfectly fine
565  }
566 
567  return alias != nullptr;
568 }
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 508 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().

509 {
510  LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
511  auto partBuf = libBuf.GetBuffer( aAlias );
512  wxCHECK( partBuf, false );
513 
514  bool res = libBuf.DeleteBuffer( partBuf );
515  m_frame.SyncLibraries( false );
516 
517  return res;
518 }
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 494 of file lib_manager.cpp.

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

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

495 {
496  auto it = m_libs.find( aLibrary );
497 
498  if( it == m_libs.end() ) // nothing to reverse
499  return false;
500 
501  m_libs.erase( it );
502  m_frame.SyncLibraries( false );
503 
504  return true;
505 }
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 469 of file lib_manager.cpp.

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

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

470 {
471  auto it = m_libs.find( aLibrary );
472 
473  if( it == m_libs.end() ) // no items to flush
474  return LIB_ID( aLibrary, aAlias );
475 
476  auto partBuf = it->second.GetBuffer( aAlias );
477  wxCHECK( partBuf, LIB_ID( aLibrary, aAlias ) );
478  LIB_PART original( *partBuf->GetOriginal() );
479 
480  if( original.GetName() != aAlias )
481  {
482  UpdatePartAfterRename( &original, aAlias, aLibrary );
483  }
484  else
485  {
486  partBuf->SetPart( new LIB_PART( original ) );
487  m_frame.SyncLibraries( false );
488  }
489 
490  return LIB_ID( aLibrary, original.GetName() );
491 }
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 663 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().

664 {
665  return m_frame.Prj().SchSymbolLibTable();
666 }
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 398 of file lib_manager.cpp.

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

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 }
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
const LIB_ID & GetLibId() const
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
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
void SetLibId(const LIB_ID &aLibId)
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.

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

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  m_frame.SyncLibraries( false );
450 
451  return true;
452 }
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: