KiCad PCB EDA Suite
Loading...
Searching...
No Matches
symbol_tree_model_adapter.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 Chris Pavlina <[email protected]>
5 * Copyright (C) 2014 Henner Zeller <[email protected]>
6 * Copyright (C) 2014-2022 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <wx/log.h>
23#include <wx/tokenzr.h>
24#include <wx/window.h>
25#include <core/kicad_algo.h>
26#include <pgm_base.h>
30#include <eda_pattern_match.h>
31#include <generate_alias_info.h>
32#include <sch_base_frame.h>
33#include <locale_io.h>
34#include <lib_symbol.h>
35#include <symbol_async_loader.h>
36#include <symbol_lib_table.h>
38#include <string_utils.h>
39
41
42#define PROGRESS_INTERVAL_MILLIS 33 // 30 FPS refresh rate
43
44
45wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
47{
48 auto* adapter = new SYMBOL_TREE_MODEL_ADAPTER( aParent, aLibs );
49 return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
50}
51
52
54 LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
55 m_libs( (SYMBOL_LIB_TABLE*) aLibs )
56{
57 // Symbols may have different value from name
58 m_availableColumns.emplace_back( wxT( "Value" ) );
59}
60
61
63{}
64
65
66bool SYMBOL_TREE_MODEL_ADAPTER::AddLibraries( const std::vector<wxString>& aNicknames,
67 SCH_BASE_FRAME* aFrame )
68{
69 std::unique_ptr<WX_PROGRESS_REPORTER> progressReporter = nullptr;
70
71 if( m_show_progress )
72 {
73 progressReporter = std::make_unique<WX_PROGRESS_REPORTER>( aFrame,
74 _( "Loading Symbol Libraries" ),
75 aNicknames.size(), true );
76 }
77
78 // Disable KIID generation: not needed for library parts; sometimes very slow
80
81 std::unordered_map<wxString, std::vector<LIB_SYMBOL*>> loadedSymbolMap;
82
83 SYMBOL_ASYNC_LOADER loader( aNicknames, m_libs, GetFilter() != nullptr, &loadedSymbolMap,
84 progressReporter.get() );
85
86 LOCALE_IO toggle;
87
88 loader.Start();
89
90 while( !loader.Done() )
91 {
92 if( progressReporter && !progressReporter->KeepRefreshing() )
93 break;
94
95 wxMilliSleep( PROGRESS_INTERVAL_MILLIS );
96 }
97
98 loader.Join();
99
100 bool cancelled = false;
101
102 if( progressReporter )
103 cancelled = progressReporter->IsCancelled();
104
105 if( !loader.GetErrors().IsEmpty() )
106 {
107 HTML_MESSAGE_BOX dlg( aFrame, _( "Load Error" ) );
108
109 dlg.MessageSet( _( "Errors loading symbols:" ) );
110
111 wxString msg = loader.GetErrors();
112 msg.Replace( "\n", "<BR>" );
113
114 dlg.AddHTML_Text( msg );
115 dlg.ShowModal();
116 }
117
118 if( loadedSymbolMap.size() > 0 )
119 {
121 PROJECT_FILE& project = aFrame->Prj().GetProjectFile();
122
123 auto addFunc =
124 [&]( const wxString& aLibName, const std::vector<LIB_SYMBOL*>& aSymbolList,
125 const wxString& aDescription )
126 {
127 std::vector<LIB_TREE_ITEM*> treeItems( aSymbolList.begin(), aSymbolList.end() );
128 bool pinned = alg::contains( cfg->m_Session.pinned_symbol_libs, aLibName )
129 || alg::contains( project.m_PinnedSymbolLibs, aLibName );
130
131 DoAddLibrary( aLibName, aDescription, treeItems, pinned, false );
132 };
133
134 for( const auto& [libNickname, libSymbols] : loadedSymbolMap )
135 {
136 SYMBOL_LIB_TABLE_ROW* row = m_libs->FindRow( libNickname );
137
138 wxCHECK2( row, continue );
139
140 if( !row->GetIsVisible() )
141 continue;
142
143 std::vector<wxString> additionalColumns;
144 row->GetAvailableSymbolFields( additionalColumns );
145
146 for( const wxString& column : additionalColumns )
147 addColumnIfNecessary( column );
148
149 if( row->SupportsSubLibraries() )
150 {
151 std::vector<wxString> subLibraries;
152 row->GetSubLibraryNames( subLibraries );
153
154 wxString parentDesc = m_libs->GetDescription( libNickname );
155
156 for( const wxString& lib : subLibraries )
157 {
158 wxString suffix = lib.IsEmpty() ? wxString( wxT( "" ) )
159 : wxString::Format( wxT( " - %s" ), lib );
160 wxString name = wxString::Format( wxT( "%s%s" ), libNickname, suffix );
161 wxString desc;
162
163 if( !parentDesc.IsEmpty() )
164 desc = wxString::Format( wxT( "%s (%s)" ), parentDesc, lib );
165
166 UTF8 utf8Lib( lib );
167
168 std::vector<LIB_SYMBOL*> symbols;
169
170 std::copy_if( libSymbols.begin(), libSymbols.end(),
171 std::back_inserter( symbols ),
172 [&utf8Lib]( LIB_SYMBOL* aSym )
173 {
174 return utf8Lib == aSym->GetLibId().GetSubLibraryName();
175 } );
176
177 addFunc( name, symbols, desc );
178 }
179 }
180 else
181 {
182 addFunc( libNickname, libSymbols, m_libs->GetDescription( libNickname ) );
183 }
184 }
185 }
186
187 KIID::CreateNilUuids( false );
188
190
191 if( progressReporter )
192 {
193 // Force immediate deletion of the APP_PROGRESS_DIALOG. Do not use Destroy(), or Destroy()
194 // followed by wxSafeYield() because on Windows, APP_PROGRESS_DIALOG has some side effects
195 // on the event loop manager.
196 // One in particular is the call of ShowModal() following SYMBOL_TREE_MODEL_ADAPTER
197 // creating a APP_PROGRESS_DIALOG (which has incorrect modal behaviour).
198 progressReporter.reset();
199 m_show_progress = false;
200 }
201
202 return !cancelled;
203}
204
205
206void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname, bool pinned )
207{
208 bool onlyPowerSymbols = ( GetFilter() != nullptr );
209 std::vector<LIB_SYMBOL*> symbols;
210 std::vector<LIB_TREE_ITEM*> comp_list;
211
212 try
213 {
214 m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
215 }
216 catch( const IO_ERROR& ioe )
217 {
218 wxLogError( _( "Error loading symbol library '%s'." ) + wxS( "\n%s" ),
219 aLibNickname,
220 ioe.What() );
221 return;
222 }
223
224 if( symbols.size() > 0 )
225 {
226 comp_list.assign( symbols.begin(), symbols.end() );
227 DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, pinned,
228 false );
229 }
230}
231
232
233wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
234{
235 return GenerateAliasInfo( m_libs, aLibId, aUnit );
236}
237
238
const char * name
Definition: DXF_plotter.cpp:57
The base frame for deriving all KiCad main window classes.
void MessageSet(const wxString &message)
Add a message (in bold) to message list.
void AddHTML_Text(const wxString &message)
Add HTML text (without any change) to message list.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
static void CreateNilUuids(bool aNil=true)
A performance optimization which disables/enables the generation of pseudo-random UUIDs.
Definition: kiid.cpp:294
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
Define a library symbol object.
Definition: lib_symbol.h:99
bool GetIsVisible() const
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.
const wxString GetDescription(const wxString &aNickname)
void addColumnIfNecessary(const wxString &aHeader)
void DoAddLibrary(const wxString &aNodeName, const wxString &aDesc, const std::vector< LIB_TREE_ITEM * > &aItemList, bool pinned, bool presorted)
Add the given list of symbols by alias.
std::function< bool(LIB_TREE_NODE &aNode)> * GetFilter() const
Return the active filter.
std::vector< wxString > m_availableColumns
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:49
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:650
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:70
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:166
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
bool Done()
Returns a string containing any errors generated during the load.
const wxString & GetErrors() const
Represents a pair of <nickname, loaded parts list>
void Start()
Spins up threads to load all the libraries in m_nicknames.
bool Join()
Finalizes the threads and combines the output into the target output map.
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_IO object i...
void GetAvailableSymbolFields(std::vector< wxString > &aNames) const
void GetSubLibraryNames(std::vector< wxString > &aNames) const
bool SupportsSubLibraries() const
void LoadSymbolLib(std::vector< LIB_SYMBOL * > &aAliasList, const wxString &aNickname, bool aPowerSymbolsOnly=false)
SYMBOL_LIB_TABLE_ROW * FindRow(const wxString &aNickName, bool aCheckIfEnabled=false)
Return an SYMBOL_LIB_TABLE_ROW if aNickName is found in this table or in any chained fallBack table f...
wxString GenerateInfo(LIB_ID const &aLibId, int aUnit) override
static wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > Create(EDA_BASE_FRAME *aParent, LIB_TABLE *aLibs)
Factory function: create a model adapter in a reference-counting container.
bool AddLibraries(const std::vector< wxString > &aNicknames, SCH_BASE_FRAME *aFrame)
Add all the libraries in a SYMBOL_LIB_TABLE to the model.
static bool m_show_progress
Flag to only show the symbol library table load progress dialog the first time.
void AddLibrary(wxString const &aLibNickname, bool pinned)
SYMBOL_TREE_MODEL_ADAPTER(EDA_BASE_FRAME *aParent, LIB_TABLE *aLibs)
Constructor; takes a set of libraries to be included in the search.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:72
#define _(s)
Abstract pattern-matching tool and implementations.
wxString GenerateAliasInfo(SYMBOL_LIB_TABLE *aSymLibTable, LIB_ID const &aLibId, int aUnit)
Return an HTML page describing a LIB_ID in a SYMBOL_LIB_TABLE.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1031
see class PGM_BASE
std::vector< wxString > pinned_symbol_libs
#define PROGRESS_INTERVAL_MILLIS