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 manager.ReloadTables( aScope, { LIBRARY_TABLE_TYPE::SYMBOL } );
779
780 // Tables are reinitialized. So reinit table reference.
781 optTable = manager.Table( LIBRARY_TABLE_TYPE::SYMBOL, aScope );
782 wxCHECK( optTable, false );
783 table = optTable.value();
784
785 if( aCreate )
786 {
787 wxCHECK( schFileType != SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY, false );
788
789 if( !adapter->CreateLibrary( libName ) )
790 {
791 table->Rows().erase( table->Rows().end() - 1 );
792 return false;
793 }
794 }
795
796 adapter->LoadOne( libName );
798 }
799
800 return success;
801}
802
803
804std::set<LIB_SYMBOL*> SYMBOL_LIBRARY_MANAGER::getOriginalSymbols( const wxString& aLibrary )
805{
806 std::set<LIB_SYMBOL*> symbols;
807 wxCHECK( LibraryExists( aLibrary ), symbols );
808
810
811 for( LIB_SYMBOL* symbol : adapter->GetSymbols( aLibrary ) )
812 symbols.insert( symbol );
813
814 return symbols;
815}
816
817
819{
820 auto it = m_libs.find( aLibrary );
821
822 if( it != m_libs.end() )
823 return it->second;
824
825 // The requested buffer does not exist yet, so create one
826 auto ret = m_libs.emplace( aLibrary, LIB_BUFFER( aLibrary ) );
827 LIB_BUFFER& buf = ret.first->second;
828
829 for( LIB_SYMBOL* symbol : getOriginalSymbols( aLibrary ) )
830 {
831 if( symbol->IsDerived() )
832 {
833 std::shared_ptr<LIB_SYMBOL> oldParent = symbol->GetParent().lock();
834
835 wxCHECK_MSG( oldParent, buf, wxString::Format( "Derived symbol '%s' found with undefined parent.",
836 symbol->GetName() ) );
837
838 LIB_SYMBOL* libParent = buf.GetSymbol( oldParent->GetName() );
839
840 if( !libParent )
841 {
842 std::unique_ptr<LIB_SYMBOL> newParent = std::make_unique<LIB_SYMBOL>( *oldParent.get() );
843 libParent = newParent.get();
844 buf.CreateBuffer( std::move( newParent ), std::make_unique<SCH_SCREEN>() );
845 }
846
847 std::unique_ptr<LIB_SYMBOL> newSymbol = std::make_unique<LIB_SYMBOL>( *symbol );
848 newSymbol->SetParent( libParent );
849 buf.CreateBuffer( std::move( newSymbol ), std::make_unique<SCH_SCREEN>() );
850 }
851 else if( !buf.GetSymbol( symbol->GetName() ) )
852 {
853 buf.CreateBuffer( std::make_unique<LIB_SYMBOL>( *symbol ), std::make_unique<SCH_SCREEN>() );
854 }
855 }
856
857 return buf;
858}
859
860
861bool SYMBOL_LIBRARY_MANAGER::UpdateLibraryBuffer( const wxString& aLibrary )
862{
863 try
864 {
865 m_libs.erase( aLibrary );
866 getLibraryBuffer( aLibrary );
867 }
868 catch( const std::exception& e )
869 {
870 wxLogError( _( "Error updating library buffer: %s" ), e.what() );
871 return false;
872 }
873 catch(...)
874 {
875 wxLogError( _( "Error updating library buffer." ) );
876 return false;
877 }
878
879 getLibraryBuffer( aLibrary );
880
881 return true;
882}
883
884
885SYMBOL_BUFFER::SYMBOL_BUFFER( std::unique_ptr<LIB_SYMBOL> aSymbol,
886 std::unique_ptr<SCH_SCREEN> aScreen ) :
887 m_screen( std::move( aScreen ) ),
888 m_symbol( std::move( aSymbol ) )
889{
890 wxASSERT( m_symbol );
891 m_original = std::make_unique<LIB_SYMBOL>( *m_symbol );
892 wxASSERT( m_original );
893}
894
895
899
900
901void SYMBOL_BUFFER::SetSymbol( std::unique_ptr<LIB_SYMBOL> aSymbol )
902{
903 wxCHECK( m_symbol != aSymbol, /* void */ );
904 wxASSERT( aSymbol );
905 m_symbol = std::move( aSymbol );
906
907 // If the symbol moves libraries then the original moves with it
908 if( m_original->GetLibId().GetLibNickname() != m_symbol->GetLibId().GetLibNickname() )
909 {
910 m_original->SetLibId( LIB_ID( m_symbol->GetLibId().GetLibNickname(),
911 m_original->GetLibId().GetLibItemName() ) );
912 }
913}
914
915
916void SYMBOL_BUFFER::SetOriginal( std::unique_ptr<LIB_SYMBOL> aSymbol )
917{
918 wxCHECK( m_original != aSymbol, /* void */ );
919 wxASSERT( aSymbol );
920 m_original = std::move( aSymbol );
921
922 // The original is not allowed to have a different library than its symbol
923 if( m_original->GetLibId().GetLibNickname() != m_symbol->GetLibId().GetLibNickname() )
924 {
925 m_original->SetLibId( LIB_ID( m_symbol->GetLibId().GetLibNickname(),
926 m_original->GetLibId().GetLibItemName() ) );
927 }
928}
929
930
932{
933 return m_screen && m_screen->IsContentModified();
934}
935
936
937LIB_SYMBOL* LIB_BUFFER::GetSymbol( const wxString& aAlias ) const
938{
939 auto buf = GetBuffer( aAlias );
940
941 if( !buf )
942 return nullptr;
943
944 LIB_SYMBOL& symbol = buf->GetSymbol();
945 return &symbol;
946}
947
948
949bool LIB_BUFFER::CreateBuffer( std::unique_ptr<LIB_SYMBOL> aCopy,
950 std::unique_ptr<SCH_SCREEN> aScreen )
951{
952 wxASSERT( aCopy );
953 wxASSERT( aCopy->GetLib() == nullptr );
954
955 // Set the parent library name,
956 // otherwise it is empty as no library has been given as the owner during object construction
957 LIB_ID libId = aCopy->GetLibId();
958 libId.SetLibNickname( m_libName );
959 aCopy->SetLibId( libId );
960
961 auto symbolBuf = std::make_shared<SYMBOL_BUFFER>( std::move( aCopy ), std::move( aScreen ) );
962 m_symbols.push_back( std::move( symbolBuf ) );
963
964 ++m_hash;
965
966 return true;
967}
968
969
970bool LIB_BUFFER::UpdateBuffer( SYMBOL_BUFFER& aSymbolBuf, const LIB_SYMBOL& aCopy )
971{
972 LIB_SYMBOL& bufferedSymbol = aSymbolBuf.GetSymbol();
973
974 bufferedSymbol = aCopy;
975 ++m_hash;
976
977 return true;
978}
979
980
982{
983 const auto sameBufferPredicate = [&]( const std::shared_ptr<SYMBOL_BUFFER>& aBuf )
984 {
985 return aBuf.get() == &aSymbolBuf;
986 };
987
988 auto symbolBufIt = std::find_if( m_symbols.begin(), m_symbols.end(), sameBufferPredicate );
989 wxCHECK( symbolBufIt != m_symbols.end(), false );
990
991 bool retv = true;
992
993 // Remove all derived symbols to prevent broken inheritance.
994 if( HasDerivedSymbols( aSymbolBuf.GetSymbol().GetName() )
995 && ( removeChildSymbols( aSymbolBuf ) == 0 ) )
996 {
997 retv = false;
998 }
999
1000 m_deleted.emplace_back( *symbolBufIt );
1001 m_symbols.erase( symbolBufIt );
1002 ++m_hash;
1003
1004 return retv;
1005}
1006
1007
1008bool LIB_BUFFER::SaveBuffer( SYMBOL_BUFFER& aSymbolBuf, const wxString& aFileName, SCH_IO* aPlugin,
1009 bool aBuffer )
1010{
1011 wxCHECK( !aFileName.IsEmpty(), false );
1012
1013 wxString errorMsg = _( "Error saving symbol %s to library '%s'." ) + wxS( "\n%s" );
1014
1015 // Set properties to prevent saving the file on every symbol save.
1016 std::map<std::string, UTF8> properties;
1017 properties.emplace( SCH_IO_KICAD_LEGACY::PropBuffering, "" );
1018
1019 LIB_SYMBOL& libSymbol = aSymbolBuf.GetSymbol();
1020
1021 {
1022 LIB_SYMBOL& originalSymbol = aSymbolBuf.GetOriginal();
1023 const wxString originalName = originalSymbol.GetName();
1024
1025 // Delete the original symbol if the symbol name has been changed.
1026 if( libSymbol.GetName() != originalSymbol.GetName() )
1027 {
1028 try
1029 {
1030 if( aPlugin->LoadSymbol( aFileName, originalName ) )
1031 aPlugin->DeleteSymbol( aFileName, originalName, &properties );
1032 }
1033 catch( const IO_ERROR& ioe )
1034 {
1035 wxLogError( errorMsg, UnescapeString( originalName ), aFileName, ioe.What() );
1036 return false;
1037 }
1038 }
1039 }
1040
1041 LIB_SYMBOL* parentSymbol = nullptr;
1042
1043 if( libSymbol.IsDerived() )
1044 {
1045 LIB_SYMBOL* newCachedSymbol = new LIB_SYMBOL( libSymbol );
1046 std::shared_ptr<LIB_SYMBOL> bufferedParent = libSymbol.GetParent().lock();
1047 parentSymbol = newCachedSymbol;
1048
1049 wxCHECK( bufferedParent, false );
1050
1051 LIB_SYMBOL* cachedParent = nullptr;
1052
1053 try
1054 {
1055 cachedParent = aPlugin->LoadSymbol( aFileName, bufferedParent->GetName() );
1056 }
1057 catch( const IO_ERROR& )
1058 {
1059 return false;
1060 }
1061
1062 if( !cachedParent )
1063 {
1064 cachedParent = new LIB_SYMBOL( *bufferedParent.get() );
1065 newCachedSymbol->SetParent( cachedParent );
1066
1067 try
1068 {
1069 aPlugin->SaveSymbol( aFileName, cachedParent, aBuffer ? &properties : nullptr );
1070 }
1071 catch( const IO_ERROR& ioe )
1072 {
1073 wxLogError( errorMsg, UnescapeString( cachedParent->GetName() ), aFileName,
1074 ioe.What() );
1075 return false;
1076 }
1077
1078 try
1079 {
1080 aPlugin->SaveSymbol( aFileName, newCachedSymbol, aBuffer ? &properties : nullptr );
1081 }
1082 catch( const IO_ERROR& ioe )
1083 {
1084 wxLogError( errorMsg, UnescapeString( newCachedSymbol->GetName() ), aFileName,
1085 ioe.What() );
1086 return false;
1087 }
1088
1089 auto originalParent = std::make_unique<LIB_SYMBOL>( *bufferedParent.get() );
1090 LIB_SYMBOL& parentRef = *originalParent;
1091 aSymbolBuf.SetOriginal( std::move( originalParent ) );
1092
1093 auto newSymbol = std::make_unique<LIB_SYMBOL>( libSymbol );
1094 newSymbol->SetParent( &parentRef );
1095 aSymbolBuf.SetOriginal( std::move( newSymbol ) );
1096 }
1097 else
1098 {
1099 // Copy embedded files from the buffered parent to the cached parent
1100 // This ensures that any embedded files added to the parent are preserved
1101 //
1102 // We do this manually rather than using the assignment operator to avoid
1103 // potential ABI issues where the size of EMBEDDED_FILES differs between
1104 // compilation units, potentially causing the assignment to overwrite
1105 // members of LIB_SYMBOL (like m_me) that follow the EMBEDDED_FILES base.
1106 cachedParent->ClearEmbeddedFiles();
1107
1108 for( const auto& [name, file] : bufferedParent->EmbeddedFileMap() )
1109 cachedParent->AddFile( new EMBEDDED_FILES::EMBEDDED_FILE( *file ) );
1110
1111 cachedParent->SetAreFontsEmbedded( bufferedParent->GetAreFontsEmbedded() );
1112 cachedParent->SetFileAddedCallback( bufferedParent->GetFileAddedCallback() );
1113
1114 newCachedSymbol->SetParent( cachedParent );
1115
1116 try
1117 {
1118 aPlugin->SaveSymbol( aFileName, newCachedSymbol, aBuffer ? &properties : nullptr );
1119 }
1120 catch( const IO_ERROR& ioe )
1121 {
1122 wxLogError( errorMsg, UnescapeString( newCachedSymbol->GetName() ), aFileName,
1123 ioe.What() );
1124 return false;
1125 }
1126
1127 auto originalBufferedParent = GetBuffer( bufferedParent->GetName() );
1128 wxCHECK( originalBufferedParent, false );
1129
1130 auto newSymbol = std::make_unique<LIB_SYMBOL>( libSymbol );
1131 newSymbol->SetParent( &originalBufferedParent->GetSymbol() );
1132 aSymbolBuf.SetOriginal( std::move( newSymbol ) );
1133 }
1134 }
1135 else
1136 {
1137 parentSymbol = new LIB_SYMBOL( libSymbol );
1138
1139 try
1140 {
1141 aPlugin->SaveSymbol( aFileName, parentSymbol, aBuffer ? &properties : nullptr );
1142 }
1143 catch( const IO_ERROR& ioe )
1144 {
1145 wxLogError( errorMsg, UnescapeString( libSymbol.GetName() ), aFileName, ioe.What() );
1146 return false;
1147 }
1148
1149 aSymbolBuf.SetOriginal( std::make_unique<LIB_SYMBOL>( libSymbol ) );
1150 }
1151
1152 wxArrayString derivedSymbols;
1153
1154 // Reparent all symbols derived from the saved symbol.
1155 if( GetDerivedSymbolNames( libSymbol.GetName(), derivedSymbols ) != 0 )
1156 {
1157 // Save the derived symbols.
1158 for( const wxString& entry : derivedSymbols )
1159 {
1160 std::shared_ptr<SYMBOL_BUFFER> symbol = GetBuffer( entry );
1161
1162 wxCHECK2( symbol, continue );
1163
1164 LIB_SYMBOL* derivedSymbol = new LIB_SYMBOL( symbol->GetSymbol() );
1165 derivedSymbol->SetParent( parentSymbol );
1166
1167 try
1168 {
1169 aPlugin->SaveSymbol( aFileName, new LIB_SYMBOL( *derivedSymbol ),
1170 aBuffer ? &properties : nullptr );
1171 }
1172 catch( const IO_ERROR& ioe )
1173 {
1174 wxLogError( errorMsg, UnescapeString( derivedSymbol->GetName() ), aFileName,
1175 ioe.What() );
1176 return false;
1177 }
1178 }
1179 }
1180
1181 ++m_hash;
1182 return true;
1183}
1184
1185
1186std::shared_ptr<SYMBOL_BUFFER> LIB_BUFFER::GetBuffer( const wxString& aSymbolName ) const
1187{
1188 for( std::shared_ptr<SYMBOL_BUFFER> entry : m_symbols )
1189 {
1190 if( entry->GetSymbol().GetName() == aSymbolName )
1191 return entry;
1192 }
1193
1194 return std::shared_ptr<SYMBOL_BUFFER>( nullptr );
1195}
1196
1197
1198bool LIB_BUFFER::HasDerivedSymbols( const wxString& aParentName ) const
1199{
1200 for( const std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1201 {
1202 if( std::shared_ptr<LIB_SYMBOL> parent = entry->GetSymbol().GetParent().lock() )
1203 {
1204 if( parent->GetName() == aParentName )
1205 return true;
1206 }
1207 }
1208
1209 return false;
1210}
1211
1212
1213void LIB_BUFFER::GetSymbolNames( wxArrayString& aSymbolNames, SYMBOL_NAME_FILTER aFilter )
1214{
1215 for( std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1216 {
1217 const LIB_SYMBOL& symbol = entry->GetSymbol();
1218 if( ( symbol.IsDerived() && ( aFilter == SYMBOL_NAME_FILTER::ROOT_ONLY ) )
1219 || ( symbol.IsRoot() && ( aFilter == SYMBOL_NAME_FILTER::DERIVED_ONLY ) ) )
1220 {
1221 continue;
1222 }
1223 aSymbolNames.Add( UnescapeString( symbol.GetName() ) );
1224 }
1225}
1226
1227
1228size_t LIB_BUFFER::GetDerivedSymbolNames( const wxString& aSymbolName, wxArrayString& aList )
1229{
1230 wxCHECK( !aSymbolName.IsEmpty(), 0 );
1231
1232 // Parent: children map
1233 std::unordered_map<std::shared_ptr<LIB_SYMBOL>, std::vector<std::shared_ptr<LIB_SYMBOL>>> derivedMap;
1234
1235 // Iterate the library once to resolve all derived symbol links.
1236 // This means we only need to iterate the library once, and we can then look up the links
1237 // as needed.
1238 for( std::shared_ptr<SYMBOL_BUFFER>& entry : m_symbols )
1239 {
1240 std::shared_ptr<LIB_SYMBOL> symbol = entry->GetSymbol().SharedPtr();
1241
1242 if( std::shared_ptr<LIB_SYMBOL> parent = symbol->GetParent().lock() )
1243 derivedMap[parent].emplace_back( std::move( symbol ) );
1244 }
1245
1246 const auto visit =
1247 [&]( LIB_SYMBOL& aSymbol )
1248 {
1249 aList.Add( aSymbol.GetName() );
1250 };
1251
1252 // Assign to std::function to allow recursion
1253 const std::function<void( std::shared_ptr<LIB_SYMBOL>& )> getDerived =
1254 [&]( std::shared_ptr<LIB_SYMBOL>& aSymbol )
1255 {
1256 auto it = derivedMap.find( aSymbol );
1257
1258 if( it != derivedMap.end() )
1259 {
1260 for( std::shared_ptr<LIB_SYMBOL>& derivedSymbol : it->second )
1261 {
1262 visit( *derivedSymbol );
1263
1264 // Recurse to get symbols derived from this one
1265 getDerived( derivedSymbol );
1266 }
1267 }
1268 };
1269
1270 // Start the recursion at the top
1271 std::shared_ptr<LIB_SYMBOL> symbol = GetSymbol( aSymbolName )->SharedPtr();
1272 getDerived( symbol );
1273
1274 return aList.GetCount();
1275}
1276
1277
1279{
1280 int cnt = 0;
1281 wxArrayString derivedSymbolNames;
1282 std::deque<std::shared_ptr<SYMBOL_BUFFER>>::iterator it;
1283
1284 if( GetDerivedSymbolNames( aSymbolBuf.GetSymbol().GetName(), derivedSymbolNames ) )
1285 {
1286 for( const wxString& symbolName : derivedSymbolNames )
1287 {
1288 it = std::find_if( m_symbols.begin(), m_symbols.end(),
1289 [symbolName]( std::shared_ptr<SYMBOL_BUFFER>& buf )
1290 {
1291 return buf->GetSymbol().GetName() == symbolName;
1292 } );
1293
1294 wxCHECK2( it != m_symbols.end(), continue );
1295
1296 m_deleted.emplace_back( *it );
1297 m_symbols.erase( it );
1298 cnt += 1;
1299 }
1300 }
1301
1302 return cnt;
1303}
const char * name
void SetContentModified(bool aModified=true)
Definition base_screen.h:59
void SetFileAddedCallback(FILE_ADDED_CALLBACK callback)
void ClearEmbeddedFiles(bool aDeleteFiles=true)
EMBEDDED_FILE * AddFile(const wxFileName &aName, bool aOverwrite)
Load a file from disk and adds it to the collection.
const std::map< wxString, EMBEDDED_FILE * > & EmbeddedFileMap() const
void SetAreFontsEmbedded(bool aEmbedFonts)
FILE_ADDED_CALLBACK GetFileAddedCallback() const
bool GetAreFontsEmbedded() const
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
bool IsLibraryLoaded(const wxString &aNickname)
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library tables.
std::vector< wxString > GetLibraryNames() const
Returns a list of library nicknames that are available (skips any that failed to load)
bool CreateLibrary(const wxString &aNickname)
Creates the library (i.e. saves to disk) for the given row if it exists.
void ReloadTables(LIBRARY_TABLE_SCOPE aScope, std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
std::optional< LIBRARY_TABLE * > Table(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope)
Retrieves a given table; creating a new empty project table if a valid project is loaded and the give...
std::optional< wxString > GetFullURI(LIBRARY_TABLE_TYPE aType, const wxString &aNickname, bool aSubstituted=false)
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:133
static SYMBOL_LIBRARY_ADAPTER * SymbolLibAdapter(PROJECT *aProject)
Accessor for project symbol library manager adapter.
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
static const wxString ShowType(SCH_FILE_T aFileType)
Return a brief name for a plugin, given aFileType enum.
static SCH_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath, int aCtl=0)
Return a plugin type given a symbol library using the file extension of aLibPath.
Base class that schematic file and library loading and saving plugins should derive from.
Definition sch_io.h:59
virtual void SaveSymbol(const wxString &aLibraryPath, const LIB_SYMBOL *aSymbol, const std::map< std::string, UTF8 > *aProperties=nullptr)
Write aSymbol to an existing library located at aLibraryPath.
Definition sch_io.cpp:108
virtual void DeleteSymbol(const wxString &aLibraryPath, const wxString &aSymbolName, const std::map< std::string, UTF8 > *aProperties=nullptr)
Delete the entire LIB_SYMBOL associated with aAliasName from the library aLibraryPath.
Definition sch_io.cpp:116
virtual LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aPartName, const std::map< std::string, UTF8 > *aProperties=nullptr)
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
Definition sch_io.cpp:100
std::unique_ptr< LIB_SYMBOL > m_symbol
std::unique_ptr< SCH_SCREEN > m_screen
void SetSymbol(std::unique_ptr< LIB_SYMBOL > aSymbol)
LIB_SYMBOL & GetSymbol() const
SYMBOL_BUFFER(std::unique_ptr< LIB_SYMBOL > aSymbol=nullptr, std::unique_ptr< SCH_SCREEN > aScreen=nullptr)
std::unique_ptr< LIB_SYMBOL > m_original
LIB_SYMBOL & GetOriginal() const
void SetOriginal(std::unique_ptr< LIB_SYMBOL > aSymbol)
An interface to the global shared library manager that is schematic-specific and linked to one projec...
std::optional< LIB_STATUS > LoadOne(LIB_DATA *aLib) override
Loads or reloads the given library, if it exists.
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
std::vector< LIB_SYMBOL * > GetSymbols(const wxString &aNickname, SYMBOL_TYPE aType=SYMBOL_TYPE::ALL_SYMBOLS)
bool IsSymbolLibWritable(const wxString &aNickname)
Return true if the library given by aNickname is writable.
bool IsLibraryReadOnly(const wxString &aLibrary) const
Return true if the library is stored in a read-only file.
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.
bool ClearLibraryModified(const wxString &aLibrary) const
Clear the modified flag for all symbols in a library.
LIB_SYMBOL * GetBufferedSymbol(const wxString &aSymbolName, const wxString &aLibrary)
Return the symbol copy from the buffer.
bool ClearSymbolModified(const wxString &aSymbolName, const wxString &aLibrary) const
Clear the modified flag for a symbol.
bool addLibrary(const wxString &aFilePath, bool aCreate, LIBRARY_TABLE_SCOPE aScope)
Helper function to add either existing or create new library.
SCH_SCREEN * GetScreen(const wxString &aSymbolName, const wxString &aLibrary)
Return the screen used to edit a specific symbol.
bool SymbolNameInUse(const wxString &aName, const wxString &aLibrary)
Return true if the symbol name is already in use in the specified library.
bool IsLibraryModified(const wxString &aLibrary) const
Return true if library has unsaved modifications.
LIB_SYMBOL * GetSymbol(const wxString &aSymbolName, const wxString &aLibrary) const
Return either an alias of a working LIB_SYMBOL copy, or alias of the original symbol if there is no w...
bool RemoveSymbol(const wxString &aSymbolName, const wxString &aLibrary)
Remove the symbol from the symbol buffer.
wxArrayString GetLibraryNames() const
Return the array of library names.
LIB_BUFFER & getLibraryBuffer(const wxString &aLibrary)
Return an existing library buffer or creates one to using symbol library table to get the original da...
wxString GetUniqueLibraryName() const
Return a library name that is not currently in use.
static wxString getLibraryName(const wxString &aFilePath)
Extract library name basing on the file name.
bool UpdateSymbolAfterRename(LIB_SYMBOL *aSymbol, const wxString &aOldSymbolName, const wxString &aLibrary)
Update the symbol buffer with a new version of the symbol when the name has changed.
bool IsSymbolModified(const wxString &aSymbolName, const wxString &aLibrary) const
Return true if symbol has unsaved modifications.
virtual void OnDataChanged() const
bool IsLibraryLoaded(const wxString &aLibrary) const
Return true if the library was successfully loaded.
void SetSymbolModified(const wxString &aSymbolName, const wxString &aLibrary)
bool RevertLibrary(const wxString &aLibrary)
Revert unsaved changes for a symbol library.
LIB_ID RevertSymbol(const wxString &aSymbolName, const wxString &aLibrary)
Revert unsaved changes for a symbol.
void GetSymbolNames(const wxString &aLibName, wxArrayString &aSymbolNames, SYMBOL_NAME_FILTER aFilter=SYMBOL_NAME_FILTER::ALL)
std::set< LIB_SYMBOL * > getOriginalSymbols(const wxString &aLibrary)
Return a set of LIB_SYMBOL objects belonging to the original library.
int GetLibraryHash(const wxString &aLibrary) const
Return a library hash value to determine if it has changed.
size_t GetDerivedSymbolNames(const wxString &aSymbolName, const wxString &aLibraryName, wxArrayString &aList)
Fetch all of the symbols derived from a aSymbolName into aList.
bool RevertAll()
Revert all pending changes.
SYMBOL_LIBRARY_MANAGER(SCH_BASE_FRAME &aFrame)
bool SymbolExists(const wxString &aSymbolName, const wxString &aLibrary) const
Return true if symbol with a specific alias exists in library (either original one or buffered).
SCH_BASE_FRAME & m_frame
Parent frame.
std::list< LIB_SYMBOL * > EnumerateSymbols(const wxString &aLibrary) const
bool UpdateSymbol(LIB_SYMBOL *aSymbol, const wxString &aLibrary)
Update the symbol buffer with a new version of the symbol.
bool SaveLibrary(const wxString &aLibrary, const wxString &aFileName, SCH_IO_MGR::SCH_FILE_T aFileType=SCH_IO_MGR::SCH_FILE_T::SCH_LEGACY)
Save library to a file, including unsaved changes.
std::map< wxString, LIB_BUFFER > m_libs
The library buffers.
bool UpdateLibraryBuffer(const wxString &aLibrary)
Update the library buffer with a new version of the library.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:202
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:177
This file is part of the common library.
#define _(s)
wxString NormalizePath(const wxFileName &aFilePath, const ENV_VAR_MAP *aEnvVars, const wxString &aProjectPath)
Normalize a file path to an environmental variable, if possible.
Definition env_paths.cpp:73
Helper functions to substitute paths with environmental variables.
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:104
STL namespace.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
wxString UnescapeString(const wxString &aSource)
wxString message
VECTOR3I res
Definition of file extensions used in Kicad.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition wx_filename.h:39