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