KiCad PCB EDA Suite
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*>> loadedSymbols;
82
83 SYMBOL_ASYNC_LOADER loader( aNicknames, m_libs,
85 &loadedSymbols, progressReporter.get() );
86
87 LOCALE_IO toggle;
88
89 loader.Start();
90
91 while( !loader.Done() )
92 {
93 if( progressReporter && !progressReporter->KeepRefreshing() )
94 break;
95
96 wxMilliSleep( PROGRESS_INTERVAL_MILLIS );
97 }
98
99 loader.Join();
100
101 bool cancelled = false;
102
103 if( progressReporter )
104 cancelled = progressReporter->IsCancelled();
105
106 if( !loader.GetErrors().IsEmpty() )
107 {
108 HTML_MESSAGE_BOX dlg( aFrame, _( "Load Error" ) );
109
110 dlg.MessageSet( _( "Errors loading symbols:" ) );
111
112 wxString msg = loader.GetErrors();
113 msg.Replace( "\n", "<BR>" );
114
115 dlg.AddHTML_Text( msg );
116 dlg.ShowModal();
117 }
118
119 if( loadedSymbols.size() > 0 )
120 {
121 COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
122 PROJECT_FILE& project = aFrame->Prj().GetProjectFile();
123
124 auto addFunc =
125 [&]( const wxString& aLibName, std::vector<LIB_SYMBOL*> aSymbolList,
126 const wxString& aDescription )
127 {
128 std::vector<LIB_TREE_ITEM*> treeItems( aSymbolList.begin(), aSymbolList.end() );
129 bool pinned = alg::contains( cfg->m_Session.pinned_symbol_libs, aLibName )
130 || alg::contains( project.m_PinnedSymbolLibs, aLibName );
131
132 DoAddLibrary( aLibName, aDescription, treeItems, pinned, false );
133 };
134
135 for( const std::pair<const wxString, std::vector<LIB_SYMBOL*>>& pair : loadedSymbols )
136 {
137 SYMBOL_LIB_TABLE_ROW* row = m_libs->FindRow( pair.first );
138
139 wxCHECK2( row, continue );
140
141 if( !row->GetIsVisible() )
142 continue;
143
144 std::vector<wxString> additionalColumns;
145 row->GetAvailableSymbolFields( additionalColumns );
146
147 for( const wxString& column : additionalColumns )
148 addColumnIfNecessary( column );
149
150 if( row->SupportsSubLibraries() )
151 {
152 std::vector<wxString> subLibraries;
153 row->GetSubLibraryNames( subLibraries );
154
155 wxString parentDesc = m_libs->GetDescription( pair.first );
156
157 for( const wxString& lib : subLibraries )
158 {
159 wxString suffix = lib.IsEmpty() ? wxString( wxT( "" ) )
160 : wxString::Format( wxT( " - %s" ), lib );
161 wxString name = wxString::Format( wxT( "%s%s" ), pair.first, suffix );
162 wxString desc;
163
164 if( !parentDesc.IsEmpty() )
165 desc = wxString::Format( wxT( "%s (%s)" ), parentDesc, lib );
166
167 std::vector<LIB_SYMBOL*> symbols;
168
169 std::copy_if( pair.second.begin(), pair.second.end(),
170 std::back_inserter( symbols ),
171 [&lib]( LIB_SYMBOL* aSym )
172 {
173 return lib.IsSameAs( aSym->GetLibId().GetSubLibraryName() );
174 } );
175
176 addFunc( name, symbols, desc );
177 }
178 }
179 else
180 {
181 addFunc( pair.first, pair.second, m_libs->GetDescription( pair.first ) );
182 }
183 }
184 }
185
186 KIID::CreateNilUuids( false );
187
189
190 if( progressReporter )
191 {
192 // Force immediate deletion of the APP_PROGRESS_DIALOG. Do not use Destroy(), or Destroy()
193 // followed by wxSafeYield() because on Windows, APP_PROGRESS_DIALOG has some side effects
194 // on the event loop manager.
195 // One in particular is the call of ShowModal() following SYMBOL_TREE_MODEL_ADAPTER
196 // creating a APP_PROGRESS_DIALOG (which has incorrect modal behaviour).
197 progressReporter.reset();
198 m_show_progress = false;
199 }
200
201 return !cancelled;
202}
203
204
205void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname, bool pinned )
206{
207 bool onlyPowerSymbols = ( GetFilter() == SYM_FILTER_POWER );
208 std::vector<LIB_SYMBOL*> symbols;
209 std::vector<LIB_TREE_ITEM*> comp_list;
210
211 try
212 {
213 m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
214 }
215 catch( const IO_ERROR& ioe )
216 {
217 wxLogError( _( "Error loading symbol library '%s'." ) + wxS( "\n%s" ),
218 aLibNickname,
219 ioe.What() );
220 return;
221 }
222
223 if( symbols.size() > 0 )
224 {
225 comp_list.assign( symbols.begin(), symbols.end() );
226 DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, pinned,
227 false );
228 }
229}
230
231
232wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
233{
234 return GenerateAliasInfo( m_libs, aLibId, aUnit );
235}
236
237
const char * name
Definition: DXF_plotter.cpp:56
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:76
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.
SYM_FILTER_TYPE GetFilter() const
Return the active filter.
std::vector< wxString > m_availableColumns
@ SYM_FILTER_POWER
list symbols flagged PWR
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:41
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:65
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:149
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_PLUGIN obje...
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.
#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:99
see class PGM_BASE
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:111
std::vector< wxString > pinned_symbol_libs
#define PROGRESS_INTERVAL_MILLIS