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_io_mgr.h>
37 #include <sch_screen.h>
38 
39 class LIB_PART;
40 class PART_LIB;
41 class SCH_PLUGIN;
42 class LIB_EDIT_FRAME;
43 class SYMBOL_LIB_TABLE;
45 
46 
47 class LIB_LOGGER : public wxLogGui
48 {
49 public:
51  m_previousLogger( nullptr ),
52  m_activated( false )
53  { }
54 
55  ~LIB_LOGGER() override
56  {
57  Deactivate();
58  }
59 
60  void Activate()
61  {
62  if( !m_activated )
63  {
64  m_previousLogger = wxLog::GetActiveTarget();
65  wxLog::SetActiveTarget( this );
66  m_activated = true;
67  }
68  }
69 
70  void Deactivate()
71  {
72  if( m_activated )
73  {
74  Flush();
75  m_activated = false;
76  wxLog::SetActiveTarget( m_previousLogger );
77  }
78  }
79 
80  void Flush() override
81  {
82  if( m_bHasMessages )
83  {
84  wxLogMessage( _( "Not all symbol libraries could be loaded. Use the Manage Symbol\n"
85  "Libraries dialog to adjust paths and add or remove libraries." ) );
86  wxLogGui::Flush();
87  }
88  }
89 
90 private:
93 };
94 
95 
100 {
101 public:
102  LIB_MANAGER( LIB_EDIT_FRAME& aFrame );
103 
107  void Sync( bool aForce = false, std::function<void(int, int, const wxString&)> aProgressCallback
108  = [](int, int, const wxString&){} );
109 
110  int GetHash() const;
111 
112  bool HasModifications() const;
113 
121  int GetLibraryHash( const wxString& aLibrary ) const;
122 
126  wxArrayString GetLibraryNames() const;
127 
131  SYMBOL_LIB_TABLE_ROW* GetLibrary( const wxString& aLibrary ) const;
132 
133  std::list<LIB_PART*> GetAliases( const wxString& aLibrary ) const;
134 
138  bool CreateLibrary( const wxString& aFilePath, SYMBOL_LIB_TABLE* aTable )
139  {
140  return addLibrary( aFilePath, true, aTable );
141  }
142 
146  bool AddLibrary( const wxString& aFilePath, SYMBOL_LIB_TABLE* aTable )
147  {
148  return addLibrary( aFilePath, false, aTable );
149  }
150 
156  bool UpdatePart( LIB_PART* aPart, const wxString& aLibrary );
157 
162  bool UpdatePartAfterRename( LIB_PART* aPart, const wxString& oldAlias,
163  const wxString& aLibrary );
164 
169  bool RemovePart( const wxString& aName, const wxString& aLibrary );
170 
175  LIB_PART* GetAlias( const wxString& aAlias, const wxString& aLibrary ) const;
176 
181  LIB_PART* GetBufferedPart( const wxString& aAlias, const wxString& aLibrary );
182 
186  SCH_SCREEN* GetScreen( const wxString& aAlias, const wxString& aLibrary );
187 
192  bool PartExists( const wxString& aAlias, const wxString& aLibrary ) const;
193 
198  bool LibraryExists( const wxString& aLibrary, bool aCheckEnabled = false ) const;
199 
203  bool IsLibraryModified( const wxString& aLibrary ) const;
204 
208  bool IsPartModified( const wxString& aAlias, const wxString& aLibrary ) const;
209 
213  bool ClearLibraryModified( const wxString& aLibrary ) const;
214 
218  bool ClearPartModified( const wxString& aAlias, const wxString& aLibrary ) const;
219 
224  bool IsLibraryReadOnly( const wxString& aLibrary ) const;
225 
231  bool FlushPart( const wxString& aAlias, const wxString& aLibrary );
232 
239  bool SaveLibrary( const wxString& aLibrary, const wxString& aFileName,
240  SCH_IO_MGR::SCH_FILE_T aFileType = SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY );
241 
247  LIB_ID RevertPart( const wxString& aAlias, const wxString& aLibrary );
248 
253  bool RevertLibrary( const wxString& aLibrary );
254 
260  bool RevertAll();
261 
266  wxString GetUniqueLibraryName() const;
267 
272 
276  const wxString& GetCurrentLib() const { return m_currentLib; }
277  void SetCurrentLib( const wxString& aLibrary ) { m_currentLib = aLibrary; }
278 
282  const wxString& GetCurrentPart() const { return m_currentPart; }
283  void SetCurrentPart( const wxString& aPart ) { m_currentPart = aPart; }
284 
289  {
290  return LIB_ID( m_currentLib, m_currentPart );
291  }
292 
293  void GetRootSymbolNames( const wxString& aLibName, wxArrayString& aRootSymbolNames );
294 
301  bool HasDerivedSymbols( const wxString& aSymbolName, const wxString& aLibraryName );
302 
303 private:
305  static wxString getLibraryName( const wxString& aFilePath );
306 
308  bool addLibrary( const wxString& aFilePath, bool aCreate, SYMBOL_LIB_TABLE* aTable );
309 
311  SYMBOL_LIB_TABLE* symTable() const;
312 
315  {
316  public:
317  PART_BUFFER( LIB_PART* aPart = nullptr, std::unique_ptr<SCH_SCREEN> aScreen = nullptr );
318  ~PART_BUFFER();
319 
320  LIB_PART* GetPart() const { return m_part; }
321  void SetPart( LIB_PART* aPart );
322 
323  LIB_PART* GetOriginal() const { return m_original; }
324  void SetOriginal( LIB_PART* aPart );
325 
326  bool IsModified() const;
327  SCH_SCREEN* GetScreen() const { return m_screen.get(); }
328 
330  std::unique_ptr<SCH_SCREEN> RemoveScreen()
331  {
332  return std::move( m_screen );
333  }
334 
335  bool SetScreen( std::unique_ptr<SCH_SCREEN> aScreen )
336  {
337  bool ret = !!m_screen;
338  m_screen = std::move( aScreen );
339  return ret;
340  }
341 
342  typedef std::shared_ptr<PART_BUFFER> PTR;
343  typedef std::weak_ptr<PART_BUFFER> WEAK_PTR;
344 
345  private:
346  std::unique_ptr<SCH_SCREEN> m_screen;
347 
348  LIB_PART* m_part; // Working copy
349  LIB_PART* m_original; // Initial state of the part
350  };
351 
352 
355  {
356  public:
357  LIB_BUFFER( const wxString& aLibrary ) :
358  m_libName( aLibrary ),
359  m_hash( 1 )
360  { }
361 
362  bool IsModified() const
363  {
364  if( !m_deleted.empty() )
365  return true;
366 
367  for( const auto& partBuf : m_parts )
368  {
369  if( partBuf->IsModified() )
370  return true;
371  }
372 
373  return false;
374  }
375 
376  int GetHash() const { return m_hash; }
377 
379  LIB_PART* GetPart( const wxString& aAlias ) const;
380 
382  bool CreateBuffer( LIB_PART* aCopy, SCH_SCREEN* aScreen );
383 
385  bool UpdateBuffer( PART_BUFFER::PTR aPartBuf, LIB_PART* aCopy );
386 
387  bool DeleteBuffer( PART_BUFFER::PTR aPartBuf );
388 
390  {
391  m_deleted.clear();
392  }
393 
396  bool SaveBuffer( PART_BUFFER::PTR aPartBuf, SYMBOL_LIB_TABLE* aLibTable );
397 
400  bool SaveBuffer( PART_BUFFER::PTR aPartBuf, SCH_PLUGIN* aPlugin, bool aBuffer );
401 
403  PART_BUFFER::PTR GetBuffer( const wxString& aAlias ) const;
404 
406  const std::deque<PART_BUFFER::PTR>& GetBuffers() const { return m_parts; }
407 
416  bool HasDerivedSymbols( const wxString& aParentName ) const;
417 
423  void GetRootSymbolNames( wxArrayString& aRootSymbolNames );
424 
434  size_t GetDerivedSymbolNames( const wxString& aSymbolName, wxArrayString& aList );
435 
436  private:
444  int removeChildSymbols( PART_BUFFER::PTR aPartBuf );
445 
446  std::deque<PART_BUFFER::PTR> m_parts;
447  std::deque<PART_BUFFER::PTR> m_deleted; // Buffer for deleted parts until library is saved
448  const wxString m_libName; // Buffered library name
449  int m_hash;
450  };
451 
453  std::set<LIB_PART*> getOriginalParts( const wxString& aLibrary );
454 
457  LIB_BUFFER& getLibraryBuffer( const wxString& aLibrary );
458 
460  std::map<wxString, LIB_BUFFER> m_libs;
461 
462  LIB_EDIT_FRAME& m_frame; // Parent frame
464  int m_syncHash; // Symbol Lib Table hash value from the last synchronization
465 
466  wxString m_currentLib; // Currently modified part
467  wxString m_currentPart; // Currently modified library
468 
471  {
472  return static_cast<SYMBOL_TREE_SYNCHRONIZING_ADAPTER*>( m_adapter.get() );
473  }
474 };
475 
476 #endif /* LIB_MANAGER_H */
std::unique_ptr< SCH_SCREEN > m_screen
Definition: lib_manager.h:346
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:49
SCH_SCREEN * GetScreen() const
Definition: lib_manager.h:327
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:343
std::list< LIB_PART * > GetAliases(const wxString &aLibrary) const
std::unique_ptr< SCH_SCREEN > RemoveScreen()
Transfer the screen ownership
Definition: lib_manager.h:330
bool RemovePart(const wxString &aName, const wxString &aLibrary)
Removes the part from the part buffer.
bool IsModified() const
Definition: lib_manager.h:362
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:138
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:406
const wxString & GetCurrentPart() const
Returns the currently modified part name.
Definition: lib_manager.h:282
wxLog * m_previousLogger
Definition: lib_manager.h:91
const wxString & GetCurrentLib() const
Returns the currently modified library name.
Definition: lib_manager.h:276
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:55
LIB_PART * GetPart() const
Definition: lib_manager.h:320
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:89
PART_BUFFER(LIB_PART *aPart=nullptr, std::unique_ptr< SCH_SCREEN > aScreen=nullptr)
LIB_BUFFER(const wxString &aLibrary)
Definition: lib_manager.h:357
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io_mgr.h:151
bool AddLibrary(const wxString &aFilePath, SYMBOL_LIB_TABLE *aTable)
Adds an existing library.
Definition: lib_manager.h:146
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:462
LIB_LOGGER m_logger
Definition: lib_manager.h:463
int GetHash() const
Definition: lib_manager.cpp:78
Define a library symbol object.
SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR m_adapter
Definition: lib_manager.h:469
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:277
Class to store a working copy of a library
Definition: lib_manager.h:354
void SetCurrentPart(const wxString &aPart)
Definition: lib_manager.h:283
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:447
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:80
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:335
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:99
LIB_PART * GetOriginal() const
Definition: lib_manager.h:323
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:60
The symbol library editor main window.
void Deactivate()
Definition: lib_manager.h:70
LIB_MANAGER(LIB_EDIT_FRAME &aFrame)
Definition: lib_manager.cpp:40
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:33
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
bool 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:66
bool IsLibraryModified(const wxString &aLibrary) const
Returns true if library has unsaved modifications.
bool m_activated
Definition: lib_manager.h:92
const wxString m_libName
Definition: lib_manager.h:448
std::shared_ptr< PART_BUFFER > PTR
Definition: lib_manager.h:342
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:466
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:460
std::deque< PART_BUFFER::PTR > m_parts
Definition: lib_manager.h:446
LIB_ID GetCurrentLibId() const
Returns the current library and part name as LIB_ID.
Definition: lib_manager.h:288
wxString m_currentPart
Definition: lib_manager.h:467
LIB_TREE_MODEL_ADAPTER::PTR & GetAdapter()
Returns the adapter object that provides the stored data.
Definition: lib_manager.h:271
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 SaveLibrary(const wxString &aLibrary, const wxString &aFileName, SCH_IO_MGR::SCH_FILE_T aFileType=SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY)
Saves library to a file, including unsaved changes.
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:314
SYMBOL_TREE_SYNCHRONIZING_ADAPTER * getAdapter()
Definition: lib_manager.h:470