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