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 <string_utils.h>
36#include <trace_helpers.h>
38
40
41#define PROGRESS_INTERVAL_MILLIS 33 // 30 FPS refresh rate
42
43
44wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
46 SYMBOL_LIBRARY_ADAPTER* aManager )
47{
48 auto* adapter = new SYMBOL_TREE_MODEL_ADAPTER( aParent, aManager );
49 return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
50}
51
52
54 SYMBOL_LIBRARY_ADAPTER* aLibs ) :
55 LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs",
56 aParent->GetViewerSettingsBase()->m_LibTree ),
57 m_adapter( aLibs ),
59{
62
63 // Description is always shown
64 //m_availableColumns.emplace_back( GetDefaultFieldName( FIELD_T::DESCRIPTION, false ) );
65
66 // Datasheet probably isn't useful, but better to leave that decision to the user:
68}
69
70
73
74
76{
79
80 auto addFunc =
81 [&]( const wxString& aLibName, const std::vector<LIB_SYMBOL*>& aSymbolList,
82 const wxString& aDescription )
83 {
84 std::vector<LIB_TREE_ITEM*> treeItems( aSymbolList.begin(), aSymbolList.end() );
85 bool pinned = alg::contains( cfg->m_Session.pinned_symbol_libs, aLibName )
86 || alg::contains( project.m_PinnedSymbolLibs, aLibName );
87
88 DoAddLibrary( aLibName, aDescription, treeItems, pinned, false );
89 };
90
92
93 std::vector<wxString> toLoad;
94
95 std::ranges::copy( m_pending_load_libraries, std::back_inserter( toLoad ) );
96
97 bool isLazyLoad = !m_pending_load_libraries.empty();
99
100 if( toLoad.empty() )
101 {
102 for( const LIBRARY_TABLE_ROW* row : manager.Rows( LIBRARY_TABLE_TYPE::SYMBOL ) )
103 {
104 if( row->Hidden() )
105 continue;
106
107 toLoad.emplace_back( row->Nickname() );
108 }
109 }
110
111 for( const wxString& lib : toLoad )
112 {
113 if( !m_adapter->GetLibraryStatus( lib ) )
114 {
115 m_pending_load_libraries.insert( lib );
116 continue;
117 }
118
119 std::optional<const LIBRARY_TABLE_ROW*> rowResult =
120 manager.GetRow( LIBRARY_TABLE_TYPE::SYMBOL, lib );
121
122 wxCHECK2( rowResult, continue );
123
124 wxString libDescription = ( *rowResult )->Description();
125
126 std::vector<LIB_SYMBOL*> libSymbols = m_adapter->GetSymbols( lib );
127
128 for( const wxString& column : m_adapter->GetAvailableExtraFields( lib ) )
129 addColumnIfNecessary( column );
130
131 if( m_adapter->SupportsSubLibraries( lib ) )
132 {
133 for( const auto& [nickname, description] : m_adapter->GetSubLibraries( lib ) )
134 {
135 wxString suffix = lib.IsEmpty() ? wxString( wxT( "" ) )
136 : wxString::Format( wxT( " - %s" ), nickname );
137 wxString name = wxString::Format( wxT( "%s%s" ), lib, suffix );
138 wxString desc = description;
139
140 if( !libDescription.IsEmpty() )
141 {
142 desc = wxString::Format( wxT( "%s (%s)" ),
143 libDescription,
144 desc.IsEmpty() ? lib : desc );
145 }
146
147 UTF8 utf8Lib( nickname );
148
149 std::vector<LIB_SYMBOL*> symbols;
150
151 std::copy_if( libSymbols.begin(), libSymbols.end(),
152 std::back_inserter( symbols ),
153 [&utf8Lib]( LIB_SYMBOL* aSym )
154 {
155 return utf8Lib == aSym->GetLibId().GetSubLibraryName();
156 } );
157
158 addFunc( name, symbols, desc );
159 }
160 }
161 else
162 {
163 addFunc( lib, libSymbols, libDescription );
164 }
165 }
166
168 {
169 m_check_pending_libraries_timer = std::make_unique<wxTimer>( aFrame );
170
171 wxLogTrace( traceLibraries, "%zu pending libraries, starting lazy load...",
173
174 aFrame->Bind( wxEVT_TIMER,
175 [&, aFrame]( wxTimerEvent& )
176 {
177 AddLibraries( aFrame );
178
179 if( m_pending_load_libraries.empty() )
180 {
183 wxLogTrace( traceLibraries, "Done lazy-loading libraries" );
184 }
185 },
187
188 m_check_pending_libraries_timer->Start( 1000 );
189 }
190
191 m_tree.AssignIntrinsicRanks( m_shownColumns );
192
193 if( isLazyLoad && m_lazyLoadHandler )
194 {
197 }
198}
199
200
201void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname, bool pinned )
202{
206 std::vector<LIB_SYMBOL*> symbols = m_adapter->GetSymbols( aLibNickname, type );
207 std::vector<LIB_TREE_ITEM*> comp_list;
208
209 std::optional<const LIBRARY_TABLE_ROW*> row =
211
212 if( row && symbols.size() > 0 )
213 {
214 comp_list.assign( symbols.begin(), symbols.end() );
215 DoAddLibrary( aLibNickname, ( *row )->Description(), comp_list, pinned, false );
216 }
217}
218
219
220wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
221{
222 return GenerateAliasInfo( m_adapter, aLibId, aUnit );
223}
const char * name
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
std::vector< LIBRARY_TABLE_ROW * > Rows(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH, bool aIncludeInvalid=false) const
Returns a flattened list of libraries of the given type.
std::optional< LIBRARY_TABLE_ROW * > GetRow(LIBRARY_TABLE_TYPE aType, const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH) const
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
LIB_TREE_MODEL_ADAPTER(EDA_BASE_FRAME *aParent, const wxString &aPinnedKey, APP_SETTINGS_BASE::LIB_TREE &aSettingsStruct)
Create the adapter.
void addColumnIfNecessary(const wxString &aHeader)
std::function< void()> m_lazyLoadHandler
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.
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:537
virtual LIBRARY_MANAGER & GetLibraryManager() const
Definition pgm_base.h:130
The backing store for a PROJECT, in JSON format.
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:205
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
An interface to the global shared library manager that is schematic-specific and linked to one projec...
wxString GenerateInfo(LIB_ID const &aLibId, int aUnit) override
void AddLibraries(SCH_BASE_FRAME *aFrame)
Add all the libraries in a SYMBOL_LIB_TABLE to the model.
static wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > Create(SCH_BASE_FRAME *aParent, SYMBOL_LIBRARY_ADAPTER *aLibs)
Factory function: create a model adapter in a reference-counting container.
std::unique_ptr< wxTimer > m_check_pending_libraries_timer
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_LIBRARY_ADAPTER * m_adapter
std::set< wxString > m_pending_load_libraries
SYMBOL_TREE_MODEL_ADAPTER(SCH_BASE_FRAME *aParent, SYMBOL_LIBRARY_ADAPTER *aManager)
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
wxString GenerateAliasInfo(SYMBOL_LIBRARY_ADAPTER *aLibs, LIB_ID const &aLibId, int aUnit)
Return an HTML page describing a LIB_ID in a #SYMBOL_LIB_TABLE.
const wxChar *const traceLibraries
Flag to enable library table and library manager tracing.
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:946
see class PGM_BASE
std::vector< wxString > pinned_symbol_libs
wxString GetDefaultFieldName(FIELD_T aFieldId, bool aTranslateForHI)
Return a default symbol field name for a mandatory field type.
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
@ DATASHEET
name of datasheet
@ VALUE
Field Value of part, i.e. "3.3K".
wxLogTrace helper definitions.