KiCad PCB EDA Suite
Loading...
Searching...
No Matches
symbol_tree_synchronizing_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 CERN
5 * Copyright (C) 2019-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * https://www.gnu.org/licenses/gpl-3.0.html
21 * or you may search the http://www.gnu.org website for the version 3 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <pgm_base.h>
30#include <symbol_lib_table.h>
32#include <project_sch.h>
33#include <string_utils.h>
35#include <widgets/wx_panel.h>
36
37
38wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
40 SYMBOL_LIBRARY_MANAGER* aLibMgr )
41{
42 auto* adapter = new SYMBOL_TREE_SYNCHRONIZING_ADAPTER( aParent, aLibMgr );
43 return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
44}
45
46
48 SYMBOL_LIBRARY_MANAGER* aLibMgr ) :
49 LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
50 m_frame( aParent ),
51 m_libMgr( aLibMgr ),
52 m_lastSyncHash( -1 )
53{
54}
55
56
58{
60}
61
62
63bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
64{
65 const LIB_TREE_NODE* node = ToNode( aItem );
66 return node ? node->m_Type == LIB_TREE_NODE::LIBRARY : true;
67}
68
69
70#define PROGRESS_INTERVAL_MILLIS 120
71
72void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync( const wxString& aForceRefresh,
73 std::function<void( int, int, const wxString& )> aProgressCallback )
74{
75 wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
76
78 int i = 0, max = GetLibrariesCount();
79
80 // Process already stored libraries
81 for( auto it = m_tree.m_Children.begin(); it != m_tree.m_Children.end(); /* iteration inside */ )
82 {
83 const wxString& name = it->get()->m_Name;
84
85 if( wxGetUTCTimeMillis() > nextUpdate )
86 {
87 aProgressCallback( i, max, name );
88 nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
89 }
90
91 // There is a bug in SYMBOL_LIBRARY_MANAGER::LibraryExists() that uses the buffered
92 // modified libraries before the symbol library table which prevents the library from
93 // being removed from the tree control.
94 if( !m_libMgr->LibraryExists( name, true )
98 || name == aForceRefresh )
99 {
100 it = deleteLibrary( it );
101 continue;
102 }
103 else
104 {
105 updateLibrary( *(LIB_TREE_NODE_LIBRARY*) it->get() );
106 }
107
108 ++it;
109 ++i;
110 }
111
112 // Look for new libraries
113 //
114 COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
116
117 for( const wxString& libName : m_libMgr->GetLibraryNames() )
118 {
119 if( m_libHashes.count( libName ) == 0 )
120 {
121 if( wxGetUTCTimeMillis() > nextUpdate )
122 {
123 aProgressCallback( i++, max, libName );
124 nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
125 }
126
128 bool pinned = alg::contains( cfg->m_Session.pinned_symbol_libs, libName )
129 || alg::contains( project.m_PinnedSymbolLibs, libName );
130
131 LIB_TREE_NODE_LIBRARY& lib_node = DoAddLibraryNode( libName, library->GetDescr(),
132 pinned );
133
134 updateLibrary( lib_node );
135 }
136 }
137
139}
140
141
143{
145
146 for( const wxString& libName : m_libMgr->GetLibraryNames() )
147 {
148 if( m_libHashes.count( libName ) == 0 )
149 ++count;
150 }
151
152 return count;
153}
154
155
157{
158 auto hashIt = m_libHashes.find( aLibNode.m_Name );
159
160 if( hashIt == m_libHashes.end() )
161 {
162 // add a new library
163 for( LIB_SYMBOL* alias : m_libMgr->GetAliases( aLibNode.m_Name ) )
164 aLibNode.AddItem( alias );
165 }
166 else if( hashIt->second != m_libMgr->GetLibraryHash( aLibNode.m_Name ) )
167 {
168 // update an existing library
169 std::list<LIB_SYMBOL*> aliases = m_libMgr->GetAliases( aLibNode.m_Name );
170
171 // remove the common part from the aliases list
172 for( auto nodeIt = aLibNode.m_Children.begin(); nodeIt != aLibNode.m_Children.end(); )
173 {
174 auto aliasIt = std::find_if( aliases.begin(), aliases.end(),
175 [&] ( const LIB_SYMBOL* a )
176 {
177 return a->GetName() == (*nodeIt)->m_LibId.GetLibItemName();
178 } );
179
180 if( aliasIt != aliases.end() )
181 {
182 // alias exists both in the symbol tree and the library manager,
183 // update only the node data.
184 static_cast<LIB_TREE_NODE_ITEM*>( nodeIt->get() )->Update( *aliasIt );
185 aliases.erase( aliasIt );
186 ++nodeIt;
187 }
188 else
189 {
190 // node does not exist in the library manager, remove the corresponding node
191 nodeIt = aLibNode.m_Children.erase( nodeIt );
192 }
193 }
194
195 // now the aliases list contains only new aliases that need to be added to the tree
196 for( LIB_SYMBOL* alias : aliases )
197 aLibNode.AddItem( alias );
198 }
199
200 aLibNode.AssignIntrinsicRanks();
201 m_libHashes[aLibNode.m_Name] = m_libMgr->GetLibraryHash( aLibNode.m_Name );
202}
203
204
205LIB_TREE_NODE::PTR_VECTOR::iterator
206SYMBOL_TREE_SYNCHRONIZING_ADAPTER::deleteLibrary( LIB_TREE_NODE::PTR_VECTOR::iterator& aLibNodeIt )
207{
208 LIB_TREE_NODE* node = aLibNodeIt->get();
209 m_libHashes.erase( node->m_Name );
210 auto it = m_tree.m_Children.erase( aLibNodeIt );
211 return it;
212}
213
214
216{
217 if( m_frame->GetCurSymbol() )
218 return FindItem( m_frame->GetCurSymbol()->GetLibId() );
219
220 return wxDataViewItem();
221}
222
223
224void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
225 unsigned int aCol ) const
226{
227 if( IsFrozen() )
228 {
229 aVariant = wxEmptyString;
230 return;
231 }
232
233 LIB_TREE_NODE* node = ToNode( aItem );
234 wxASSERT( node );
235
236 switch( aCol )
237 {
238 case NAME_COL:
239 if( m_frame->GetCurSymbol() && m_frame->GetCurSymbol()->GetLibId() == node->m_LibId )
241
242 if( node->m_Pinned )
243 aVariant = GetPinningSymbol() + UnescapeString( node->m_Name );
244 else
245 aVariant = UnescapeString( node->m_Name );
246
247 // mark modified items with an asterisk
248 if( node->m_Type == LIB_TREE_NODE::LIBRARY )
249 {
250 if( m_libMgr->IsLibraryModified( node->m_Name ) )
251 aVariant = aVariant.GetString() + " *";
252 }
253 else if( node->m_Type == LIB_TREE_NODE::ITEM )
254 {
255 if( m_libMgr->IsSymbolModified( node->m_Name, node->m_Parent->m_Name ) )
256 aVariant = aVariant.GetString() + " *";
257 }
258
259 break;
260
261 default:
262 if( m_colIdxMap.count( aCol ) )
263 {
264 if( node->m_Type == LIB_TREE_NODE::LIBRARY )
265 {
267 SYMBOL_LIB_TABLE_ROW* lib = libMgr.GetLibrary( node->m_LibId.GetLibNickname() );
268
269 if( lib )
270 node->m_Desc = lib->GetDescr();
271
272 if( !m_libMgr->IsLibraryLoaded( node->m_Name ) )
273 aVariant = _( "(failed to load)" ) + wxS( " " ) + aVariant.GetString();
274 }
275
276 const wxString& key = m_colIdxMap.at( aCol );
277
278 if( m_frame->GetCurSymbol() && m_frame->GetCurSymbol()->GetLibId() == node->m_LibId )
279 {
281 }
282
283 wxString valueStr;
284
285 if( key == wxT( "Description" ) )
286 valueStr = node->m_Desc;
287 else if( node->m_Fields.count( key ) )
288 valueStr = node->m_Fields.at( key );
289 else
290 valueStr = wxEmptyString;
291
292 valueStr.Replace( wxS( "\n" ), wxS( " " ) ); // Clear line breaks
293
294 aVariant = valueStr;
295 }
296 break;
297 }
298}
299
300
301bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
302 wxDataViewItemAttr& aAttr ) const
303{
304 if( IsFrozen() )
305 return false;
306
307 LIB_TREE_NODE* node = ToNode( aItem );
308 wxCHECK( node, false );
309
310 // Mark both columns of unloaded libraries using grey text color (to look disabled)
311 if( node->m_Type == LIB_TREE_NODE::LIBRARY && !m_libMgr->IsLibraryLoaded( node->m_Name ) )
312 {
313 aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
314 return true;
315 }
316
317 // The remaining attributes are only for the name column
318 if( aCol != NAME_COL )
319 return false;
320
321 LIB_SYMBOL* curSymbol = m_frame->GetCurSymbol();
322
323 switch( node->m_Type )
324 {
326 // mark modified libs with bold font
327 aAttr.SetBold( m_libMgr->IsLibraryModified( node->m_Name ) );
328
329 // mark the current library if it's collapsed
330 if( curSymbol && curSymbol->GetLibId().GetLibNickname() == node->m_LibId.GetLibNickname() )
331 {
332 if( !m_widget->IsExpanded( ToItem( node ) ) )
333 {
334 aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
335 // proxy for "is canvas item"
336 }
337 }
338 break;
339
341 // mark modified part with bold font
342 aAttr.SetBold( m_libMgr->IsSymbolModified( node->m_Name, node->m_Parent->m_Name ) );
343
344 // mark aliases with italic font
345 aAttr.SetItalic( !node->m_IsRoot );
346
347 // mark the current (on-canvas) part
348 if( curSymbol && curSymbol->GetLibId() == node->m_LibId )
349 {
350 aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
351 // proxy for "is canvas item"
352 }
353 break;
354
355 default:
356 return false;
357 }
358
359 return true;
360}
361
362
363bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::HasPreview( const wxDataViewItem& aItem )
364{
365 LIB_TREE_NODE* node = ToNode( aItem );
366 wxCHECK( node, false );
367
368 return node->m_Type == LIB_TREE_NODE::ITEM && node->m_LibId != m_frame->GetTargetLibId();
369}
370
371
373 const wxDataViewItem& aItem )
374{
375 static const wxString c_previewName = wxS( "symHoverPreview" );
376
377 LIB_TREE_NODE* node = ToNode( aItem );
378 wxCHECK( node, /* void */ );
379
380 SYMBOL_PREVIEW_WIDGET* preview = dynamic_cast<SYMBOL_PREVIEW_WIDGET*>(
381 wxWindow::FindWindowByName( c_previewName, aParent ) );
382
383 if( !preview )
384 {
385 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
386 aParent->SetSizer( mainSizer );
387
388 WX_PANEL* panel = new WX_PANEL( aParent );
389 panel->SetBorders( true, true, true, true );
391
392 wxBoxSizer* panelSizer = new wxBoxSizer( wxVERTICAL );
393 panel->SetSizer( panelSizer );
394
396 preview = new SYMBOL_PREVIEW_WIDGET( panel, &m_frame->Kiway(), false, backend );
397 preview->SetName( c_previewName );
398 preview->SetLayoutDirection( wxLayout_LeftToRight );
399
400 panelSizer->Add( preview, 1, wxEXPAND | wxALL, 1 );
401 mainSizer->Add( panel, 1, wxEXPAND, 0 );
402 aParent->Layout();
403 }
404
405 preview->DisplaySymbol( node->m_LibId, node->m_Unit );
406}
const char * name
Definition: DXF_plotter.cpp:57
GAL_TYPE GetBackend() const
Return the type of backend currently used by GAL canvas.
static const COLOR4D BLACK
Definition: color4d.h:386
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:53
const wxString GetUniStringLibItemName() const
Get strings for display messages in dialogs.
Definition: lib_id.h:112
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
Symbol library management helper that is specific to the symbol library editor frame.
Define a library symbol object.
Definition: lib_symbol.h:99
LIB_ID GetLibId() const override
Definition: lib_symbol.h:163
wxString GetDescription() override
Definition: lib_symbol.h:178
const wxString & GetDescr() const
Return the description of the library referenced by this row.
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
static LIB_TREE_NODE * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> #SYM_TREE_NODE.
static wxDataViewItem ToItem(const LIB_TREE_NODE *aNode)
Convert #SYM_TREE_NODE -> wxDataViewItem.
static const wxString GetPinningSymbol()
@ NAME_COL
Library or library item name column.
std::map< unsigned, wxString > m_colIdxMap
virtual int GetLibrariesCount() const
Return the number of libraries loaded in the tree.
LIB_TREE_NODE_LIBRARY & DoAddLibraryNode(const wxString &aNodeName, const wxString &aDesc, bool pinned)
wxDataViewItem FindItem(const LIB_ID &aLibId)
Returns tree item corresponding to part.
Node type: LIB_ID.
Node type: library.
LIB_TREE_NODE_ITEM & AddItem(LIB_TREE_ITEM *aItem)
Construct a new alias node, add it to this library, and return it.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
enum TYPE m_Type
std::map< wxString, wxString > m_Fields
List of weighted search terms.
PTR_VECTOR m_Children
LIB_TREE_NODE * m_Parent
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:70
static SYMBOL_LIB_TABLE * SchSymbolLibTable(PROJECT *aProject)
Accessor for project symbol library table.
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:166
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
Handle actions for the various symbol editor and viewers.
The symbol library editor main window.
LIB_ID GetTargetLibId() const
Return either the symbol selected in the symbol tree (if context menu is active) or the symbol on the...
LIB_SYMBOL * GetCurSymbol() const
Return the current symbol being edited or NULL if none selected.
LIB_SYMBOL_LIBRARY_MANAGER & GetLibManager()
Class to handle modifications to the symbol libraries.
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.
bool IsLibraryModified(const wxString &aLibrary) const
Return true if library has unsaved modifications.
wxArrayString GetLibraryNames() const
Return the array of library names.
bool IsSymbolModified(const wxString &aAlias, const wxString &aLibrary) const
Return true if symbol has unsaved modifications.
bool IsLibraryLoaded(const wxString &aLibrary) const
Return true if the library was successfully loaded.
int GetLibraryHash(const wxString &aLibrary) const
Return a library hash value to determine if it has changed.
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Find a single library within the (aggregate) library table.
std::list< LIB_SYMBOL * > GetAliases(const wxString &aLibrary) const
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_IO object i...
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...
void DisplaySymbol(const LIB_ID &aSymbolID, int aUnit, int aBodyStyle=0)
Set the currently displayed symbol.
LIB_TREE_NODE::PTR_VECTOR::iterator deleteLibrary(LIB_TREE_NODE::PTR_VECTOR::iterator &aLibNodeIt)
SYMBOL_LIBRARY_MANAGER * m_libMgr
Hashes to decide whether a library needs an update.
void GetValue(wxVariant &aVariant, wxDataViewItem const &aItem, unsigned int aCol) const override
bool GetAttr(wxDataViewItem const &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
void updateLibrary(LIB_TREE_NODE_LIBRARY &aLibNode)
static wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > Create(SYMBOL_EDIT_FRAME *aParent, SYMBOL_LIBRARY_MANAGER *aLibs)
virtual void ShowPreview(wxWindow *aParent, const wxDataViewItem &aItem) override
int GetLibrariesCount() const override
Return the number of libraries loaded in the tree.
void Sync(const wxString &aForceRefresh, std::function< void(int, int, const wxString &)> aProgressCallback)
bool IsContainer(const wxDataViewItem &aItem) const override
virtual bool HasPreview(const wxDataViewItem &aItem) override
std::map< wxString, int > m_libHashes
SYMBOL_LIBRARY_MANAGER hash value returned in the last synchronization.
SYMBOL_TREE_SYNCHRONIZING_ADAPTER(SYMBOL_EDIT_FRAME *aParent, SYMBOL_LIBRARY_MANAGER *aLibMgr)
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
void SetBorders(bool aLeft, bool aRight, bool aTop, bool aBottom)
Definition: wx_panel.h:39
void SetBorderColor(const KIGFX::COLOR4D &aColor)
Definition: wx_panel.h:47
#define _(s)
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:119
wxString UnescapeString(const wxString &aSource)
std::vector< wxString > pinned_symbol_libs
#define PROGRESS_INTERVAL_MILLIS