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 <generate_alias_info.h>
31#include <sch_base_frame.h>
32#include <locale_io.h>
33#include <symbol_async_loader.h>
35#include <string_utils.h>
36
38
39#define PROGRESS_INTERVAL_MILLIS 33 // 30 FPS refresh rate
40
41
42wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
44{
45 auto* adapter = new SYMBOL_TREE_MODEL_ADAPTER( aParent, aLibs );
46 return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
47}
48
49
51 LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
52 m_libs( (SYMBOL_LIB_TABLE*) aLibs )
53{
54 // Symbols may have different value from name
55 m_availableColumns.emplace_back( wxT( "Value" ) );
56}
57
58
60{}
61
62
63bool SYMBOL_TREE_MODEL_ADAPTER::AddLibraries( const std::vector<wxString>& aNicknames,
64 SCH_BASE_FRAME* aFrame )
65{
66 std::unique_ptr<WX_PROGRESS_REPORTER> progressReporter = nullptr;
67
68 if( m_show_progress )
69 {
70 progressReporter = std::make_unique<WX_PROGRESS_REPORTER>( aFrame,
71 _( "Loading Symbol Libraries" ),
72 aNicknames.size(), true );
73 }
74
75 // Disable KIID generation: not needed for library parts; sometimes very slow
77
78 std::unordered_map<wxString, std::vector<LIB_SYMBOL*>> loadedSymbolMap;
79
80 SYMBOL_ASYNC_LOADER loader( aNicknames, m_libs, GetFilter() != nullptr, &loadedSymbolMap,
81 progressReporter.get() );
82
83 LOCALE_IO toggle;
84
85 loader.Start();
86
87 while( !loader.Done() )
88 {
89 if( progressReporter && !progressReporter->KeepRefreshing() )
90 break;
91
92 wxMilliSleep( PROGRESS_INTERVAL_MILLIS );
93 }
94
95 loader.Join();
96
97 bool cancelled = false;
98
99 if( progressReporter )
100 cancelled = progressReporter->IsCancelled();
101
102 if( !loader.GetErrors().IsEmpty() )
103 {
104 HTML_MESSAGE_BOX dlg( aFrame, _( "Load Error" ) );
105
106 dlg.MessageSet( _( "Errors loading symbols:" ) );
107
108 wxString msg = loader.GetErrors();
109 msg.Replace( "\n", "<BR>" );
110
111 dlg.AddHTML_Text( msg );
112 dlg.ShowModal();
113 }
114
115 if( loadedSymbolMap.size() > 0 )
116 {
118 PROJECT_FILE& project = aFrame->Prj().GetProjectFile();
119
120 auto addFunc =
121 [&]( const wxString& aLibName, const std::vector<LIB_SYMBOL*>& aSymbolList,
122 const wxString& aDescription )
123 {
124 std::vector<LIB_TREE_ITEM*> treeItems( aSymbolList.begin(), aSymbolList.end() );
125 bool pinned = alg::contains( cfg->m_Session.pinned_symbol_libs, aLibName )
126 || alg::contains( project.m_PinnedSymbolLibs, aLibName );
127
128 DoAddLibrary( aLibName, aDescription, treeItems, pinned, false );
129 };
130
131 for( const auto& [libNickname, libSymbols] : loadedSymbolMap )
132 {
133 SYMBOL_LIB_TABLE_ROW* row = m_libs->FindRow( libNickname );
134
135 wxCHECK2( row, continue );
136
137 if( !row->GetIsVisible() )
138 continue;
139
140 std::vector<wxString> additionalColumns;
141 row->GetAvailableSymbolFields( additionalColumns );
142
143 for( const wxString& column : additionalColumns )
144 addColumnIfNecessary( column );
145
146 if( row->SupportsSubLibraries() )
147 {
148 std::vector<wxString> subLibraries;
149 row->GetSubLibraryNames( subLibraries );
150
151 wxString parentDesc = m_libs->GetDescription( libNickname );
152
153 for( const wxString& lib : subLibraries )
154 {
155 wxString suffix = lib.IsEmpty() ? wxString( wxT( "" ) )
156 : wxString::Format( wxT( " - %s" ), lib );
157 wxString name = wxString::Format( wxT( "%s%s" ), libNickname, suffix );
158 wxString desc = row->GetSubLibraryDescription( lib );
159
160 if( !parentDesc.IsEmpty() )
161 {
162 desc = wxString::Format( wxT( "%s (%s)" ),
163 parentDesc,
164 desc.IsEmpty() ? lib : desc );
165 }
166
167 UTF8 utf8Lib( lib );
168
169 std::vector<LIB_SYMBOL*> symbols;
170
171 std::copy_if( libSymbols.begin(), libSymbols.end(),
172 std::back_inserter( symbols ),
173 [&utf8Lib]( LIB_SYMBOL* aSym )
174 {
175 return utf8Lib == aSym->GetLibId().GetSubLibraryName();
176 } );
177
178 addFunc( name, symbols, desc );
179 }
180 }
181 else
182 {
183 addFunc( libNickname, libSymbols, m_libs->GetDescription( libNickname ) );
184 }
185 }
186 }
187
188 KIID::CreateNilUuids( false );
189
191
192 if( progressReporter )
193 {
194 // Force immediate deletion of the APP_PROGRESS_DIALOG. Do not use Destroy(), or Destroy()
195 // followed by wxSafeYield() because on Windows, APP_PROGRESS_DIALOG has some side effects
196 // on the event loop manager.
197 // One in particular is the call of ShowModal() following SYMBOL_TREE_MODEL_ADAPTER
198 // creating a APP_PROGRESS_DIALOG (which has incorrect modal behaviour).
199 progressReporter.reset();
200 m_show_progress = false;
201 }
202
203 return !cancelled;
204}
205
206
207void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname, bool pinned )
208{
209 bool onlyPowerSymbols = ( GetFilter() != nullptr );
210 std::vector<LIB_SYMBOL*> symbols;
211 std::vector<LIB_TREE_ITEM*> comp_list;
212
213 try
214 {
215 m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
216 }
217 catch( const IO_ERROR& ioe )
218 {
219 wxLogError( _( "Error loading symbol library '%s'." ) + wxS( "\n%s" ),
220 aLibNickname,
221 ioe.What() );
222 return;
223 }
224
225 if( symbols.size() > 0 )
226 {
227 comp_list.assign( symbols.begin(), symbols.end() );
228 DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, pinned,
229 false );
230 }
231}
232
233
234wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
235{
236 return GenerateAliasInfo( m_libs, aLibId, aUnit );
237}
238
239
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:288
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:78
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:678
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
wxString GetSubLibraryDescription(const wxString &aName) 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)
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:1059
see class PGM_BASE
std::vector< wxString > pinned_symbol_libs
#define PROGRESS_INTERVAL_MILLIS