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::TYPE::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 //
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().wx_str();
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 return m_tree.m_Children.erase( aLibNodeIt );
211}
212
213
215{
216 if( m_frame->GetCurSymbol() )
217 return FindItem( m_frame->GetCurSymbol()->GetLibId() );
218
219 return wxDataViewItem();
220}
221
222
223void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
224 unsigned int aCol ) const
225{
226 if( IsFrozen() )
227 {
228 aVariant = wxEmptyString;
229 return;
230 }
231
232 LIB_TREE_NODE* node = ToNode( aItem );
233 wxASSERT( node );
234
235 switch( aCol )
236 {
237 case NAME_COL:
238 if( m_frame->GetCurSymbol() && m_frame->GetCurSymbol()->GetLibId() == node->m_LibId )
240
241 if( node->m_Pinned )
242 aVariant = GetPinningSymbol() + UnescapeString( node->m_Name );
243 else
244 aVariant = UnescapeString( node->m_Name );
245
246 // mark modified items with an asterisk
247 if( node->m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
248 {
249 if( m_libMgr->IsLibraryModified( node->m_Name ) )
250 aVariant = aVariant.GetString() + " *";
251 }
252 else if( node->m_Type == LIB_TREE_NODE::TYPE::ITEM )
253 {
254 if( m_libMgr->IsSymbolModified( node->m_Name, node->m_Parent->m_Name ) )
255 aVariant = aVariant.GetString() + " *";
256 }
257
258 break;
259
260 default:
261 if( m_colIdxMap.count( aCol ) )
262 {
263 if( node->m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
264 {
266 SYMBOL_LIB_TABLE_ROW* lib = libMgr.GetLibrary( node->m_LibId.GetLibNickname() );
267
268 if( lib )
269 node->m_Desc = lib->GetDescr();
270
271 if( !m_libMgr->IsLibraryLoaded( node->m_Name ) )
272 aVariant = _( "(failed to load)" ) + wxS( " " ) + aVariant.GetString();
273 }
274
275 const wxString& key = m_colIdxMap.at( aCol );
276
277 if( m_frame->GetCurSymbol() && m_frame->GetCurSymbol()->GetLibId() == node->m_LibId )
278 {
280 }
281
282 wxString valueStr;
283
284 if( key == wxT( "Description" ) )
285 valueStr = node->m_Desc;
286 else if( node->m_Fields.count( key ) )
287 valueStr = node->m_Fields.at( key );
288 else
289 valueStr = wxEmptyString;
290
291 valueStr.Replace( wxS( "\n" ), wxS( " " ) ); // Clear line breaks
292
293 aVariant = valueStr;
294 }
295 break;
296 }
297}
298
299
300bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
301 wxDataViewItemAttr& aAttr ) const
302{
303 if( IsFrozen() )
304 return false;
305
306 LIB_TREE_NODE* node = ToNode( aItem );
307 wxCHECK( node, false );
308
309 // Mark both columns of unloaded libraries using grey text color (to look disabled)
310 if( node->m_Type == LIB_TREE_NODE::TYPE::LIBRARY && !m_libMgr->IsLibraryLoaded( node->m_Name ) )
311 {
312 aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
313 return true;
314 }
315
316 // The remaining attributes are only for the name column
317 if( aCol != NAME_COL )
318 return false;
319
320 LIB_SYMBOL* curSymbol = m_frame->GetCurSymbol();
321
322 switch( node->m_Type )
323 {
324 case LIB_TREE_NODE::TYPE::LIBRARY:
325 // mark modified libs with bold font
326 aAttr.SetBold( m_libMgr->IsLibraryModified( node->m_Name ) );
327
328 // mark the current library if it's collapsed
329 if( curSymbol && curSymbol->GetLibId().GetLibNickname() == node->m_LibId.GetLibNickname() )
330 {
331 if( !m_widget->IsExpanded( ToItem( node ) ) )
332 {
333 aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
334 // proxy for "is canvas item"
335 }
336 }
337 break;
338
339 case LIB_TREE_NODE::TYPE::ITEM:
340 // mark modified part with bold font
341 aAttr.SetBold( m_libMgr->IsSymbolModified( node->m_Name, node->m_Parent->m_Name ) );
342
343 // mark aliases with italic font
344 aAttr.SetItalic( !node->m_IsRoot );
345
346 // mark the current (on-canvas) part
347 if( curSymbol && curSymbol->GetLibId() == node->m_LibId )
348 {
349 aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
350 // proxy for "is canvas item"
351 }
352 break;
353
354 default:
355 return false;
356 }
357
358 return true;
359}
360
361
362bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::HasPreview( const wxDataViewItem& aItem )
363{
364 LIB_TREE_NODE* node = ToNode( aItem );
365 wxCHECK( node, false );
366
367 return node->m_Type == LIB_TREE_NODE::TYPE::ITEM && node->m_LibId != m_frame->GetTargetLibId();
368}
369
370
372 const wxDataViewItem& aItem )
373{
374 static const wxString c_previewName = wxS( "symHoverPreview" );
375
376 LIB_TREE_NODE* node = ToNode( aItem );
377 wxCHECK( node, /* void */ );
378
379 SYMBOL_PREVIEW_WIDGET* preview = dynamic_cast<SYMBOL_PREVIEW_WIDGET*>(
380 wxWindow::FindWindowByName( c_previewName, aParent ) );
381
382 if( !preview )
383 {
384 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
385 aParent->SetSizer( mainSizer );
386
387 WX_PANEL* panel = new WX_PANEL( aParent );
388 panel->SetBorders( true, true, true, true );
390
391 wxBoxSizer* panelSizer = new wxBoxSizer( wxVERTICAL );
392 panel->SetSizer( panelSizer );
393
395 preview = new SYMBOL_PREVIEW_WIDGET( panel, &m_frame->Kiway(), false, backend );
396 preview->SetName( c_previewName );
397 preview->SetLayoutDirection( wxLayout_LeftToRight );
398
399 panelSizer->Add( preview, 1, wxEXPAND | wxALL, 1 );
400 mainSizer->Add( panel, 1, wxEXPAND, 0 );
401 aParent->Layout();
402 }
403
404 preview->DisplaySymbol( node->m_LibId, node->m_Unit );
405}
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:402
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:55
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:77
wxString GetDescription() const override
Definition: lib_symbol.h:157
const LIB_ID & GetLibId() const override
Definition: lib_symbol.h:142
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.
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:678
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_SYMBOL * GetCurSymbol() const
Return the current symbol being edited or NULL if none selected.
LIB_ID GetTargetLibId() const override
Return either the symbol selected in the symbol tree (if context menu is active) or the symbol on the...
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
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1059
see class PGM_BASE
wxString UnescapeString(const wxString &aSource)
std::vector< wxString > pinned_symbol_libs
#define PROGRESS_INTERVAL_MILLIS