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(...)
856 {
857 wxLogError( _( "Error updating library buffer." ) );
858 return false;
859 }
860
861 getLibraryBuffer( aLibrary );
862
863 return true;
864}
865
866
867SYMBOL_BUFFER::SYMBOL_BUFFER( std::unique_ptr<LIB_SYMBOL> aSymbol,
868 std::unique_ptr<SCH_SCREEN> aScreen ) :
869 m_screen( std::move( aScreen ) ),
870 m_symbol( std::move( aSymbol ) )
871{
872 wxASSERT( m_symbol );
873 m_original = std::make_unique<LIB_SYMBOL>( *m_symbol );
874 wxASSERT( m_original );
875}
876
877
881
882
883void SYMBOL_BUFFER::SetSymbol( std::unique_ptr<LIB_SYMBOL> aSymbol )
884{
885 wxCHECK( m_symbol != aSymbol, /* void */ );
886 wxASSERT( aSymbol );
887 m_symbol = std::move( aSymbol );
888
889 // If the symbol moves libraries then the original moves with it
890 if( m_original->GetLibId().GetLibNickname() != m_symbol->GetLibId().GetLibNickname() )
891 {
892 m_original->SetLibId( LIB_ID( m_symbol->GetLibId().GetLibNickname(),
893 m_original->GetLibId().GetLibItemName() ) );
894 }
895}
896
897
898void SYMBOL_BUFFER::SetOriginal( std::unique_ptr<LIB_SYMBOL> aSymbol )
899{
900 wxCHECK( m_original != aSymbol, /* void */ );
901 wxASSERT( aSymbol );
902 m_original = std::move( aSymbol );
903
904 // The original is not allowed to have a different library than its symbol
905 if( m_original->GetLibId().GetLibNickname() != m_symbol->GetLibId().GetLibNickname() )
906 {
907 m_original->SetLibId( LIB_ID( m_symbol->GetLibId().GetLibNickname(),
908 m_original->GetLibId().GetLibItemName() ) );
909 }
910}
911
912
914{
915 return m_screen && m_screen->IsContentModified();
916}
917
918
919LIB_SYMBOL* LIB_BUFFER::GetSymbol( const wxString& aAlias ) const
920{
921 auto buf = GetBuffer( aAlias );
922
923 if( !buf )
924 return nullptr;
925
926 LIB_SYMBOL& symbol = buf->GetSymbol();
927 return &symbol;
928}
929
930
931bool LIB_BUFFER::CreateBuffer( std::unique_ptr<LIB_SYMBOL> aCopy,
932 std::unique_ptr<SCH_SCREEN> aScreen )
933{
934 wxASSERT( aCopy );
935 wxASSERT( aCopy->GetLib() == nullptr );
936
937 // Set the parent library name,
938 // otherwise it is empty as no library has been given as the owner during object construction
939 LIB_ID libId = aCopy->GetLibId();
940 libId.SetLibNickname( m_libName );
941 aCopy->SetLibId( libId );
942
943 auto symbolBuf = std::make_shared<SYMBOL_BUFFER>( std::move( aCopy ), std::move( aScreen ) );
944 m_symbols.push_back( std::move( symbolBuf ) );
945
946 ++m_hash;
947
948 return true;
949}
950
951
952bool LIB_BUFFER::UpdateBuffer( SYMBOL_BUFFER& aSymbolBuf, const LIB_SYMBOL& aCopy )
953{
954 LIB_SYMBOL& bufferedSymbol = aSymbolBuf.GetSymbol();
955
956 bufferedSymbol = aCopy;
957 ++m_hash;
958
959 return true;
960}
961
962
964{
965 const auto sameBufferPredicate = [&]( const std::shared_ptr<SYMBOL_BUFFER>& aBuf )
966 {
967 return aBuf.get() == &aSymbolBuf;
968 };
969
970 auto symbolBufIt = std::find_if( m_symbols.begin(), m_symbols.end(), sameBufferPredicate );
971 wxCHECK( symbolBufIt != m_symbols.end(), false );
972
973 bool retv = true;
974
975 // Remove all derived symbols to prevent broken inheritance.
976 if( HasDerivedSymbols( aSymbolBuf.GetSymbol().GetName() )
977 && ( removeChildSymbols( aSymbolBuf ) == 0 ) )
978 {
979 retv = false;
980 }
981
982 m_deleted.emplace_back( *symbolBufIt );
983 m_symbols.erase( symbolBufIt );
984 ++m_hash;
985
986 return retv;
987}
988
989
990bool LIB_BUFFER::SaveBuffer( SYMBOL_BUFFER& aSymbolBuf, const wxString& aFileName, SCH_IO* aPlugin,
991 bool aBuffer )
992{
993 wxCHECK( !aFileName.IsEmpty(), false );
994
995 wxString errorMsg = _( "Error saving symbol %s to library '%s'." ) + wxS( "\n%s" );
996
997 // Set properties to prevent saving the file on every symbol save.
998 std::map<std::string, UTF8> properties;
999 properties.emplace( SCH_IO_KICAD_LEGACY::PropBuffering, "" );
1000
1001 LIB_SYMBOL& libSymbol = aSymbolBuf.GetSymbol();
1002
1003 {
1004 LIB_SYMBOL& originalSymbol = aSymbolBuf.GetOriginal();
1005 const wxString originalName = originalSymbol.GetName();
1006
1007 // Delete the original symbol if the symbol name has been changed.
1008 if( libSymbol.GetName() != originalSymbol.GetName() )
1009 {
1010 try
1011 {
1012 if( aPlugin->LoadSymbol( aFileName, originalName ) )
1013 aPlugin->DeleteSymbol( aFileName, originalName, &properties );
1014 }
1015 catch( const IO_ERROR& ioe )
1016 {
1017 wxLogError( errorMsg, UnescapeString( originalName ), aFileName, ioe.What() );
1018 return false;
1019 }
1020 }
1021 }
1022
1023 LIB_SYMBOL* parentSymbol = nullptr;
1024
1025 if( libSymbol.IsDerived() )
1026 {
1027 LIB_SYMBOL* newCachedSymbol = new LIB_SYMBOL( libSymbol );
1028 std::shared_ptr<LIB_SYMBOL> bufferedParent = libSymbol.GetParent().lock();
1029 parentSymbol = newCachedSymbol;
1030
1031 wxCHECK( bufferedParent, false );
1032
1033 LIB_SYMBOL* cachedParent = nullptr;
1034
1035 try
1036 {
1037 cachedParent = aPlugin->LoadSymbol( aFileName, bufferedParent->GetName() );
1038 }
1039 catch( const IO_ERROR& )
1040 {
1041 return false;
1042 }
1043
1044 if( !cachedParent )
1045 {
1046 cachedParent = new LIB_SYMBOL( *bufferedParent.get() );
1047 newCachedSymbol->SetParent( cachedParent );
1048
1049 try
1050 {
1051 aPlugin->SaveSymbol( aFileName, cachedParent, aBuffer ? &properties : nullptr );
1052 }
1053 catch( const IO_ERROR& ioe )
1054 {
1055 wxLogError( errorMsg, UnescapeString( cachedParent->GetName() ), aFileName,
1056 ioe.What() );
1057 return false;
1058 }
1059
1060 try
1061 {
1062 aPlugin->SaveSymbol( aFileName, newCachedSymbol, aBuffer ? &properties : nullptr );
1063 }
1064 catch( const IO_ERROR& ioe )
1065 {
1066 wxLogError( errorMsg, UnescapeString( newCachedSymbol->GetName() ), aFileName,
1067 ioe.What() );
1068 return false;
1069 }
1070
1071 auto originalParent = std::make_unique<LIB_SYMBOL>( *bufferedParent.get() );
1072 LIB_SYMBOL& parentRef = *originalParent;
1073 aSymbolBuf.SetOriginal( std::move( originalParent ) );
1074
1075 auto newSymbol = std::make_unique<LIB_SYMBOL>( libSymbol );
1076 newSymbol->SetParent( &parentRef );
1077 aSymbolBuf.SetOriginal( std::move( newSymbol ) );
1078 }
1079 else
1080 {
1081 // Copy embedded files from the buffered parent to the cached parent
1082 // This ensures that any embedded files added to the parent are preserved
1083 //
1084 // We do this manually rather than using the assignment operator to avoid
1085 // potential ABI issues where the size of EMBEDDED_FILES differs between
1086 // compilation units, potentially causing the assignment to overwrite
1087 // members of LIB_SYMBOL (like m_me) that follow the EMBEDDED_FILES base.
1088 cachedParent->ClearEmbeddedFiles();
1089
1090 for( const auto& [name, file] : bufferedParent->EmbeddedFileMap() )
1091 cachedParent->AddFile( new EMBEDDED_FILES::EMBEDDED_FILE( *file ) );
1092
1093 cachedParent->SetAreFontsEmbedded( bufferedParent->GetAreFontsEmbedded() );
1094 cachedParent->SetFileAddedCallback( bufferedParent->GetFileAddedCallback() );
1095
1096 newCachedSymbol->SetParent( cachedParent );
1097
1098 try
1099 {
1100 aPlugin->SaveSymbol( aFileName, newCachedSymbol, aBuffer ? &properties : nullptr );
1101 }
1102 catch( const IO_ERROR& ioe )
1103 {
1104 wxLogError( errorMsg, UnescapeString( newCachedSymbol->GetName() ), aFileName,
1105 ioe.What() );
1106 return false;
1107 }
1108
1109 auto originalBufferedParent = GetBuffer( bufferedParent->GetName() );
1110 wxCHECK( originalBufferedParent, false );
1111
1112 auto newSymbol = std::make_unique<LIB_SYMBOL>( libSymbol );
1113 newSymbol->SetParent( &originalBufferedParent->GetSymbol() );
1114 aSymbolBuf.SetOriginal( std::move( newSymbol ) );
1115 }
1116 }
1117 else
1118 {
1119 parentSymbol = new LIB_SYMBOL( libSymbol );
1120
1121 try
1122 {
1123 aPlugin->SaveSymbol( aFileName, parentSymbol, aBuffer ? &properties : nullptr );
1124 }
1125 catch( const IO_ERROR& ioe )
1126 {
1127 wxLogError( errorMsg, UnescapeString( libSymbol.GetName() ), aFileName, ioe.What() );
1128 return false;
1129 }
1130
1131 aSymbolBuf.SetOriginal( std::make_unique<LIB_SYMBOL>( libSymbol ) );
1132 }
1133
1134 wxArrayString derivedSymbols;
1135
1136 // Reparent all symbols derived from the saved symbol.
1137 if( GetDerivedSymbolNames( libSymbol.GetName(), derivedSymbols ) != 0 )
1138 {
1139 // Save the derived symbols.
1140 for( const wxString& entry : derivedSymbols )
1141 {
1142 std::shared_ptr<SYMBOL_BUFFER> symbol = GetBuffer( entry );
1143
1144 wxCHECK2( symbol, continue );
1145
1146 LIB_SYMBOL* derivedSymbol = new LIB_SYMBOL( symbol->GetSymbol() );
1147 derivedSymbol->SetParent( parentSymbol );
1148
1149 try
1150 {
1151 aPlugin->SaveSymbol( aFileName, new LIB_SYMBOL( *derivedSymbol ),
1152 aBuffer ? &properties : nullptr );
1153 }
1154 catch( const IO_ERROR& ioe )
1155 {
1156 wxLogError( errorMsg, UnescapeString( derivedSymbol->GetName() ), aFileName,
1157 ioe.What() );
1158 return false;
1159 }
1160 }
1161 }
1162
1163 ++m_hash;
1164 return true;
1165}
1166
1167
1168std::shared_ptr<SYMBOL_BUFFER> LIB_BUFFER::GetBuffer( const wxString& aSymbolName ) const
1169{
1170 for( std::shared_ptr<SYMBOL_BUFFER> entry : m_symbols )
1171 {
1172 if( entry->GetSymbol().GetName() == aSymbolName )
1173 return entry;
1174 }
1175
1176 return std::shared_ptr<SYMBOL_BUFFER>( nullptr );
1177}
1178
1179
1180bool LIB_BUFFER::HasDerivedSymbols( const wxString& aParentName ) const
1181{
1182 for( const std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1183 {
1184 if( std::shared_ptr<LIB_SYMBOL> parent = entry->GetSymbol().GetParent().lock() )
1185 {
1186 if( parent->GetName() == aParentName )
1187 return true;
1188 }
1189 }
1190
1191 return false;
1192}
1193
1194
1195void LIB_BUFFER::GetSymbolNames( wxArrayString& aSymbolNames, SYMBOL_NAME_FILTER aFilter )
1196{
1197 for( std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1198 {
1199 const LIB_SYMBOL& symbol = entry->GetSymbol();
1200 if( ( symbol.IsDerived() && ( aFilter == SYMBOL_NAME_FILTER::ROOT_ONLY ) )
1201 || ( symbol.IsRoot() && ( aFilter == SYMBOL_NAME_FILTER::DERIVED_ONLY ) ) )
1202 {
1203 continue;
1204 }
1205 aSymbolNames.Add( UnescapeString( symbol.GetName() ) );
1206 }
1207}
1208
1209
1210size_t LIB_BUFFER::GetDerivedSymbolNames( const wxString& aSymbolName, wxArrayString& aList )
1211{
1212 wxCHECK( !aSymbolName.IsEmpty(), 0 );
1213
1214 // Parent: children map
1215 std::unordered_map<std::shared_ptr<LIB_SYMBOL>, std::vector<std::shared_ptr<LIB_SYMBOL>>> derivedMap;
1216
1217 // Iterate the library once to resolve all derived symbol links.
1218 // This means we only need to iterate the library once, and we can then look up the links
1219 // as needed.
1220 for( std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1221 {
1222 std::shared_ptr<LIB_SYMBOL> symbol = entry->GetSymbol().SharedPtr();
1223
1224 if( std::shared_ptr<LIB_SYMBOL> parent = symbol->GetParent().lock() )
1225 derivedMap[parent].emplace_back( std::move( symbol ) );
1226 }
1227
1228 const auto visit =
1229 [&]( LIB_SYMBOL& aSymbol )
1230 {
1231 aList.Add( aSymbol.GetName() );
1232 };
1233
1234 // Assign to std::function to allow recursion
1235 const std::function<void( std::shared_ptr<LIB_SYMBOL>& )> getDerived =
1236 [&]( std::shared_ptr<LIB_SYMBOL>& aSymbol )
1237 {
1238 auto it = derivedMap.find( aSymbol );
1239
1240 if( it != derivedMap.end() )
1241 {
1242 for( std::shared_ptr<LIB_SYMBOL>& derivedSymbol : it->second )
1243 {
1244 visit( *derivedSymbol );
1245
1246 // Recurse to get symbols derived from this one
1247 getDerived( derivedSymbol );
1248 }
1249 }
1250 };
1251
1252 // Start the recursion at the top
1253 std::shared_ptr<LIB_SYMBOL> symbol = GetSymbol( aSymbolName )->SharedPtr();
1254 getDerived( symbol );
1255
1256 return aList.GetCount();
1257}
1258
1259
1261{
1262 int cnt = 0;
1263 wxArrayString derivedSymbolNames;
1264 std::deque<std::shared_ptr<SYMBOL_BUFFER>>::iterator it;
1265
1266 if( GetDerivedSymbolNames( aSymbolBuf.GetSymbol().GetName(), derivedSymbolNames ) )
1267 {
1268 for( const wxString& symbolName : derivedSymbolNames )
1269 {
1270 it = std::find_if( m_symbols.begin(), m_symbols.end(),
1271 [symbolName]( std::shared_ptr<SYMBOL_BUFFER>& buf )
1272 {
1273 return buf->GetSymbol().GetName() == symbolName;
1274 } );
1275
1276 wxCHECK2( it != m_symbols.end(), continue );
1277
1278 m_deleted.emplace_back( *it );
1279 m_symbols.erase( it );
1280 cnt += 1;
1281 }
1282 }
1283
1284 return cnt;
1285}
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:152
std::weak_ptr< LIB_SYMBOL > & GetParent()
Definition lib_symbol.h:114
bool IsRoot() const override
For symbols derived from other symbols, IsRoot() indicates no derivation.
Definition lib_symbol.h:202
bool IsDerived() const
Definition lib_symbol.h:203
void SetParent(LIB_SYMBOL *aParent=nullptr)
wxString GetName() const override
Definition lib_symbol.h:145
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:92
void SetLibId(const LIB_ID &aLibId)
Definition lib_symbol.h:153
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