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 (C) 2019-2023 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
28#include <symbol_library.h>
30#include <symbol_edit_frame.h>
31#include <symbol_lib_table.h>
32#include <env_paths.h>
33#include <pgm_base.h>
34#include <project_sch.h>
35#include <kiway.h>
36#include <core/profile.h>
37#include <wx_filename.h>
39#include <symbol_async_loader.h>
40#include <progress_reporter.h>
41#include <list>
42#include <locale_io.h>
43#include <confirm.h>
44#include <string_utils.h>
45#include "lib_logger.h"
46
47
49 m_frame( aFrame )
50{
51 m_logger = new LIB_LOGGER();
52}
53
54
56{
57 delete m_logger;
58}
59
60
62{
63 SYMBOL_ASYNC_LOADER loader( symTable()->GetLogicalLibs(), symTable(), false, nullptr,
64 &aReporter );
65
66 LOCALE_IO toggle;
67
68 loader.Start();
69
70 while( !loader.Done() )
71 {
72 if( !aReporter.KeepRefreshing() )
73 break;
74
75 wxMilliSleep( 33 /* 30 FPS refresh rate */ );
76 }
77
78 loader.Join();
79
80 if( !loader.GetErrors().IsEmpty() )
81 {
82 HTML_MESSAGE_BOX dlg( &m_frame, _( "Load Error" ) );
83
84 dlg.MessageSet( _( "Errors loading symbols:" ) );
85
86 wxString msg = loader.GetErrors();
87 msg.Replace( "\n", "<BR>" );
88
89 dlg.AddHTML_Text( msg );
90 dlg.ShowModal();
91 }
92}
93
94
96{
97 for( const auto& [name, buffer] : m_libs )
98 {
99 if( buffer.IsModified() )
100 return true;
101 }
102
103 return false;
104}
105
106
108{
109 int hash = symTable()->GetModifyHash();
110
111 for( const auto& [name, buffer] : m_libs )
112 hash += buffer.GetHash();
113
114 return hash;
115}
116
117
118int SYMBOL_LIBRARY_MANAGER::GetLibraryHash( const wxString& aLibrary ) const
119{
120 const auto libBufIt = m_libs.find( aLibrary );
121
122 if( libBufIt != m_libs.end() )
123 return libBufIt->second.GetHash();
124
125 SYMBOL_LIB_TABLE_ROW* row = GetLibrary( aLibrary );
126
127 // return -1 if library does not exist or 0 if not modified
128 return row ? std::hash<std::string>{}( aLibrary.ToStdString() +
129 row->GetFullURI( true ).ToStdString() ) : -1;
130}
131
132
134{
135 wxArrayString res;
136
137 for( const wxString& libName : symTable()->GetLogicalLibs() )
138 {
139 // Database libraries are hidden from the symbol editor at the moment
140 if( GetLibrary( libName )->SchLibType() == SCH_IO_MGR::SCH_DATABASE )
141 continue;
142
143 res.Add( libName );
144 }
145
146 return res;
147}
148
149
151{
152 SYMBOL_LIB_TABLE_ROW* row = nullptr;
153
154 try
155 {
156 row = symTable()->FindRow( aLibrary, true );
157 }
158 catch( const IO_ERROR& e )
159 {
160 wxString msg;
161
162 msg.Printf( _( "Library '%s' not found in the Symbol Library Table." ), aLibrary );
163 DisplayErrorMessage( &m_frame, msg, e.What() );
164 }
165
166 return row;
167}
168
169
170bool SYMBOL_LIBRARY_MANAGER::SaveLibrary( const wxString& aLibrary, const wxString& aFileName,
171 SCH_IO_MGR::SCH_FILE_T aFileType )
172{
173 wxCHECK( aFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY, false );
174 wxCHECK_MSG( LibraryExists( aLibrary ), false,
175 wxString::Format( "Library missing: %s", aLibrary ) );
176
177 wxFileName fn( aFileName );
178 wxCHECK( !fn.FileExists() || fn.IsFileWritable(), false );
179
180 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( aFileType ) );
181 bool res = true; // assume all libraries are successfully saved
182 std::map<std::string, UTF8> properties;
183
184 properties.emplace( SCH_IO_KICAD_LEGACY::PropBuffering, "" );
185
186 auto it = m_libs.find( aLibrary );
187
188 if( it != m_libs.end() )
189 {
190 // Handle buffered library
191 LIB_BUFFER& libBuf = it->second;
192
193 const auto& symbolBuffers = libBuf.GetBuffers();
194
195 for( const std::shared_ptr<SYMBOL_BUFFER>& symbolBuf : symbolBuffers )
196 {
197 wxCHECK2( symbolBuf, continue );
198 if( !libBuf.SaveBuffer( *symbolBuf, aFileName, &*pi, true ) )
199 {
200 // Something went wrong, but try to save other libraries
201 res = false;
202 }
203 }
204
205 // clear the deleted symbols buffer only if data is saved to the original file
206 wxFileName original, destination( aFileName );
207 SYMBOL_LIB_TABLE_ROW* row = GetLibrary( aLibrary );
208
209 if( row )
210 {
211 original = row->GetFullURI();
212 original.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
213 }
214
215 destination.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
216
217 if( res && original == destination )
218 libBuf.ClearDeletedBuffer();
219 }
220 else
221 {
222 // Handle original library
223 for( LIB_SYMBOL* symbol : getOriginalSymbols( aLibrary ) )
224 {
225 LIB_SYMBOL* newSymbol;
226
227 try
228 {
229 if( symbol->IsAlias() )
230 {
231 std::shared_ptr< LIB_SYMBOL > oldParent = symbol->GetParent().lock();
232
233 wxCHECK_MSG( oldParent, false,
234 wxString::Format( wxT( "Derived symbol '%s' found with "
235 "undefined parent." ),
236 symbol->GetName() ) );
237
238 LIB_SYMBOL* libParent = pi->LoadSymbol( aLibrary, oldParent->GetName(),
239 &properties );
240
241 if( !libParent )
242 {
243 libParent = new LIB_SYMBOL( *oldParent.get() );
244 pi->SaveSymbol( aLibrary, libParent, &properties );
245 }
246
247 newSymbol = new LIB_SYMBOL( *symbol );
248 newSymbol->SetParent( libParent );
249 pi->SaveSymbol( aLibrary, newSymbol, &properties );
250 }
251 else if( !pi->LoadSymbol( aLibrary, symbol->GetName(), &properties ) )
252 {
253 pi->SaveSymbol( aLibrary, new LIB_SYMBOL( *symbol ), &properties );
254 }
255 }
256 catch( ... )
257 {
258 res = false;
259 break;
260 }
261 }
262 }
263
264 try
265 {
266 pi->SaveLibrary( aFileName );
267 }
268 catch( ... )
269 {
270 // return false because something happens.
271 // The library is not successfully saved
272 res = false;
273 }
274
275 return res;
276}
277
278
279bool SYMBOL_LIBRARY_MANAGER::IsLibraryModified( const wxString& aLibrary ) const
280{
281 auto it = m_libs.find( aLibrary );
282 return it != m_libs.end() ? it->second.IsModified() : false;
283}
284
285
286bool SYMBOL_LIBRARY_MANAGER::IsSymbolModified( const wxString& aAlias,
287 const wxString& aLibrary ) const
288{
289 auto libIt = m_libs.find( aLibrary );
290
291 if( libIt == m_libs.end() )
292 return false;
293
294 const LIB_BUFFER& buf = libIt->second;
295 const std::shared_ptr<SYMBOL_BUFFER> symbolBuf = buf.GetBuffer( aAlias );
296
297 return symbolBuf ? symbolBuf->IsModified() : false;
298}
299
300
301void SYMBOL_LIBRARY_MANAGER::SetSymbolModified( const wxString& aAlias, const wxString& aLibrary )
302{
303 auto libIt = m_libs.find( aLibrary );
304
305 if( libIt == m_libs.end() )
306 return;
307
308 const LIB_BUFFER& buf = libIt->second;
309 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = buf.GetBuffer( aAlias );
310
311 wxCHECK( symbolBuf, /* void */ );
312
313 symbolBuf->GetScreen()->SetContentModified();
314}
315
316
317bool SYMBOL_LIBRARY_MANAGER::ClearLibraryModified( const wxString& aLibrary ) const
318{
319 auto libIt = m_libs.find( aLibrary );
320
321 if( libIt == m_libs.end() )
322 return false;
323
324 for( auto& symbolBuf : libIt->second.GetBuffers() )
325 {
326 SCH_SCREEN* screen = symbolBuf->GetScreen();
327
328 if( screen )
329 screen->SetContentModified( false );
330 }
331
332 return true;
333}
334
335
337 const wxString& aLibrary ) const
338{
339 auto libIt = m_libs.find( aLibrary );
340
341 if( libIt == m_libs.end() )
342 return false;
343
344 auto symbolBuf = libIt->second.GetBuffer( aAlias );
345 wxCHECK( symbolBuf, false );
346
347 symbolBuf->GetScreen()->SetContentModified( false );
348 return true;
349}
350
351
352bool SYMBOL_LIBRARY_MANAGER::IsLibraryReadOnly( const wxString& aLibrary ) const
353{
354 wxCHECK_MSG( LibraryExists( aLibrary ), true,
355 wxString::Format( "Library missing: %s", aLibrary ) );
356
357 return !symTable()->IsSymbolLibWritable( aLibrary );
358}
359
360
361bool SYMBOL_LIBRARY_MANAGER::IsLibraryLoaded( const wxString& aLibrary ) const
362{
363 wxCHECK_MSG( LibraryExists( aLibrary ), false,
364 wxString::Format( "Library missing: %s", aLibrary ) );
365
366 return symTable()->IsSymbolLibLoaded( aLibrary );
367}
368
369
370std::list<LIB_SYMBOL*> SYMBOL_LIBRARY_MANAGER::GetAliases( const wxString& aLibrary ) const
371{
372 std::list<LIB_SYMBOL*> ret;
373 wxCHECK_MSG( LibraryExists( aLibrary ), ret,
374 wxString::Format( "Library missing: %s", aLibrary ) );
375
376 auto libIt = m_libs.find( aLibrary );
377
378 if( libIt != m_libs.end() )
379 {
380 for( const std::shared_ptr<SYMBOL_BUFFER>& symbolBuf : libIt->second.GetBuffers() )
381 ret.push_back( &symbolBuf->GetSymbol() );
382 }
383 else
384 {
385 std::vector<LIB_SYMBOL*> aliases;
386
387 try
388 {
389 symTable()->LoadSymbolLib( aliases, aLibrary );
390 }
391 catch( const IO_ERROR& e )
392 {
393 wxLogWarning( e.Problem() );
394 }
395
396 std::copy( aliases.begin(), aliases.end(), std::back_inserter( ret ) );
397 }
398
399 return ret;
400}
401
402
404 const wxString& aLibrary )
405{
406 wxCHECK_MSG( LibraryExists( aLibrary ), nullptr,
407 wxString::Format( "Library missing: %s, for alias %s", aLibrary, aAlias ) );
408
409 // try the library buffers first
410 LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
411 LIB_SYMBOL* bufferedSymbol = libBuf.GetSymbol( aAlias );
412
413 if( !bufferedSymbol ) // no buffer symbol found
414 {
415 // create a copy of the symbol
416 try
417 {
418 LIB_SYMBOL* symbol = symTable()->LoadSymbol( aLibrary, aAlias );
419
420 if( symbol == nullptr )
421 THROW_IO_ERROR( _( "Symbol not found." ) );
422
423 LIB_SYMBOL* bufferedParent = nullptr;
424
425 // Create parent symbols on demand so parent symbol can be set.
426 if( symbol->IsAlias() )
427 {
428 std::shared_ptr<LIB_SYMBOL> parent = symbol->GetParent().lock();
429 wxCHECK_MSG( parent, nullptr,
430 wxString::Format( "Derived symbol '%s' found with undefined parent.",
431 symbol->GetName() ) );
432
433 // Check if the parent symbol buffer has already be created.
434 bufferedParent = libBuf.GetSymbol( parent->GetName() );
435
436 if( !bufferedParent )
437 {
438 auto newParent = std::make_unique<LIB_SYMBOL>( *parent.get() );
439 bufferedParent = newParent.get();
440 libBuf.CreateBuffer( std::move( newParent ), std::make_unique<SCH_SCREEN>() );
441 }
442 }
443
444 auto newSymbol = std::make_unique<LIB_SYMBOL>( *symbol );
445 bufferedSymbol = newSymbol.get();
446
447 if( bufferedParent )
448 newSymbol->SetParent( bufferedParent );
449
450 libBuf.CreateBuffer( std::move( newSymbol ), std::make_unique<SCH_SCREEN>() );
451 }
452 catch( const IO_ERROR& e )
453 {
454 wxString msg;
455
456 msg.Printf( _( "Error loading symbol %s from library '%s'." ), aAlias, aLibrary );
457 DisplayErrorMessage( &m_frame, msg, e.What() );
458 bufferedSymbol = nullptr;
459 }
460 }
461
462 return bufferedSymbol;
463}
464
465
466SCH_SCREEN* SYMBOL_LIBRARY_MANAGER::GetScreen( const wxString& aAlias, const wxString& aLibrary )
467{
468 wxCHECK_MSG( LibraryExists( aLibrary ), nullptr,
469 wxString::Format( "Library missing: %s, for alias %s", aLibrary, aAlias ) );
470 wxCHECK_MSG( !aAlias.IsEmpty(), nullptr,
471 wxString::Format( "Alias missing in library: %s", aLibrary ) );
472 auto it = m_libs.find( aLibrary );
473 wxCHECK( it != m_libs.end(), nullptr );
474
475 LIB_BUFFER& buf = it->second;
476 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = buf.GetBuffer( aAlias );
477
478 return symbolBuf ? symbolBuf->GetScreen() : nullptr;
479}
480
481
482bool SYMBOL_LIBRARY_MANAGER::UpdateSymbol( LIB_SYMBOL* aSymbol, const wxString& aLibrary )
483{
484 wxCHECK_MSG( aSymbol, false, wxString::Format( "Null symbol in library: %s", aLibrary ) );
485 wxCHECK_MSG( LibraryExists( aLibrary ), false,
486 wxString::Format( "Library missing: %s, for smybol %s", aLibrary,
487 aSymbol->GetName() ) );
488
489 LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
490 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = libBuf.GetBuffer( aSymbol->GetName() );
491
492 if( symbolBuf ) // Existing symbol.
493 {
494 LIB_SYMBOL& bufferedSymbol = symbolBuf->GetSymbol();
495
496 // If we are coming from a different library, the library ID needs to be preserved
497 const LIB_ID libId = bufferedSymbol.GetLibId();
498 bufferedSymbol = *aSymbol;
499 bufferedSymbol.SetLibId( libId );
500
501 symbolBuf->GetScreen()->SetContentModified();
502 }
503 else // New symbol
504 {
505 auto symbolCopy = std::make_unique<LIB_SYMBOL>( *aSymbol, nullptr );
506
507 symbolCopy->SetLibId( LIB_ID( aLibrary, aSymbol->GetLibId().GetLibItemName() ) );
508
509 auto newScreen = std::make_unique<SCH_SCREEN>();
510 SCH_SCREEN& screen = *newScreen;
511 libBuf.CreateBuffer( std::move( symbolCopy ), std::move( newScreen ) );
512 screen.SetContentModified();
513 }
514
515 return true;
516}
517
518
519bool SYMBOL_LIBRARY_MANAGER::UpdateSymbolAfterRename( LIB_SYMBOL* aSymbol, const wxString& aOldName,
520 const wxString& aLibrary )
521{
522 LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
523 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = libBuf.GetBuffer( aOldName );
524
525 wxCHECK( symbolBuf && aSymbol, false );
526
527 libBuf.UpdateBuffer( *symbolBuf, *aSymbol );
529
530 return true;
531}
532
533
534LIB_ID SYMBOL_LIBRARY_MANAGER::RevertSymbol( const wxString& aAlias, const wxString& aLibrary )
535{
536 auto it = m_libs.find( aLibrary );
537
538 if( it == m_libs.end() ) // no items to flush
539 return LIB_ID( aLibrary, aAlias );
540
541 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = it->second.GetBuffer( aAlias );
542 wxCHECK( symbolBuf, LIB_ID( aLibrary, aAlias ) );
543 LIB_SYMBOL original( symbolBuf->GetOriginal() );
544
545 if( original.GetName() != aAlias )
546 {
547 UpdateSymbolAfterRename( &original, aAlias, aLibrary );
548 }
549 else
550 {
551 // copy the initial data to the current symbol to restore
552 symbolBuf->GetSymbol() = original;
554 }
555
556 return LIB_ID( aLibrary, original.GetName() );
557}
558
559
560bool SYMBOL_LIBRARY_MANAGER::RevertLibrary( const wxString& aLibrary )
561{
562 auto it = m_libs.find( aLibrary );
563
564 if( it == m_libs.end() ) // nothing to reverse
565 return false;
566
567 m_libs.erase( it );
569
570 return true;
571}
572
573
575{
576 bool retv = true;
577
578 // Nothing to revert.
579 if( GetHash() == 0 )
580 return true;
581
582 for( const auto& lib : m_libs )
583 {
584 if( !lib.second.IsModified() )
585 continue;
586
587 for( const std::shared_ptr<SYMBOL_BUFFER>& buffer : lib.second.GetBuffers() )
588 {
589 if( !buffer->IsModified() )
590 continue;
591
592 RevertSymbol( lib.first, buffer->GetOriginal().GetName() );
593 }
594 }
595
596 return retv;
597}
598
599
600bool SYMBOL_LIBRARY_MANAGER::RemoveSymbol( const wxString& aAlias, const wxString& aLibrary )
601{
602 LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
603 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = libBuf.GetBuffer( aAlias );
604 wxCHECK( symbolBuf, false );
605
606 bool retv = true;
607
608 retv &= libBuf.DeleteBuffer( *symbolBuf );
610
611 return retv;
612}
613
614
616 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( aAlias );
624
625 if( symbol )
626 return symbol;
627 }
628
629 // Get the original symbol
630 LIB_SYMBOL* alias = nullptr;
631
632 try
633 {
634 alias = symTable()->LoadSymbol( aLibrary, aAlias );
635 }
636 catch( const IO_ERROR& e )
637 {
638 wxString msg;
639
640 msg.Printf( _( "Cannot load symbol '%s' from library '%s'." ), aAlias, aLibrary );
641 DisplayErrorMessage( &m_frame, msg, e.What() );
642 }
643
644 return alias;
645}
646
647
648bool SYMBOL_LIBRARY_MANAGER::SymbolExists( const wxString& aAlias, const wxString& aLibrary ) const
649{
650 auto libBufIt = m_libs.find( aLibrary );
651 LIB_SYMBOL* alias = nullptr;
652
653 if( libBufIt != m_libs.end() )
654 return !!libBufIt->second.GetBuffer( aAlias );
655
656 try
657 {
658 alias = symTable()->LoadSymbol( aLibrary, aAlias );
659 }
660 catch( IO_ERROR& )
661 {
662 // checking if certain symbol exists, so its absence is perfectly fine
663 }
664
665 return alias != nullptr;
666}
667
668
669bool SYMBOL_LIBRARY_MANAGER::LibraryExists( const wxString& aLibrary, bool aCheckEnabled ) const
670{
671 if( aLibrary.IsEmpty() )
672 return false;
673
674 if( m_libs.count( aLibrary ) > 0 )
675 return true;
676
677 return symTable()->HasLibrary( aLibrary, aCheckEnabled );
678}
679
680
682{
683 wxString name = "New_Library";
684
685 if( !LibraryExists( name ) )
686 return name;
687
688 name += "_";
689
690 for( unsigned int i = 0; i < std::numeric_limits<unsigned int>::max(); ++i )
691 {
692 if( !LibraryExists( name + wxString::Format( "%u", i ) ) )
693 return name + wxString::Format( "%u", i );
694 }
695
696 wxFAIL;
697 return wxEmptyString;
698}
699
700
701void SYMBOL_LIBRARY_MANAGER::GetSymbolNames( const wxString& aLibraryName,
702 wxArrayString& aSymbolNames,
703 SYMBOL_NAME_FILTER aFilter )
704{
705 LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
706
707 libBuf.GetSymbolNames( aSymbolNames, aFilter );
708}
709
710
711bool SYMBOL_LIBRARY_MANAGER:: HasDerivedSymbols( const wxString& aSymbolName,
712 const wxString& aLibraryName )
713{
714 LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
715
716 return libBuf.HasDerivedSymbols( aSymbolName );
717}
718
719
721{
722 return symTable()->GetLogicalLibs().size();
723}
724
725
726wxString SYMBOL_LIBRARY_MANAGER::getLibraryName( const wxString& aFilePath )
727{
728 wxFileName fn( aFilePath );
729 return fn.GetName();
730}
731
732
733bool SYMBOL_LIBRARY_MANAGER::addLibrary( const wxString& aFilePath, bool aCreate,
734 SYMBOL_LIB_TABLE& aTable )
735{
736 wxString libName = getLibraryName( aFilePath );
737 wxCHECK( !LibraryExists( libName ), false ); // either create or add an existing one
738
739 // try to use path normalized to an environmental variable or project path
740 wxString relPath = NormalizePath( aFilePath, &Pgm().GetLocalEnvVariables(), &m_frame.Prj() );
741
742 SCH_IO_MGR::SCH_FILE_T schFileType = SCH_IO_MGR::GuessPluginTypeFromLibPath( aFilePath );
743
744 if( schFileType == SCH_IO_MGR::SCH_FILE_UNKNOWN )
745 schFileType = SCH_IO_MGR::SCH_LEGACY;
746
747 wxString typeName = SCH_IO_MGR::ShowType( schFileType );
748 SYMBOL_LIB_TABLE_ROW* libRow = new SYMBOL_LIB_TABLE_ROW( libName, relPath, typeName );
749 aTable.InsertRow( libRow );
750
751 if( aCreate )
752 {
753 wxCHECK( schFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY, false );
754
755 try
756 {
757 aTable.CreateSymbolLib( libName );
758 }
759 catch( const IO_ERROR& )
760 {
761 aTable.RemoveRow( libRow );
762 return false;
763 }
764 }
765
767
768 return true;
769}
770
771
773{
775}
776
777
778std::set<LIB_SYMBOL*> SYMBOL_LIBRARY_MANAGER::getOriginalSymbols( const wxString& aLibrary )
779{
780 std::set<LIB_SYMBOL*> symbols;
781 wxCHECK( LibraryExists( aLibrary ), symbols );
782
783 try
784 {
785 wxArrayString aliases;
786 symTable()->EnumerateSymbolLib( aLibrary, aliases );
787
788 for( const auto& aliasName : aliases )
789 {
790 LIB_SYMBOL* alias = symTable()->LoadSymbol( aLibrary, aliasName );
791 symbols.insert( alias );
792 }
793 }
794 catch( const IO_ERROR& e )
795 {
796 wxString msg;
797
798 msg.Printf( _( "Cannot enumerate library '%s'." ), aLibrary );
799 DisplayErrorMessage( &m_frame, msg, e.What() );
800 }
801
802 return symbols;
803}
804
805
807{
808 auto it = m_libs.find( aLibrary );
809
810 if( it != m_libs.end() )
811 return it->second;
812
813 // The requested buffer does not exist yet, so create one
814 auto ret = m_libs.emplace( aLibrary, LIB_BUFFER( aLibrary ) );
815 LIB_BUFFER& buf = ret.first->second;
816
817 for( LIB_SYMBOL* symbol : getOriginalSymbols( aLibrary ) )
818 {
819 if( symbol->IsAlias() )
820 {
821 std::shared_ptr<LIB_SYMBOL> oldParent = symbol->GetParent().lock();
822
823 wxCHECK_MSG( oldParent, buf,
824 wxString::Format( "Derived symbol '%s' found with undefined parent.",
825 symbol->GetName() ) );
826
827 LIB_SYMBOL* libParent = buf.GetSymbol( oldParent->GetName() );
828
829 if( !libParent )
830 {
831 auto newParent = std::make_unique<LIB_SYMBOL>( *oldParent.get() );
832 libParent = newParent.get();
833 buf.CreateBuffer( std::move( newParent ), std::make_unique<SCH_SCREEN>() );
834 }
835
836 auto newSymbol = std::make_unique<LIB_SYMBOL>( *symbol );
837 newSymbol->SetParent( libParent );
838 buf.CreateBuffer( std::move( newSymbol ), std::make_unique<SCH_SCREEN>() );
839 }
840 else if( !buf.GetSymbol( symbol->GetName() ) )
841 {
842 buf.CreateBuffer( std::make_unique<LIB_SYMBOL>( *symbol ),
843 std::make_unique<SCH_SCREEN>() );
844 }
845 }
846
847 return buf;
848}
849
850
851bool SYMBOL_LIBRARY_MANAGER::UpdateLibraryBuffer( const wxString& aLibrary )
852{
853 try
854 {
855 m_libs.erase( aLibrary );
856 getLibraryBuffer( aLibrary );
857 }
858 catch(const std::exception& e)
859 {
860 wxLogError( _( "Error updating library buffer: %s" ), e.what() );
861 return false;
862 }
863 catch( const IO_ERROR& e )
864 {
865 wxLogError( _( "Error updating library buffer: %s" ), e.What() );
866 return false;
867 }
868 catch(...)
869 {
870 wxLogError( _( "Error updating library buffer." ) );
871 return false;
872 }
873
874 getLibraryBuffer( aLibrary );
875
876 return true;
877}
878
879
880SYMBOL_BUFFER::SYMBOL_BUFFER( std::unique_ptr<LIB_SYMBOL> aSymbol,
881 std::unique_ptr<SCH_SCREEN> aScreen ) :
882 m_screen( std::move( aScreen ) ), m_symbol( std::move( aSymbol ) )
883{
884 wxASSERT( m_symbol );
885 m_original = std::make_unique<LIB_SYMBOL>( *m_symbol );
886 wxASSERT( m_original );
887}
888
889
891{
892}
893
894
895void SYMBOL_BUFFER::SetSymbol( std::unique_ptr<LIB_SYMBOL> aSymbol )
896{
897 wxCHECK( m_symbol != aSymbol, /* void */ );
898 wxASSERT( aSymbol );
899 m_symbol = std::move( aSymbol );
900
901 // If the symbol moves libraries then the original moves with it
902 if( m_original->GetLibId().GetLibNickname() != m_symbol->GetLibId().GetLibNickname() )
903 {
904 m_original->SetLibId( LIB_ID( m_symbol->GetLibId().GetLibNickname(),
905 m_original->GetLibId().GetLibItemName() ) );
906 }
907}
908
909
910void SYMBOL_BUFFER::SetOriginal( std::unique_ptr<LIB_SYMBOL> aSymbol )
911{
912 wxCHECK( m_original != aSymbol, /* void */ );
913 wxASSERT( aSymbol );
914 m_original = std::move( aSymbol );
915
916 // The original is not allowed to have a different library than its symbol
917 if( m_original->GetLibId().GetLibNickname() != m_symbol->GetLibId().GetLibNickname() )
918 {
919 m_original->SetLibId( LIB_ID( m_symbol->GetLibId().GetLibNickname(),
920 m_original->GetLibId().GetLibItemName() ) );
921 }
922}
923
924
926{
927 return m_screen && m_screen->IsContentModified();
928}
929
930
931LIB_SYMBOL* LIB_BUFFER::GetSymbol( const wxString& aAlias ) const
932{
933 auto buf = GetBuffer( aAlias );
934
935 if( !buf )
936 return nullptr;
937
938 LIB_SYMBOL& symbol = buf->GetSymbol();
939 return &symbol;
940}
941
942
943bool LIB_BUFFER::CreateBuffer( std::unique_ptr<LIB_SYMBOL> aCopy,
944 std::unique_ptr<SCH_SCREEN> aScreen )
945{
946 wxASSERT( aCopy );
947 wxASSERT( aCopy->GetLib() == nullptr );
948
949 // Set the parent library name,
950 // otherwise it is empty as no library has been given as the owner during object construction
951 LIB_ID libId = aCopy->GetLibId();
952 libId.SetLibNickname( m_libName );
953 aCopy->SetLibId( libId );
954
955 auto symbolBuf = std::make_shared<SYMBOL_BUFFER>( std::move( aCopy ), std::move( aScreen ) );
956 m_symbols.push_back( symbolBuf );
957
958 ++m_hash;
959
960 return true;
961}
962
963
964bool LIB_BUFFER::UpdateBuffer( SYMBOL_BUFFER& aSymbolBuf, const LIB_SYMBOL& aCopy )
965{
966 LIB_SYMBOL& bufferedSymbol = aSymbolBuf.GetSymbol();
967
968 bufferedSymbol = aCopy;
969 ++m_hash;
970
971 return true;
972}
973
974
976{
977 const auto sameBufferPredicate = [&]( const std::shared_ptr<SYMBOL_BUFFER>& aBuf )
978 {
979 return aBuf.get() == &aSymbolBuf;
980 };
981
982 auto symbolBufIt = std::find_if( m_symbols.begin(), m_symbols.end(), sameBufferPredicate );
983 wxCHECK( symbolBufIt != m_symbols.end(), false );
984
985 bool retv = true;
986
987 // Remove all derived symbols to prevent broken inheritance.
988 if( HasDerivedSymbols( aSymbolBuf.GetSymbol().GetName() )
989 && ( removeChildSymbols( aSymbolBuf ) == 0 ) )
990 {
991 retv = false;
992 }
993
994 m_deleted.emplace_back( *symbolBufIt );
995 m_symbols.erase( symbolBufIt );
996 ++m_hash;
997
998 return retv;
999}
1000
1001
1002bool LIB_BUFFER::SaveBuffer( SYMBOL_BUFFER& aSymbolBuf, const wxString& aFileName, SCH_IO* aPlugin,
1003 bool aBuffer )
1004{
1005 wxCHECK( !aFileName.IsEmpty(), false );
1006
1007 wxString errorMsg = _( "Error saving symbol %s to library '%s'." ) + wxS( "\n%s" );
1008
1009 // Set properties to prevent saving the file on every symbol save.
1010 std::map<std::string, UTF8> properties;
1011 properties.emplace( SCH_IO_KICAD_LEGACY::PropBuffering, "" );
1012
1013 LIB_SYMBOL& libSymbol = aSymbolBuf.GetSymbol();
1014
1015 {
1016 LIB_SYMBOL& originalSymbol = aSymbolBuf.GetOriginal();
1017 const wxString originalName = originalSymbol.GetName();
1018
1019 // Delete the original symbol if the symbol name has been changed.
1020 if( libSymbol.GetName() != originalSymbol.GetName() )
1021 {
1022 try
1023 {
1024 if( aPlugin->LoadSymbol( aFileName, originalName ) )
1025 aPlugin->DeleteSymbol( aFileName, originalName, &properties );
1026 }
1027 catch( const IO_ERROR& ioe )
1028 {
1029 wxLogError( errorMsg, UnescapeString( originalName ), aFileName, ioe.What() );
1030 return false;
1031 }
1032 }
1033 }
1034
1035 LIB_SYMBOL* parentSymbol = nullptr;
1036
1037 if( libSymbol.IsAlias() )
1038 {
1039 LIB_SYMBOL* newCachedSymbol = new LIB_SYMBOL( libSymbol );
1040 std::shared_ptr<LIB_SYMBOL> bufferedParent = libSymbol.GetParent().lock();
1041 parentSymbol = newCachedSymbol;
1042
1043 wxCHECK( bufferedParent, false );
1044
1045 LIB_SYMBOL* cachedParent = nullptr;
1046
1047 try
1048 {
1049 cachedParent = aPlugin->LoadSymbol( aFileName, bufferedParent->GetName() );
1050 }
1051 catch( const IO_ERROR& )
1052 {
1053 return false;
1054 }
1055
1056 if( !cachedParent )
1057 {
1058 cachedParent = new LIB_SYMBOL( *bufferedParent.get() );
1059 newCachedSymbol->SetParent( cachedParent );
1060
1061 try
1062 {
1063 aPlugin->SaveSymbol( aFileName, cachedParent, aBuffer ? &properties : nullptr );
1064 }
1065 catch( const IO_ERROR& ioe )
1066 {
1067 wxLogError( errorMsg, UnescapeString( cachedParent->GetName() ), aFileName,
1068 ioe.What() );
1069 return false;
1070 }
1071
1072 try
1073 {
1074 aPlugin->SaveSymbol( aFileName, newCachedSymbol, aBuffer ? &properties : nullptr );
1075 }
1076 catch( const IO_ERROR& ioe )
1077 {
1078 wxLogError( errorMsg, UnescapeString( newCachedSymbol->GetName() ), aFileName,
1079 ioe.What() );
1080 return false;
1081 }
1082
1083 auto originalParent = std::make_unique<LIB_SYMBOL>( *bufferedParent.get() );
1084 LIB_SYMBOL& parentRef = *originalParent;
1085 aSymbolBuf.SetOriginal( std::move( originalParent ) );
1086
1087 auto newSymbol = std::make_unique<LIB_SYMBOL>( libSymbol );
1088 newSymbol->SetParent( &parentRef );
1089 aSymbolBuf.SetOriginal( std::move( newSymbol ) );
1090 }
1091 else
1092 {
1093 newCachedSymbol->SetParent( cachedParent );
1094
1095 try
1096 {
1097 aPlugin->SaveSymbol( aFileName, newCachedSymbol, aBuffer ? &properties : nullptr );
1098 }
1099 catch( const IO_ERROR& ioe )
1100 {
1101 wxLogError( errorMsg, UnescapeString( newCachedSymbol->GetName() ), aFileName,
1102 ioe.What() );
1103 return false;
1104 }
1105
1106 auto originalBufferedParent = GetBuffer( bufferedParent->GetName() );
1107 wxCHECK( originalBufferedParent, false );
1108
1109 auto newSymbol = std::make_unique<LIB_SYMBOL>( libSymbol );
1110 newSymbol->SetParent( &originalBufferedParent->GetSymbol() );
1111 aSymbolBuf.SetOriginal( std::move( newSymbol ) );
1112 }
1113 }
1114 else
1115 {
1116 parentSymbol = new LIB_SYMBOL( libSymbol );
1117
1118 try
1119 {
1120 aPlugin->SaveSymbol( aFileName, parentSymbol, aBuffer ? &properties : nullptr );
1121 }
1122 catch( const IO_ERROR& ioe )
1123 {
1124 wxLogError( errorMsg, UnescapeString( libSymbol.GetName() ), aFileName, ioe.What() );
1125 return false;
1126 }
1127
1128 aSymbolBuf.SetOriginal( std::make_unique<LIB_SYMBOL>( libSymbol ) );
1129 }
1130
1131 wxArrayString derivedSymbols;
1132
1133 // Reparent all symbols derived from the saved symbol.
1134 if( GetDerivedSymbolNames( libSymbol.GetName(), derivedSymbols ) != 0 )
1135 {
1136 // Save the derived symbols.
1137 for( const wxString& entry : derivedSymbols )
1138 {
1139 std::shared_ptr<SYMBOL_BUFFER> symbol = GetBuffer( entry );
1140
1141 wxCHECK2( symbol, continue );
1142
1143 LIB_SYMBOL* derivedSymbol = new LIB_SYMBOL( symbol->GetSymbol() );
1144 derivedSymbol->SetParent( parentSymbol );
1145
1146 try
1147 {
1148 aPlugin->SaveSymbol( aFileName, new LIB_SYMBOL( *derivedSymbol ),
1149 aBuffer ? &properties : nullptr );
1150 }
1151 catch( const IO_ERROR& ioe )
1152 {
1153 wxLogError( errorMsg, UnescapeString( derivedSymbol->GetName() ), aFileName,
1154 ioe.What() );
1155 return false;
1156 }
1157 }
1158 }
1159
1160 ++m_hash;
1161 return true;
1162}
1163
1164
1165std::shared_ptr<SYMBOL_BUFFER> LIB_BUFFER::GetBuffer( const wxString& aAlias ) const
1166{
1167 for( std::shared_ptr<SYMBOL_BUFFER> entry : m_symbols )
1168 {
1169 if( entry->GetSymbol().GetName() == aAlias )
1170 return entry;
1171 }
1172
1173 return std::shared_ptr<SYMBOL_BUFFER>( nullptr );
1174}
1175
1176
1177bool LIB_BUFFER::HasDerivedSymbols( const wxString& aParentName ) const
1178{
1179 for( const std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1180 {
1181 if( entry->GetSymbol().IsAlias() )
1182 {
1183 LIB_SYMBOL_SPTR parent = entry->GetSymbol().GetParent().lock();
1184
1185 // Check for inherited symbol without a valid parent.
1186 wxCHECK( parent, false );
1187
1188 if( parent->GetName() == aParentName )
1189 return true;
1190 }
1191 }
1192
1193 return false;
1194}
1195
1196
1197void LIB_BUFFER::GetSymbolNames( wxArrayString& aSymbolNames, SYMBOL_NAME_FILTER aFilter )
1198{
1199 for( std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1200 {
1201 const LIB_SYMBOL& symbol = entry->GetSymbol();
1202 if( ( symbol.IsAlias() && ( aFilter == SYMBOL_NAME_FILTER::ROOT_ONLY ) )
1203 || ( symbol.IsRoot() && ( aFilter == SYMBOL_NAME_FILTER::DERIVED_ONLY ) ) )
1204 {
1205 continue;
1206 }
1207 aSymbolNames.Add( UnescapeString( symbol.GetName() ) );
1208 }
1209}
1210
1211
1212size_t LIB_BUFFER::GetDerivedSymbolNames( const wxString& aSymbolName, wxArrayString& aList )
1213{
1214 wxCHECK( !aSymbolName.IsEmpty(), 0 );
1215
1216 for( std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1217 {
1218 const LIB_SYMBOL& symbol = entry->GetSymbol();
1219 if( symbol.IsAlias() )
1220 {
1221 LIB_SYMBOL_SPTR parent = symbol.GetParent().lock();
1222
1223 // Check for inherited symbol without a valid parent.
1224 wxCHECK2( parent, continue );
1225
1226 if( parent->GetName() == aSymbolName )
1227 {
1228 aList.Add( symbol.GetName() );
1229
1230 GetDerivedSymbolNames( symbol.GetName(), aList );
1231 }
1232 }
1233 }
1234
1235 return aList.GetCount();
1236}
1237
1238
1240{
1241 int cnt = 0;
1242 wxArrayString derivedSymbolNames;
1243 std::deque<std::shared_ptr<SYMBOL_BUFFER>>::iterator it;
1244
1245 if( GetDerivedSymbolNames( aSymbolBuf.GetSymbol().GetName(), derivedSymbolNames ) )
1246 {
1247 for( const wxString& symbolName : derivedSymbolNames )
1248 {
1249 it = std::find_if( m_symbols.begin(), m_symbols.end(),
1250 [symbolName]( std::shared_ptr<SYMBOL_BUFFER>& buf )
1251 {
1252 return buf->GetSymbol().GetName() == symbolName;
1253 } );
1254
1255 wxCHECK2( it != m_symbols.end(), continue );
1256
1257 m_deleted.emplace_back( *it );
1258 m_symbols.erase( it );
1259 cnt += 1;
1260 }
1261 }
1262
1263 return cnt;
1264}
const char * name
Definition: DXF_plotter.cpp:57
void SetContentModified(bool aModified=true)
Definition: base_screen.h:59
int ShowModal() override
void MessageSet(const wxString &message)
Add a message (in bold) to message list.
void AddHTML_Text(const wxString &message)
Add HTML text (without any change) to message list.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
virtual const wxString Problem() const
what was the problem?
Definition: exceptions.cpp:46
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
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)
Update the buffered symbol with the contents of aCopy.
bool DeleteBuffer(const SYMBOL_BUFFER &aSymbolBuf)
Delete the given symbol buffer from the library buffer.
void ClearDeletedBuffer()
Save stored modifications using a plugin.
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)
Return a symbol buffer with LIB_SYMBOL holding a symbolicular alias.
const std::deque< std::shared_ptr< SYMBOL_BUFFER > > & GetBuffers() const
std::shared_ptr< SYMBOL_BUFFER > GetBuffer(const wxString &aAlias) const
Return all buffered symbols.
LIB_SYMBOL * GetSymbol(const wxString &aAlias) const
Create a new buffer to store a symbol. LIB_BUFFER takes ownership of aCopy.
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)
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:99
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
Define a library symbol object.
Definition: lib_symbol.h:78
const LIB_ID & GetLibId() const override
Definition: lib_symbol.h:143
bool IsRoot() const override
For symbols derived from other symbols, IsRoot() indicates no derivation.
Definition: lib_symbol.h:194
bool IsAlias() const
Definition: lib_symbol.h:195
void SetParent(LIB_SYMBOL *aParent=nullptr)
Definition: lib_symbol.cpp:295
wxString GetName() const override
Definition: lib_symbol.h:137
void SetLibId(const LIB_ID &aLibId)
Definition: lib_symbol.h:144
LIB_SYMBOL_REF & GetParent()
Definition: lib_symbol.h:106
const wxString GetFullURI(bool aSubstituted=false) const
Return the full location specifying URI for the LIB, either in original UI form or in environment var...
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
bool InsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Adds aRow if it does not already exist or if doReplace is true.
bool RemoveRow(const LIB_TABLE_ROW *aRow)
Removes a row from the table and frees the pointer.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:49
A progress reporter interface for use in multi-threaded environments.
virtual bool KeepRefreshing(bool aWait=false)=0
Update the UI (if any).
static SYMBOL_LIB_TABLE * SchSymbolLibTable(PROJECT *aProject)
Accessor for project symbol library table.
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.
Definition: sch_io_mgr.cpp:83
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.
Definition: sch_io_mgr.cpp:140
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io.h:57
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
bool Done()
Returns a string containing any errors generated during the load.
const wxString & GetErrors() const
Represents a pair of <nickname, loaded parts list>
void Start()
Spins up threads to load all the libraries in m_nicknames.
bool Join()
Finalizes the threads and combines the output into the target output map.
Store a working copy of a library.
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)
LIB_SYMBOL * GetBufferedSymbol(const wxString &aAlias, const wxString &aLibrary)
Return the symbol copy from the buffer.
bool UpdateSymbolAfterRename(LIB_SYMBOL *aSymbol, const wxString &oldAlias, const wxString &aLibrary)
Update the symbol buffer with a new version of the symbol when the name has changed.
bool IsLibraryReadOnly(const wxString &aLibrary) const
Return true if the library is stored in a read-only file.
bool ClearSymbolModified(const wxString &aAlias, const wxString &aLibrary) const
Clear the modified flag for a symbol.
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 * GetAlias(const wxString &aAlias, 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...
SCH_SCREEN * GetScreen(const wxString &aAlias, const wxString &aLibrary)
Return the screen used to edit a specific symbol.
bool IsLibraryModified(const wxString &aLibrary) const
Return true if library has unsaved modifications.
wxArrayString GetLibraryNames() const
Return the array of library names.
void SetSymbolModified(const wxString &aAlias, const wxString &aLibrary)
bool SymbolExists(const wxString &aAlias, const wxString &aLibrary) const
Return true if symbol with a specific alias exists in library (either original one or buffered).
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.
bool RemoveSymbol(const wxString &aName, const wxString &aLibrary)
Remove the symbol from the symbol buffer.
static wxString getLibraryName(const wxString &aFilePath)
Helper function to add either existing or create new library.
virtual void OnDataChanged() const
Extract library name basing on the file name.
bool addLibrary(const wxString &aFilePath, bool aCreate, SYMBOL_LIB_TABLE &aTable)
Return the current Symbol Library Table.
bool IsSymbolModified(const wxString &aAlias, const wxString &aLibrary) const
Return true if symbol has unsaved modifications.
bool IsLibraryLoaded(const wxString &aLibrary) const
Return true if the library was successfully loaded.
SYMBOL_LIB_TABLE * symTable() const
Class to store a working copy of a LIB_SYMBOL object and editor context.
bool RevertLibrary(const wxString &aLibrary)
Revert unsaved changes for a symbolicular 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.
void Preload(PROGRESS_REPORTER &aReporter)
Preloads all symbol libraries in the symbol library table using SYMBOL_ASYNC_LOADER.
bool RevertAll()
Revert all pending changes.
SYMBOL_LIBRARY_MANAGER(SCH_BASE_FRAME &aFrame)
LIB_ID RevertSymbol(const wxString &aAlias, const wxString &aLibrary)
Revert unsaved changes for a symbolicular symbol.
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Find a single library within the (aggregate) library table.
SCH_BASE_FRAME & m_frame
Parent frame.
std::list< LIB_SYMBOL * > GetAliases(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 if symbol aSymbolName in library aLibraryName is a root symbol that has derived symbols.
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.
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_IO object i...
void LoadSymbolLib(std::vector< LIB_SYMBOL * > &aAliasList, const wxString &aNickname, bool aPowerSymbolsOnly=false)
bool IsSymbolLibWritable(const wxString &aNickname)
Return true if the library given by aNickname is writable.
bool IsSymbolLibLoaded(const wxString &aNickname)
Return true if the library given by aNickname was successfully loaded.
void CreateSymbolLib(const wxString &aNickname)
void EnumerateSymbolLib(const wxString &aNickname, wxArrayString &aAliasNames, bool aPowerSymbolsOnly=false)
Return a list of symbol alias names contained within the library given by aNickname.
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
SYMBOL_LIB_TABLE_ROW * FindRow(const wxString &aNickName, bool aCheckIfEnabled=false)
Return an SYMBOL_LIB_TABLE_ROW if aNickName is found in this table or in any chained fallBack table f...
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:195
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:71
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)
Definition: ki_exception.h:39
std::shared_ptr< LIB_SYMBOL > LIB_SYMBOL_SPTR
shared pointer to LIB_SYMBOL
Definition: lib_symbol.h:46
STL namespace.
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
see class PGM_BASE
wxString UnescapeString(const wxString &aSource)
Definition for symbol library class.
SYMBOL_NAME_FILTER
VECTOR3I res
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition: wx_filename.h:39