KiCad PCB EDA Suite
lib_manager.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2017 CERN
5  * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * https://www.gnu.org/licenses/gpl-3.0.html
21  * or you may search the http://www.gnu.org website for the version 3 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #ifndef LIB_MANAGER_H
27 #define LIB_MANAGER_H
28 
29 #include <map>
30 #include <list>
31 #include <deque>
32 #include <set>
33 #include <memory>
34 #include <wx/arrstr.h>
36 #include <sch_screen.h>
37 
38 class LIB_PART;
39 class PART_LIB;
40 class SCH_PLUGIN;
41 class LIB_EDIT_FRAME;
42 class SYMBOL_LIB_TABLE;
44 
45 
46 class LIB_LOGGER : public wxLogGui
47 {
48 public:
50  m_previousLogger( nullptr ),
51  m_activated( false )
52  { }
53 
54  ~LIB_LOGGER() override
55  {
56  Deactivate();
57  }
58 
59  void Activate()
60  {
61  if( !m_activated )
62  {
63  m_previousLogger = wxLog::GetActiveTarget();
64  wxLog::SetActiveTarget( this );
65  m_activated = true;
66  }
67  }
68 
69  void Deactivate()
70  {
71  if( m_activated )
72  {
73  Flush();
74  m_activated = false;
75  wxLog::SetActiveTarget( m_previousLogger );
76  }
77  }
78 
79  void Flush() override
80  {
81  if( m_bHasMessages )
82  {
83  wxLogMessage( _( "Not all symbol libraries could be loaded. Use the Manage Symbol\n"
84  "Libraries dialog to adjust paths and add or remove libraries." ) );
85  wxLogGui::Flush();
86  }
87  }
88 
89 private:
92 };
93 
94 
99 {
100 public:
101  LIB_MANAGER( LIB_EDIT_FRAME& aFrame );
102 
106  void Sync( bool aForce = false, std::function<void(int, int, const wxString&)> aProgressCallback
107  = [](int, int, const wxString&){} );
108 
109  int GetHash() const;
110 
111  bool HasModifications() const;
112 
120  int GetLibraryHash( const wxString& aLibrary ) const;
121 
125  wxArrayString GetLibraryNames() const;
126 
130  SYMBOL_LIB_TABLE_ROW* GetLibrary( const wxString& aLibrary ) const;
131 
132  std::list<LIB_PART*> GetAliases( const wxString& aLibrary ) const;
133 
137  bool CreateLibrary( const wxString& aFilePath, SYMBOL_LIB_TABLE* aTable )
138  {
139  return addLibrary( aFilePath, true, aTable );
140  }
141 
145  bool AddLibrary( const wxString& aFilePath, SYMBOL_LIB_TABLE* aTable )
146  {
147  return addLibrary( aFilePath, false, aTable );
148  }
149 
155  bool UpdatePart( LIB_PART* aPart, const wxString& aLibrary );
156 
161  bool UpdatePartAfterRename( LIB_PART* aPart, const wxString& oldAlias,
162  const wxString& aLibrary );
163 
168  bool RemovePart( const wxString& aName, const wxString& aLibrary );
169 
174  LIB_PART* GetAlias( const wxString& aAlias, const wxString& aLibrary ) const;
175 
180  LIB_PART* GetBufferedPart( const wxString& aAlias, const wxString& aLibrary );
181 
185  SCH_SCREEN* GetScreen( const wxString& aAlias, const wxString& aLibrary );
186 
191  bool PartExists( const wxString& aAlias, const wxString& aLibrary ) const;
192 
197  bool LibraryExists( const wxString& aLibrary, bool aCheckEnabled = false ) const;
198 
202  bool IsLibraryModified( const wxString& aLibrary ) const;
203 
207  bool IsPartModified( const wxString& aAlias, const wxString& aLibrary ) const;
208 
212  bool ClearLibraryModified( const wxString& aLibrary ) const;
213 
217  bool ClearPartModified( const wxString& aAlias, const wxString& aLibrary ) const;
218 
223  bool IsLibraryReadOnly( const wxString& aLibrary ) const;
224 
230  bool FlushPart( const wxString& aAlias, const wxString& aLibrary );
231 
238  bool SaveLibrary( const wxString& aLibrary, const wxString& aFileName );
239 
245  LIB_ID RevertPart( const wxString& aAlias, const wxString& aLibrary );
246 
251  bool RevertLibrary( const wxString& aLibrary );
252 
258  bool RevertAll();
259 
264  wxString GetUniqueLibraryName() const;
265 
270 
274  const wxString& GetCurrentLib() const { return m_currentLib; }
275  void SetCurrentLib( const wxString& aLibrary ) { m_currentLib = aLibrary; }
276 
280  const wxString& GetCurrentPart() const { return m_currentPart; }
281  void SetCurrentPart( const wxString& aPart ) { m_currentPart = aPart; }
282 
287  {
288  return LIB_ID( m_currentLib, m_currentPart );
289  }
290 
291  void GetRootSymbolNames( const wxString& aLibName, wxArrayString& aRootSymbolNames );
292 
299  bool HasDerivedSymbols( const wxString& aSymbolName, const wxString& aLibraryName );
300 
301 private:
303  static wxString getLibraryName( const wxString& aFilePath );
304 
306  bool addLibrary( const wxString& aFilePath, bool aCreate, SYMBOL_LIB_TABLE* aTable );
307 
309  SYMBOL_LIB_TABLE* symTable() const;
310 
313  {
314  public:
315  PART_BUFFER( LIB_PART* aPart = nullptr, std::unique_ptr<SCH_SCREEN> aScreen = nullptr );
316  ~PART_BUFFER();
317 
318  LIB_PART* GetPart() const { return m_part; }
319  void SetPart( LIB_PART* aPart );
320 
321  LIB_PART* GetOriginal() const { return m_original; }
322  void SetOriginal( LIB_PART* aPart );
323 
324  bool IsModified() const;
325  SCH_SCREEN* GetScreen() const { return m_screen.get(); }
326 
328  std::unique_ptr<SCH_SCREEN> RemoveScreen()
329  {
330  return std::move( m_screen );
331  }
332 
333  bool SetScreen( std::unique_ptr<SCH_SCREEN> aScreen )
334  {
335  bool ret = !!m_screen;
336  m_screen = std::move( aScreen );
337  return ret;
338  }
339 
340  typedef std::shared_ptr<PART_BUFFER> PTR;
341  typedef std::weak_ptr<PART_BUFFER> WEAK_PTR;
342 
343  private:
344  std::unique_ptr<SCH_SCREEN> m_screen;
345 
346  LIB_PART* m_part; // Working copy
347  LIB_PART* m_original; // Initial state of the part
348  };
349 
350 
353  {
354  public:
355  LIB_BUFFER( const wxString& aLibrary ) :
356  m_libName( aLibrary ),
357  m_hash( 1 )
358  { }
359 
360  bool IsModified() const
361  {
362  if( !m_deleted.empty() )
363  return true;
364 
365  for( const auto& partBuf : m_parts )
366  {
367  if( partBuf->IsModified() )
368  return true;
369  }
370 
371  return false;
372  }
373 
374  int GetHash() const { return m_hash; }
375 
377  LIB_PART* GetPart( const wxString& aAlias ) const;
378 
380  bool CreateBuffer( LIB_PART* aCopy, SCH_SCREEN* aScreen );
381 
383  bool UpdateBuffer( PART_BUFFER::PTR aPartBuf, LIB_PART* aCopy );
384 
385  bool DeleteBuffer( PART_BUFFER::PTR aPartBuf );
386 
388  {
389  m_deleted.clear();
390  }
391 
394  bool SaveBuffer( PART_BUFFER::PTR aPartBuf, SYMBOL_LIB_TABLE* aLibTable );
395 
398  bool SaveBuffer( PART_BUFFER::PTR aPartBuf, SCH_PLUGIN* aPlugin, bool aBuffer );
399 
401  PART_BUFFER::PTR GetBuffer( const wxString& aAlias ) const;
402 
404  const std::deque<PART_BUFFER::PTR>& GetBuffers() const { return m_parts; }
405 
414  bool HasDerivedSymbols( const wxString& aParentName ) const;
415 
421  void GetRootSymbolNames( wxArrayString& aRootSymbolNames );
422 
432  size_t GetDerivedSymbolNames( const wxString& aSymbolName, wxArrayString& aList );
433 
434  private:
442  int removeChildSymbols( PART_BUFFER::PTR aPartBuf );
443 
444  std::deque<PART_BUFFER::PTR> m_parts;
445  std::deque<PART_BUFFER::PTR> m_deleted; // Buffer for deleted parts until library is saved
446  const wxString m_libName; // Buffered library name
447  int m_hash;
448  };
449 
451  std::set<LIB_PART*> getOriginalParts( const wxString& aLibrary );
452 
455  LIB_BUFFER& getLibraryBuffer( const wxString& aLibrary );
456 
458  std::map<wxString, LIB_BUFFER> m_libs;
459 
460  LIB_EDIT_FRAME& m_frame; // Parent frame
462  int m_syncHash; // Symbol Lib Table hash value from the last synchronization
463 
464  wxString m_currentLib; // Currently modified part
465  wxString m_currentPart; // Currently modified library
466 
469  {
470  return static_cast<SYMBOL_TREE_SYNCHRONIZING_ADAPTER*>( m_adapter.get() );
471  }
472 };
473 
474 #endif /* LIB_MANAGER_H */
std::unique_ptr< SCH_SCREEN > m_screen
Definition: lib_manager.h:344
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.
Definition: lib_manager.cpp:48
SCH_SCREEN * GetScreen() const
Definition: lib_manager.h:325
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
bool HasDerivedSymbols(const wxString &aSymbolName, const wxString &aLibraryName)
Check if symbol aSymbolName in library aLibraryName is a root symbol that has derived symbols.
std::weak_ptr< PART_BUFFER > WEAK_PTR
Definition: lib_manager.h:341
std::list< LIB_PART * > GetAliases(const wxString &aLibrary) const
std::unique_ptr< SCH_SCREEN > RemoveScreen()
Transfer the screen ownership
Definition: lib_manager.h:328
bool RemovePart(const wxString &aName, const wxString &aLibrary)
Removes the part from the part buffer.
bool IsModified() const
Definition: lib_manager.h:360
bool CreateBuffer(LIB_PART *aCopy, SCH_SCREEN *aScreen)
Creates a new buffer to store a part. LIB_BUFFER takes ownership of aCopy.
bool CreateLibrary(const wxString &aFilePath, SYMBOL_LIB_TABLE *aTable)
Creates an empty library and adds it to the library table.
Definition: lib_manager.h:137
void SetPart(LIB_PART *aPart)
LIB_PART * GetPart(const wxString &aAlias) const
Returns the working copy of a LIB_PART root object with specified alias.
const std::deque< PART_BUFFER::PTR > & GetBuffers() const
Returns all buffered parts
Definition: lib_manager.h:404
const wxString & GetCurrentPart() const
Returns the currently modified part name.
Definition: lib_manager.h:280
wxLog * m_previousLogger
Definition: lib_manager.h:90
const wxString & GetCurrentLib() const
Returns the currently modified library name.
Definition: lib_manager.h:274
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
bool DeleteBuffer(PART_BUFFER::PTR aPartBuf)
~LIB_LOGGER() override
Definition: lib_manager.h:54
LIB_PART * GetPart() const
Definition: lib_manager.h:318
void GetRootSymbolNames(wxArrayString &aRootSymbolNames)
Fetchs a list of root symbols names from the library buffer.
LIB_PART * 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 work...
int GetLibraryHash(const wxString &aLibrary) const
Returns a library hash value to determine if it has changed.
Definition: lib_manager.cpp:88
PART_BUFFER(LIB_PART *aPart=nullptr, std::unique_ptr< SCH_SCREEN > aScreen=nullptr)
LIB_BUFFER(const wxString &aLibrary)
Definition: lib_manager.h:355
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io_mgr.h:187
bool AddLibrary(const wxString &aFilePath, SYMBOL_LIB_TABLE *aTable)
Adds an existing library.
Definition: lib_manager.h:145
bool UpdateBuffer(PART_BUFFER::PTR aPartBuf, LIB_PART *aCopy)
Updates the buffered part with the contents of aCopy.
wxString GetUniqueLibraryName() const
Returns a library name that is not currently in use.
LIB_ID RevertPart(const wxString &aAlias, const wxString &aLibrary)
Reverts unsaved changes for a particular part.
bool IsPartModified(const wxString &aAlias, const wxString &aLibrary) const
Returns true if part has unsaved modifications.
SCH_SCREEN * GetScreen(const wxString &aAlias, const wxString &aLibrary)
Returns the screen used to edit a specific part.
LIB_EDIT_FRAME & m_frame
Definition: lib_manager.h:460
LIB_LOGGER m_logger
Definition: lib_manager.h:461
int GetHash() const
Definition: lib_manager.cpp:77
Define a library symbol object.
SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR m_adapter
Definition: lib_manager.h:467
int removeChildSymbols(PART_BUFFER::PTR aPartBuf)
Remove all symbols derived from aParent from the library buffer.
wxArrayString GetLibraryNames() const
Returns the array of library names.
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Finds a single library within the (aggregate) library table.
void SetCurrentLib(const wxString &aLibrary)
Definition: lib_manager.h:275
Class to store a working copy of a library
Definition: lib_manager.h:352
void SetCurrentPart(const wxString &aPart)
Definition: lib_manager.h:281
bool FlushPart(const wxString &aAlias, const wxString &aLibrary)
Saves part changes to the library copy used by the schematic editor.
std::deque< PART_BUFFER::PTR > m_deleted
Definition: lib_manager.h:445
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).
size_t GetDerivedSymbolNames(const wxString &aSymbolName, wxArrayString &aList)
Fetch all of the symbols derived from a aSymbolName into aList.
bool ClearLibraryModified(const wxString &aLibrary) const
Clears the modified flag for all parts in a library.
void Flush() override
Definition: lib_manager.h:79
bool SaveLibrary(const wxString &aLibrary, const wxString &aFileName)
Saves library to a file, including unsaved changes.
PART_BUFFER::PTR GetBuffer(const wxString &aAlias) const
Returns a part buffer with LIB_PART holding a particular alias
bool SetScreen(std::unique_ptr< SCH_SCREEN > aScreen)
Definition: lib_manager.h:333
bool UpdatePart(LIB_PART *aPart, const wxString &aLibrary)
Updates the part buffer with a new version of the part.
bool SaveBuffer(PART_BUFFER::PTR aPartBuf, SYMBOL_LIB_TABLE *aLibTable)
Saves stored modifications to Symbol Lib Table.
Class to handle modifications to the symbol libraries.
Definition: lib_manager.h:98
LIB_PART * GetOriginal() const
Definition: lib_manager.h:321
void GetRootSymbolNames(const wxString &aLibName, wxArrayString &aRootSymbolNames)
bool addLibrary(const wxString &aFilePath, bool aCreate, SYMBOL_LIB_TABLE *aTable)
Helper function to add either existing or create new library
LIB_PART * GetBufferedPart(const wxString &aAlias, const wxString &aLibrary)
Returns the part copy from the buffer.
void Activate()
Definition: lib_manager.h:59
The symbol library editor main window.
void Deactivate()
Definition: lib_manager.h:69
LIB_MANAGER(LIB_EDIT_FRAME &aFrame)
Definition: lib_manager.cpp:39
std::set< LIB_PART * > getOriginalParts(const wxString &aLibrary)
Returns a set of LIB_PART objects belonging to the original 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...
#define _(s)
Definition: 3d_actions.cpp:31
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
bool HasDerivedSymbols(const wxString &aParentName) const
Checks to see any parts in the buffer are derived from a parent named aParentName.
bool HasModifications() const
Definition: lib_manager.cpp:65
bool IsLibraryModified(const wxString &aLibrary) const
Returns true if library has unsaved modifications.
bool m_activated
Definition: lib_manager.h:91
const wxString m_libName
Definition: lib_manager.h:446
std::shared_ptr< PART_BUFFER > PTR
Definition: lib_manager.h:340
void SetOriginal(LIB_PART *aPart)
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > PTR
Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER_BASE.
wxString m_currentLib
Definition: lib_manager.h:464
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:458
std::deque< PART_BUFFER::PTR > m_parts
Definition: lib_manager.h:444
LIB_ID GetCurrentLibId() const
Returns the current library and part name as LIB_ID.
Definition: lib_manager.h:286
wxString m_currentPart
Definition: lib_manager.h:465
LIB_TREE_MODEL_ADAPTER::PTR & GetAdapter()
Returns the adapter object that provides the stored data.
Definition: lib_manager.h:269
Object used to load, save, search, and otherwise manipulate symbol library files.
static wxString getLibraryName(const wxString &aFilePath)
Extracts library name basing on the file name
bool RevertAll()
Revert all pending changes.
bool IsLibraryReadOnly(const wxString &aLibrary) const
Returns true if the library is stored in a read-only file.
bool ClearPartModified(const wxString &aAlias, const wxString &aLibrary) const
Clears the modified flag for a part.
bool RevertLibrary(const wxString &aLibrary)
Reverts unsaved changes for a particular library.
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.
Class to store a working copy of a LIB_PART object and editor context.
Definition: lib_manager.h:312
SYMBOL_TREE_SYNCHRONIZING_ADAPTER * getAdapter()
Definition: lib_manager.h:468