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 The 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
23
24#include <wx/log.h>
25#include <wx/tokenzr.h>
26#include <wx/window.h>
27#include <core/kicad_algo.h>
28#include <pgm_base.h>
32#include <generate_alias_info.h>
33#include <sch_base_frame.h>
34#include <locale_io.h>
35#include <symbol_async_loader.h>
36#include <symbol_lib_table.h>
37#include <string_utils.h>
38
40
41#define PROGRESS_INTERVAL_MILLIS 33 // 30 FPS refresh rate
42
43
44wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
46{
47 auto* adapter = new SYMBOL_TREE_MODEL_ADAPTER( aParent, aLibs );
48 return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
49}
50
51
53 LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs", aParent->GetViewerSettingsBase()->m_LibTree ),
54 m_libs( (SYMBOL_LIB_TABLE*) aLibs )
55{
56 m_availableColumns.emplace_back( GetDefaultFieldName( FIELD_T::VALUE, false ) );
57 m_availableColumns.emplace_back( GetDefaultFieldName( FIELD_T::FOOTPRINT, false ) );
58
59 // Description is always shown
60 //m_availableColumns.emplace_back( GetDefaultFieldName( FIELD_T::DESCRIPTION, false ) );
61
62 // Datasheet probably isn't useful, but better to leave that decision to the user:
63 m_availableColumns.emplace_back( GetDefaultFieldName( FIELD_T::DATASHEET, false ) );
64}
65
66
68{}
69
70
71bool SYMBOL_TREE_MODEL_ADAPTER::AddLibraries( const std::vector<wxString>& aNicknames,
72 SCH_BASE_FRAME* aFrame )
73{
74 std::unique_ptr<WX_PROGRESS_REPORTER> progressReporter = nullptr;
75
76 if( m_show_progress )
77 {
78 progressReporter = std::make_unique<WX_PROGRESS_REPORTER>( aFrame, _( "Load Symbol Libraries" ),
79 aNicknames.size(), PR_CAN_ABORT );
80 }
81
82 // Disable KIID generation: not needed for library parts; sometimes very slow
84
85 std::unordered_map<wxString, std::vector<LIB_SYMBOL*>> loadedSymbolMap;
86
87 SYMBOL_ASYNC_LOADER loader( aNicknames, m_libs, GetFilter() != nullptr, &loadedSymbolMap,
88 progressReporter.get() );
89
90 LOCALE_IO toggle;
91
92 loader.Start();
93
94 while( !loader.Done() )
95 {
96 if( progressReporter && !progressReporter->KeepRefreshing() )
97 break;
98
99 wxMilliSleep( PROGRESS_INTERVAL_MILLIS );
100 }
101
102 loader.Join();
103
104 bool cancelled = false;
105
106 if( progressReporter )
107 cancelled = progressReporter->IsCancelled();
108
109 if( !loader.GetErrors().IsEmpty() )
110 {
111 HTML_MESSAGE_BOX dlg( aFrame, _( "Load Error" ) );
112
113 dlg.MessageSet( _( "Errors loading symbols:" ) );
114
115 wxString msg = loader.GetErrors();
116 msg.Replace( "\n", "<BR>" );
117
118 dlg.AddHTML_Text( msg );
119 dlg.ShowModal();
120 }
121
122 if( loadedSymbolMap.size() > 0 )
123 {
125 PROJECT_FILE& project = aFrame->Prj().GetProjectFile();
126
127 auto addFunc =
128 [&]( const wxString& aLibName, const std::vector<LIB_SYMBOL*>& aSymbolList,
129 const wxString& aDescription )
130 {
131 std::vector<LIB_TREE_ITEM*> treeItems( aSymbolList.begin(), aSymbolList.end() );
132 bool pinned = alg::contains( cfg->m_Session.pinned_symbol_libs, aLibName )
133 || alg::contains( project.m_PinnedSymbolLibs, aLibName );
134
135 DoAddLibrary( aLibName, aDescription, treeItems, pinned, false );
136 };
137
138 for( const auto& [libNickname, libSymbols] : loadedSymbolMap )
139 {
140 SYMBOL_LIB_TABLE_ROW* row = m_libs->FindRow( libNickname );
141
142 wxCHECK2( row, continue );
143
144 if( !row->GetIsVisible() )
145 continue;
146
147 std::vector<wxString> additionalColumns;
148 row->GetAvailableSymbolFields( additionalColumns );
149
150 for( const wxString& column : additionalColumns )
151 addColumnIfNecessary( column );
152
153 if( row->SupportsSubLibraries() )
154 {
155 std::vector<wxString> subLibraries;
156 row->GetSubLibraryNames( subLibraries );
157
158 wxString parentDesc = m_libs->GetDescription( libNickname );
159
160 for( const wxString& lib : subLibraries )
161 {
162 wxString suffix = lib.IsEmpty() ? wxString( wxT( "" ) )
163 : wxString::Format( wxT( " - %s" ), lib );
164 wxString name = wxString::Format( wxT( "%s%s" ), libNickname, suffix );
165 wxString desc = row->GetSubLibraryDescription( lib );
166
167 if( !parentDesc.IsEmpty() )
168 {
169 desc = wxString::Format( wxT( "%s (%s)" ),
170 parentDesc,
171 desc.IsEmpty() ? lib : desc );
172 }
173
174 UTF8 utf8Lib( lib );
175
176 std::vector<LIB_SYMBOL*> symbols;
177
178 std::copy_if( libSymbols.begin(), libSymbols.end(),
179 std::back_inserter( symbols ),
180 [&utf8Lib]( LIB_SYMBOL* aSym )
181 {
182 return utf8Lib == aSym->GetLibId().GetSubLibraryName();
183 } );
184
185 addFunc( name, symbols, desc );
186 }
187 }
188 else
189 {
190 addFunc( libNickname, libSymbols, m_libs->GetDescription( libNickname ) );
191 }
192 }
193 }
194
195 KIID::CreateNilUuids( false );
196
198
199 if( progressReporter )
200 {
201 // Force immediate deletion of the APP_PROGRESS_DIALOG. Do not use Destroy(), or Destroy()
202 // followed by wxSafeYield() because on Windows, APP_PROGRESS_DIALOG has some side effects
203 // on the event loop manager.
204 // One in particular is the call of ShowModal() following SYMBOL_TREE_MODEL_ADAPTER
205 // creating a APP_PROGRESS_DIALOG (which has incorrect modal behaviour).
206 progressReporter.reset();
207 m_show_progress = false;
208 }
209
210 return !cancelled;
211}
212
213
214void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname, bool pinned )
215{
216 bool onlyPowerSymbols = ( GetFilter() != nullptr );
217 std::vector<LIB_SYMBOL*> symbols;
218 std::vector<LIB_TREE_ITEM*> comp_list;
219
220 try
221 {
222 m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
223 }
224 catch( const IO_ERROR& ioe )
225 {
226 wxLogError( _( "Error loading symbol library '%s'." ) + wxS( "\n%s" ),
227 aLibNickname,
228 ioe.What() );
229 return;
230 }
231
232 if( symbols.size() > 0 )
233 {
234 comp_list.assign( symbols.begin(), symbols.end() );
235 DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, pinned,
236 false );
237 }
238}
239
240
241wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
242{
243 return GenerateAliasInfo( m_libs, aLibId, aUnit );
244}
245
246
const char * name
Definition: DXF_plotter.cpp:62
int ShowModal() override
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:85
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)
std::function< bool(LIB_TREE_NODE &aNode)> * GetFilter() const
Return the active filter.
std::vector< wxString > m_shownColumns
std::vector< wxString > m_availableColumns
LIB_TREE_NODE_LIBRARY & 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.
void AssignIntrinsicRanks(const std::vector< wxString > &aShownColumns, 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
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:565
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:68
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:204
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
const wxString & GetErrors() const
void Start()
Spin up threads to load all the libraries in m_nicknames.
bool Join()
Finalize 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
bool AddLibraries(const std::vector< wxString > &aNicknames, SCH_BASE_FRAME *aFrame)
Add all the libraries in a SYMBOL_LIB_TABLE to the model.
SYMBOL_TREE_MODEL_ADAPTER(SCH_BASE_FRAME *aParent, LIB_TABLE *aLibs)
Constructor; takes a set of libraries to be included in the search.
static wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > Create(SCH_BASE_FRAME *aParent, LIB_TABLE *aLibs)
Factory function: create a model adapter in a reference-counting container.
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)
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:902
see class PGM_BASE
std::vector< wxString > pinned_symbol_libs
#define PROGRESS_INTERVAL_MILLIS
wxString GetDefaultFieldName(FIELD_T aFieldId, bool aTranslateForHI)
Return a default symbol field name for a mandatory field type.
#define PR_CAN_ABORT