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