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