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