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  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * https://www.gnu.org/licenses/gpl-3.0.html
20  * or you may search the http://www.gnu.org website for the version 3 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #ifndef LIB_MANAGER_H
26 #define LIB_MANAGER_H
27 
28 #include <map>
29 #include <list>
30 #include <deque>
31 #include <set>
32 #include <memory>
33 #include <wx/arrstr.h>
35 #include <sch_screen.h>
36 
37 class LIB_ALIAS;
38 class LIB_PART;
39 class LIB_BUFFER;
40 class PART_LIB;
41 class SCH_PLUGIN;
42 class LIB_EDIT_FRAME;
43 class SYMBOL_LIB_TABLE;
45 
50 {
51 public:
52  LIB_MANAGER( LIB_EDIT_FRAME& aFrame );
53 
57  void Sync( bool aForce = false, std::function<void(int, int, const wxString&)> aProgressCallback
58  = [](int, int, const wxString&){} );
59 
60  int GetHash() const;
61 
69  int GetLibraryHash( const wxString& aLibrary ) const;
70 
74  wxArrayString GetLibraryNames() const;
75 
79  SYMBOL_LIB_TABLE_ROW* GetLibrary( const wxString& aLibrary ) const;
80 
84  wxArrayString GetAliasNames( const wxString& aLibrary ) const;
85 
86  std::list<LIB_ALIAS*> GetAliases( const wxString& aLibrary ) const;
87 
91  bool CreateLibrary( const wxString& aFilePath, SYMBOL_LIB_TABLE* aTable )
92  {
93  return addLibrary( aFilePath, true, aTable );
94  }
95 
99  bool AddLibrary( const wxString& aFilePath, SYMBOL_LIB_TABLE* aTable )
100  {
101  return addLibrary( aFilePath, false, aTable );
102  }
103 
109  bool UpdatePart( LIB_PART* aPart, const wxString& aLibrary );
110 
115  bool UpdatePartAfterRename( LIB_PART* aPart, const wxString& oldAlias,
116  const wxString& aLibrary );
117 
122  bool RemovePart( const wxString& aName, const wxString& aLibrary );
123 
128  LIB_ALIAS* GetAlias( const wxString& aAlias, const wxString& aLibrary ) const;
129 
134  LIB_PART* GetBufferedPart( const wxString& aAlias, const wxString& aLibrary );
135 
139  SCH_SCREEN* GetScreen( const wxString& aAlias, const wxString& aLibrary );
140 
144  bool PartExists( const wxString& aAlias, const wxString& aLibrary ) const;
145 
150  bool LibraryExists( const wxString& aLibrary, bool aCheckEnabled = false ) const;
151 
155  bool IsLibraryModified( const wxString& aLibrary ) const;
156 
160  bool IsPartModified( const wxString& aAlias, const wxString& aLibrary ) const;
161 
165  bool ClearLibraryModified( const wxString& aLibrary ) const;
166 
170  bool ClearPartModified( const wxString& aAlias, const wxString& aLibrary ) const;
171 
176  bool IsLibraryReadOnly( const wxString& aLibrary ) const;
177 
183  bool FlushPart( const wxString& aAlias, const wxString& aLibrary );
184 
191  bool FlushLibrary( const wxString& aLibrary );
192 
199  bool SaveLibrary( const wxString& aLibrary, const wxString& aFileName );
200 
205  bool FlushAll();
206 
212  LIB_ID RevertPart( const wxString& aAlias, const wxString& aLibrary );
213 
218  bool RevertLibrary( const wxString& aLibrary );
219 
224  wxString GetUniqueLibraryName() const;
225 
230  wxString GetUniqueComponentName( const wxString& aLibrary ) const;
231 
236 
240  const wxString& GetCurrentLib() const
241  {
242  return m_currentLib;
243  }
244 
248  void SetCurrentLib( const wxString& aLibrary )
249  {
250  m_currentLib = aLibrary;
251  }
252 
256  const wxString& GetCurrentPart() const
257  {
258  return m_currentPart;
259  }
260 
264  void SetCurrentPart( const wxString& aPart )
265  {
266  m_currentPart = aPart;
267  }
268 
273  {
274  return LIB_ID( m_currentLib, m_currentPart );
275  }
276 
277 private:
280 
282  static wxString getLibraryName( const wxString& aFilePath );
283 
285  bool addLibrary( const wxString& aFilePath, bool aCreate, SYMBOL_LIB_TABLE* aTable );
286 
288  SYMBOL_LIB_TABLE* symTable() const;
289 
292  {
293  public:
294  PART_BUFFER( LIB_PART* aPart = nullptr, std::unique_ptr<SCH_SCREEN> aScreen = nullptr );
295  ~PART_BUFFER();
296 
297  LIB_PART* GetPart() const { return m_part; }
298  void SetPart( LIB_PART* aPart );
299 
300  LIB_PART* GetOriginal() const { return m_original; }
301  void SetOriginal( LIB_PART* aPart );
302 
303  bool IsModified() const;
304  SCH_SCREEN* GetScreen() const { return m_screen.get(); }
305 
307  std::unique_ptr<SCH_SCREEN> RemoveScreen()
308  {
309  return std::move( m_screen );
310  }
311 
312  bool SetScreen( std::unique_ptr<SCH_SCREEN> aScreen )
313  {
314  bool ret = !!m_screen;
315  m_screen = std::move( aScreen );
316  return ret;
317  }
318 
319  typedef std::shared_ptr<PART_BUFFER> PTR;
320  typedef std::weak_ptr<PART_BUFFER> WEAK_PTR;
321 
322  private:
323  std::unique_ptr<SCH_SCREEN> m_screen;
324 
327 
330  };
331 
332 
335  {
336  public:
337  LIB_BUFFER( const wxString& aLibrary )
338  : m_libName( aLibrary ), m_hash( 1 )
339  {
340  }
341 
342  bool IsModified() const
343  {
344  if( !m_deleted.empty() )
345  return true;
346 
347  for( const auto& partBuf : m_parts )
348  {
349  if( partBuf->IsModified() )
350  return true;
351  }
352 
353  return false;
354  }
355 
356  int GetHash() const
357  {
358  return m_hash;
359  }
360 
362  wxArrayString GetAliasNames() const;
363 
365  LIB_PART* GetPart( const wxString& aAlias ) const
366  {
367  auto buf = GetBuffer( aAlias );
368  return buf ? buf->GetPart() : nullptr;
369  }
370 
372  bool CreateBuffer( LIB_PART* aCopy, SCH_SCREEN* aScreen );
373 
375  bool UpdateBuffer( PART_BUFFER::PTR aPartBuf, LIB_PART* aCopy );
376 
377  bool DeleteBuffer( PART_BUFFER::PTR aPartBuf );
378 
380  {
381  m_deleted.clear();
382  }
383 
386  bool SaveBuffer( PART_BUFFER::PTR aPartBuf, SYMBOL_LIB_TABLE* aLibTable );
387 
390  bool SaveBuffer( PART_BUFFER::PTR aPartBuf, SCH_PLUGIN* aPlugin, bool aBuffer );
391 
393  PART_BUFFER::PTR GetBuffer( const wxString& aAlias ) const
394  {
395  auto it = m_aliases.find( aAlias );
396  return it != m_aliases.end() ? it->second.lock() : PART_BUFFER::PTR( nullptr );
397  }
398 
400  const std::deque<PART_BUFFER::PTR>& GetBuffers() const
401  {
402  return m_parts;
403  }
404 
406  const std::map<wxString, PART_BUFFER::WEAK_PTR>& GetAliases() const
407  {
408  return m_aliases;
409  }
410 
411  private:
413  bool addAliases( PART_BUFFER::PTR aPartBuf );
414 
416  bool removeAliases( PART_BUFFER::PTR aPartBuf );
417 
418  std::map<wxString, PART_BUFFER::WEAK_PTR> m_aliases;
419  std::deque<PART_BUFFER::PTR> m_parts;
420 
422  std::deque<PART_BUFFER::PTR> m_deleted;
423 
425  const wxString m_libName;
426 
427  int m_hash;
428 
429  friend class PART_BUFFER;
430  };
431 
433  std::set<LIB_PART*> getOriginalParts( const wxString& aLibrary );
434 
437  LIB_BUFFER& getLibraryBuffer( const wxString& aLibrary );
438 
440  std::map<wxString, LIB_BUFFER> m_libs;
441 
444 
446  wxString m_currentLib;
447 
449  wxString m_currentPart;
450 
453 };
454 
455 #endif /* LIB_MANAGER_H */
std::unique_ptr< SCH_SCREEN > m_screen
Definition: lib_manager.h:323
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:47
Part library alias object definition.
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
LIB_PART * GetOriginal() const
Definition: lib_manager.h:300
std::weak_ptr< PART_BUFFER > WEAK_PTR
Definition: lib_manager.h:320
std::unique_ptr< SCH_SCREEN > RemoveScreen()
Transfer the screen ownership
Definition: lib_manager.h:307
bool RemovePart(const wxString &aName, const wxString &aLibrary)
Removes the part from the part buffer.
LIB_PART * m_part
Working copy
Definition: lib_manager.h:326
bool CreateLibrary(const wxString &aFilePath, SYMBOL_LIB_TABLE *aTable)
Creates an empty library and adds it to the library table.
Definition: lib_manager.h:91
void SetPart(LIB_PART *aPart)
LIB_ID GetCurrentLibId() const
Returns the current library and part name as LIB_ID.
Definition: lib_manager.h:272
bool ClearLibraryModified(const wxString &aLibrary) const
Clears the modified flag for all parts in a library.
LIB_PART * GetPart() const
Definition: lib_manager.h:297
int GetHash() const
Definition: lib_manager.cpp:59
const std::deque< PART_BUFFER::PTR > & GetBuffers() const
Returns all buffered parts
Definition: lib_manager.h:400
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
wxString GetUniqueComponentName(const wxString &aLibrary) const
Returns a component name that is not stored in a library.
SCH_SCREEN * GetScreen() const
Definition: lib_manager.h:304
LIB_PART * GetPart(const wxString &aAlias) const
Returns the working copy of a LIB_PART object with specified alias
Definition: lib_manager.h:365
PART_BUFFER::PTR GetBuffer(const wxString &aAlias) const
Returns a part buffer with LIB_PART holding a particular alias
Definition: lib_manager.h:393
std::list< LIB_ALIAS * > GetAliases(const wxString &aLibrary) const
PART_BUFFER(LIB_PART *aPart=nullptr, std::unique_ptr< SCH_SCREEN > aScreen=nullptr)
bool IsModified() const
Definition: lib_manager.h:342
LIB_BUFFER(const wxString &aLibrary)
Definition: lib_manager.h:337
Base class that schematic file and library loading and saving plugins should derive from...
Definition: sch_io_mgr.h:188
bool AddLibrary(const wxString &aFilePath, SYMBOL_LIB_TABLE *aTable)
Adds an existing library.
Definition: lib_manager.h:99
LIB_ID RevertPart(const wxString &aAlias, const wxString &aLibrary)
Reverts unsaved changes for a particular part.
const std::map< wxString, PART_BUFFER::WEAK_PTR > & GetAliases() const
Returns all aliases of buffered parts
Definition: lib_manager.h:406
bool FlushLibrary(const wxString &aLibrary)
Saves changes to the library copy used by the schematic editor.
bool FlushAll()
Saves all changes to libraries.
bool ClearPartModified(const wxString &aAlias, const wxString &aLibrary) const
Clears the modified flag for a part.
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Finds a single library within the (aggregate) library table.
Definition: lib_manager.cpp:95
SCH_SCREEN * GetScreen(const wxString &aAlias, const wxString &aLibrary)
Returns the screen used to edit a specific part.
LIB_EDIT_FRAME & m_frame
Parent frame
Definition: lib_manager.h:279
Define a library symbol object.
SYMBOL_TREE_SYNCHRONIZING_ADAPTER::PTR m_adapter
Definition: lib_manager.h:451
bool IsLibraryModified(const wxString &aLibrary) const
Returns true if library has unsaved modifications.
wxArrayString GetAliasNames(const wxString &aLibrary) const
Returns a set containing all part names for a specific library.
void SetCurrentLib(const wxString &aLibrary)
Sets the currently modified library name.
Definition: lib_manager.h:248
Class to store a working copy of a library
Definition: lib_manager.h:334
void SetCurrentPart(const wxString &aPart)
Sets the currently modified part name.
Definition: lib_manager.h:264
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
Buffer to keep deleted parts until the library is saved
Definition: lib_manager.h:422
bool SaveLibrary(const wxString &aLibrary, const wxString &aFileName)
Saves library to a file, including unsaved changes.
bool IsLibraryReadOnly(const wxString &aLibrary) const
Returns true if the library is stored in a read-only file.
bool SetScreen(std::unique_ptr< SCH_SCREEN > aScreen)
Definition: lib_manager.h:312
bool UpdatePart(LIB_PART *aPart, const wxString &aLibrary)
Updates the part buffer with a new version of the part.
int GetLibraryHash(const wxString &aLibrary) const
Retruns a library hash value to determine if it has changed.
Definition: lib_manager.cpp:70
Class to handle modifications to the symbol libraries.
Definition: lib_manager.h:49
Definitions for the Eeschema program SCH_SCREEN class.
SYMBOL_LIB_TABLE * symTable() const
Returns the current Symbol Library Table
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.
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)...
wxArrayString GetLibraryNames() const
Returns the array of library names.
Definition: lib_manager.cpp:84
The symbol library editor main window.
LIB_MANAGER(LIB_EDIT_FRAME &aFrame)
Definition: lib_manager.cpp:39
LIB_PART * m_original
Initial state of the part
Definition: lib_manager.h:329
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...
std::map< wxString, PART_BUFFER::WEAK_PTR > m_aliases
Definition: lib_manager.h:418
const wxString & GetCurrentPart() const
Returns the currently modified part name.
Definition: lib_manager.h:256
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
LIB_ALIAS * 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...
wxString GetUniqueLibraryName() const
Returns a library name that is not currently in use.
bool IsPartModified(const wxString &aAlias, const wxString &aLibrary) const
Returns true if part has unsaved modifications.
const wxString m_libName
Buffered library name.
Definition: lib_manager.h:425
std::shared_ptr< PART_BUFFER > PTR
Definition: lib_manager.h:319
int m_syncHash
Symbol Lib Table hash value returned during the last synchronization
Definition: lib_manager.h:443
void SetOriginal(LIB_PART *aPart)
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > PTR
Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER_BASE.
const wxString & GetCurrentLib() const
Returns the currently modified library name.
Definition: lib_manager.h:240
wxString m_currentLib
Currently modified part
Definition: lib_manager.h:446
std::map< wxString, LIB_BUFFER > m_libs
The library buffers
Definition: lib_manager.h:440
std::deque< PART_BUFFER::PTR > m_parts
Definition: lib_manager.h:419
wxString m_currentPart
Currently modified library
Definition: lib_manager.h:449
LIB_TREE_MODEL_ADAPTER::PTR & GetAdapter()
Returns the adapter object that provides the stored data.
Definition: lib_manager.h:235
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 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:291
SYMBOL_TREE_SYNCHRONIZING_ADAPTER * getAdapter()
Definition: lib_manager.h:452