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