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