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 The 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 aParent->GetViewerSettingsBase()->m_LibTree ),
51 m_frame( aParent ),
52 m_libMgr( aLibMgr ),
53 m_lastSyncHash( -1 )
54{
55}
56
57
59{
61}
62
63
64bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
65{
66 const LIB_TREE_NODE* node = ToNode( aItem );
67 return node ? node->m_Type == LIB_TREE_NODE::TYPE::LIBRARY : true;
68}
69
70
71#define PROGRESS_INTERVAL_MILLIS 120
72
73void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync( const wxString& aForceRefresh,
74 std::function<void( int, int, const wxString& )> aProgressCallback )
75{
76 wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
77
79 int i = 0, max = GetLibrariesCount();
80
81 // Process already stored libraries
82 for( auto it = m_tree.m_Children.begin(); it != m_tree.m_Children.end(); /* iteration inside */ )
83 {
84 const wxString& name = it->get()->m_Name;
85
86 if( wxGetUTCTimeMillis() > nextUpdate )
87 {
88 aProgressCallback( i, max, name );
89 nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
90 }
91
92 // There is a bug in SYMBOL_LIBRARY_MANAGER::LibraryExists() that uses the buffered
93 // modified libraries before the symbol library table which prevents the library from
94 // being removed from the tree control.
95 if( !m_libMgr->LibraryExists( name, true )
99 || name == aForceRefresh )
100 {
101 it = deleteLibrary( it );
102 continue;
103 }
104 else
105 {
106 updateLibrary( *(LIB_TREE_NODE_LIBRARY*) it->get() );
107 }
108
109 ++it;
110 ++i;
111 }
112
113 // Look for new libraries
114 //
117
118 for( const wxString& libName : m_libMgr->GetLibraryNames() )
119 {
120 if( m_libHashes.count( libName ) == 0 )
121 {
122 if( wxGetUTCTimeMillis() > nextUpdate )
123 {
124 aProgressCallback( i++, max, libName );
125 nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
126 }
127
129 bool pinned = alg::contains( cfg->m_Session.pinned_symbol_libs, libName )
130 || alg::contains( project.m_PinnedSymbolLibs, libName );
131
132 LIB_TREE_NODE_LIBRARY& lib_node = DoAddLibraryNode( libName, library->GetDescr(),
133 pinned );
134
135 updateLibrary( lib_node );
136 }
137 }
138
140}
141
142
144{
146
147 for( const wxString& libName : m_libMgr->GetLibraryNames() )
148 {
149 if( m_libHashes.count( libName ) == 0 )
150 ++count;
151 }
152
153 return count;
154}
155
156
158{
159 auto hashIt = m_libHashes.find( aLibNode.m_Name );
160
161 if( hashIt == m_libHashes.end() )
162 {
163 // add a new library
164 for( LIB_SYMBOL* alias : m_libMgr->GetAliases( aLibNode.m_Name ) )
165 aLibNode.AddItem( alias );
166 }
167 else if( hashIt->second != m_libMgr->GetLibraryHash( aLibNode.m_Name ) )
168 {
169 // update an existing library
170 std::list<LIB_SYMBOL*> aliases = m_libMgr->GetAliases( aLibNode.m_Name );
171
172 // remove the common part from the aliases list
173 for( auto nodeIt = aLibNode.m_Children.begin(); nodeIt != aLibNode.m_Children.end(); )
174 {
175 auto aliasIt = std::find_if( aliases.begin(), aliases.end(),
176 [&] ( const LIB_SYMBOL* a )
177 {
178 return a->GetName() == (*nodeIt)->m_LibId.GetLibItemName().wx_str();
179 } );
180
181 if( aliasIt != aliases.end() )
182 {
183 // alias exists both in the symbol tree and the library manager,
184 // update only the node data.
185 static_cast<LIB_TREE_NODE_ITEM*>( nodeIt->get() )->Update( *aliasIt );
186 aliases.erase( aliasIt );
187 ++nodeIt;
188 }
189 else
190 {
191 // node does not exist in the library manager, remove the corresponding node
192 nodeIt = aLibNode.m_Children.erase( nodeIt );
193 }
194 }
195
196 // now the aliases list contains only new aliases that need to be added to the tree
197 for( LIB_SYMBOL* alias : aliases )
198 aLibNode.AddItem( alias );
199 }
200
201 aLibNode.AssignIntrinsicRanks();
202 m_libHashes[aLibNode.m_Name] = m_libMgr->GetLibraryHash( aLibNode.m_Name );
203}
204
205
206LIB_TREE_NODE::PTR_VECTOR::iterator
207SYMBOL_TREE_SYNCHRONIZING_ADAPTER::deleteLibrary( LIB_TREE_NODE::PTR_VECTOR::iterator& aLibNodeIt )
208{
209 LIB_TREE_NODE* node = aLibNodeIt->get();
210 m_libHashes.erase( node->m_Name );
211 return m_tree.m_Children.erase( aLibNodeIt );
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::TYPE::LIBRARY )
249 {
250 if( m_libMgr->IsLibraryModified( node->m_Name ) )
251 aVariant = aVariant.GetString() + " *";
252 }
253 else if( node->m_Type == LIB_TREE_NODE::TYPE::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::TYPE::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 else if( m_libMgr->IsLibraryReadOnly( node->m_Name ) )
275 aVariant = _( "(read-only)" ) + wxS( " " ) + aVariant.GetString();
276 }
277
278 const wxString& key = m_colIdxMap.at( aCol );
279
280 if( m_frame->GetCurSymbol() && m_frame->GetCurSymbol()->GetLibId() == node->m_LibId )
281 {
283 }
284
285 wxString valueStr;
286
287 if( key == wxT( "Description" ) )
288 valueStr = node->m_Desc;
289 else if( node->m_Fields.count( key ) )
290 valueStr = node->m_Fields.at( key );
291 else
292 valueStr = wxEmptyString;
293
294 valueStr.Replace( wxS( "\n" ), wxS( " " ) ); // Clear line breaks
295
296 if( !aVariant.GetString().IsEmpty() )
297 {
298 if( !valueStr.IsEmpty() )
299 aVariant = valueStr + wxS( " - " ) + aVariant.GetString();
300 }
301 else
302 {
303 aVariant = valueStr;
304 }
305 }
306 break;
307 }
308}
309
310
311bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
312 wxDataViewItemAttr& aAttr ) const
313{
314 if( IsFrozen() )
315 return false;
316
317 LIB_TREE_NODE* node = ToNode( aItem );
318 wxCHECK( node, false );
319
320 // Mark both columns of unloaded libraries using grey text color (to look disabled)
321 if( node->m_Type == LIB_TREE_NODE::TYPE::LIBRARY && !m_libMgr->IsLibraryLoaded( node->m_Name ) )
322 {
323 aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
324 return true;
325 }
326
327 // The remaining attributes are only for the name column
328 if( aCol != NAME_COL )
329 return false;
330
331 LIB_SYMBOL* curSymbol = m_frame->GetCurSymbol();
332
333 switch( node->m_Type )
334 {
335 case LIB_TREE_NODE::TYPE::LIBRARY:
336 // mark modified libs with bold font
337 aAttr.SetBold( m_libMgr->IsLibraryModified( node->m_Name ) );
338
339 // mark the current library if it's collapsed
340 if( curSymbol && curSymbol->GetLibId().GetLibNickname() == node->m_LibId.GetLibNickname() )
341 {
342 if( !m_widget->IsExpanded( ToItem( node ) ) )
343 {
344 aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
345 // proxy for "is canvas item"
346 }
347 }
348 break;
349
350 case LIB_TREE_NODE::TYPE::ITEM:
351 // mark modified part with bold font
352 aAttr.SetBold( m_libMgr->IsSymbolModified( node->m_Name, node->m_Parent->m_Name ) );
353
354 // mark aliases with italic font
355 aAttr.SetItalic( !node->m_IsRoot );
356
357 // mark the current (on-canvas) part
358 if( curSymbol && curSymbol->GetLibId() == node->m_LibId )
359 {
360 aAttr.SetStrikethrough( true ); // LIB_TREE_RENDERER uses strikethrough as a
361 // proxy for "is canvas item"
362 }
363 break;
364
365 default:
366 return false;
367 }
368
369 return true;
370}
371
372
373bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::HasPreview( const wxDataViewItem& aItem )
374{
375 LIB_TREE_NODE* node = ToNode( aItem );
376 wxCHECK( node, false );
377
378 return node->m_Type == LIB_TREE_NODE::TYPE::ITEM && node->m_LibId != m_frame->GetTargetLibId();
379}
380
381
383 const wxDataViewItem& aItem )
384{
385 static const wxString c_previewName = wxS( "symHoverPreview" );
386
387 LIB_TREE_NODE* node = ToNode( aItem );
388 wxCHECK( node, /* void */ );
389
390 SYMBOL_PREVIEW_WIDGET* preview = dynamic_cast<SYMBOL_PREVIEW_WIDGET*>(
391 wxWindow::FindWindowByName( c_previewName, aParent ) );
392
393 if( !preview )
394 {
395 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
396 aParent->SetSizer( mainSizer );
397
398 WX_PANEL* panel = new WX_PANEL( aParent );
399 panel->SetBorders( true, true, true, true );
401
402 wxBoxSizer* panelSizer = new wxBoxSizer( wxVERTICAL );
403 panel->SetSizer( panelSizer );
404
406 preview = new SYMBOL_PREVIEW_WIDGET( panel, &m_frame->Kiway(), false, backend );
407 preview->SetName( c_previewName );
408 preview->SetLayoutDirection( wxLayout_LeftToRight );
409
410 panelSizer->Add( preview, 1, wxEXPAND | wxALL, 1 );
411 mainSizer->Add( panel, 1, wxEXPAND, 0 );
412 aParent->Layout();
413 }
414
415 preview->DisplaySymbol( node->m_LibId, node->m_Unit );
416}
const char * name
Definition: DXF_plotter.cpp:59
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:84
wxString GetDescription() const override
Definition: lib_symbol.h:169
const LIB_ID & GetLibId() const override
Definition: lib_symbol.h:154
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:689
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:1073
see class PGM_BASE
wxString UnescapeString(const wxString &aSource)
std::vector< wxString > pinned_symbol_libs
#define PROGRESS_INTERVAL_MILLIS