KiCad PCB EDA Suite
Loading...
Searching...
No Matches
symbol_library_manager.h
Go to the documentation of this file.
1/*
2 * This program source code file is symbol of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2017 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Maciej Suminski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 3
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A SYMBOLICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef SYMBOL_LIBRARY_MANAGER_H
24#define SYMBOL_LIBRARY_MANAGER_H
25
26#include <map>
27#include <list>
28#include <deque>
29#include <set>
30#include <memory>
31#include <wx/arrstr.h>
32#include <sch_io/sch_io_mgr.h>
33#include <sch_screen.h>
35
36class LIB_SYMBOL;
39class SCH_IO;
40class SCH_BASE_FRAME;
41class LIB_LOGGER;
42
43
50
51
53{
54public:
55 SYMBOL_BUFFER( std::unique_ptr<LIB_SYMBOL> aSymbol = nullptr,
56 std::unique_ptr<SCH_SCREEN> aScreen = nullptr );
58
59 LIB_SYMBOL& GetSymbol() const { return *m_symbol; }
60 void SetSymbol( std::unique_ptr<LIB_SYMBOL> aSymbol );
61
62 LIB_SYMBOL& GetOriginal() const { return *m_original; }
63 void SetOriginal( std::unique_ptr<LIB_SYMBOL> aSymbol );
64
65 bool IsModified() const;
66 SCH_SCREEN* GetScreen() const { return m_screen.get(); }
67
68private:
69 std::unique_ptr<SCH_SCREEN> m_screen;
70 std::unique_ptr<LIB_SYMBOL> m_symbol; // Working copy
71 std::unique_ptr<LIB_SYMBOL> m_original; // Initial state of the symbol
72};
73
74
77{
78public:
79 LIB_BUFFER( const wxString& aLibrary ) :
80 m_libName( aLibrary ),
81 m_hash( 1 )
82 {}
83
84 bool IsModified() const
85 {
86 if( !m_deleted.empty() )
87 return true;
88
89 for( const std::shared_ptr<SYMBOL_BUFFER>& symbolBuf : m_symbols )
90 {
91 if( symbolBuf->IsModified() )
92 return true;
93 }
94
95 return false;
96 }
97
98 int GetHash() const { return m_hash; }
99
101 LIB_SYMBOL* GetSymbol( const wxString& aAlias ) const;
102
104 bool CreateBuffer( std::unique_ptr<LIB_SYMBOL> aCopy, std::unique_ptr<SCH_SCREEN> aScreen );
105
107 bool UpdateBuffer( SYMBOL_BUFFER& aSymbolBuf, const LIB_SYMBOL& aCopy );
108
112 bool DeleteBuffer( const SYMBOL_BUFFER& aSymbolBuf );
113
115 const std::deque<std::shared_ptr<SYMBOL_BUFFER>>& GetDeletedBuffers() const { return m_deleted; }
116
117 void ClearDeletedBuffer() { m_deleted.clear(); }
118
121 bool SaveBuffer( SYMBOL_BUFFER& aSymbolBuf, const wxString& aFileName, SCH_IO* aPlugin,
122 bool aBuffer );
123
125 std::shared_ptr<SYMBOL_BUFFER> GetBuffer( const wxString& aAlias ) const;
126
128 const std::deque<std::shared_ptr<SYMBOL_BUFFER>>& GetBuffers() const { return m_symbols; }
129
137 bool HasDerivedSymbols( const wxString& aParentName ) const;
138
145 void GetSymbolNames( wxArrayString& aSymbolNames,
147
156 size_t GetDerivedSymbolNames( const wxString& aSymbolName, wxArrayString& aList );
157
158private:
165 int removeChildSymbols( const SYMBOL_BUFFER& aSymbolBuf );
166
167private:
168 std::deque<std::shared_ptr<SYMBOL_BUFFER>> m_symbols;
169 std::deque<std::shared_ptr<SYMBOL_BUFFER>> m_deleted;
171 const wxString m_libName;
173};
174
175
181{
182public:
184 virtual ~SYMBOL_LIBRARY_MANAGER();
185
186 int GetHash() const;
187
188 bool HasModifications() const;
189
197 int GetLibraryHash( const wxString& aLibrary ) const;
198
202 wxArrayString GetLibraryNames() const;
203
204 std::list<LIB_SYMBOL*> EnumerateSymbols( const wxString& aLibrary ) const;
205
211 bool CreateLibrary( const wxString& aFilePath, LIBRARY_TABLE_SCOPE aScope )
212 {
213 return addLibrary( aFilePath, true, aScope );
214 }
215
221 bool AddLibrary( const wxString& aFilePath, LIBRARY_TABLE_SCOPE aScope )
222 {
223 return addLibrary( aFilePath, false, aScope );
224 }
225
233 bool UpdateSymbol( LIB_SYMBOL* aSymbol, const wxString& aLibrary );
234
240 bool UpdateSymbolAfterRename( LIB_SYMBOL* aSymbol, const wxString& aOldSymbolName,
241 const wxString& aLibrary );
242
246 bool UpdateLibraryBuffer( const wxString& aLibrary );
247
253 bool RemoveSymbol( const wxString& aSymbolName, const wxString& aLibrary );
254
259 LIB_SYMBOL* GetSymbol( const wxString& aSymbolName, const wxString& aLibrary ) const;
260
267 LIB_SYMBOL* GetBufferedSymbol( const wxString& aSymbolName, const wxString& aLibrary );
268
273 SCH_SCREEN* GetScreen( const wxString& aSymbolName, const wxString& aLibrary );
274
280 SYMBOL_BUFFER* GetBuffer( const wxString& aSymbolName, const wxString& aLibrary );
281
286 bool SymbolExists( const wxString& aSymbolName, const wxString& aLibrary ) const;
287
291 bool SymbolNameInUse( const wxString& aName, const wxString& aLibrary );
292
298 bool LibraryExists( const wxString& aLibrary, bool aCheckEnabled = false ) const;
299
303 bool IsLibraryLoaded( const wxString& aLibrary ) const;
304
308 bool IsLibraryModified( const wxString& aLibrary ) const;
309
313 bool IsSymbolModified( const wxString& aSymbolName, const wxString& aLibrary ) const;
314
315 void SetSymbolModified( const wxString& aSymbolName, const wxString& aLibrary );
316
320 bool ClearLibraryModified( const wxString& aLibrary ) const;
321
325 bool ClearSymbolModified( const wxString& aSymbolName, const wxString& aLibrary ) const;
326
332 bool IsLibraryReadOnly( const wxString& aLibrary ) const;
333
341 bool SaveLibrary( const wxString& aLibrary, const wxString& aFileName,
342 SCH_IO_MGR::SCH_FILE_T aFileType = SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY );
343
350 LIB_ID RevertSymbol( const wxString& aSymbolName, const wxString& aLibrary );
351
357 bool RevertLibrary( const wxString& aLibrary );
358
364 bool RevertAll();
365
371 wxString GetUniqueLibraryName() const;
372
373 void GetSymbolNames( const wxString& aLibName, wxArrayString& aSymbolNames,
375
379 size_t GetDerivedSymbolNames( const wxString& aSymbolName, const wxString& aLibraryName, wxArrayString& aList );
380
381 size_t GetLibraryCount() const;
382
383protected:
384 virtual void OnDataChanged() const {}
385
387 static wxString getLibraryName( const wxString& aFilePath );
388
390 bool addLibrary( const wxString& aFilePath, bool aCreate, LIBRARY_TABLE_SCOPE aScope );
391
395 std::set<LIB_SYMBOL*> getOriginalSymbols( const wxString& aLibrary );
396
401 LIB_BUFFER& getLibraryBuffer( const wxString& aLibrary );
402
403protected:
404 std::map<wxString, LIB_BUFFER> m_libs;
407};
408
409#endif /* SYMBOL_LIBRARY_MANAGER_H */
Object used to load, save, search, and otherwise manipulate symbol library files.
Store a working copy of a library.
size_t GetDerivedSymbolNames(const wxString &aSymbolName, wxArrayString &aList)
Fetch all of the symbols derived from a aSymbolName into aList.
bool CreateBuffer(std::unique_ptr< LIB_SYMBOL > aCopy, std::unique_ptr< SCH_SCREEN > aScreen)
Create a new buffer to store a symbol. LIB_BUFFER takes ownership of aCopy.
bool IsModified() const
bool DeleteBuffer(const SYMBOL_BUFFER &aSymbolBuf)
Delete the given symbol buffer from the library buffer.
void GetSymbolNames(wxArrayString &aSymbolNames, SYMBOL_NAME_FILTER aFilter=SYMBOL_NAME_FILTER::ALL)
Fetch a list of root symbols names from the library buffer.
const std::deque< std::shared_ptr< SYMBOL_BUFFER > > & GetDeletedBuffers() const
Return the deleted symbol buffers that need to be removed from the library file.
LIB_BUFFER(const wxString &aLibrary)
std::deque< std::shared_ptr< SYMBOL_BUFFER > > m_symbols
std::deque< std::shared_ptr< SYMBOL_BUFFER > > m_deleted
Buffer for deleted symbols until library is saved.
bool SaveBuffer(SYMBOL_BUFFER &aSymbolBuf, const wxString &aFileName, SCH_IO *aPlugin, bool aBuffer)
Save stored modifications using a plugin.
const std::deque< std::shared_ptr< SYMBOL_BUFFER > > & GetBuffers() const
Return all buffered symbols.
std::shared_ptr< SYMBOL_BUFFER > GetBuffer(const wxString &aAlias) const
Return a symbol buffer with LIB_SYMBOL holding a symbolic alias.
LIB_SYMBOL * GetSymbol(const wxString &aAlias) const
Return the working copy of a LIB_SYMBOL root object with specified alias.
int removeChildSymbols(const SYMBOL_BUFFER &aSymbolBuf)
Remove all symbols derived from aParent from the library buffer.
bool HasDerivedSymbols(const wxString &aParentName) const
Check to see any symbols in the buffer are derived from a parent named aParentName.
bool UpdateBuffer(SYMBOL_BUFFER &aSymbolBuf, const LIB_SYMBOL &aCopy)
Update the buffered symbol with the contents of aCopy.
const wxString m_libName
Buffered library name.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
Define a library symbol object.
Definition lib_symbol.h:79
A progress reporter interface for use in multi-threaded environments.
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
Base class that schematic file and library loading and saving plugins should derive from.
Definition sch_io.h:59
std::unique_ptr< LIB_SYMBOL > m_symbol
std::unique_ptr< SCH_SCREEN > m_screen
void SetSymbol(std::unique_ptr< LIB_SYMBOL > aSymbol)
LIB_SYMBOL & GetSymbol() const
SCH_SCREEN * GetScreen() const
SYMBOL_BUFFER(std::unique_ptr< LIB_SYMBOL > aSymbol=nullptr, std::unique_ptr< SCH_SCREEN > aScreen=nullptr)
std::unique_ptr< LIB_SYMBOL > m_original
LIB_SYMBOL & GetOriginal() const
void SetOriginal(std::unique_ptr< LIB_SYMBOL > aSymbol)
bool IsLibraryReadOnly(const wxString &aLibrary) const
Return true if the library is stored in a read-only file.
SYMBOL_BUFFER * GetBuffer(const wxString &aSymbolName, const wxString &aLibrary)
Return the working buffer holding the symbol/screen pair, or nullptr when none exists.
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.
bool ClearLibraryModified(const wxString &aLibrary) const
Clear the modified flag for all symbols in a library.
LIB_SYMBOL * GetBufferedSymbol(const wxString &aSymbolName, const wxString &aLibrary)
Return the symbol copy from the buffer.
bool CreateLibrary(const wxString &aFilePath, LIBRARY_TABLE_SCOPE aScope)
Create an empty library and adds it to the library table.
bool ClearSymbolModified(const wxString &aSymbolName, const wxString &aLibrary) const
Clear the modified flag for a symbol.
bool addLibrary(const wxString &aFilePath, bool aCreate, LIBRARY_TABLE_SCOPE aScope)
Helper function to add either existing or create new library.
bool AddLibrary(const wxString &aFilePath, LIBRARY_TABLE_SCOPE aScope)
Add an existing library.
SCH_SCREEN * GetScreen(const wxString &aSymbolName, const wxString &aLibrary)
Return the screen used to edit a specific symbol.
bool SymbolNameInUse(const wxString &aName, const wxString &aLibrary)
Return true if the symbol name is already in use in the specified library.
bool IsLibraryModified(const wxString &aLibrary) const
Return true if library has unsaved modifications.
LIB_SYMBOL * GetSymbol(const wxString &aSymbolName, const wxString &aLibrary) const
Return either an alias of a working LIB_SYMBOL copy, or alias of the original symbol if there is no w...
bool RemoveSymbol(const wxString &aSymbolName, const wxString &aLibrary)
Remove the symbol from the symbol buffer.
wxArrayString GetLibraryNames() const
Return the array of library names.
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Return an existing library buffer or creates one to using symbol library table to get the original da...
wxString GetUniqueLibraryName() const
Return a library name that is not currently in use.
static wxString getLibraryName(const wxString &aFilePath)
Extract library name basing on the file name.
bool UpdateSymbolAfterRename(LIB_SYMBOL *aSymbol, const wxString &aOldSymbolName, const wxString &aLibrary)
Update the symbol buffer with a new version of the symbol when the name has changed.
bool IsSymbolModified(const wxString &aSymbolName, const wxString &aLibrary) const
Return true if symbol has unsaved modifications.
virtual void OnDataChanged() const
bool IsLibraryLoaded(const wxString &aLibrary) const
Return true if the library was successfully loaded.
void SetSymbolModified(const wxString &aSymbolName, const wxString &aLibrary)
bool RevertLibrary(const wxString &aLibrary)
Revert unsaved changes for a symbol library.
LIB_ID RevertSymbol(const wxString &aSymbolName, const wxString &aLibrary)
Revert unsaved changes for a symbol.
void GetSymbolNames(const wxString &aLibName, wxArrayString &aSymbolNames, SYMBOL_NAME_FILTER aFilter=SYMBOL_NAME_FILTER::ALL)
std::set< LIB_SYMBOL * > getOriginalSymbols(const wxString &aLibrary)
Return a set of LIB_SYMBOL objects belonging to the original library.
int GetLibraryHash(const wxString &aLibrary) const
Return a library hash value to determine if it has changed.
size_t GetDerivedSymbolNames(const wxString &aSymbolName, const wxString &aLibraryName, wxArrayString &aList)
Fetch all of the symbols derived from a aSymbolName into aList.
bool RevertAll()
Revert all pending changes.
SYMBOL_LIBRARY_MANAGER(SCH_BASE_FRAME &aFrame)
bool SymbolExists(const wxString &aSymbolName, const wxString &aLibrary) const
Return true if symbol with a specific alias exists in library (either original one or buffered).
SCH_BASE_FRAME & m_frame
Parent frame.
std::list< LIB_SYMBOL * > EnumerateSymbols(const wxString &aLibrary) const
bool UpdateSymbol(LIB_SYMBOL *aSymbol, const wxString &aLibrary)
Update the symbol buffer with a new version of the symbol.
bool SaveLibrary(const wxString &aLibrary, const wxString &aFileName, SCH_IO_MGR::SCH_FILE_T aFileType=SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY)
Save library to a file, including unsaved changes.
std::map< wxString, LIB_BUFFER > m_libs
The library buffers.
bool UpdateLibraryBuffer(const wxString &aLibrary)
Update the library buffer with a new version of the library.
LIBRARY_TABLE_SCOPE