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
53
54
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
747size_t SYMBOL_LIBRARY_MANAGER::GetDerivedSymbolNames( const wxString& aSymbolName, const wxString& aLibraryName,
748 wxArrayString& aList )
749{
750 LIB_BUFFER& libBuf = getLibraryBuffer( aLibraryName );
751
752 return libBuf.GetDerivedSymbolNames( aSymbolName, aList );
753}
754
755
757{
758 return symTable()->GetLogicalLibs().size();
759}
760
761
762wxString SYMBOL_LIBRARY_MANAGER::getLibraryName( const wxString& aFilePath )
763{
764 wxFileName fn( aFilePath );
765 return fn.GetName();
766}
767
768
769bool SYMBOL_LIBRARY_MANAGER::addLibrary( const wxString& aFilePath, bool aCreate,
770 SYMBOL_LIB_TABLE& aTable )
771{
772 wxString libName = getLibraryName( aFilePath );
773 wxCHECK( !LibraryExists( libName ), false ); // either create or add an existing one
774
775 // try to use path normalized to an environmental variable or project path
776 wxString relPath = NormalizePath( aFilePath, &Pgm().GetLocalEnvVariables(), &m_frame.Prj() );
777
778 SCH_IO_MGR::SCH_FILE_T schFileType = SCH_IO_MGR::GuessPluginTypeFromLibPath( aFilePath,
779 aCreate ? KICTL_CREATE : 0 );
780
781 if( schFileType == SCH_IO_MGR::SCH_FILE_UNKNOWN )
782 schFileType = SCH_IO_MGR::SCH_LEGACY;
783
784 wxString typeName = SCH_IO_MGR::ShowType( schFileType );
785 SYMBOL_LIB_TABLE_ROW* libRow = new SYMBOL_LIB_TABLE_ROW( libName, relPath, typeName );
786 aTable.InsertRow( libRow );
787
788 if( aCreate )
789 {
790 wxCHECK( schFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY, false );
791
792 try
793 {
794 aTable.CreateSymbolLib( libName );
795 }
796 catch( const IO_ERROR& )
797 {
798 aTable.RemoveRow( libRow );
799 return false;
800 }
801 }
802
804
805 return true;
806}
807
808
813
814
815std::set<LIB_SYMBOL*> SYMBOL_LIBRARY_MANAGER::getOriginalSymbols( const wxString& aLibrary )
816{
817 std::set<LIB_SYMBOL*> symbols;
818 wxCHECK( LibraryExists( aLibrary ), symbols );
819
820 try
821 {
822 wxArrayString symbolNames;
823 symTable()->EnumerateSymbolLib( aLibrary, symbolNames );
824
825 for( const auto& symbolName : symbolNames )
826 {
827 LIB_SYMBOL* symbol = symTable()->LoadSymbol( aLibrary, symbolName );
828 symbols.insert( symbol );
829 }
830 }
831 catch( const IO_ERROR& e )
832 {
833 wxLogError( wxString::Format( _( "Cannot enumerate library '%s'." ), aLibrary ), e.What() );
834 }
835
836 return symbols;
837}
838
839
841{
842 auto it = m_libs.find( aLibrary );
843
844 if( it != m_libs.end() )
845 return it->second;
846
847 // The requested buffer does not exist yet, so create one
848 auto ret = m_libs.emplace( aLibrary, LIB_BUFFER( aLibrary ) );
849 LIB_BUFFER& buf = ret.first->second;
850
851 for( LIB_SYMBOL* symbol : getOriginalSymbols( aLibrary ) )
852 {
853 if( symbol->IsDerived() )
854 {
855 std::shared_ptr<LIB_SYMBOL> oldParent = symbol->GetParent().lock();
856
857 wxCHECK_MSG( oldParent, buf, wxString::Format( "Derived symbol '%s' found with undefined parent.",
858 symbol->GetName() ) );
859
860 LIB_SYMBOL* libParent = buf.GetSymbol( oldParent->GetName() );
861
862 if( !libParent )
863 {
864 std::unique_ptr<LIB_SYMBOL> newParent = std::make_unique<LIB_SYMBOL>( *oldParent.get() );
865 libParent = newParent.get();
866 buf.CreateBuffer( std::move( newParent ), std::make_unique<SCH_SCREEN>() );
867 }
868
869 std::unique_ptr<LIB_SYMBOL> newSymbol = std::make_unique<LIB_SYMBOL>( *symbol );
870 newSymbol->SetParent( libParent );
871 buf.CreateBuffer( std::move( newSymbol ), std::make_unique<SCH_SCREEN>() );
872 }
873 else if( !buf.GetSymbol( symbol->GetName() ) )
874 {
875 buf.CreateBuffer( std::make_unique<LIB_SYMBOL>( *symbol ), std::make_unique<SCH_SCREEN>() );
876 }
877 }
878
879 return buf;
880}
881
882
883bool SYMBOL_LIBRARY_MANAGER::UpdateLibraryBuffer( const wxString& aLibrary )
884{
885 try
886 {
887 m_libs.erase( aLibrary );
888 getLibraryBuffer( aLibrary );
889 }
890 catch(const std::exception& e)
891 {
892 wxLogError( _( "Error updating library buffer: %s" ), e.what() );
893 return false;
894 }
895 catch( const IO_ERROR& e )
896 {
897 wxLogError( _( "Error updating library buffer: %s" ), e.What() );
898 return false;
899 }
900 catch(...)
901 {
902 wxLogError( _( "Error updating library buffer." ) );
903 return false;
904 }
905
906 getLibraryBuffer( aLibrary );
907
908 return true;
909}
910
911
912SYMBOL_BUFFER::SYMBOL_BUFFER( std::unique_ptr<LIB_SYMBOL> aSymbol,
913 std::unique_ptr<SCH_SCREEN> aScreen ) :
914 m_screen( std::move( aScreen ) ),
915 m_symbol( std::move( aSymbol ) )
916{
917 wxASSERT( m_symbol );
918 m_original = std::make_unique<LIB_SYMBOL>( *m_symbol );
919 wxASSERT( m_original );
920}
921
922
926
927
928void SYMBOL_BUFFER::SetSymbol( std::unique_ptr<LIB_SYMBOL> aSymbol )
929{
930 wxCHECK( m_symbol != aSymbol, /* void */ );
931 wxASSERT( aSymbol );
932 m_symbol = std::move( aSymbol );
933
934 // If the symbol moves libraries then the original moves with it
935 if( m_original->GetLibId().GetLibNickname() != m_symbol->GetLibId().GetLibNickname() )
936 {
937 m_original->SetLibId( LIB_ID( m_symbol->GetLibId().GetLibNickname(),
938 m_original->GetLibId().GetLibItemName() ) );
939 }
940}
941
942
943void SYMBOL_BUFFER::SetOriginal( std::unique_ptr<LIB_SYMBOL> aSymbol )
944{
945 wxCHECK( m_original != aSymbol, /* void */ );
946 wxASSERT( aSymbol );
947 m_original = std::move( aSymbol );
948
949 // The original is not allowed to have a different library than its symbol
950 if( m_original->GetLibId().GetLibNickname() != m_symbol->GetLibId().GetLibNickname() )
951 {
952 m_original->SetLibId( LIB_ID( m_symbol->GetLibId().GetLibNickname(),
953 m_original->GetLibId().GetLibItemName() ) );
954 }
955}
956
957
959{
960 return m_screen && m_screen->IsContentModified();
961}
962
963
964LIB_SYMBOL* LIB_BUFFER::GetSymbol( const wxString& aAlias ) const
965{
966 auto buf = GetBuffer( aAlias );
967
968 if( !buf )
969 return nullptr;
970
971 LIB_SYMBOL& symbol = buf->GetSymbol();
972 return &symbol;
973}
974
975
976bool LIB_BUFFER::CreateBuffer( std::unique_ptr<LIB_SYMBOL> aCopy,
977 std::unique_ptr<SCH_SCREEN> aScreen )
978{
979 wxASSERT( aCopy );
980 wxASSERT( aCopy->GetLib() == nullptr );
981
982 // Set the parent library name,
983 // otherwise it is empty as no library has been given as the owner during object construction
984 LIB_ID libId = aCopy->GetLibId();
985 libId.SetLibNickname( m_libName );
986 aCopy->SetLibId( libId );
987
988 auto symbolBuf = std::make_shared<SYMBOL_BUFFER>( std::move( aCopy ), std::move( aScreen ) );
989 m_symbols.push_back( std::move( symbolBuf ) );
990
991 ++m_hash;
992
993 return true;
994}
995
996
997bool LIB_BUFFER::UpdateBuffer( SYMBOL_BUFFER& aSymbolBuf, const LIB_SYMBOL& aCopy )
998{
999 LIB_SYMBOL& bufferedSymbol = aSymbolBuf.GetSymbol();
1000
1001 bufferedSymbol = aCopy;
1002 ++m_hash;
1003
1004 return true;
1005}
1006
1007
1009{
1010 const auto sameBufferPredicate = [&]( const std::shared_ptr<SYMBOL_BUFFER>& aBuf )
1011 {
1012 return aBuf.get() == &aSymbolBuf;
1013 };
1014
1015 auto symbolBufIt = std::find_if( m_symbols.begin(), m_symbols.end(), sameBufferPredicate );
1016 wxCHECK( symbolBufIt != m_symbols.end(), false );
1017
1018 bool retv = true;
1019
1020 // Remove all derived symbols to prevent broken inheritance.
1021 if( HasDerivedSymbols( aSymbolBuf.GetSymbol().GetName() )
1022 && ( removeChildSymbols( aSymbolBuf ) == 0 ) )
1023 {
1024 retv = false;
1025 }
1026
1027 m_deleted.emplace_back( *symbolBufIt );
1028 m_symbols.erase( symbolBufIt );
1029 ++m_hash;
1030
1031 return retv;
1032}
1033
1034
1035bool LIB_BUFFER::SaveBuffer( SYMBOL_BUFFER& aSymbolBuf, const wxString& aFileName, SCH_IO* aPlugin,
1036 bool aBuffer )
1037{
1038 wxCHECK( !aFileName.IsEmpty(), false );
1039
1040 wxString errorMsg = _( "Error saving symbol %s to library '%s'." ) + wxS( "\n%s" );
1041
1042 // Set properties to prevent saving the file on every symbol save.
1043 std::map<std::string, UTF8> properties;
1044 properties.emplace( SCH_IO_KICAD_LEGACY::PropBuffering, "" );
1045
1046 LIB_SYMBOL& libSymbol = aSymbolBuf.GetSymbol();
1047
1048 {
1049 LIB_SYMBOL& originalSymbol = aSymbolBuf.GetOriginal();
1050 const wxString originalName = originalSymbol.GetName();
1051
1052 // Delete the original symbol if the symbol name has been changed.
1053 if( libSymbol.GetName() != originalSymbol.GetName() )
1054 {
1055 try
1056 {
1057 if( aPlugin->LoadSymbol( aFileName, originalName ) )
1058 aPlugin->DeleteSymbol( aFileName, originalName, &properties );
1059 }
1060 catch( const IO_ERROR& ioe )
1061 {
1062 wxLogError( errorMsg, UnescapeString( originalName ), aFileName, ioe.What() );
1063 return false;
1064 }
1065 }
1066 }
1067
1068 LIB_SYMBOL* parentSymbol = nullptr;
1069
1070 if( libSymbol.IsDerived() )
1071 {
1072 LIB_SYMBOL* newCachedSymbol = new LIB_SYMBOL( libSymbol );
1073 std::shared_ptr<LIB_SYMBOL> bufferedParent = libSymbol.GetParent().lock();
1074 parentSymbol = newCachedSymbol;
1075
1076 wxCHECK( bufferedParent, false );
1077
1078 LIB_SYMBOL* cachedParent = nullptr;
1079
1080 try
1081 {
1082 cachedParent = aPlugin->LoadSymbol( aFileName, bufferedParent->GetName() );
1083 }
1084 catch( const IO_ERROR& )
1085 {
1086 return false;
1087 }
1088
1089 if( !cachedParent )
1090 {
1091 cachedParent = new LIB_SYMBOL( *bufferedParent.get() );
1092 newCachedSymbol->SetParent( cachedParent );
1093
1094 try
1095 {
1096 aPlugin->SaveSymbol( aFileName, cachedParent, aBuffer ? &properties : nullptr );
1097 }
1098 catch( const IO_ERROR& ioe )
1099 {
1100 wxLogError( errorMsg, UnescapeString( cachedParent->GetName() ), aFileName,
1101 ioe.What() );
1102 return false;
1103 }
1104
1105 try
1106 {
1107 aPlugin->SaveSymbol( aFileName, newCachedSymbol, aBuffer ? &properties : nullptr );
1108 }
1109 catch( const IO_ERROR& ioe )
1110 {
1111 wxLogError( errorMsg, UnescapeString( newCachedSymbol->GetName() ), aFileName,
1112 ioe.What() );
1113 return false;
1114 }
1115
1116 auto originalParent = std::make_unique<LIB_SYMBOL>( *bufferedParent.get() );
1117 LIB_SYMBOL& parentRef = *originalParent;
1118 aSymbolBuf.SetOriginal( std::move( originalParent ) );
1119
1120 auto newSymbol = std::make_unique<LIB_SYMBOL>( libSymbol );
1121 newSymbol->SetParent( &parentRef );
1122 aSymbolBuf.SetOriginal( std::move( newSymbol ) );
1123 }
1124 else
1125 {
1126 newCachedSymbol->SetParent( cachedParent );
1127
1128 try
1129 {
1130 aPlugin->SaveSymbol( aFileName, newCachedSymbol, aBuffer ? &properties : nullptr );
1131 }
1132 catch( const IO_ERROR& ioe )
1133 {
1134 wxLogError( errorMsg, UnescapeString( newCachedSymbol->GetName() ), aFileName,
1135 ioe.What() );
1136 return false;
1137 }
1138
1139 auto originalBufferedParent = GetBuffer( bufferedParent->GetName() );
1140 wxCHECK( originalBufferedParent, false );
1141
1142 auto newSymbol = std::make_unique<LIB_SYMBOL>( libSymbol );
1143 newSymbol->SetParent( &originalBufferedParent->GetSymbol() );
1144 aSymbolBuf.SetOriginal( std::move( newSymbol ) );
1145 }
1146 }
1147 else
1148 {
1149 parentSymbol = new LIB_SYMBOL( libSymbol );
1150
1151 try
1152 {
1153 aPlugin->SaveSymbol( aFileName, parentSymbol, aBuffer ? &properties : nullptr );
1154 }
1155 catch( const IO_ERROR& ioe )
1156 {
1157 wxLogError( errorMsg, UnescapeString( libSymbol.GetName() ), aFileName, ioe.What() );
1158 return false;
1159 }
1160
1161 aSymbolBuf.SetOriginal( std::make_unique<LIB_SYMBOL>( libSymbol ) );
1162 }
1163
1164 wxArrayString derivedSymbols;
1165
1166 // Reparent all symbols derived from the saved symbol.
1167 if( GetDerivedSymbolNames( libSymbol.GetName(), derivedSymbols ) != 0 )
1168 {
1169 // Save the derived symbols.
1170 for( const wxString& entry : derivedSymbols )
1171 {
1172 std::shared_ptr<SYMBOL_BUFFER> symbol = GetBuffer( entry );
1173
1174 wxCHECK2( symbol, continue );
1175
1176 LIB_SYMBOL* derivedSymbol = new LIB_SYMBOL( symbol->GetSymbol() );
1177 derivedSymbol->SetParent( parentSymbol );
1178
1179 try
1180 {
1181 aPlugin->SaveSymbol( aFileName, new LIB_SYMBOL( *derivedSymbol ),
1182 aBuffer ? &properties : nullptr );
1183 }
1184 catch( const IO_ERROR& ioe )
1185 {
1186 wxLogError( errorMsg, UnescapeString( derivedSymbol->GetName() ), aFileName,
1187 ioe.What() );
1188 return false;
1189 }
1190 }
1191 }
1192
1193 ++m_hash;
1194 return true;
1195}
1196
1197
1198std::shared_ptr<SYMBOL_BUFFER> LIB_BUFFER::GetBuffer( const wxString& aSymbolName ) const
1199{
1200 for( std::shared_ptr<SYMBOL_BUFFER> entry : m_symbols )
1201 {
1202 if( entry->GetSymbol().GetName() == aSymbolName )
1203 return entry;
1204 }
1205
1206 return std::shared_ptr<SYMBOL_BUFFER>( nullptr );
1207}
1208
1209
1210bool LIB_BUFFER::HasDerivedSymbols( const wxString& aParentName ) const
1211{
1212 for( const std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1213 {
1214 if( entry->GetSymbol().IsDerived() )
1215 {
1216 LIB_SYMBOL_SPTR parent = entry->GetSymbol().GetParent().lock();
1217
1218 // Check for inherited symbol without a valid parent.
1219 wxCHECK( parent, false );
1220
1221 if( parent->GetName() == aParentName )
1222 return true;
1223 }
1224 }
1225
1226 return false;
1227}
1228
1229
1230void LIB_BUFFER::GetSymbolNames( wxArrayString& aSymbolNames, SYMBOL_NAME_FILTER aFilter )
1231{
1232 for( std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1233 {
1234 const LIB_SYMBOL& symbol = entry->GetSymbol();
1235 if( ( symbol.IsDerived() && ( aFilter == SYMBOL_NAME_FILTER::ROOT_ONLY ) )
1236 || ( symbol.IsRoot() && ( aFilter == SYMBOL_NAME_FILTER::DERIVED_ONLY ) ) )
1237 {
1238 continue;
1239 }
1240 aSymbolNames.Add( UnescapeString( symbol.GetName() ) );
1241 }
1242}
1243
1244
1245size_t LIB_BUFFER::GetDerivedSymbolNames( const wxString& aSymbolName, wxArrayString& aList )
1246{
1247 wxCHECK( !aSymbolName.IsEmpty(), 0 );
1248
1249 // Parent: children map
1250 std::unordered_map<LIB_SYMBOL_SPTR, std::vector<LIB_SYMBOL_SPTR>> derivedMap;
1251
1252 // Iterate the library once to resolve all derived symbol links.
1253 // This means we only need to iterate the library once, and we can then look up the links
1254 // as needed.
1255 for( std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1256 {
1257 LIB_SYMBOL_SPTR symbol = entry->GetSymbol().SharedPtr();
1258
1259 if( symbol->IsDerived() )
1260 {
1261 LIB_SYMBOL_SPTR parent = symbol->GetParent().lock();
1262
1263 // Check for inherited symbol without a valid parent.
1264 wxCHECK2( parent, continue );
1265
1266 derivedMap[parent].emplace_back( std::move( symbol ) );
1267 }
1268 }
1269
1270 const auto visit = [&]( LIB_SYMBOL& aSymbol )
1271 {
1272 aList.Add( aSymbol.GetName() );
1273 };
1274
1275 // Assign to std::function to allow recursion
1276 const std::function<void( LIB_SYMBOL_SPTR& )> getDerived =
1277 [&]( LIB_SYMBOL_SPTR& aSymbol )
1278 {
1279 auto it = derivedMap.find( aSymbol );
1280
1281 if( it != derivedMap.end() )
1282 {
1283 for( LIB_SYMBOL_SPTR& derivedSymbol : it->second )
1284 {
1285 visit( *derivedSymbol );
1286
1287 // Recurse to get symbols derived from this one
1288 getDerived( derivedSymbol );
1289 }
1290 }
1291 };
1292
1293 // Start the recursion at the top
1294 LIB_SYMBOL_SPTR symbol = GetSymbol( aSymbolName )->SharedPtr();
1295 getDerived( symbol );
1296
1297 return aList.GetCount();
1298}
1299
1300
1302{
1303 int cnt = 0;
1304 wxArrayString derivedSymbolNames;
1305 std::deque<std::shared_ptr<SYMBOL_BUFFER>>::iterator it;
1306
1307 if( GetDerivedSymbolNames( aSymbolBuf.GetSymbol().GetName(), derivedSymbolNames ) )
1308 {
1309 for( const wxString& symbolName : derivedSymbolNames )
1310 {
1311 it = std::find_if( m_symbols.begin(), m_symbols.end(),
1312 [symbolName]( std::shared_ptr<SYMBOL_BUFFER>& buf )
1313 {
1314 return buf->GetSymbol().GetName() == symbolName;
1315 } );
1316
1317 wxCHECK2( it != m_symbols.end(), continue );
1318
1319 m_deleted.emplace_back( *it );
1320 m_symbols.erase( it );
1321 cnt += 1;
1322 }
1323 }
1324
1325 return cnt;
1326}
const char * name
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.
virtual const wxString What() const
A composite of Problem() and Where()
virtual const wxString Problem() const
what was the problem?
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:154
bool IsRoot() const override
For symbols derived from other symbols, IsRoot() indicates no derivation.
Definition lib_symbol.h:205
bool IsDerived() const
Definition lib_symbol.h:206
void SetParent(LIB_SYMBOL *aParent=nullptr)
wxString GetName() const override
Definition lib_symbol.h:148
LIB_SYMBOL_SPTR SharedPtr() const
http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/sp_techniques.html#weak_without_shared.
Definition lib_symbol.h:95
void SetLibId(const LIB_ID &aLibId)
Definition lib_symbol.h:155
LIB_SYMBOL_REF & GetParent()
Definition lib_symbol.h:117
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.
static SCH_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath, int aCtl=0)
Return a plugin type given a symbol library using the file extension of aLibPath.
Base class that schematic file and library loading and saving plugins should derive from.
Definition sch_io.h: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.
size_t GetDerivedSymbolNames(const wxString &aSymbolName, const wxString &aLibraryName, wxArrayString &aList)
Fetch all of the symbols derived from a aSymbolName into aList.
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...
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)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define KICTL_CREATE
caller thinks requested project files may not exist.
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:913
see class PGM_BASE
wxString UnescapeString(const wxString &aSource)
Definition for symbol library class.
VECTOR3I res
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition wx_filename.h:39