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-2022 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 <string_utils.h>
34#include <widgets/wx_panel.h>
35
36
37wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
39 SYMBOL_LIBRARY_MANAGER* aLibMgr )
40{
41 auto* adapter = new SYMBOL_TREE_SYNCHRONIZING_ADAPTER( aParent, aLibMgr );
42 return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
43}
44
45
47 SYMBOL_LIBRARY_MANAGER* aLibMgr ) :
48 LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
49 m_frame( aParent ),
50 m_libMgr( aLibMgr ),
51 m_lastSyncHash( -1 )
52{
53}
54
55
57{
59}
60
61
62bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
63{
64 const LIB_TREE_NODE* node = ToNode( aItem );
65 return node ? node->m_Type == LIB_TREE_NODE::LIB : true;
66}
67
68
69#define PROGRESS_INTERVAL_MILLIS 120
70
71void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync( const wxString& aForceRefresh,
72 std::function<void( int, int, const wxString& )> aProgressCallback )
73{
74 wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
75
77 int i = 0, max = GetLibrariesCount();
78
79 // Process already stored libraries
80 for( auto it = m_tree.m_Children.begin(); it != m_tree.m_Children.end(); /* iteration inside */ )
81 {
82 const wxString& name = it->get()->m_Name;
83
84 if( wxGetUTCTimeMillis() > nextUpdate )
85 {
86 aProgressCallback( i, max, name );
87 nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
88 }
89
90 // There is a bug in SYMBOL_LIBRARY_MANAGER::LibraryExists() that uses the buffered
91 // modified libraries before the symbol library table which prevents the library from
92 // being removed from the tree control.
93 if( !m_libMgr->LibraryExists( name, true )
94 || !m_frame->Prj().SchSymbolLibTable()->HasLibrary( name, true )
95 || m_frame->Prj().SchSymbolLibTable()->FindRow( name, true ) !=
96 m_frame->Prj().SchSymbolLibTable()->FindRow( name, false )
97 || name == aForceRefresh )
98 {
99 it = deleteLibrary( it );
100 continue;
101 }
102 else
103 {
104 updateLibrary( *(LIB_TREE_NODE_LIB*) it->get() );
105 }
106
107 ++it;
108 ++i;
109 }
110
111 // Look for new libraries
112 //
113 COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
115
116 for( const wxString& libName : m_libMgr->GetLibraryNames() )
117 {
118 if( m_libHashes.count( libName ) == 0 )
119 {
120 if( wxGetUTCTimeMillis() > nextUpdate )
121 {
122 aProgressCallback( i++, max, libName );
123 nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
124 }
125
127 bool pinned = alg::contains( cfg->m_Session.pinned_symbol_libs, libName )
128 || alg::contains( project.m_PinnedSymbolLibs, libName );
129
130 LIB_TREE_NODE_LIB& lib_node = DoAddLibraryNode( libName, library->GetDescr(), pinned );
131
132 updateLibrary( lib_node );
133 }
134 }
135
137}
138
139
141{
143
144 for( const wxString& libName : m_libMgr->GetLibraryNames() )
145 {
146 if( m_libHashes.count( libName ) == 0 )
147 ++count;
148 }
149
150 return count;
151}
152
153
155{
156 auto hashIt = m_libHashes.find( aLibNode.m_Name );
157
158 if( hashIt == m_libHashes.end() )
159 {
160 // add a new library
161 for( LIB_SYMBOL* alias : m_libMgr->GetAliases( aLibNode.m_Name ) )
162 aLibNode.AddItem( alias );
163 }
164 else if( hashIt->second != m_libMgr->GetLibraryHash( aLibNode.m_Name ) )
165 {
166 // update an existing library
167 std::list<LIB_SYMBOL*> aliases = m_libMgr->GetAliases( aLibNode.m_Name );
168
169 // remove the common part from the aliases list
170 for( auto nodeIt = aLibNode.m_Children.begin(); nodeIt != aLibNode.m_Children.end(); )
171 {
172 auto aliasIt = std::find_if( aliases.begin(), aliases.end(),
173 [&] ( const LIB_SYMBOL* a )
174 {
175 return a->GetName() == (*nodeIt)->m_LibId.GetLibItemName();
176 } );
177
178 if( aliasIt != aliases.end() )
179 {
180 // alias exists both in the symbol tree and the library manager,
181 // update only the node data.
182 static_cast<LIB_TREE_NODE_LIB_ID*>( nodeIt->get() )->Update( *aliasIt );
183 aliases.erase( aliasIt );
184 ++nodeIt;
185 }
186 else
187 {
188 // node does not exist in the library manager, remove the corresponding node
189 nodeIt = aLibNode.m_Children.erase( nodeIt );
190 }
191 }
192
193 // now the aliases list contains only new aliases that need to be added to the tree
194 for( LIB_SYMBOL* alias : aliases )
195 aLibNode.AddItem( alias );
196 }
197
198 aLibNode.AssignIntrinsicRanks();
199 m_libHashes[aLibNode.m_Name] = m_libMgr->GetLibraryHash( aLibNode.m_Name );
200}
201
202
203LIB_TREE_NODE::PTR_VECTOR::iterator
204SYMBOL_TREE_SYNCHRONIZING_ADAPTER::deleteLibrary( LIB_TREE_NODE::PTR_VECTOR::iterator& aLibNodeIt )
205{
206 LIB_TREE_NODE* node = aLibNodeIt->get();
207 m_libHashes.erase( node->m_Name );
208 auto it = m_tree.m_Children.erase( aLibNodeIt );
209 return it;
210}
211
212
214{
215 if( m_frame->GetCurSymbol() )
216 return FindItem( m_frame->GetCurSymbol()->GetLibId() );
217
218 return wxDataViewItem();
219}
220
221
222void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
223 unsigned int aCol ) const
224{
225 if( IsFrozen() )
226 {
227 aVariant = wxEmptyString;
228 return;
229 }
230
231 LIB_TREE_NODE* node = ToNode( aItem );
232 wxASSERT( node );
233
234 switch( aCol )
235 {
236 case NAME_COL:
237 if( m_frame->GetCurSymbol() && m_frame->GetCurSymbol()->GetLibId() == node->m_LibId )
239
240 if( node->m_Pinned )
241 aVariant = GetPinningSymbol() + UnescapeString( node->m_Name );
242 else
243 aVariant = UnescapeString( node->m_Name );
244
245 // mark modified items with an asterisk
246 if( node->m_Type == LIB_TREE_NODE::LIB )
247 {
248 if( m_libMgr->IsLibraryModified( node->m_Name ) )
249 aVariant = aVariant.GetString() + " *";
250 }
251 else if( node->m_Type == LIB_TREE_NODE::LIBID )
252 {
253 if( m_libMgr->IsSymbolModified( node->m_Name, node->m_Parent->m_Name ) )
254 aVariant = aVariant.GetString() + " *";
255 }
256
257 break;
258
259 default:
260 if( m_colIdxMap.count( aCol ) )
261 {
262 if( node->m_Type == LIB_TREE_NODE::LIB )
263 {
265 SYMBOL_LIB_TABLE_ROW* lib = libMgr.GetLibrary( node->m_LibId.GetLibNickname() );
266
267 if( lib )
268 node->m_Desc = lib->GetDescr();
269
270 if( !m_libMgr->IsLibraryLoaded( node->m_Name ) )
271 aVariant = _( "(failed to load)" ) + wxS( " " ) + aVariant.GetString();
272 }
273
274 const wxString& key = m_colIdxMap.at( aCol );
275
276 if( m_frame->GetCurSymbol() && m_frame->GetCurSymbol()->GetLibId() == node->m_LibId )
277 {
279 }
280
281 if( node->m_Fields.count( key ) )
282 aVariant = node->m_Fields.at( key );
283 else if( key == wxT( "Description" ) )
284 aVariant = node->m_Desc;
285 else
286 aVariant = wxEmptyString;
287 }
288 break;
289 }
290}
291
292
293bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
294 wxDataViewItemAttr& aAttr ) const
295{
296 if( IsFrozen() )
297 return false;
298
299 LIB_TREE_NODE* node = ToNode( aItem );
300 wxCHECK( node, false );
301
302 // Mark both columns of unloaded libraries using grey text color (to look disabled)
303 if( node->m_Type == LIB_TREE_NODE::LIB && !m_libMgr->IsLibraryLoaded( node->m_Name ) )
304 {
305 aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
306 return true;
307 }
308
309 // The remaining attributes are only for the name column
310 if( aCol != NAME_COL )
311 return false;
312
313 LIB_SYMBOL* curSymbol = m_frame->GetCurSymbol();
314
315 switch( node->m_Type )
316 {
318 // mark modified libs with bold font
319 aAttr.SetBold( m_libMgr->IsLibraryModified( node->m_Name ) );
320
321 // mark the current library with background color
322 if( curSymbol && curSymbol->GetLibId().GetLibNickname() == node->m_LibId.GetLibNickname() )
323 {
324#ifdef __WXGTK__
325 // The native wxGTK+ impl ignores background colour, so set the text colour instead.
326 // This works reasonably well in dark themes, and quite poorly in light ones....
327 aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
328#else
329 aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
330 aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
331#endif
332 }
333 break;
334
336 // mark modified part with bold font
337 aAttr.SetBold( m_libMgr->IsSymbolModified( node->m_Name, node->m_Parent->m_Name ) );
338
339 // mark aliases with italic font
340 aAttr.SetItalic( !node->m_IsRoot );
341
342 // mark the current part with background color
343 if( curSymbol && curSymbol->GetLibId() == node->m_LibId )
344 {
345#ifdef __WXGTK__
346 // The native wxGTK+ impl ignores background colour, so set the text colour instead.
347 // This works reasonably well in dark themes, and quite poorly in light ones....
348 aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
349#else
350 aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
351 aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
352#endif
353 }
354 break;
355
356 default:
357 return false;
358 }
359
360 return true;
361}
362
363
364bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::HasPreview( const wxDataViewItem& aItem )
365{
366 LIB_TREE_NODE* node = ToNode( aItem );
367 wxCHECK( node, false );
368
369 return node->m_Type == LIB_TREE_NODE::LIBID && node->m_LibId != m_frame->GetTargetLibId();
370}
371
372
373void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::ShowPreview( wxWindow* aParent, const wxDataViewItem& aItem )
374{
375 LIB_TREE_NODE* node = ToNode( aItem );
376 wxCHECK( node, /* void */ );
377
378 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
379 aParent->SetSizer( mainSizer );
380
381 WX_PANEL* panel = new WX_PANEL( aParent );
382 panel->SetBorders( true, true, true, true );
384
385 wxBoxSizer* panelSizer = new wxBoxSizer( wxVERTICAL );
386 panel->SetSizer( panelSizer );
387
389 SYMBOL_PREVIEW_WIDGET* preview = new SYMBOL_PREVIEW_WIDGET( panel, &m_frame->Kiway(),
390 false, backend );
391 preview->SetLayoutDirection( wxLayout_LeftToRight );
392
393 panelSizer->Add( preview, 1, wxEXPAND|wxALL, 1 );
394 mainSizer->Add( panel, 1, wxEXPAND, 0 );
395 aParent->Layout();
396
397 preview->DisplaySymbol( node->m_LibId, node->m_Unit );
398}
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.
LIB_TREE_NODE_LIB & DoAddLibraryNode(const wxString &aNodeName, const wxString &aDesc, bool pinned)
static LIB_TREE_NODE * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> #SYM_TREE_NODE.
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.
wxDataViewItem FindItem(const LIB_ID &aLibId)
Returns tree item corresponding to part.
Node type: LIB_ID.
Node type: library.
LIB_TREE_NODE_LIB_ID & 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:69
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:158
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_PLUGIN obje...
void DisplaySymbol(const LIB_ID &aSymbolID, int aUnit, int aConvert=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
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:99
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:115
wxString UnescapeString(const wxString &aSource)
std::vector< wxString > pinned_symbol_libs
#define PROGRESS_INTERVAL_MILLIS