KiCad PCB EDA Suite
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 (C) 2019-2022 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, you may find one here:
21 * https://www.gnu.org/licenses/gpl-3.0.html
22 * or you may search the http://www.gnu.org website for the version 3 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#ifndef SYMBOL_LIBRARY_MANAGER_H
28#define SYMBOL_LIBRARY_MANAGER_H
29
30#include <map>
31#include <list>
32#include <deque>
33#include <set>
34#include <memory>
35#include <wx/arrstr.h>
36#include <sch_io_mgr.h>
37#include <sch_screen.h>
38
39class LIB_SYMBOL;
40class SYMBOL_LIB;
42class SCH_PLUGIN;
43class SCH_BASE_FRAME;
46class LIB_LOGGER;
47
48
53{
54public:
57
63 void Preload( PROGRESS_REPORTER& aReporter );
64
65 int GetHash() const;
66
67 bool HasModifications() const;
68
76 int GetLibraryHash( const wxString& aLibrary ) const;
77
81 wxArrayString GetLibraryNames() const;
82
86 SYMBOL_LIB_TABLE_ROW* GetLibrary( const wxString& aLibrary ) const;
87
88 std::list<LIB_SYMBOL*> GetAliases( const wxString& aLibrary ) const;
89
93 bool CreateLibrary( const wxString& aFilePath, SYMBOL_LIB_TABLE* aTable )
94 {
95 return addLibrary( aFilePath, true, aTable );
96 }
97
101 bool AddLibrary( const wxString& aFilePath, SYMBOL_LIB_TABLE* aTable )
102 {
103 return addLibrary( aFilePath, false, aTable );
104 }
105
111 bool UpdateSymbol( LIB_SYMBOL* aSymbol, const wxString& aLibrary );
112
117 bool UpdateSymbolAfterRename( LIB_SYMBOL* aSymbol, const wxString& oldAlias,
118 const wxString& aLibrary );
119
124 bool RemoveSymbol( const wxString& aName, const wxString& aLibrary );
125
130 LIB_SYMBOL* GetAlias( const wxString& aAlias, const wxString& aLibrary ) const;
131
136 LIB_SYMBOL* GetBufferedSymbol( const wxString& aAlias, const wxString& aLibrary );
137
142 SCH_SCREEN* GetScreen( const wxString& aAlias, const wxString& aLibrary );
143
148 bool SymbolExists( const wxString& aAlias, const wxString& aLibrary ) const;
149
154 bool LibraryExists( const wxString& aLibrary, bool aCheckEnabled = false ) const;
155
159 bool IsLibraryLoaded( const wxString& aLibrary ) const;
160
164 bool IsLibraryModified( const wxString& aLibrary ) const;
165
169 bool IsSymbolModified( const wxString& aAlias, const wxString& aLibrary ) const;
170
171 void SetSymbolModified( const wxString& aAlias, const wxString& aLibrary );
172
176 bool ClearLibraryModified( const wxString& aLibrary ) const;
177
181 bool ClearSymbolModified( const wxString& aAlias, const wxString& aLibrary ) const;
182
188 bool IsLibraryReadOnly( const wxString& aLibrary ) const;
189
196 bool FlushSymbol( const wxString& aAlias, const wxString& aLibrary );
197
205 bool SaveLibrary( const wxString& aLibrary, const wxString& aFileName,
206 SCH_IO_MGR::SCH_FILE_T aFileType = SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY );
207
214 LIB_ID RevertSymbol( const wxString& aAlias, const wxString& aLibrary );
215
221 bool RevertLibrary( const wxString& aLibrary );
222
228 bool RevertAll();
229
234 wxString GetUniqueLibraryName() const;
235
236 void GetRootSymbolNames( const wxString& aLibName, wxArrayString& aRootSymbolNames );
237
244 bool HasDerivedSymbols( const wxString& aSymbolName, const wxString& aLibraryName );
245
246 size_t GetLibraryCount() const;
247
248protected:
249 virtual void OnDataChanged() const {}
250
252 static wxString getLibraryName( const wxString& aFilePath );
253
255 bool addLibrary( const wxString& aFilePath, bool aCreate, SYMBOL_LIB_TABLE* aTable );
256
257
259 SYMBOL_LIB_TABLE* symTable() const;
260
263 {
264 public:
265 SYMBOL_BUFFER( LIB_SYMBOL* aSymbol = nullptr,
266 std::unique_ptr<SCH_SCREEN> aScreen = nullptr );
268
269 LIB_SYMBOL* GetSymbol() const { return m_symbol; }
270 void SetSymbol( LIB_SYMBOL* aSymbol );
271
272 LIB_SYMBOL* GetOriginal() const { return m_original; }
273 void SetOriginal( LIB_SYMBOL* aSymbol );
274
275 bool IsModified() const;
276 SCH_SCREEN* GetScreen() const { return m_screen.get(); }
277
279 std::unique_ptr<SCH_SCREEN> RemoveScreen()
280 {
281 return std::move( m_screen );
282 }
283
284 bool SetScreen( std::unique_ptr<SCH_SCREEN> aScreen )
285 {
286 bool ret = !!m_screen;
287 m_screen = std::move( aScreen );
288 return ret;
289 }
290
291 private:
292 std::unique_ptr<SCH_SCREEN> m_screen;
293
294 LIB_SYMBOL* m_symbol; // Working copy
295 LIB_SYMBOL* m_original; // Initial state of the symbol
296 };
297
298
301 {
302 public:
303 LIB_BUFFER( const wxString& aLibrary ) :
304 m_libName( aLibrary ),
305 m_hash( 1 )
306 { }
307
308 bool IsModified() const
309 {
310 if( !m_deleted.empty() )
311 return true;
312
313 for( const std::shared_ptr<SYMBOL_BUFFER>& symbolBuf : m_symbols )
314 {
315 if( symbolBuf->IsModified() )
316 return true;
317 }
318
319 return false;
320 }
321
322 int GetHash() const { return m_hash; }
323
325 LIB_SYMBOL* GetSymbol( const wxString& aAlias ) const;
326
328 bool CreateBuffer( LIB_SYMBOL* aCopy, SCH_SCREEN* aScreen );
329
331 bool UpdateBuffer( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf, LIB_SYMBOL* aCopy );
332
333 bool DeleteBuffer( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf );
334
336 {
337 m_deleted.clear();
338 }
339
342 bool SaveBuffer( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf, SYMBOL_LIB_TABLE* aLibTable );
343
346 bool SaveBuffer( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf, const wxString& aFileName,
347 SCH_PLUGIN* aPlugin, bool aBuffer );
348
350 std::shared_ptr<SYMBOL_BUFFER> GetBuffer( const wxString& aAlias ) const;
351
353 const std::deque< std::shared_ptr<SYMBOL_BUFFER> >& GetBuffers() const { return m_symbols; }
354
362 bool HasDerivedSymbols( const wxString& aParentName ) const;
363
369 void GetRootSymbolNames( wxArrayString& aRootSymbolNames );
370
379 size_t GetDerivedSymbolNames( const wxString& aSymbolName, wxArrayString& aList );
380
381 private:
388 int removeChildSymbols( std::shared_ptr<SYMBOL_BUFFER> aSymbolBuf );
389
390 std::deque< std::shared_ptr<SYMBOL_BUFFER> > m_symbols;
391
393 std::deque< std::shared_ptr<SYMBOL_BUFFER> > m_deleted;
394 const wxString m_libName; // Buffered library name
396 };
397
401 std::set<LIB_SYMBOL*> getOriginalSymbols( const wxString& aLibrary );
402
407 LIB_BUFFER& getLibraryBuffer( const wxString& aLibrary );
408
410 std::map<wxString, LIB_BUFFER> m_libs;
411
414};
415
416#endif /* SYMBOL_LIBRARY_MANAGER_H */
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
Define a library symbol object.
Definition: lib_symbol.h:99
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_mgr.h:155
std::deque< std::shared_ptr< SYMBOL_BUFFER > > m_deleted
LIB_SYMBOL * GetSymbol(const wxString &aAlias) const
Create a new buffer to store a symbol. LIB_BUFFER takes ownership of aCopy.
void ClearDeletedBuffer()
Save stored modifications to Symbol Lib Table.
bool HasDerivedSymbols(const wxString &aParentName) const
Check to see any symbols in the buffer are derived from a parent named aParentName.
std::deque< std::shared_ptr< SYMBOL_BUFFER > > m_symbols
Buffer for deleted symbols until library is saved.
bool SaveBuffer(std::shared_ptr< SYMBOL_BUFFER > aSymbolBuf, SYMBOL_LIB_TABLE *aLibTable)
Save stored modifications using a plugin.
void GetRootSymbolNames(wxArrayString &aRootSymbolNames)
Fetch a list of root symbols names from the library buffer.
const std::deque< std::shared_ptr< SYMBOL_BUFFER > > & GetBuffers() const
int GetHash() const
Return the working copy of a LIB_SYMBOL root object with specified alias.
int removeChildSymbols(std::shared_ptr< SYMBOL_BUFFER > aSymbolBuf)
Remove all symbols derived from aParent from the library buffer.
std::shared_ptr< SYMBOL_BUFFER > GetBuffer(const wxString &aAlias) const
Return all buffered symbols.
bool UpdateBuffer(std::shared_ptr< SYMBOL_BUFFER > aSymbolBuf, LIB_SYMBOL *aCopy)
bool DeleteBuffer(std::shared_ptr< SYMBOL_BUFFER > aSymbolBuf)
bool CreateBuffer(LIB_SYMBOL *aCopy, SCH_SCREEN *aScreen)
Update the buffered symbol with the contents of aCopy.
size_t GetDerivedSymbolNames(const wxString &aSymbolName, wxArrayString &aList)
Fetch all of the symbols derived from a aSymbolName into aList.
Store a working copy of a library.
bool SetScreen(std::unique_ptr< SCH_SCREEN > aScreen)
SYMBOL_BUFFER(LIB_SYMBOL *aSymbol=nullptr, std::unique_ptr< SCH_SCREEN > aScreen=nullptr)
std::unique_ptr< SCH_SCREEN > RemoveScreen()
SCH_SCREEN * GetScreen() const
Transfer the screen ownership.
std::unique_ptr< SCH_SCREEN > m_screen
Class to handle modifications to the symbol libraries.
LIB_SYMBOL * GetBufferedSymbol(const wxString &aAlias, const wxString &aLibrary)
Return the symbol copy from the buffer.
bool UpdateSymbolAfterRename(LIB_SYMBOL *aSymbol, const wxString &oldAlias, const wxString &aLibrary)
Update the symbol buffer with a new version of the symbol when the name has changed.
bool IsLibraryReadOnly(const wxString &aLibrary) const
Return true if the library is stored in a read-only file.
bool addLibrary(const wxString &aFilePath, bool aCreate, SYMBOL_LIB_TABLE *aTable)
Return the current Symbol Library Table.
bool ClearSymbolModified(const wxString &aAlias, const wxString &aLibrary) const
Clear the modified flag for a symbol.
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 * GetAlias(const wxString &aAlias, 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...
SCH_SCREEN * GetScreen(const wxString &aAlias, const wxString &aLibrary)
Return the screen used to edit a specific symbol.
bool IsLibraryModified(const wxString &aLibrary) const
Return true if library has unsaved modifications.
wxArrayString GetLibraryNames() const
Return the array of library names.
void SetSymbolModified(const wxString &aAlias, const wxString &aLibrary)
void GetRootSymbolNames(const wxString &aLibName, wxArrayString &aRootSymbolNames)
bool SymbolExists(const wxString &aAlias, const wxString &aLibrary) const
Return true if symbol with a specific alias exists in library (either original one or buffered).
wxString GetUniqueLibraryName() const
Return a library name that is not currently in use.
bool RemoveSymbol(const wxString &aName, const wxString &aLibrary)
Remove the symbol from the symbol buffer.
static wxString getLibraryName(const wxString &aFilePath)
Helper function to add either existing or create new library.
bool AddLibrary(const wxString &aFilePath, SYMBOL_LIB_TABLE *aTable)
Add an existing library.
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Return an existing library buffer or creates one to using Symbol Library Table to get the original da...
virtual void OnDataChanged() const
Extract library name basing on the file name.
bool CreateLibrary(const wxString &aFilePath, SYMBOL_LIB_TABLE *aTable)
Create an empty library and adds it to the library table.
bool IsSymbolModified(const wxString &aAlias, const wxString &aLibrary) const
Return true if symbol has unsaved modifications.
bool IsLibraryLoaded(const wxString &aLibrary) const
Return true if the library was successfully loaded.
SYMBOL_LIB_TABLE * symTable() const
Class to store a working copy of a LIB_SYMBOL object and editor context.
bool RevertLibrary(const wxString &aLibrary)
Revert unsaved changes for a symbolicular library.
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.
void Preload(PROGRESS_REPORTER &aReporter)
Preloads all symbol libraries in the symbol library table using SYMBOL_ASYNC_LOADER.
bool RevertAll()
Revert all pending changes.
SYMBOL_LIBRARY_MANAGER(SCH_BASE_FRAME &aFrame)
LIB_ID RevertSymbol(const wxString &aAlias, const wxString &aLibrary)
Revert unsaved changes for a symbolicular symbol.
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Find a single library within the (aggregate) library table.
SCH_BASE_FRAME & m_frame
Parent frame.
std::list< LIB_SYMBOL * > GetAliases(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.
bool HasDerivedSymbols(const wxString &aSymbolName, const wxString &aLibraryName)
Check if symbol aSymbolName in library aLibraryName is a root symbol that has derived symbols.
std::map< wxString, LIB_BUFFER > m_libs
bool FlushSymbol(const wxString &aAlias, const wxString &aLibrary)
Save symbol changes to the library copy used by the schematic editor.
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
Object used to load, save, search, and otherwise manipulate symbol library files.