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 SYMBOL_LIB_TABLE_ROW* row = GetLibrary( libName );
141
142 if( !row || row->SchLibType() == SCH_IO_MGR::SCH_DATABASE )
143 continue;
144
145 res.Add( libName );
146 }
147
148 return res;
149}
150
151
153{
154 SYMBOL_LIB_TABLE_ROW* row = nullptr;
155
156 try
157 {
158 row = symTable()->FindRow( aLibrary, true );
159 }
160 catch( const IO_ERROR& e )
161 {
162 wxString msg;
163
164 msg.Printf( _( "Library '%s' not found in the Symbol Library Table." ), aLibrary );
165 DisplayErrorMessage( &m_frame, msg, e.What() );
166 }
167
168 return row;
169}
170
171
172bool SYMBOL_LIBRARY_MANAGER::SaveLibrary( const wxString& aLibrary, const wxString& aFileName,
173 SCH_IO_MGR::SCH_FILE_T aFileType )
174{
175 wxCHECK( aFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY, false );
176 wxCHECK_MSG( LibraryExists( aLibrary ), false,
177 wxString::Format( "Library missing: %s", aLibrary ) );
178
179 wxFileName fn( aFileName );
180 wxCHECK( !fn.FileExists() || fn.IsFileWritable(), false );
181
182 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( aFileType ) );
183 bool res = true; // assume all libraries are successfully saved
184 std::map<std::string, UTF8> properties;
185
186 properties.emplace( SCH_IO_KICAD_LEGACY::PropBuffering, "" );
187
188 auto it = m_libs.find( aLibrary );
189
190 if( it != m_libs.end() )
191 {
192 // Handle buffered library
193 LIB_BUFFER& libBuf = it->second;
194
195 const auto& symbolBuffers = libBuf.GetBuffers();
196
197 for( const std::shared_ptr<SYMBOL_BUFFER>& symbolBuf : symbolBuffers )
198 {
199 wxCHECK2( symbolBuf, continue );
200 if( !libBuf.SaveBuffer( *symbolBuf, aFileName, &*pi, true ) )
201 {
202 // Something went wrong, but try to save other libraries
203 res = false;
204 }
205 }
206
207 // clear the deleted symbols buffer only if data is saved to the original file
208 wxFileName original, destination( aFileName );
209 SYMBOL_LIB_TABLE_ROW* row = GetLibrary( aLibrary );
210
211 if( row )
212 {
213 original = row->GetFullURI();
214 original.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
215 }
216
217 destination.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
218
219 if( res && original == destination )
220 libBuf.ClearDeletedBuffer();
221 }
222 else
223 {
224 // Handle original library
225 for( LIB_SYMBOL* symbol : getOriginalSymbols( aLibrary ) )
226 {
227 LIB_SYMBOL* newSymbol;
228
229 try
230 {
231 if( symbol->IsAlias() )
232 {
233 std::shared_ptr< LIB_SYMBOL > oldParent = symbol->GetParent().lock();
234
235 wxCHECK_MSG( oldParent, false,
236 wxString::Format( wxT( "Derived symbol '%s' found with "
237 "undefined parent." ),
238 symbol->GetName() ) );
239
240 LIB_SYMBOL* libParent = pi->LoadSymbol( aLibrary, oldParent->GetName(),
241 &properties );
242
243 if( !libParent )
244 {
245 libParent = new LIB_SYMBOL( *oldParent.get() );
246 pi->SaveSymbol( aLibrary, libParent, &properties );
247 }
248
249 newSymbol = new LIB_SYMBOL( *symbol );
250 newSymbol->SetParent( libParent );
251 pi->SaveSymbol( aLibrary, newSymbol, &properties );
252 }
253 else if( !pi->LoadSymbol( aLibrary, symbol->GetName(), &properties ) )
254 {
255 pi->SaveSymbol( aLibrary, new LIB_SYMBOL( *symbol ), &properties );
256 }
257 }
258 catch( ... )
259 {
260 res = false;
261 break;
262 }
263 }
264 }
265
266 try
267 {
268 pi->SaveLibrary( aFileName );
269 }
270 catch( ... )
271 {
272 // return false because something happens.
273 // The library is not successfully saved
274 res = false;
275 }
276
277 return res;
278}
279
280
281bool SYMBOL_LIBRARY_MANAGER::IsLibraryModified( const wxString& aLibrary ) const
282{
283 auto it = m_libs.find( aLibrary );
284 return it != m_libs.end() ? it->second.IsModified() : false;
285}
286
287
288bool SYMBOL_LIBRARY_MANAGER::IsSymbolModified( const wxString& aAlias,
289 const wxString& aLibrary ) const
290{
291 auto libIt = m_libs.find( aLibrary );
292
293 if( libIt == m_libs.end() )
294 return false;
295
296 const LIB_BUFFER& buf = libIt->second;
297 const std::shared_ptr<SYMBOL_BUFFER> symbolBuf = buf.GetBuffer( aAlias );
298
299 return symbolBuf ? symbolBuf->IsModified() : false;
300}
301
302
303void SYMBOL_LIBRARY_MANAGER::SetSymbolModified( const wxString& aAlias, const wxString& aLibrary )
304{
305 auto libIt = m_libs.find( aLibrary );
306
307 if( libIt == m_libs.end() )
308 return;
309
310 const LIB_BUFFER& buf = libIt->second;
311 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = buf.GetBuffer( aAlias );
312
313 wxCHECK( symbolBuf, /* void */ );
314
315 symbolBuf->GetScreen()->SetContentModified();
316}
317
318
319bool SYMBOL_LIBRARY_MANAGER::ClearLibraryModified( const wxString& aLibrary ) const
320{
321 auto libIt = m_libs.find( aLibrary );
322
323 if( libIt == m_libs.end() )
324 return false;
325
326 for( auto& symbolBuf : libIt->second.GetBuffers() )
327 {
328 SCH_SCREEN* screen = symbolBuf->GetScreen();
329
330 if( screen )
331 screen->SetContentModified( false );
332 }
333
334 return true;
335}
336
337
339 const wxString& aLibrary ) const
340{
341 auto libIt = m_libs.find( aLibrary );
342
343 if( libIt == m_libs.end() )
344 return false;
345
346 auto symbolBuf = libIt->second.GetBuffer( aAlias );
347 wxCHECK( symbolBuf, false );
348
349 symbolBuf->GetScreen()->SetContentModified( false );
350 return true;
351}
352
353
354bool SYMBOL_LIBRARY_MANAGER::IsLibraryReadOnly( const wxString& aLibrary ) const
355{
356 wxCHECK_MSG( LibraryExists( aLibrary ), true,
357 wxString::Format( "Library missing: %s", aLibrary ) );
358
359 return !symTable()->IsSymbolLibWritable( aLibrary );
360}
361
362
363bool SYMBOL_LIBRARY_MANAGER::IsLibraryLoaded( const wxString& aLibrary ) const
364{
365 wxCHECK_MSG( LibraryExists( aLibrary ), false,
366 wxString::Format( "Library missing: %s", aLibrary ) );
367
368 return symTable()->IsSymbolLibLoaded( aLibrary );
369}
370
371
372std::list<LIB_SYMBOL*> SYMBOL_LIBRARY_MANAGER::GetAliases( const wxString& aLibrary ) const
373{
374 std::list<LIB_SYMBOL*> ret;
375 wxCHECK_MSG( LibraryExists( aLibrary ), ret,
376 wxString::Format( "Library missing: %s", aLibrary ) );
377
378 auto libIt = m_libs.find( aLibrary );
379
380 if( libIt != m_libs.end() )
381 {
382 for( const std::shared_ptr<SYMBOL_BUFFER>& symbolBuf : libIt->second.GetBuffers() )
383 ret.push_back( &symbolBuf->GetSymbol() );
384 }
385 else
386 {
387 std::vector<LIB_SYMBOL*> aliases;
388
389 try
390 {
391 symTable()->LoadSymbolLib( aliases, aLibrary );
392 }
393 catch( const IO_ERROR& e )
394 {
395 wxLogWarning( e.Problem() );
396 }
397
398 std::copy( aliases.begin(), aliases.end(), std::back_inserter( ret ) );
399 }
400
401 return ret;
402}
403
404
406 const wxString& aLibrary )
407{
408 wxCHECK_MSG( LibraryExists( aLibrary ), nullptr,
409 wxString::Format( "Library missing: %s, for alias %s", aLibrary, aAlias ) );
410
411 // try the library buffers first
412 LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
413 LIB_SYMBOL* bufferedSymbol = libBuf.GetSymbol( aAlias );
414
415 if( !bufferedSymbol ) // no buffer symbol found
416 {
417 // create a copy of the symbol
418 try
419 {
420 LIB_SYMBOL* symbol = symTable()->LoadSymbol( aLibrary, aAlias );
421
422 if( symbol == nullptr )
423 THROW_IO_ERROR( _( "Symbol not found." ) );
424
425 LIB_SYMBOL* bufferedParent = nullptr;
426
427 // Create parent symbols on demand so parent symbol can be set.
428 if( symbol->IsAlias() )
429 {
430 std::shared_ptr<LIB_SYMBOL> parent = symbol->GetParent().lock();
431 wxCHECK_MSG( parent, nullptr,
432 wxString::Format( "Derived symbol '%s' found with undefined parent.",
433 symbol->GetName() ) );
434
435 // Check if the parent symbol buffer has already be created.
436 bufferedParent = libBuf.GetSymbol( parent->GetName() );
437
438 if( !bufferedParent )
439 {
440 auto newParent = std::make_unique<LIB_SYMBOL>( *parent.get() );
441 bufferedParent = newParent.get();
442 libBuf.CreateBuffer( std::move( newParent ), std::make_unique<SCH_SCREEN>() );
443 }
444 }
445
446 auto newSymbol = std::make_unique<LIB_SYMBOL>( *symbol );
447 bufferedSymbol = newSymbol.get();
448
449 if( bufferedParent )
450 newSymbol->SetParent( bufferedParent );
451
452 libBuf.CreateBuffer( std::move( newSymbol ), std::make_unique<SCH_SCREEN>() );
453 }
454 catch( const IO_ERROR& e )
455 {
456 wxString msg;
457
458 msg.Printf( _( "Error loading symbol %s from library '%s'." ), aAlias, aLibrary );
459 DisplayErrorMessage( &m_frame, msg, e.What() );
460 bufferedSymbol = nullptr;
461 }
462 }
463
464 return bufferedSymbol;
465}
466
467
468SCH_SCREEN* SYMBOL_LIBRARY_MANAGER::GetScreen( const wxString& aAlias, const wxString& aLibrary )
469{
470 wxCHECK_MSG( LibraryExists( aLibrary ), nullptr,
471 wxString::Format( "Library missing: %s, for alias %s", aLibrary, aAlias ) );
472 wxCHECK_MSG( !aAlias.IsEmpty(), nullptr,
473 wxString::Format( "Alias missing in library: %s", aLibrary ) );
474 auto it = m_libs.find( aLibrary );
475 wxCHECK( it != m_libs.end(), nullptr );
476
477 LIB_BUFFER& buf = it->second;
478 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = buf.GetBuffer( aAlias );
479
480 return symbolBuf ? symbolBuf->GetScreen() : nullptr;
481}
482
483
484bool SYMBOL_LIBRARY_MANAGER::UpdateSymbol( LIB_SYMBOL* aSymbol, const wxString& aLibrary )
485{
486 wxCHECK_MSG( aSymbol, false, wxString::Format( "Null symbol in library: %s", aLibrary ) );
487 wxCHECK_MSG( LibraryExists( aLibrary ), false,
488 wxString::Format( "Library missing: %s, for smybol %s", aLibrary,
489 aSymbol->GetName() ) );
490
491 LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
492 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = libBuf.GetBuffer( aSymbol->GetName() );
493
494 if( symbolBuf ) // Existing symbol.
495 {
496 LIB_SYMBOL& bufferedSymbol = symbolBuf->GetSymbol();
497
498 // If we are coming from a different library, the library ID needs to be preserved
499 const LIB_ID libId = bufferedSymbol.GetLibId();
500 bufferedSymbol = *aSymbol;
501 bufferedSymbol.SetLibId( libId );
502
503 symbolBuf->GetScreen()->SetContentModified();
504 }
505 else // New symbol
506 {
507 auto symbolCopy = std::make_unique<LIB_SYMBOL>( *aSymbol, nullptr );
508
509 symbolCopy->SetLibId( LIB_ID( aLibrary, aSymbol->GetLibId().GetLibItemName() ) );
510
511 auto newScreen = std::make_unique<SCH_SCREEN>();
512 SCH_SCREEN& screen = *newScreen;
513 libBuf.CreateBuffer( std::move( symbolCopy ), std::move( newScreen ) );
514 screen.SetContentModified();
515 }
516
517 return true;
518}
519
520
521bool SYMBOL_LIBRARY_MANAGER::UpdateSymbolAfterRename( LIB_SYMBOL* aSymbol, const wxString& aOldName,
522 const wxString& aLibrary )
523{
524 LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
525 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = libBuf.GetBuffer( aOldName );
526
527 wxCHECK( symbolBuf && aSymbol, false );
528
529 libBuf.UpdateBuffer( *symbolBuf, *aSymbol );
531
532 return true;
533}
534
535
536LIB_ID SYMBOL_LIBRARY_MANAGER::RevertSymbol( const wxString& aAlias, const wxString& aLibrary )
537{
538 auto it = m_libs.find( aLibrary );
539
540 if( it == m_libs.end() ) // no items to flush
541 return LIB_ID( aLibrary, aAlias );
542
543 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = it->second.GetBuffer( aAlias );
544 wxCHECK( symbolBuf, LIB_ID( aLibrary, aAlias ) );
545 LIB_SYMBOL original( symbolBuf->GetOriginal() );
546
547 if( original.GetName() != aAlias )
548 {
549 UpdateSymbolAfterRename( &original, aAlias, aLibrary );
550 }
551 else
552 {
553 // copy the initial data to the current symbol to restore
554 symbolBuf->GetSymbol() = original;
556 }
557
558 return LIB_ID( aLibrary, original.GetName() );
559}
560
561
562bool SYMBOL_LIBRARY_MANAGER::RevertLibrary( const wxString& aLibrary )
563{
564 auto it = m_libs.find( aLibrary );
565
566 if( it == m_libs.end() ) // nothing to reverse
567 return false;
568
569 m_libs.erase( it );
571
572 return true;
573}
574
575
577{
578 bool retv = true;
579
580 // Nothing to revert.
581 if( GetHash() == 0 )
582 return true;
583
584 for( const auto& lib : m_libs )
585 {
586 if( !lib.second.IsModified() )
587 continue;
588
589 for( const std::shared_ptr<SYMBOL_BUFFER>& buffer : lib.second.GetBuffers() )
590 {
591 if( !buffer->IsModified() )
592 continue;
593
594 RevertSymbol( lib.first, buffer->GetOriginal().GetName() );
595 }
596 }
597
598 return retv;
599}
600
601
602bool SYMBOL_LIBRARY_MANAGER::RemoveSymbol( const wxString& aAlias, const wxString& aLibrary )
603{
604 LIB_BUFFER& libBuf = getLibraryBuffer( aLibrary );
605 std::shared_ptr<SYMBOL_BUFFER> symbolBuf = libBuf.GetBuffer( aAlias );
606 wxCHECK( symbolBuf, false );
607
608 bool retv = true;
609
610 retv &= libBuf.DeleteBuffer( *symbolBuf );
612
613 return retv;
614}
615
616
618 const wxString& aLibrary ) const
619{
620 // Try the library buffers first
621 auto libIt = m_libs.find( aLibrary );
622
623 if( libIt != m_libs.end() )
624 {
625 LIB_SYMBOL* symbol = libIt->second.GetSymbol( aAlias );
626
627 if( symbol )
628 return symbol;
629 }
630
631 // Get the original symbol
632 LIB_SYMBOL* alias = nullptr;
633
634 try
635 {
636 alias = symTable()->LoadSymbol( aLibrary, aAlias );
637 }
638 catch( const IO_ERROR& e )
639 {
640 wxString msg;
641
642 msg.Printf( _( "Cannot load symbol '%s' from library '%s'." ), aAlias, aLibrary );
643 DisplayErrorMessage( &m_frame, msg, e.What() );
644 }
645
646 return alias;
647}
648
649
650bool SYMBOL_LIBRARY_MANAGER::SymbolExists( const wxString& aAlias, const wxString& aLibrary ) const
651{
652 auto libBufIt = m_libs.find( aLibrary );
653 LIB_SYMBOL* alias = nullptr;
654
655 if( libBufIt != m_libs.end() )
656 return !!libBufIt->second.GetBuffer( aAlias );
657
658 try
659 {
660 alias = symTable()->LoadSymbol( aLibrary, aAlias );
661 }
662 catch( IO_ERROR& )
663 {
664 // checking if certain symbol exists, so its absence is perfectly fine
665 }
666
667 return alias != nullptr;
668}
669
670
671bool SYMBOL_LIBRARY_MANAGER::LibraryExists( const wxString& aLibrary, bool aCheckEnabled ) const
672{
673 if( aLibrary.IsEmpty() )
674 return false;
675
676 if( m_libs.count( aLibrary ) > 0 )
677 return true;
678
679 return symTable()->HasLibrary( aLibrary, aCheckEnabled );
680}
681
682
684{
685 wxString name = "New_Library";
686
687 if( !LibraryExists( name ) )
688 return name;
689
690 name += "_";
691
692 for( unsigned int i = 0; i < std::numeric_limits<unsigned int>::max(); ++i )
693 {
694 if( !LibraryExists( name + wxString::Format( "%u", i ) ) )
695 return name + wxString::Format( "%u", i );
696 }
697
698 wxFAIL;
699 return wxEmptyString;
700}
701
702
703void SYMBOL_LIBRARY_MANAGER::GetSymbolNames( const wxString& aLibraryName,
704 wxArrayString& aSymbolNames,
705 SYMBOL_NAME_FILTER aFilter )
706{
707 LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
708
709 libBuf.GetSymbolNames( aSymbolNames, aFilter );
710}
711
712
713bool SYMBOL_LIBRARY_MANAGER:: HasDerivedSymbols( const wxString& aSymbolName,
714 const wxString& aLibraryName )
715{
716 LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
717
718 return libBuf.HasDerivedSymbols( aSymbolName );
719}
720
721
723{
724 return symTable()->GetLogicalLibs().size();
725}
726
727
728wxString SYMBOL_LIBRARY_MANAGER::getLibraryName( const wxString& aFilePath )
729{
730 wxFileName fn( aFilePath );
731 return fn.GetName();
732}
733
734
735bool SYMBOL_LIBRARY_MANAGER::addLibrary( const wxString& aFilePath, bool aCreate,
736 SYMBOL_LIB_TABLE& aTable )
737{
738 wxString libName = getLibraryName( aFilePath );
739 wxCHECK( !LibraryExists( libName ), false ); // either create or add an existing one
740
741 // try to use path normalized to an environmental variable or project path
742 wxString relPath = NormalizePath( aFilePath, &Pgm().GetLocalEnvVariables(), &m_frame.Prj() );
743
744 SCH_IO_MGR::SCH_FILE_T schFileType = SCH_IO_MGR::GuessPluginTypeFromLibPath( aFilePath );
745
746 if( schFileType == SCH_IO_MGR::SCH_FILE_UNKNOWN )
747 schFileType = SCH_IO_MGR::SCH_LEGACY;
748
749 wxString typeName = SCH_IO_MGR::ShowType( schFileType );
750 SYMBOL_LIB_TABLE_ROW* libRow = new SYMBOL_LIB_TABLE_ROW( libName, relPath, typeName );
751 aTable.InsertRow( libRow );
752
753 if( aCreate )
754 {
755 wxCHECK( schFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY, false );
756
757 try
758 {
759 aTable.CreateSymbolLib( libName );
760 }
761 catch( const IO_ERROR& )
762 {
763 aTable.RemoveRow( libRow );
764 return false;
765 }
766 }
767
769
770 return true;
771}
772
773
775{
777}
778
779
780std::set<LIB_SYMBOL*> SYMBOL_LIBRARY_MANAGER::getOriginalSymbols( const wxString& aLibrary )
781{
782 std::set<LIB_SYMBOL*> symbols;
783 wxCHECK( LibraryExists( aLibrary ), symbols );
784
785 try
786 {
787 wxArrayString aliases;
788 symTable()->EnumerateSymbolLib( aLibrary, aliases );
789
790 for( const auto& aliasName : aliases )
791 {
792 LIB_SYMBOL* alias = symTable()->LoadSymbol( aLibrary, aliasName );
793 symbols.insert( alias );
794 }
795 }
796 catch( const IO_ERROR& e )
797 {
798 wxString msg;
799
800 msg.Printf( _( "Cannot enumerate library '%s'." ), aLibrary );
801 DisplayErrorMessage( &m_frame, msg, e.What() );
802 }
803
804 return symbols;
805}
806
807
809{
810 auto it = m_libs.find( aLibrary );
811
812 if( it != m_libs.end() )
813 return it->second;
814
815 // The requested buffer does not exist yet, so create one
816 auto ret = m_libs.emplace( aLibrary, LIB_BUFFER( aLibrary ) );
817 LIB_BUFFER& buf = ret.first->second;
818
819 for( LIB_SYMBOL* symbol : getOriginalSymbols( aLibrary ) )
820 {
821 if( symbol->IsAlias() )
822 {
823 std::shared_ptr<LIB_SYMBOL> oldParent = symbol->GetParent().lock();
824
825 wxCHECK_MSG( oldParent, buf,
826 wxString::Format( "Derived symbol '%s' found with undefined parent.",
827 symbol->GetName() ) );
828
829 LIB_SYMBOL* libParent = buf.GetSymbol( oldParent->GetName() );
830
831 if( !libParent )
832 {
833 auto newParent = std::make_unique<LIB_SYMBOL>( *oldParent.get() );
834 libParent = newParent.get();
835 buf.CreateBuffer( std::move( newParent ), std::make_unique<SCH_SCREEN>() );
836 }
837
838 auto newSymbol = std::make_unique<LIB_SYMBOL>( *symbol );
839 newSymbol->SetParent( libParent );
840 buf.CreateBuffer( std::move( newSymbol ), std::make_unique<SCH_SCREEN>() );
841 }
842 else if( !buf.GetSymbol( symbol->GetName() ) )
843 {
844 buf.CreateBuffer( std::make_unique<LIB_SYMBOL>( *symbol ),
845 std::make_unique<SCH_SCREEN>() );
846 }
847 }
848
849 return buf;
850}
851
852
853bool SYMBOL_LIBRARY_MANAGER::UpdateLibraryBuffer( const wxString& aLibrary )
854{
855 try
856 {
857 m_libs.erase( aLibrary );
858 getLibraryBuffer( aLibrary );
859 }
860 catch(const std::exception& e)
861 {
862 wxLogError( _( "Error updating library buffer: %s" ), e.what() );
863 return false;
864 }
865 catch( const IO_ERROR& e )
866 {
867 wxLogError( _( "Error updating library buffer: %s" ), e.What() );
868 return false;
869 }
870 catch(...)
871 {
872 wxLogError( _( "Error updating library buffer." ) );
873 return false;
874 }
875
876 getLibraryBuffer( aLibrary );
877
878 return true;
879}
880
881
882SYMBOL_BUFFER::SYMBOL_BUFFER( std::unique_ptr<LIB_SYMBOL> aSymbol,
883 std::unique_ptr<SCH_SCREEN> aScreen ) :
884 m_screen( std::move( aScreen ) ), m_symbol( std::move( aSymbol ) )
885{
886 wxASSERT( m_symbol );
887 m_original = std::make_unique<LIB_SYMBOL>( *m_symbol );
888 wxASSERT( m_original );
889}
890
891
893{
894}
895
896
897void SYMBOL_BUFFER::SetSymbol( std::unique_ptr<LIB_SYMBOL> aSymbol )
898{
899 wxCHECK( m_symbol != aSymbol, /* void */ );
900 wxASSERT( aSymbol );
901 m_symbol = std::move( aSymbol );
902
903 // If the symbol moves libraries then the original moves with it
904 if( m_original->GetLibId().GetLibNickname() != m_symbol->GetLibId().GetLibNickname() )
905 {
906 m_original->SetLibId( LIB_ID( m_symbol->GetLibId().GetLibNickname(),
907 m_original->GetLibId().GetLibItemName() ) );
908 }
909}
910
911
912void SYMBOL_BUFFER::SetOriginal( std::unique_ptr<LIB_SYMBOL> aSymbol )
913{
914 wxCHECK( m_original != aSymbol, /* void */ );
915 wxASSERT( aSymbol );
916 m_original = std::move( aSymbol );
917
918 // The original is not allowed to have a different library than its symbol
919 if( m_original->GetLibId().GetLibNickname() != m_symbol->GetLibId().GetLibNickname() )
920 {
921 m_original->SetLibId( LIB_ID( m_symbol->GetLibId().GetLibNickname(),
922 m_original->GetLibId().GetLibItemName() ) );
923 }
924}
925
926
928{
929 return m_screen && m_screen->IsContentModified();
930}
931
932
933LIB_SYMBOL* LIB_BUFFER::GetSymbol( const wxString& aAlias ) const
934{
935 auto buf = GetBuffer( aAlias );
936
937 if( !buf )
938 return nullptr;
939
940 LIB_SYMBOL& symbol = buf->GetSymbol();
941 return &symbol;
942}
943
944
945bool LIB_BUFFER::CreateBuffer( std::unique_ptr<LIB_SYMBOL> aCopy,
946 std::unique_ptr<SCH_SCREEN> aScreen )
947{
948 wxASSERT( aCopy );
949 wxASSERT( aCopy->GetLib() == nullptr );
950
951 // Set the parent library name,
952 // otherwise it is empty as no library has been given as the owner during object construction
953 LIB_ID libId = aCopy->GetLibId();
954 libId.SetLibNickname( m_libName );
955 aCopy->SetLibId( libId );
956
957 auto symbolBuf = std::make_shared<SYMBOL_BUFFER>( std::move( aCopy ), std::move( aScreen ) );
958 m_symbols.push_back( symbolBuf );
959
960 ++m_hash;
961
962 return true;
963}
964
965
966bool LIB_BUFFER::UpdateBuffer( SYMBOL_BUFFER& aSymbolBuf, const LIB_SYMBOL& aCopy )
967{
968 LIB_SYMBOL& bufferedSymbol = aSymbolBuf.GetSymbol();
969
970 bufferedSymbol = aCopy;
971 ++m_hash;
972
973 return true;
974}
975
976
978{
979 const auto sameBufferPredicate = [&]( const std::shared_ptr<SYMBOL_BUFFER>& aBuf )
980 {
981 return aBuf.get() == &aSymbolBuf;
982 };
983
984 auto symbolBufIt = std::find_if( m_symbols.begin(), m_symbols.end(), sameBufferPredicate );
985 wxCHECK( symbolBufIt != m_symbols.end(), false );
986
987 bool retv = true;
988
989 // Remove all derived symbols to prevent broken inheritance.
990 if( HasDerivedSymbols( aSymbolBuf.GetSymbol().GetName() )
991 && ( removeChildSymbols( aSymbolBuf ) == 0 ) )
992 {
993 retv = false;
994 }
995
996 m_deleted.emplace_back( *symbolBufIt );
997 m_symbols.erase( symbolBufIt );
998 ++m_hash;
999
1000 return retv;
1001}
1002
1003
1004bool LIB_BUFFER::SaveBuffer( SYMBOL_BUFFER& aSymbolBuf, const wxString& aFileName, SCH_IO* aPlugin,
1005 bool aBuffer )
1006{
1007 wxCHECK( !aFileName.IsEmpty(), false );
1008
1009 wxString errorMsg = _( "Error saving symbol %s to library '%s'." ) + wxS( "\n%s" );
1010
1011 // Set properties to prevent saving the file on every symbol save.
1012 std::map<std::string, UTF8> properties;
1013 properties.emplace( SCH_IO_KICAD_LEGACY::PropBuffering, "" );
1014
1015 LIB_SYMBOL& libSymbol = aSymbolBuf.GetSymbol();
1016
1017 {
1018 LIB_SYMBOL& originalSymbol = aSymbolBuf.GetOriginal();
1019 const wxString originalName = originalSymbol.GetName();
1020
1021 // Delete the original symbol if the symbol name has been changed.
1022 if( libSymbol.GetName() != originalSymbol.GetName() )
1023 {
1024 try
1025 {
1026 if( aPlugin->LoadSymbol( aFileName, originalName ) )
1027 aPlugin->DeleteSymbol( aFileName, originalName, &properties );
1028 }
1029 catch( const IO_ERROR& ioe )
1030 {
1031 wxLogError( errorMsg, UnescapeString( originalName ), aFileName, ioe.What() );
1032 return false;
1033 }
1034 }
1035 }
1036
1037 LIB_SYMBOL* parentSymbol = nullptr;
1038
1039 if( libSymbol.IsAlias() )
1040 {
1041 LIB_SYMBOL* newCachedSymbol = new LIB_SYMBOL( libSymbol );
1042 std::shared_ptr<LIB_SYMBOL> bufferedParent = libSymbol.GetParent().lock();
1043 parentSymbol = newCachedSymbol;
1044
1045 wxCHECK( bufferedParent, false );
1046
1047 LIB_SYMBOL* cachedParent = nullptr;
1048
1049 try
1050 {
1051 cachedParent = aPlugin->LoadSymbol( aFileName, bufferedParent->GetName() );
1052 }
1053 catch( const IO_ERROR& )
1054 {
1055 return false;
1056 }
1057
1058 if( !cachedParent )
1059 {
1060 cachedParent = new LIB_SYMBOL( *bufferedParent.get() );
1061 newCachedSymbol->SetParent( cachedParent );
1062
1063 try
1064 {
1065 aPlugin->SaveSymbol( aFileName, cachedParent, aBuffer ? &properties : nullptr );
1066 }
1067 catch( const IO_ERROR& ioe )
1068 {
1069 wxLogError( errorMsg, UnescapeString( cachedParent->GetName() ), aFileName,
1070 ioe.What() );
1071 return false;
1072 }
1073
1074 try
1075 {
1076 aPlugin->SaveSymbol( aFileName, newCachedSymbol, aBuffer ? &properties : nullptr );
1077 }
1078 catch( const IO_ERROR& ioe )
1079 {
1080 wxLogError( errorMsg, UnescapeString( newCachedSymbol->GetName() ), aFileName,
1081 ioe.What() );
1082 return false;
1083 }
1084
1085 auto originalParent = std::make_unique<LIB_SYMBOL>( *bufferedParent.get() );
1086 LIB_SYMBOL& parentRef = *originalParent;
1087 aSymbolBuf.SetOriginal( std::move( originalParent ) );
1088
1089 auto newSymbol = std::make_unique<LIB_SYMBOL>( libSymbol );
1090 newSymbol->SetParent( &parentRef );
1091 aSymbolBuf.SetOriginal( std::move( newSymbol ) );
1092 }
1093 else
1094 {
1095 newCachedSymbol->SetParent( cachedParent );
1096
1097 try
1098 {
1099 aPlugin->SaveSymbol( aFileName, newCachedSymbol, aBuffer ? &properties : nullptr );
1100 }
1101 catch( const IO_ERROR& ioe )
1102 {
1103 wxLogError( errorMsg, UnescapeString( newCachedSymbol->GetName() ), aFileName,
1104 ioe.What() );
1105 return false;
1106 }
1107
1108 auto originalBufferedParent = GetBuffer( bufferedParent->GetName() );
1109 wxCHECK( originalBufferedParent, false );
1110
1111 auto newSymbol = std::make_unique<LIB_SYMBOL>( libSymbol );
1112 newSymbol->SetParent( &originalBufferedParent->GetSymbol() );
1113 aSymbolBuf.SetOriginal( std::move( newSymbol ) );
1114 }
1115 }
1116 else
1117 {
1118 parentSymbol = new LIB_SYMBOL( libSymbol );
1119
1120 try
1121 {
1122 aPlugin->SaveSymbol( aFileName, parentSymbol, aBuffer ? &properties : nullptr );
1123 }
1124 catch( const IO_ERROR& ioe )
1125 {
1126 wxLogError( errorMsg, UnescapeString( libSymbol.GetName() ), aFileName, ioe.What() );
1127 return false;
1128 }
1129
1130 aSymbolBuf.SetOriginal( std::make_unique<LIB_SYMBOL>( libSymbol ) );
1131 }
1132
1133 wxArrayString derivedSymbols;
1134
1135 // Reparent all symbols derived from the saved symbol.
1136 if( GetDerivedSymbolNames( libSymbol.GetName(), derivedSymbols ) != 0 )
1137 {
1138 // Save the derived symbols.
1139 for( const wxString& entry : derivedSymbols )
1140 {
1141 std::shared_ptr<SYMBOL_BUFFER> symbol = GetBuffer( entry );
1142
1143 wxCHECK2( symbol, continue );
1144
1145 LIB_SYMBOL* derivedSymbol = new LIB_SYMBOL( symbol->GetSymbol() );
1146 derivedSymbol->SetParent( parentSymbol );
1147
1148 try
1149 {
1150 aPlugin->SaveSymbol( aFileName, new LIB_SYMBOL( *derivedSymbol ),
1151 aBuffer ? &properties : nullptr );
1152 }
1153 catch( const IO_ERROR& ioe )
1154 {
1155 wxLogError( errorMsg, UnescapeString( derivedSymbol->GetName() ), aFileName,
1156 ioe.What() );
1157 return false;
1158 }
1159 }
1160 }
1161
1162 ++m_hash;
1163 return true;
1164}
1165
1166
1167std::shared_ptr<SYMBOL_BUFFER> LIB_BUFFER::GetBuffer( const wxString& aAlias ) const
1168{
1169 for( std::shared_ptr<SYMBOL_BUFFER> entry : m_symbols )
1170 {
1171 if( entry->GetSymbol().GetName() == aAlias )
1172 return entry;
1173 }
1174
1175 return std::shared_ptr<SYMBOL_BUFFER>( nullptr );
1176}
1177
1178
1179bool LIB_BUFFER::HasDerivedSymbols( const wxString& aParentName ) const
1180{
1181 for( const std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1182 {
1183 if( entry->GetSymbol().IsAlias() )
1184 {
1185 LIB_SYMBOL_SPTR parent = entry->GetSymbol().GetParent().lock();
1186
1187 // Check for inherited symbol without a valid parent.
1188 wxCHECK( parent, false );
1189
1190 if( parent->GetName() == aParentName )
1191 return true;
1192 }
1193 }
1194
1195 return false;
1196}
1197
1198
1199void LIB_BUFFER::GetSymbolNames( wxArrayString& aSymbolNames, SYMBOL_NAME_FILTER aFilter )
1200{
1201 for( std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1202 {
1203 const LIB_SYMBOL& symbol = entry->GetSymbol();
1204 if( ( symbol.IsAlias() && ( aFilter == SYMBOL_NAME_FILTER::ROOT_ONLY ) )
1205 || ( symbol.IsRoot() && ( aFilter == SYMBOL_NAME_FILTER::DERIVED_ONLY ) ) )
1206 {
1207 continue;
1208 }
1209 aSymbolNames.Add( UnescapeString( symbol.GetName() ) );
1210 }
1211}
1212
1213
1214size_t LIB_BUFFER::GetDerivedSymbolNames( const wxString& aSymbolName, wxArrayString& aList )
1215{
1216 wxCHECK( !aSymbolName.IsEmpty(), 0 );
1217
1218 for( std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1219 {
1220 const LIB_SYMBOL& symbol = entry->GetSymbol();
1221 if( symbol.IsAlias() )
1222 {
1223 LIB_SYMBOL_SPTR parent = symbol.GetParent().lock();
1224
1225 // Check for inherited symbol without a valid parent.
1226 wxCHECK2( parent, continue );
1227
1228 if( parent->GetName() == aSymbolName )
1229 {
1230 aList.Add( symbol.GetName() );
1231
1232 GetDerivedSymbolNames( symbol.GetName(), aList );
1233 }
1234 }
1235 }
1236
1237 return aList.GetCount();
1238}
1239
1240
1242{
1243 int cnt = 0;
1244 wxArrayString derivedSymbolNames;
1245 std::deque<std::shared_ptr<SYMBOL_BUFFER>>::iterator it;
1246
1247 if( GetDerivedSymbolNames( aSymbolBuf.GetSymbol().GetName(), derivedSymbolNames ) )
1248 {
1249 for( const wxString& symbolName : derivedSymbolNames )
1250 {
1251 it = std::find_if( m_symbols.begin(), m_symbols.end(),
1252 [symbolName]( std::shared_ptr<SYMBOL_BUFFER>& buf )
1253 {
1254 return buf->GetSymbol().GetName() == symbolName;
1255 } );
1256
1257 wxCHECK2( it != m_symbols.end(), continue );
1258
1259 m_deleted.emplace_back( *it );
1260 m_symbols.erase( it );
1261 cnt += 1;
1262 }
1263 }
1264
1265 return cnt;
1266}
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...
LIB_T SchLibType() const
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