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