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