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