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", aParent->GetViewerSettingsBase() ),
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 else if( m_libMgr->IsLibraryReadOnly( node->m_Name ) )
274 aVariant = _( "(read-only)" ) + wxS( " " ) + aVariant.GetString();
275 }
276
277 const wxString& key = m_colIdxMap.at( aCol );
278
279 if( m_frame->GetCurSymbol() && m_frame->GetCurSymbol()->GetLibId() == node->m_LibId )
280 {
282 }
283
284 wxString valueStr;
285
286 if( key == wxT( "Description" ) )
287 valueStr = node->m_Desc;
288 else if( node->m_Fields.count( key ) )
289 valueStr = node->m_Fields.at( key );
290 else
291 valueStr = wxEmptyString;
292
293 valueStr.Replace( wxS( "\n" ), wxS( " " ) ); // Clear line breaks
294
295 if( !aVariant.GetString().IsEmpty() )
296 {
297 if( !valueStr.IsEmpty() )
298 aVariant = valueStr + wxS( " - " ) + aVariant.GetString();
299 }
300 else
301 {
302 aVariant = valueStr;
303 }
304 }
305 break;
306 }
307}
308
309
310bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
311 wxDataViewItemAttr& aAttr ) const
312{
313 if( IsFrozen() )
314 return false;
315
316 LIB_TREE_NODE* node = ToNode( aItem );
317 wxCHECK( node, false );
318
319 // Mark both columns of unloaded libraries using grey text color (to look disabled)
320 if( node->m_Type == LIB_TREE_NODE::TYPE::LIBRARY && !m_libMgr->IsLibraryLoaded( node->m_Name ) )
321 {
322 aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
323 return true;
324 }
325
326 // The remaining attributes are only for the name column
327 if( aCol != NAME_COL )
328 return false;
329
330 LIB_SYMBOL* curSymbol = m_frame->GetCurSymbol();
331
332 switch( node->m_Type )
333 {
334 case LIB_TREE_NODE::TYPE::LIBRARY:
335 // mark modified libs with bold font
336 aAttr.SetBold( m_libMgr->IsLibraryModified( node->m_Name ) );
337
338 // mark the current library if it's collapsed
339 if( curSymbol && curSymbol->GetLibId().GetLibNickname() == node->m_LibId.GetLibNickname() )
340 {
341 if( !m_widget->IsExpanded( ToItem( node ) ) )
342 {
343 aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
344 // proxy for "is canvas item"
345 }
346 }
347 break;
348
349 case LIB_TREE_NODE::TYPE::ITEM:
350 // mark modified part with bold font
351 aAttr.SetBold( m_libMgr->IsSymbolModified( node->m_Name, node->m_Parent->m_Name ) );
352
353 // mark aliases with italic font
354 aAttr.SetItalic( !node->m_IsRoot );
355
356 // mark the current (on-canvas) part
357 if( curSymbol && curSymbol->GetLibId() == node->m_LibId )
358 {
359 aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
360 // proxy for "is canvas item"
361 }
362 break;
363
364 default:
365 return false;
366 }
367
368 return true;
369}
370
371
372bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::HasPreview( const wxDataViewItem& aItem )
373{
374 LIB_TREE_NODE* node = ToNode( aItem );
375 wxCHECK( node, false );
376
377 return node->m_Type == LIB_TREE_NODE::TYPE::ITEM && node->m_LibId != m_frame->GetTargetLibId();
378}
379
380
382 const wxDataViewItem& aItem )
383{
384 static const wxString c_previewName = wxS( "symHoverPreview" );
385
386 LIB_TREE_NODE* node = ToNode( aItem );
387 wxCHECK( node, /* void */ );
388
389 SYMBOL_PREVIEW_WIDGET* preview = dynamic_cast<SYMBOL_PREVIEW_WIDGET*>(
390 wxWindow::FindWindowByName( c_previewName, aParent ) );
391
392 if( !preview )
393 {
394 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
395 aParent->SetSizer( mainSizer );
396
397 WX_PANEL* panel = new WX_PANEL( aParent );
398 panel->SetBorders( true, true, true, true );
400
401 wxBoxSizer* panelSizer = new wxBoxSizer( wxVERTICAL );
402 panel->SetSizer( panelSizer );
403
405 preview = new SYMBOL_PREVIEW_WIDGET( panel, &m_frame->Kiway(), false, backend );
406 preview->SetName( c_previewName );
407 preview->SetLayoutDirection( wxLayout_LeftToRight );
408
409 panelSizer->Add( preview, 1, wxEXPAND | wxALL, 1 );
410 mainSizer->Add( panel, 1, wxEXPAND, 0 );
411 aParent->Layout();
412 }
413
414 preview->DisplaySymbol( node->m_LibId, node->m_Unit );
415}
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:78
wxString GetDescription() const override
Definition: lib_symbol.h:158
const LIB_ID & GetLibId() const override
Definition: lib_symbol.h:143
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:679
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:72
static SYMBOL_LIB_TABLE * SchSymbolLibTable(PROJECT *aProject)
Accessor for project symbol library table.
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:200
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 IsLibraryReadOnly(const wxString &aLibrary) const
Return true if the library is stored in a read-only file.
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:1060
see class PGM_BASE
wxString UnescapeString(const wxString &aSource)
std::vector< wxString > pinned_symbol_libs
#define PROGRESS_INTERVAL_MILLIS