KiCad PCB EDA Suite
Loading...
Searching...
No Matches
symbol_library_manager.cpp
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 The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
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, see <https://www.gnu.org/licenses/>.
20 */
21
23
25#include <symbol_edit_frame.h>
26#include <env_paths.h>
27#include <pgm_base.h>
28#include <project_sch.h>
29#include <kiway.h>
30#include <core/profile.h>
31#include <wx_filename.h>
33#include <progress_reporter.h>
34#include <list>
35#include <locale_io.h>
36#include <confirm.h>
37#include <string_utils.h>
39#include <kiplatform/io.h>
42
43#include "lib_logger.h"
44
45
51
52
57
58
60{
61 for( const auto& [name, buffer] : m_libs )
62 {
63 if( buffer.IsModified() )
64 return true;
65 }
66
67 return false;
68}
69
70
72{
74 return adapter->GetModifyHash();
75}
76
77
78int SYMBOL_LIBRARY_MANAGER::GetLibraryHash( const wxString& aLibrary ) const
79{
80 if( const auto libBufIt = m_libs.find( aLibrary ); libBufIt != m_libs.end() )
81 return libBufIt->second.GetHash();
82
84
85 if( auto uri = manager.GetFullURI( LIBRARY_TABLE_TYPE::SYMBOL, aLibrary, true ); uri )
86 {
87 // Mix a file modification timestamp into the hash so that external changes (e.g. a git
88 // branch switch) are detected by the library tree synchronizer without a restart.
89 wxFileName fn( *uri );
90 long long mtime = 0;
91
92 wxLogNull silence;
93
94 fn.Normalize( FN_NORMALIZE_FLAGS );
95
96 if( fn.DirExists() )
97 mtime = KIPLATFORM::IO::TimestampDir( fn.GetFullPath(),
98 wxS( "*." ) + wxString( FILEEXT::KiCadSymbolLibFileExtension ) );
99 else if( fn.IsFileReadable() )
100 mtime = fn.GetModificationTime().GetValue().GetValue();
101
102 size_t base = std::hash<std::string>{}( aLibrary.ToStdString() + uri->ToStdString() );
103 return static_cast<int>( base ^ static_cast<size_t>( mtime ) );
104 }
105
106 return -1;
107}
108
109
111{
112 wxArrayString res;
113
115
116 for( const LIBRARY_TABLE_ROW* row : manager.Rows( LIBRARY_TABLE_TYPE::SYMBOL ) )
117 {
118 // Database libraries are hidden from the symbol editor at the moment
119 if( row->Type() == SCH_IO_MGR::ShowType( SCH_IO_MGR::SCH_DATABASE ) )
120 continue;
121
122 res.Add( row->Nickname() );
123 }
124
125 return res;
126}
127
128
129bool SYMBOL_LIBRARY_MANAGER::SaveLibrary( const wxString& aLibrary, const wxString& aFileName,
130 SCH_IO_MGR::SCH_FILE_T aFileType )
131{
132 wxCHECK( aFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY, false );
133
134 wxFileName fn( aFileName );
135
136 if( fn.FileExists() && !fn.IsFileWritable() )
137 return false;
138
139 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( aFileType ) );
140 bool res = true; // assume all libraries are successfully saved
141 std::map<std::string, UTF8> properties;
142
143 properties.emplace( SCH_IO_KICAD_LEGACY::PropBuffering, "" );
144
145 auto it = m_libs.find( aLibrary );
146
147 if( it != m_libs.end() )
148 {
149 // Handle buffered library
150 LIB_BUFFER& libBuf = it->second;
151
152 const auto& symbolBuffers = libBuf.GetBuffers();
153
154 for( const std::shared_ptr<SYMBOL_BUFFER>& symbolBuf : symbolBuffers )
155 {
156 wxCHECK2( symbolBuf, continue );
157
158 if( !libBuf.SaveBuffer( *symbolBuf, aFileName, &*pi, true ) )
159 {
160 // Something went wrong, but try to save other libraries
161 res = false;
162 }
163 }
164
165 // clear the deleted symbols buffer only if data is saved to the original file
166 wxFileName original, destination( aFileName );
168
169 if( auto uri = manager.GetFullURI( LIBRARY_TABLE_TYPE::SYMBOL, aLibrary, true ); uri )
170 {
171 original = *uri;
172 original.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
173 }
174
175 destination.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
176
177 if( res && original == destination )
178 {
179 // Delete symbols that were removed from the buffer before clearing the deleted list
180 for( const std::shared_ptr<SYMBOL_BUFFER>& deletedBuf : libBuf.GetDeletedBuffers() )
181 {
182 wxCHECK2( deletedBuf, continue );
183
184 const wxString& originalName = deletedBuf->GetOriginal().GetName();
185
186 try
187 {
188 if( pi->LoadSymbol( aFileName, originalName ) )
189 pi->DeleteSymbol( aFileName, originalName, &properties );
190 }
191 catch( const IO_ERROR& ioe )
192 {
193 wxLogError( _( "Error deleting symbol %s from library '%s'." ) + wxS( "\n%s" ),
194 UnescapeString( originalName ), aFileName, ioe.What() );
195 res = false;
196 }
197 }
198
199 libBuf.ClearDeletedBuffer();
200 }
201 }
202 else
203 {
204 // Handle original library
205 for( LIB_SYMBOL* symbol : getOriginalSymbols( aLibrary ) )
206 {
207 LIB_SYMBOL* newSymbol;
208
209 try
210 {
211 if( symbol->IsDerived() )
212 {
213 std::shared_ptr< LIB_SYMBOL > oldParent = symbol->GetParent().lock();
214
215 wxCHECK_MSG( oldParent, false,
216 wxString::Format( wxT( "Derived symbol '%s' found with undefined parent." ),
217 symbol->GetName() ) );
218
219 LIB_SYMBOL* libParent = pi->LoadSymbol( aFileName, oldParent->GetName(), &properties );
220
221 if( !libParent )
222 {
223 libParent = new LIB_SYMBOL( *oldParent );
224 pi->SaveSymbol( aFileName, libParent, &properties );
225 }
226 else
227 {
228 // Copy embedded files from the in-memory parent to the loaded parent
229 // This ensures that any embedded files added to the parent are preserved
230 //
231 // We do this manually rather than using the assignment operator to avoid
232 // potential ABI issues where the size of EMBEDDED_FILES differs between
233 // compilation units, potentially causing the assignment to overwrite
234 // members of LIB_SYMBOL (like m_me) that follow the EMBEDDED_FILES base.
235 libParent->ClearEmbeddedFiles();
236
237 for( const auto& [name, file] : oldParent->EmbeddedFileMap() )
238 libParent->AddFile( new EMBEDDED_FILES::EMBEDDED_FILE( *file ) );
239
240 libParent->SetAreFontsEmbedded( oldParent->GetAreFontsEmbedded() );
241 libParent->SetFileAddedCallback( oldParent->GetFileAddedCallback() );
242 }
243
244 newSymbol = new LIB_SYMBOL( *symbol );
245 newSymbol->SetParent( libParent );
246 pi->SaveSymbol( aFileName, newSymbol, &properties );
247 }
248 else if( !pi->LoadSymbol( aFileName, symbol->GetName(), &properties ) )
249 {
250 pi->SaveSymbol( aFileName, new LIB_SYMBOL( *symbol ), &properties );
251 }
252 }
253 catch( ... )
254 {
255 res = false;
256 break;
257 }
258 }
259 }
260
261 try
262 {
263 pi->SaveLibrary( aFileName );
264 }
265 catch( ... )
266 {
267 // return false because something happens.
268 // The library is not successfully saved
269 res = false;
270 }
271
272 return res;
273}
274
275
276bool SYMBOL_LIBRARY_MANAGER::IsLibraryModified( const wxString& aLibrary ) const
277{
278 auto it = m_libs.find( aLibrary );
279 return it != m_libs.end() ? it->second.IsModified() : false;
280}
281
282
283bool SYMBOL_LIBRARY_MANAGER::IsSymbolModified( const wxString& aSymbolName, const wxString& aLibrary ) const
284{
285 auto libIt = m_libs.find( aLibrary );
286
287 if( libIt == m_libs.end() )
288 return false;
289
290 const LIB_BUFFER& buf = libIt->second;
291 const std::shared_ptr<SYMBOL_BUFFER> symbolBuf = buf.GetBuffer( aSymbolName );
292
293 return symbolBuf ? symbolBuf->IsModified() : false;
294}
295
296
297void SYMBOL_LIBRARY_MANAGER::SetSymbolModified( const wxString& aSymbolName, const wxString& aLibrary )
298{
299 auto libIt = m_libs.find( aLibrary );
300
301 if( libIt == m_libs.end() )
302 return;
303
304 const LIB_BUFFER& buf = libIt->second;
305 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = buf.GetBuffer( aSymbolName );
306
307 wxCHECK( symbolBuf, /* void */ );
308
309 symbolBuf->GetScreen()->SetContentModified();
310}
311
312
313bool SYMBOL_LIBRARY_MANAGER::ClearLibraryModified( const wxString& aLibrary ) const
314{
315 auto libIt = m_libs.find( aLibrary );
316
317 if( libIt == m_libs.end() )
318 return false;
319
320 for( auto& symbolBuf : libIt->second.GetBuffers() )
321 {
322 SCH_SCREEN* screen = symbolBuf->GetScreen();
323
324 if( screen )
325 screen->SetContentModified( false );
326 }
327
328 return true;
329}
330
331
332bool SYMBOL_LIBRARY_MANAGER::ClearSymbolModified( const wxString& aSymbolName, const wxString& aLibrary ) const
333{
334 auto libIt = m_libs.find( aLibrary );
335
336 if( libIt == m_libs.end() )
337 return false;
338
339 auto symbolBuf = libIt->second.GetBuffer( aSymbolName );
340 wxCHECK( symbolBuf, false );
341
342 symbolBuf->GetScreen()->SetContentModified( false );
343 return true;
344}
345
346
347bool SYMBOL_LIBRARY_MANAGER::IsLibraryReadOnly( const wxString& aLibrary ) const
348{
350 return !adapter->IsSymbolLibWritable( aLibrary );
351}
352
353
354bool SYMBOL_LIBRARY_MANAGER::IsLibraryLoaded( const wxString& aLibrary ) const
355{
357 return adapter->IsLibraryLoaded( aLibrary );
358}
359
360
361std::list<LIB_SYMBOL*> SYMBOL_LIBRARY_MANAGER::EnumerateSymbols( const wxString& aLibrary ) const
362{
363 std::list<LIB_SYMBOL*> ret;
364 auto libIt = m_libs.find( aLibrary );
365
366 if( libIt != m_libs.end() )
367 {
368 for( const std::shared_ptr<SYMBOL_BUFFER>& symbolBuf : libIt->second.GetBuffers() )
369 ret.push_back( &symbolBuf->GetSymbol() );
370 }
371 else
372 {
374 std::vector<LIB_SYMBOL*> symbols = adapter->GetSymbols( aLibrary );
375
376 std::copy( symbols.begin(), symbols.end(), std::back_inserter( ret ) );
377 }
378
379 return ret;
380}
381
382
383LIB_SYMBOL* SYMBOL_LIBRARY_MANAGER::GetBufferedSymbol( const wxString& aSymbolName, const wxString& aLibrary )
384{
385 // try the library buffers first
386 LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
387 LIB_SYMBOL* bufferedSymbol = libBuf.GetSymbol( aSymbolName );
388
389 if( !bufferedSymbol ) // no buffer symbol found
390 {
392
393 // create a copy of the symbol
394 try
395 {
396 LIB_SYMBOL* symbol = adapter->LoadSymbol( aLibrary, aSymbolName );
397
398 if( symbol == nullptr )
399 THROW_IO_ERROR( _( "Symbol not found." ) );
400
401 LIB_SYMBOL* bufferedParent = nullptr;
402
403 // Create parent symbols on demand so parent symbol can be set.
404 if( symbol->IsDerived() )
405 {
406 std::shared_ptr<LIB_SYMBOL> parent = symbol->GetParent().lock();
407 wxCHECK_MSG( parent, nullptr, wxString::Format( "Derived symbol '%s' found with undefined parent.",
408 symbol->GetName() ) );
409
410 // Check if the parent symbol buffer has already be created.
411 bufferedParent = libBuf.GetSymbol( parent->GetName() );
412
413 if( !bufferedParent )
414 {
415 std::unique_ptr<LIB_SYMBOL> newParent = std::make_unique<LIB_SYMBOL>( *parent );
416 bufferedParent = newParent.get();
417 libBuf.CreateBuffer( std::move( newParent ), std::make_unique<SCH_SCREEN>() );
418 }
419 }
420
421 std::unique_ptr<LIB_SYMBOL> newSymbol = std::make_unique<LIB_SYMBOL>( *symbol );
422 bufferedSymbol = newSymbol.get();
423
424 if( bufferedParent )
425 newSymbol->SetParent( bufferedParent );
426
427 libBuf.CreateBuffer( std::move( newSymbol ), std::make_unique<SCH_SCREEN>() );
428 }
429 catch( const IO_ERROR& e )
430 {
431 wxString msg;
432
433 msg.Printf( _( "Error loading symbol %s from library '%s'." ),
434 aSymbolName,
435 aLibrary );
436 DisplayErrorMessage( &m_frame, msg, e.What() );
437 bufferedSymbol = nullptr;
438 }
439 }
440
441 return bufferedSymbol;
442}
443
444
445SCH_SCREEN* SYMBOL_LIBRARY_MANAGER::GetScreen( const wxString& aSymbolName, const wxString& aLibrary )
446{
447 auto it = m_libs.find( aLibrary );
448 wxCHECK( it != m_libs.end(), nullptr );
449
450 LIB_BUFFER& buf = it->second;
451 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = buf.GetBuffer( aSymbolName );
452
453 return symbolBuf ? symbolBuf->GetScreen() : nullptr;
454}
455
456
458 const wxString& aLibrary )
459{
460 if( !LibraryExists( aLibrary ) )
461 return nullptr;
462
463 LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
464
465 // Buffers live as long as the manager, so the raw pointer dangles only if a caller outlives it.
466 return libBuf.GetBuffer( aSymbolName ).get();
467}
468
469
470bool SYMBOL_LIBRARY_MANAGER::UpdateSymbol( LIB_SYMBOL* aSymbol, const wxString& aLibrary )
471{
472 LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
473 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = libBuf.GetBuffer( aSymbol->GetName() );
474
475 if( symbolBuf ) // Existing symbol.
476 {
477 LIB_SYMBOL& bufferedSymbol = symbolBuf->GetSymbol();
478
479 // If we are coming from a different library, the library ID needs to be preserved
480 const LIB_ID libId = bufferedSymbol.GetLibId();
481 bufferedSymbol = *aSymbol;
482 bufferedSymbol.SetLibId( libId );
483
484 symbolBuf->GetScreen()->SetContentModified();
485 }
486 else // New symbol
487 {
488 std::unique_ptr<LIB_SYMBOL> symbolCopy = std::make_unique<LIB_SYMBOL>( *aSymbol, nullptr );
489
490 symbolCopy->SetLibId( LIB_ID( aLibrary, aSymbol->GetLibId().GetLibItemName() ) );
491
492 auto newScreen = std::make_unique<SCH_SCREEN>();
493 SCH_SCREEN& screen = *newScreen;
494 libBuf.CreateBuffer( std::move( symbolCopy ), std::move( newScreen ) );
495 screen.SetContentModified();
496 }
497
498 return true;
499}
500
501
502bool SYMBOL_LIBRARY_MANAGER::UpdateSymbolAfterRename( LIB_SYMBOL* aSymbol, const wxString& aOldName,
503 const wxString& aLibrary )
504{
505 LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
506 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = libBuf.GetBuffer( aOldName );
507
508 wxCHECK( symbolBuf && aSymbol, false );
509
510 libBuf.UpdateBuffer( *symbolBuf, *aSymbol );
512
513 return true;
514}
515
516
517LIB_ID SYMBOL_LIBRARY_MANAGER::RevertSymbol( const wxString& aSymbolName, const wxString& aLibrary )
518{
519 auto it = m_libs.find( aLibrary );
520
521 if( it == m_libs.end() ) // no items to flush
522 return LIB_ID( aLibrary, aSymbolName );
523
524 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = it->second.GetBuffer( aSymbolName );
525 wxCHECK( symbolBuf, LIB_ID( aLibrary, aSymbolName ) );
526 LIB_SYMBOL original( symbolBuf->GetOriginal() );
527
528 if( original.GetName() != aSymbolName )
529 {
530 UpdateSymbolAfterRename( &original, aSymbolName, aLibrary );
531 }
532 else
533 {
534 // copy the initial data to the current symbol to restore
535 symbolBuf->GetSymbol() = original;
537 }
538
539 return LIB_ID( aLibrary, original.GetName() );
540}
541
542
543bool SYMBOL_LIBRARY_MANAGER::RevertLibrary( const wxString& aLibrary )
544{
545 auto it = m_libs.find( aLibrary );
546
547 if( it == m_libs.end() ) // nothing to reverse
548 return false;
549
550 m_libs.erase( it );
552
553 return true;
554}
555
556
558{
559 bool retv = true;
560
561 // Nothing to revert.
562 if( GetHash() == 0 )
563 return true;
564
565 for( const auto& lib : m_libs )
566 {
567 if( !lib.second.IsModified() )
568 continue;
569
570 for( const std::shared_ptr<SYMBOL_BUFFER>& buffer : lib.second.GetBuffers() )
571 {
572 if( !buffer->IsModified() )
573 continue;
574
575 RevertSymbol( lib.first, buffer->GetOriginal().GetName() );
576 }
577 }
578
579 return retv;
580}
581
582
583bool SYMBOL_LIBRARY_MANAGER::RemoveSymbol( const wxString& aSymbolName, const wxString& aLibrary )
584{
585 LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
586 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = libBuf.GetBuffer( aSymbolName );
587 wxCHECK( symbolBuf, false );
588
589 bool retv = true;
590
591 retv &= libBuf.DeleteBuffer( *symbolBuf );
593
594 return retv;
595}
596
597
598LIB_SYMBOL* SYMBOL_LIBRARY_MANAGER::GetSymbol( const wxString& aSymbolName, const wxString& aLibrary ) const
599{
600 // Try the library buffers first
601 auto libIt = m_libs.find( aLibrary );
602
603 if( libIt != m_libs.end() )
604 {
605 LIB_SYMBOL* symbol = libIt->second.GetSymbol( aSymbolName );
606
607 if( symbol )
608 return symbol;
609 }
610
611 // Get the original symbol
613 LIB_SYMBOL* symbol = nullptr;
614
615 try
616 {
617 symbol = adapter->LoadSymbol( aLibrary, aSymbolName );
618 }
619 catch( const IO_ERROR& e )
620 {
621 wxString msg;
622
623 msg.Printf( _( "Cannot load symbol '%s' from library '%s'." ),
624 aSymbolName,
625 aLibrary );
626 DisplayErrorMessage( &m_frame, msg, e.What() );
627 }
628
629 return symbol;
630}
631
632
633bool SYMBOL_LIBRARY_MANAGER::SymbolExists( const wxString& aSymbolName,
634 const wxString& aLibrary ) const
635{
636 auto libBufIt = m_libs.find( aLibrary );
637 LIB_SYMBOL* symbol = nullptr;
638
639 if( libBufIt != m_libs.end() )
640 return libBufIt->second.GetBuffer( aSymbolName ) != nullptr;
641
643
644 try
645 {
646 symbol = adapter->LoadSymbol( aLibrary, aSymbolName );
647 }
648 catch( IO_ERROR& )
649 {
650 // checking if certain symbol exists, so its absence is perfectly fine
651 }
652
653 return symbol != nullptr;
654}
655
656
657bool SYMBOL_LIBRARY_MANAGER::SymbolNameInUse( const wxString& aName, const wxString& aLibrary )
658{
659 wxArrayString existing;
660
661 // NB: GetSymbolNames() is mostly used for GUI stuff, so it returns unescaped names
662 GetSymbolNames( aLibrary, existing );
663
664 wxString unescapedName = UnescapeString( aName );
665
666 for( wxString& candidate : existing )
667 {
668 if( candidate.CmpNoCase( unescapedName ) == 0 )
669 return true;
670 }
671
672 return false;
673}
674
675
676bool SYMBOL_LIBRARY_MANAGER::LibraryExists( const wxString& aLibrary, bool aCheckEnabled ) const
677{
678 if( aLibrary.IsEmpty() )
679 return false;
680
681 if( m_libs.count( aLibrary ) > 0 )
682 return true;
683
685
686 return adapter->HasLibrary( aLibrary, aCheckEnabled );
687}
688
689
691{
692 wxString name = "New_Library";
693
694 if( !LibraryExists( name ) )
695 return name;
696
697 name += "_";
698
699 for( unsigned int i = 0; i < std::numeric_limits<unsigned int>::max(); ++i )
700 {
701 if( !LibraryExists( name + wxString::Format( "%u", i ) ) )
702 return name + wxString::Format( "%u", i );
703 }
704
705 wxFAIL;
706 return wxEmptyString;
707}
708
709
710void SYMBOL_LIBRARY_MANAGER::GetSymbolNames( const wxString& aLibraryName, wxArrayString& aSymbolNames,
711 SYMBOL_NAME_FILTER aFilter )
712{
713 LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
714
715 libBuf.GetSymbolNames( aSymbolNames, aFilter );
716}
717
718
719size_t SYMBOL_LIBRARY_MANAGER::GetDerivedSymbolNames( const wxString& aSymbolName, const wxString& aLibraryName,
720 wxArrayString& aList )
721{
722 LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
723
724 return libBuf.GetDerivedSymbolNames( aSymbolName, aList );
725}
726
727
729{
731 return adapter->GetLibraryNames().size();
732}
733
734
735wxString SYMBOL_LIBRARY_MANAGER::getLibraryName( const wxString& aFilePath )
736{
737 wxFileName fn( aFilePath );
738 return fn.GetName();
739}
740
741
742bool SYMBOL_LIBRARY_MANAGER::addLibrary( const wxString& aFilePath, bool aCreate, LIBRARY_TABLE_SCOPE aScope )
743{
744 wxString libName = getLibraryName( aFilePath );
745 wxCHECK( !LibraryExists( libName ), false ); // either create or add an existing one
746
747 // try to use path normalized to an environmental variable or project path
748 wxString relPath = NormalizePath( aFilePath, &Pgm().GetLocalEnvVariables(), &m_frame.Prj() );
749
750 SCH_IO_MGR::SCH_FILE_T schFileType = SCH_IO_MGR::GuessPluginTypeFromLibPath( aFilePath, aCreate ? KICTL_CREATE
751 : 0 );
752
753 if( schFileType == SCH_IO_MGR::SCH_FILE_UNKNOWN )
754 schFileType = SCH_IO_MGR::SCH_LEGACY;
755
758 std::optional<LIBRARY_TABLE*> optTable = manager.Table( LIBRARY_TABLE_TYPE::SYMBOL, aScope );
759 wxCHECK( optTable, false );
760 LIBRARY_TABLE* table = optTable.value();
761 bool success = true;
762
763 try
764 {
765 LIBRARY_TABLE_ROW& row = table->InsertRow();
766
767 row.SetNickname( libName );
768 row.SetURI( relPath );
769 row.SetType( SCH_IO_MGR::ShowType( schFileType ) );
770
771 table->Save().map_error(
772 [&]( const LIBRARY_ERROR& aError )
773 {
774 wxMessageBox( _( "Error saving library table:\n\n" ) + aError.message,
775 _( "File Save Error" ), wxOK | wxICON_ERROR );
776 success = false;
777 } );
778 }
779 catch( const IO_ERROR& ioe )
780 {
781 DisplayError( nullptr, ioe.What() );
782 return false;
783 }
784
785 if( success )
786 {
787 if( aCreate )
788 {
789 wxCHECK( schFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY, false );
790
791 if( !adapter->CreateLibrary( libName ) )
792 {
793 table->Rows().erase( table->Rows().end() - 1 );
794 return false;
795 }
796 }
797
798 adapter->LoadOne( libName );
800 }
801
802 return success;
803}
804
805
806std::set<LIB_SYMBOL*> SYMBOL_LIBRARY_MANAGER::getOriginalSymbols( const wxString& aLibrary )
807{
808 std::set<LIB_SYMBOL*> symbols;
809 wxCHECK( LibraryExists( aLibrary ), symbols );
810
812
813 for( LIB_SYMBOL* symbol : adapter->GetSymbols( aLibrary ) )
814 symbols.insert( symbol );
815
816 return symbols;
817}
818
819
821{
822 auto it = m_libs.find( aLibrary );
823
824 if( it != m_libs.end() )
825 return it->second;
826
827 // The requested buffer does not exist yet, so create one
828 auto ret = m_libs.emplace( aLibrary, LIB_BUFFER( aLibrary ) );
829 LIB_BUFFER& buf = ret.first->second;
830
831 for( LIB_SYMBOL* symbol : getOriginalSymbols( aLibrary ) )
832 {
833 if( symbol->IsDerived() )
834 {
835 std::shared_ptr<LIB_SYMBOL> oldParent = symbol->GetParent().lock();
836
837 wxCHECK_MSG( oldParent, buf, wxString::Format( "Derived symbol '%s' found with undefined parent.",
838 symbol->GetName() ) );
839
840 LIB_SYMBOL* libParent = buf.GetSymbol( oldParent->GetName() );
841
842 if( !libParent )
843 {
844 std::unique_ptr<LIB_SYMBOL> newParent = std::make_unique<LIB_SYMBOL>( *oldParent.get() );
845 libParent = newParent.get();
846 buf.CreateBuffer( std::move( newParent ), std::make_unique<SCH_SCREEN>() );
847 }
848
849 std::unique_ptr<LIB_SYMBOL> newSymbol = std::make_unique<LIB_SYMBOL>( *symbol );
850 newSymbol->SetParent( libParent );
851 buf.CreateBuffer( std::move( newSymbol ), std::make_unique<SCH_SCREEN>() );
852 }
853 else if( !buf.GetSymbol( symbol->GetName() ) )
854 {
855 buf.CreateBuffer( std::make_unique<LIB_SYMBOL>( *symbol ), std::make_unique<SCH_SCREEN>() );
856 }
857 }
858
859 return buf;
860}
861
862
863bool SYMBOL_LIBRARY_MANAGER::UpdateLibraryBuffer( const wxString& aLibrary )
864{
865 try
866 {
867 m_libs.erase( aLibrary );
868 getLibraryBuffer( aLibrary );
869 }
870 catch( const std::exception& e )
871 {
872 wxLogError( _( "Error updating library buffer: %s" ), e.what() );
873 return false;
874 }
875 catch(...)
876 {
877 wxLogError( _( "Error updating library buffer." ) );
878 return false;
879 }
880
881 getLibraryBuffer( aLibrary );
882
883 return true;
884}
885
886
887SYMBOL_BUFFER::SYMBOL_BUFFER( std::unique_ptr<LIB_SYMBOL> aSymbol,
888 std::unique_ptr<SCH_SCREEN> aScreen ) :
889 m_screen( std::move( aScreen ) ),
890 m_symbol( std::move( aSymbol ) )
891{
892 wxASSERT( m_symbol );
893 m_original = std::make_unique<LIB_SYMBOL>( *m_symbol );
894 wxASSERT( m_original );
895}
896
897
901
902
903void SYMBOL_BUFFER::SetSymbol( std::unique_ptr<LIB_SYMBOL> aSymbol )
904{
905 wxCHECK( m_symbol != aSymbol, /* void */ );
906 wxASSERT( aSymbol );
907 m_symbol = std::move( aSymbol );
908
909 // If the symbol moves libraries then the original moves with it
910 if( m_original->GetLibId().GetLibNickname() != m_symbol->GetLibId().GetLibNickname() )
911 {
912 m_original->SetLibId( LIB_ID( m_symbol->GetLibId().GetLibNickname(),
913 m_original->GetLibId().GetLibItemName() ) );
914 }
915}
916
917
918void SYMBOL_BUFFER::SetOriginal( std::unique_ptr<LIB_SYMBOL> aSymbol )
919{
920 wxCHECK( m_original != aSymbol, /* void */ );
921 wxASSERT( aSymbol );
922 m_original = std::move( aSymbol );
923
924 // The original is not allowed to have a different library than its symbol
925 if( m_original->GetLibId().GetLibNickname() != m_symbol->GetLibId().GetLibNickname() )
926 {
927 m_original->SetLibId( LIB_ID( m_symbol->GetLibId().GetLibNickname(),
928 m_original->GetLibId().GetLibItemName() ) );
929 }
930}
931
932
934{
935 return m_screen && m_screen->IsContentModified();
936}
937
938
939LIB_SYMBOL* LIB_BUFFER::GetSymbol( const wxString& aAlias ) const
940{
941 auto buf = GetBuffer( aAlias );
942
943 if( !buf )
944 return nullptr;
945
946 LIB_SYMBOL& symbol = buf->GetSymbol();
947 return &symbol;
948}
949
950
951bool LIB_BUFFER::CreateBuffer( std::unique_ptr<LIB_SYMBOL> aCopy,
952 std::unique_ptr<SCH_SCREEN> aScreen )
953{
954 wxASSERT( aCopy );
955 wxASSERT( aCopy->GetLib() == nullptr );
956
957 // Set the parent library name,
958 // otherwise it is empty as no library has been given as the owner during object construction
959 LIB_ID libId = aCopy->GetLibId();
960 libId.SetLibNickname( m_libName );
961 aCopy->SetLibId( libId );
962
963 auto symbolBuf = std::make_shared<SYMBOL_BUFFER>( std::move( aCopy ), std::move( aScreen ) );
964 m_symbols.push_back( std::move( symbolBuf ) );
965
966 ++m_hash;
967
968 return true;
969}
970
971
972bool LIB_BUFFER::UpdateBuffer( SYMBOL_BUFFER& aSymbolBuf, const LIB_SYMBOL& aCopy )
973{
974 LIB_SYMBOL& bufferedSymbol = aSymbolBuf.GetSymbol();
975
976 bufferedSymbol = aCopy;
977 ++m_hash;
978
979 return true;
980}
981
982
984{
985 const auto sameBufferPredicate = [&]( const std::shared_ptr<SYMBOL_BUFFER>& aBuf )
986 {
987 return aBuf.get() == &aSymbolBuf;
988 };
989
990 auto symbolBufIt = std::find_if( m_symbols.begin(), m_symbols.end(), sameBufferPredicate );
991 wxCHECK( symbolBufIt != m_symbols.end(), false );
992
993 bool retv = true;
994
995 // Remove all derived symbols to prevent broken inheritance.
996 if( HasDerivedSymbols( aSymbolBuf.GetSymbol().GetName() )
997 && ( removeChildSymbols( aSymbolBuf ) == 0 ) )
998 {
999 retv = false;
1000 }
1001
1002 m_deleted.emplace_back( *symbolBufIt );
1003 m_symbols.erase( symbolBufIt );
1004 ++m_hash;
1005
1006 return retv;
1007}
1008
1009
1010bool LIB_BUFFER::SaveBuffer( SYMBOL_BUFFER& aSymbolBuf, const wxString& aFileName, SCH_IO* aPlugin,
1011 bool aBuffer )
1012{
1013 wxCHECK( !aFileName.IsEmpty(), false );
1014
1015 wxString errorMsg = _( "Error saving symbol %s to library '%s'." ) + wxS( "\n%s" );
1016
1017 // Set properties to prevent saving the file on every symbol save.
1018 std::map<std::string, UTF8> properties;
1019 properties.emplace( SCH_IO_KICAD_LEGACY::PropBuffering, "" );
1020
1021 LIB_SYMBOL& libSymbol = aSymbolBuf.GetSymbol();
1022
1023 {
1024 LIB_SYMBOL& originalSymbol = aSymbolBuf.GetOriginal();
1025 const wxString originalName = originalSymbol.GetName();
1026
1027 // Delete the original symbol if the symbol name has been changed.
1028 if( libSymbol.GetName() != originalSymbol.GetName() )
1029 {
1030 try
1031 {
1032 if( aPlugin->LoadSymbol( aFileName, originalName ) )
1033 aPlugin->DeleteSymbol( aFileName, originalName, &properties );
1034 }
1035 catch( const IO_ERROR& ioe )
1036 {
1037 wxLogError( errorMsg, UnescapeString( originalName ), aFileName, ioe.What() );
1038 return false;
1039 }
1040 }
1041 }
1042
1043 LIB_SYMBOL* parentSymbol = nullptr;
1044
1045 if( libSymbol.IsDerived() )
1046 {
1047 LIB_SYMBOL* newCachedSymbol = new LIB_SYMBOL( libSymbol );
1048 std::shared_ptr<LIB_SYMBOL> bufferedParent = libSymbol.GetParent().lock();
1049 parentSymbol = newCachedSymbol;
1050
1051 wxCHECK( bufferedParent, false );
1052
1053 LIB_SYMBOL* cachedParent = nullptr;
1054
1055 try
1056 {
1057 cachedParent = aPlugin->LoadSymbol( aFileName, bufferedParent->GetName() );
1058 }
1059 catch( const IO_ERROR& )
1060 {
1061 return false;
1062 }
1063
1064 if( !cachedParent )
1065 {
1066 cachedParent = new LIB_SYMBOL( *bufferedParent.get() );
1067 newCachedSymbol->SetParent( cachedParent );
1068
1069 try
1070 {
1071 aPlugin->SaveSymbol( aFileName, cachedParent, aBuffer ? &properties : nullptr );
1072 }
1073 catch( const IO_ERROR& ioe )
1074 {
1075 wxLogError( errorMsg, UnescapeString( cachedParent->GetName() ), aFileName,
1076 ioe.What() );
1077 return false;
1078 }
1079
1080 try
1081 {
1082 aPlugin->SaveSymbol( aFileName, newCachedSymbol, aBuffer ? &properties : nullptr );
1083 }
1084 catch( const IO_ERROR& ioe )
1085 {
1086 wxLogError( errorMsg, UnescapeString( newCachedSymbol->GetName() ), aFileName,
1087 ioe.What() );
1088 return false;
1089 }
1090
1091 auto originalParent = std::make_unique<LIB_SYMBOL>( *bufferedParent.get() );
1092 LIB_SYMBOL& parentRef = *originalParent;
1093 aSymbolBuf.SetOriginal( std::move( originalParent ) );
1094
1095 auto newSymbol = std::make_unique<LIB_SYMBOL>( libSymbol );
1096 newSymbol->SetParent( &parentRef );
1097 aSymbolBuf.SetOriginal( std::move( newSymbol ) );
1098 }
1099 else
1100 {
1101 // Copy embedded files from the buffered parent to the cached parent
1102 // This ensures that any embedded files added to the parent are preserved
1103 //
1104 // We do this manually rather than using the assignment operator to avoid
1105 // potential ABI issues where the size of EMBEDDED_FILES differs between
1106 // compilation units, potentially causing the assignment to overwrite
1107 // members of LIB_SYMBOL (like m_me) that follow the EMBEDDED_FILES base.
1108 cachedParent->ClearEmbeddedFiles();
1109
1110 for( const auto& [name, file] : bufferedParent->EmbeddedFileMap() )
1111 cachedParent->AddFile( new EMBEDDED_FILES::EMBEDDED_FILE( *file ) );
1112
1113 cachedParent->SetAreFontsEmbedded( bufferedParent->GetAreFontsEmbedded() );
1114 cachedParent->SetFileAddedCallback( bufferedParent->GetFileAddedCallback() );
1115
1116 newCachedSymbol->SetParent( cachedParent );
1117
1118 try
1119 {
1120 aPlugin->SaveSymbol( aFileName, newCachedSymbol, aBuffer ? &properties : nullptr );
1121 }
1122 catch( const IO_ERROR& ioe )
1123 {
1124 wxLogError( errorMsg, UnescapeString( newCachedSymbol->GetName() ), aFileName,
1125 ioe.What() );
1126 return false;
1127 }
1128
1129 auto originalBufferedParent = GetBuffer( bufferedParent->GetName() );
1130 wxCHECK( originalBufferedParent, false );
1131
1132 auto newSymbol = std::make_unique<LIB_SYMBOL>( libSymbol );
1133 newSymbol->SetParent( &originalBufferedParent->GetSymbol() );
1134 aSymbolBuf.SetOriginal( std::move( newSymbol ) );
1135 }
1136 }
1137 else
1138 {
1139 parentSymbol = new LIB_SYMBOL( libSymbol );
1140
1141 try
1142 {
1143 aPlugin->SaveSymbol( aFileName, parentSymbol, aBuffer ? &properties : nullptr );
1144 }
1145 catch( const IO_ERROR& ioe )
1146 {
1147 wxLogError( errorMsg, UnescapeString( libSymbol.GetName() ), aFileName, ioe.What() );
1148 return false;
1149 }
1150
1151 aSymbolBuf.SetOriginal( std::make_unique<LIB_SYMBOL>( libSymbol ) );
1152 }
1153
1154 wxArrayString derivedSymbols;
1155
1156 // Reparent all symbols derived from the saved symbol.
1157 if( GetDerivedSymbolNames( libSymbol.GetName(), derivedSymbols ) != 0 )
1158 {
1159 // Save the derived symbols.
1160 for( const wxString& entry : derivedSymbols )
1161 {
1162 std::shared_ptr<SYMBOL_BUFFER> symbol = GetBuffer( entry );
1163
1164 wxCHECK2( symbol, continue );
1165
1166 LIB_SYMBOL* derivedSymbol = new LIB_SYMBOL( symbol->GetSymbol() );
1167 derivedSymbol->SetParent( parentSymbol );
1168
1169 try
1170 {
1171 aPlugin->SaveSymbol( aFileName, new LIB_SYMBOL( *derivedSymbol ),
1172 aBuffer ? &properties : nullptr );
1173 }
1174 catch( const IO_ERROR& ioe )
1175 {
1176 wxLogError( errorMsg, UnescapeString( derivedSymbol->GetName() ), aFileName,
1177 ioe.What() );
1178 return false;
1179 }
1180 }
1181 }
1182
1183 ++m_hash;
1184 return true;
1185}
1186
1187
1188std::shared_ptr<SYMBOL_BUFFER> LIB_BUFFER::GetBuffer( const wxString& aSymbolName ) const
1189{
1190 for( std::shared_ptr<SYMBOL_BUFFER> entry : m_symbols )
1191 {
1192 if( entry->GetSymbol().GetName() == aSymbolName )
1193 return entry;
1194 }
1195
1196 return std::shared_ptr<SYMBOL_BUFFER>( nullptr );
1197}
1198
1199
1200bool LIB_BUFFER::HasDerivedSymbols( const wxString& aParentName ) const
1201{
1202 for( const std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1203 {
1204 if( std::shared_ptr<LIB_SYMBOL> parent = entry->GetSymbol().GetParent().lock() )
1205 {
1206 if( parent->GetName() == aParentName )
1207 return true;
1208 }
1209 }
1210
1211 return false;
1212}
1213
1214
1215void LIB_BUFFER::GetSymbolNames( wxArrayString& aSymbolNames, SYMBOL_NAME_FILTER aFilter )
1216{
1217 for( std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1218 {
1219 const LIB_SYMBOL& symbol = entry->GetSymbol();
1220 if( ( symbol.IsDerived() && ( aFilter == SYMBOL_NAME_FILTER::ROOT_ONLY ) )
1221 || ( symbol.IsRoot() && ( aFilter == SYMBOL_NAME_FILTER::DERIVED_ONLY ) ) )
1222 {
1223 continue;
1224 }
1225 aSymbolNames.Add( UnescapeString( symbol.GetName() ) );
1226 }
1227}
1228
1229
1230size_t LIB_BUFFER::GetDerivedSymbolNames( const wxString& aSymbolName, wxArrayString& aList )
1231{
1232 wxCHECK( !aSymbolName.IsEmpty(), 0 );
1233
1234 // Parent: children map
1235 std::unordered_map<std::shared_ptr<LIB_SYMBOL>, std::vector<std::shared_ptr<LIB_SYMBOL>>> derivedMap;
1236
1237 // Iterate the library once to resolve all derived symbol links.
1238 // This means we only need to iterate the library once, and we can then look up the links
1239 // as needed.
1240 for( std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1241 {
1242 std::shared_ptr<LIB_SYMBOL> symbol = entry->GetSymbol().SharedPtr();
1243
1244 if( std::shared_ptr<LIB_SYMBOL> parent = symbol->GetParent().lock() )
1245 derivedMap[parent].emplace_back( std::move( symbol ) );
1246 }
1247
1248 const auto visit =
1249 [&]( LIB_SYMBOL& aSymbol )
1250 {
1251 aList.Add( aSymbol.GetName() );
1252 };
1253
1254 // Assign to std::function to allow recursion
1255 const std::function<void( std::shared_ptr<LIB_SYMBOL>& )> getDerived =
1256 [&]( std::shared_ptr<LIB_SYMBOL>& aSymbol )
1257 {
1258 auto it = derivedMap.find( aSymbol );
1259
1260 if( it != derivedMap.end() )
1261 {
1262 for( std::shared_ptr<LIB_SYMBOL>& derivedSymbol : it->second )
1263 {
1264 visit( *derivedSymbol );
1265
1266 // Recurse to get symbols derived from this one
1267 getDerived( derivedSymbol );
1268 }
1269 }
1270 };
1271
1272 // Start the recursion at the top
1273 std::shared_ptr<LIB_SYMBOL> symbol = GetSymbol( aSymbolName )->SharedPtr();
1274 getDerived( symbol );
1275
1276 return aList.GetCount();
1277}
1278
1279
1281{
1282 int cnt = 0;
1283 wxArrayString derivedSymbolNames;
1284 std::deque<std::shared_ptr<SYMBOL_BUFFER>>::iterator it;
1285
1286 if( GetDerivedSymbolNames( aSymbolBuf.GetSymbol().GetName(), derivedSymbolNames ) )
1287 {
1288 for( const wxString& symbolName : derivedSymbolNames )
1289 {
1290 it = std::find_if( m_symbols.begin(), m_symbols.end(),
1291 [symbolName]( std::shared_ptr<SYMBOL_BUFFER>& buf )
1292 {
1293 return buf->GetSymbol().GetName() == symbolName;
1294 } );
1295
1296 wxCHECK2( it != m_symbols.end(), continue );
1297
1298 m_deleted.emplace_back( *it );
1299 m_symbols.erase( it );
1300 cnt += 1;
1301 }
1302 }
1303
1304 return cnt;
1305}
const char * name
void SetContentModified(bool aModified=true)
Definition base_screen.h:55
void SetFileAddedCallback(FILE_ADDED_CALLBACK callback)
void ClearEmbeddedFiles(bool aDeleteFiles=true)
EMBEDDED_FILE * AddFile(const wxFileName &aName, bool aOverwrite)
Load a file from disk and adds it to the collection.
const std::map< wxString, EMBEDDED_FILE * > & EmbeddedFileMap() const
void SetAreFontsEmbedded(bool aEmbedFonts)
FILE_ADDED_CALLBACK GetFileAddedCallback() const
bool GetAreFontsEmbedded() const
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
bool IsLibraryLoaded(const wxString &aNickname)
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library tables.
std::vector< wxString > GetLibraryNames() const
Returns a list of library nicknames that are available (skips any that failed to load)
bool CreateLibrary(const wxString &aNickname)
Creates the library (i.e. saves to disk) for the given row if it exists.
std::optional< LIBRARY_TABLE * > Table(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope)
Retrieves a given table; creating a new empty project table if a valid project is loaded and the give...
std::optional< wxString > GetFullURI(LIBRARY_TABLE_TYPE aType, const wxString &aNickname, bool aSubstituted=false)
Return the full location specifying URI for the LIB, either in original UI form or in environment var...
std::vector< LIBRARY_TABLE_ROW * > Rows(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH, bool aIncludeInvalid=false) const
Returns a flattened list of libraries of the given type.
void SetNickname(const wxString &aNickname)
void SetType(const wxString &aType)
void SetURI(const wxString &aUri)
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 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.
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
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
Definition lib_id.cpp:96
const UTF8 & GetLibItemName() const
Definition lib_id.h:98
Define a library symbol object.
Definition lib_symbol.h:79
const LIB_ID & GetLibId() const override
Definition lib_symbol.h:148
std::weak_ptr< LIB_SYMBOL > & GetParent()
Definition lib_symbol.h:110
bool IsRoot() const override
For symbols derived from other symbols, IsRoot() indicates no derivation.
Definition lib_symbol.h:195
bool IsDerived() const
Definition lib_symbol.h:196
void SetParent(LIB_SYMBOL *aParent=nullptr)
wxString GetName() const override
Definition lib_symbol.h:141
std::shared_ptr< LIB_SYMBOL > SharedPtr() const
http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/sp_techniques.html#weak_without_shared.
Definition lib_symbol.h:88
void SetLibId(const LIB_ID &aLibId)
virtual LIBRARY_MANAGER & GetLibraryManager() const
Definition pgm_base.h:126
static SYMBOL_LIBRARY_ADAPTER * SymbolLibAdapter(PROJECT *aProject)
Accessor for project symbol library manager adapter.
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
static const wxString ShowType(SCH_FILE_T aFileType)
Return a brief name for a plugin, given aFileType enum.
static SCH_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath, int aCtl=0)
Return a plugin type given a symbol library using the file extension of aLibPath.
Base class that schematic file and library loading and saving plugins should derive from.
Definition sch_io.h:59
virtual void SaveSymbol(const wxString &aLibraryPath, const LIB_SYMBOL *aSymbol, const std::map< std::string, UTF8 > *aProperties=nullptr)
Write aSymbol to an existing library located at aLibraryPath.
Definition sch_io.cpp:108
virtual void DeleteSymbol(const wxString &aLibraryPath, const wxString &aSymbolName, const std::map< std::string, UTF8 > *aProperties=nullptr)
Delete the entire LIB_SYMBOL associated with aAliasName from the library aLibraryPath.
Definition sch_io.cpp:116
virtual LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aPartName, const std::map< std::string, UTF8 > *aProperties=nullptr)
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
Definition sch_io.cpp:100
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
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)
An interface to the global shared library manager that is schematic-specific and linked to one projec...
std::optional< LIB_STATUS > LoadOne(LIB_DATA *aLib) override
Loads or reloads the given library, if it exists.
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
std::vector< LIB_SYMBOL * > GetSymbols(const wxString &aNickname, SYMBOL_TYPE aType=SYMBOL_TYPE::ALL_SYMBOLS)
bool IsSymbolLibWritable(const wxString &aNickname)
Return true if the library given by aNickname is writable.
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 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.
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.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:192
This file is part of the common library.
#define _(s)
wxString NormalizePath(const wxFileName &aFilePath, const ENV_VAR_MAP *aEnvVars, const wxString &aProjectPath)
Normalize a file path to an environmental variable, if possible.
Definition env_paths.cpp:73
Helper functions to substitute paths with environmental variables.
static const std::string KiCadSymbolLibFileExtension
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition io_mgr.h:33
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define KICTL_CREATE
caller thinks requested project files may not exist.
LIBRARY_TABLE_SCOPE
long long TimestampDir(const wxString &aDirPath, const wxString &aFilespec)
Computes a hash of modification times and sizes for files matching a pattern.
Definition unix/io.cpp:123
STL namespace.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
wxString UnescapeString(const wxString &aSource)
wxString message
VECTOR3I res
std::vector< std::vector< std::string > > table
Definition of file extensions used in Kicad.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition wx_filename.h:35