KiCad PCB EDA Suite
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-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 
27 #include <symbol_library_manager.h>
28 #include <symbol_lib_table.h>
30 #include <string_utils.h>
31 
32 
33 wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
35  SYMBOL_LIBRARY_MANAGER* aLibMgr )
36 {
37  auto* adapter = new SYMBOL_TREE_SYNCHRONIZING_ADAPTER( aParent, aLibMgr );
38  return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
39 }
40 
41 
43  SYMBOL_LIBRARY_MANAGER* aLibMgr ) :
44  LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
45  m_frame( aParent ),
46  m_libMgr( aLibMgr ),
47  m_lastSyncHash( -1 )
48 {
49 }
50 
51 
53 {
55 }
56 
57 
58 bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
59 {
60  const LIB_TREE_NODE* node = ToNode( aItem );
61  return node ? node->m_Type == LIB_TREE_NODE::LIB : true;
62 }
63 
64 
65 #define PROGRESS_INTERVAL_MILLIS 120
66 
67 void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync( const wxString& aForceRefresh,
68  std::function<void( int, int, const wxString& )> aProgressCallback )
69 {
70  wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
71 
73  int i = 0, max = GetLibrariesCount();
74 
75  // Process already stored libraries
76  for( auto it = m_tree.m_Children.begin(); it != m_tree.m_Children.end(); /* iteration inside */ )
77  {
78  const wxString& name = it->get()->m_Name;
79 
80  if( wxGetUTCTimeMillis() > nextUpdate )
81  {
82  aProgressCallback( i, max, name );
83  nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
84  }
85 
86  // There is a bug in SYMBOL_LIBRARY_MANAGER::LibraryExists() that uses the buffered
87  // modified libraries before the symbol library table which prevents the library from
88  // being removed from the tree control.
89  if( !m_libMgr->LibraryExists( name, true )
90  || !m_frame->Prj().SchSymbolLibTable()->HasLibrary( name, true )
91  || m_frame->Prj().SchSymbolLibTable()->FindRow( name, true ) !=
92  m_frame->Prj().SchSymbolLibTable()->FindRow( name, false )
93  || name == aForceRefresh )
94  {
95  it = deleteLibrary( it );
96  continue;
97  }
98  else
99  {
100  updateLibrary( *(LIB_TREE_NODE_LIB*) it->get() );
101  }
102 
103  ++it;
104  ++i;
105  }
106 
107  // Look for new libraries
108  for( const wxString& libName : m_libMgr->GetLibraryNames() )
109  {
110  if( m_libHashes.count( libName ) == 0 )
111  {
112  if( wxGetUTCTimeMillis() > nextUpdate )
113  {
114  aProgressCallback( i++, max, libName );
115  nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
116  }
117 
119  LIB_TREE_NODE_LIB& lib_node = DoAddLibraryNode( libName, library->GetDescr() );
120 
121  updateLibrary( lib_node );
122  }
123  }
124 
126 }
127 
128 
130 {
132 
133  for( const wxString& libName : m_libMgr->GetLibraryNames() )
134  {
135  if( m_libHashes.count( libName ) == 0 )
136  ++count;
137  }
138 
139  return count;
140 }
141 
142 
144 {
145  auto hashIt = m_libHashes.find( aLibNode.m_Name );
146 
147  if( hashIt == m_libHashes.end() )
148  {
149  // add a new library
150  for( LIB_SYMBOL* alias : m_libMgr->GetAliases( aLibNode.m_Name ) )
151  aLibNode.AddItem( alias );
152  }
153  else if( hashIt->second != m_libMgr->GetLibraryHash( aLibNode.m_Name ) )
154  {
155  // update an existing library
156  std::list<LIB_SYMBOL*> aliases = m_libMgr->GetAliases( aLibNode.m_Name );
157 
158  // remove the common part from the aliases list
159  for( auto nodeIt = aLibNode.m_Children.begin(); nodeIt != aLibNode.m_Children.end(); )
160  {
161  auto aliasIt = std::find_if( aliases.begin(), aliases.end(),
162  [&] ( const LIB_SYMBOL* a )
163  {
164  return a->GetName() == (*nodeIt)->m_LibId.GetLibItemName();
165  } );
166 
167  if( aliasIt != aliases.end() )
168  {
169  // alias exists both in the symbol tree and the library manager,
170  // update only the node data.
171  static_cast<LIB_TREE_NODE_LIB_ID*>( nodeIt->get() )->Update( *aliasIt );
172  aliases.erase( aliasIt );
173  ++nodeIt;
174  }
175  else
176  {
177  // node does not exist in the library manager, remove the corresponding node
178  nodeIt = aLibNode.m_Children.erase( nodeIt );
179  }
180  }
181 
182  // now the aliases list contains only new aliases that need to be added to the tree
183  for( LIB_SYMBOL* alias : aliases )
184  aLibNode.AddItem( alias );
185  }
186 
187  aLibNode.AssignIntrinsicRanks();
188  m_libHashes[aLibNode.m_Name] = m_libMgr->GetLibraryHash( aLibNode.m_Name );
189 }
190 
191 
192 LIB_TREE_NODE::PTR_VECTOR::iterator SYMBOL_TREE_SYNCHRONIZING_ADAPTER::deleteLibrary(
193  LIB_TREE_NODE::PTR_VECTOR::iterator& aLibNodeIt )
194 {
195  LIB_TREE_NODE* node = aLibNodeIt->get();
196  m_libHashes.erase( node->m_Name );
197  auto it = m_tree.m_Children.erase( aLibNodeIt );
198  return it;
199 }
200 
201 
202 void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
203  unsigned int aCol ) const
204 {
205  if( IsFrozen() )
206  {
207  aVariant = wxEmptyString;
208  return;
209  }
210 
211  LIB_TREE_NODE* node = ToNode( aItem );
212  wxASSERT( node );
213 
214  switch( aCol )
215  {
216  case 0:
217  if( m_frame->GetCurSymbol() && m_frame->GetCurSymbol()->GetLibId() == node->m_LibId )
219 
220  if( node->m_Pinned )
221  aVariant = GetPinningSymbol() + UnescapeString( node->m_Name );
222  else
223  aVariant = UnescapeString( node->m_Name );
224 
225  // mark modified items with an asterisk
226  if( node->m_Type == LIB_TREE_NODE::LIB )
227  {
228  if( m_libMgr->IsLibraryModified( node->m_Name ) )
229  aVariant = aVariant.GetString() + " *";
230  }
231  else if( node->m_Type == LIB_TREE_NODE::LIBID )
232  {
233  if( m_libMgr->IsSymbolModified( node->m_Name, node->m_Parent->m_Name ) )
234  aVariant = aVariant.GetString() + " *";
235  }
236 
237  break;
238 
239  case 1:
240  if( m_frame->GetCurSymbol() && m_frame->GetCurSymbol()->GetLibId() == node->m_LibId )
241  {
243  }
244  else if( node->m_Type == LIB_TREE_NODE::LIB )
245  {
247  SYMBOL_LIB_TABLE_ROW* lib = libMgr.GetLibrary( node->m_LibId.GetLibNickname() );
248 
249  if( lib )
250  node->m_Desc = lib->GetDescr();
251  }
252 
253  aVariant = node->m_Desc;
254 
255  // Annotate that the library failed to load in the description column
256  if( node->m_Type == LIB_TREE_NODE::LIB )
257  {
258  if( !m_libMgr->IsLibraryLoaded( node->m_Name ) )
259  aVariant = _( "(failed to load)" ) + wxS( " " ) + aVariant.GetString();
260  }
261 
262  break;
263 
264  default: // column == -1 is used for default Compare function
265  aVariant = node->m_Name;
266  break;
267  }
268 }
269 
270 
271 bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
272  wxDataViewItemAttr& aAttr ) const
273 {
274  if( IsFrozen() )
275  return false;
276 
277  LIB_TREE_NODE* node = ToNode( aItem );
278  wxCHECK( node, false );
279 
280  // Mark both columns of unloaded libraries using grey text color (to look disabled)
281  if( node->m_Type == LIB_TREE_NODE::LIB && !m_libMgr->IsLibraryLoaded( node->m_Name ) )
282  {
283  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
284  return true;
285  }
286 
287  // The remaining attributes are only for the name column
288  if( aCol != 0 )
289  return false;
290 
291  LIB_SYMBOL* curSymbol = m_frame->GetCurSymbol();
292 
293  switch( node->m_Type )
294  {
295  case LIB_TREE_NODE::LIB:
296  // mark modified libs with bold font
297  aAttr.SetBold( m_libMgr->IsLibraryModified( node->m_Name ) );
298 
299  // mark the current library with background color
300  if( curSymbol && curSymbol->GetLibId().GetLibNickname() == node->m_LibId.GetLibNickname() )
301  {
302 #ifdef __WXGTK__
303  // The native wxGTK+ impl ignores background colour, so set the text colour instead.
304  // This works reasonably well in dark themes, and quite poorly in light ones....
305  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
306 #else
307  aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
308  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
309 #endif
310  }
311  break;
312 
314  // mark modified part with bold font
315  aAttr.SetBold( m_libMgr->IsSymbolModified( node->m_Name, node->m_Parent->m_Name ) );
316 
317  // mark aliases with italic font
318  aAttr.SetItalic( !node->m_IsRoot );
319 
320  // mark the current part with background color
321  if( curSymbol && curSymbol->GetLibId() == node->m_LibId )
322  {
323 #ifdef __WXGTK__
324  // The native wxGTK+ impl ignores background colour, so set the text colour instead.
325  // This works reasonably well in dark themes, and quite poorly in light ones....
326  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
327 #else
328  aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
329  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
330 #endif
331  }
332  break;
333 
334  default:
335  return false;
336  }
337 
338  return true;
339 }
const UTF8 & GetLibItemName() const
Definition: lib_id.h:104
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
static wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > Create(SYMBOL_EDIT_FRAME *aParent, SYMBOL_LIBRARY_MANAGER *aLibs)
virtual int GetLibrariesCount() const
Return the number of libraries loaded in the tree.
LIB_TREE_NODE_LIB & DoAddLibraryNode(const wxString &aNodeName, const wxString &aDesc)
bool IsContainer(const wxDataViewItem &aItem) const override
SYMBOL_TREE_SYNCHRONIZING_ADAPTER(SYMBOL_EDIT_FRAME *aParent, SYMBOL_LIBRARY_MANAGER *aLibMgr)
std::map< wxString, int > m_libHashes
SYMBOL_LIBRARY_MANAGER hash value returned in the last synchronization.
Define a library symbol object.
Definition: lib_symbol.h:96
int GetLibrariesCount() const override
Return the number of libraries loaded in the tree.
const wxString & GetDescr() const
Return the description of the library referenced by this row.
void Sync(const wxString &aForceRefresh, std::function< void(int, int, const wxString &)> aProgressCallback)
#define PROGRESS_INTERVAL_MILLIS
LIB_SYMBOL * GetCurSymbol() const
Return the current symbol being edited or NULL if none selected.
LIB_TREE_NODE_LIB_ID & AddItem(LIB_TREE_ITEM *aItem)
Construct a new alias node, add it to this library, and return it.
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Find a single library within the (aggregate) library table.
int GetLibraryHash(const wxString &aLibrary) const
Return a library hash value to determine if it has changed.
LIB_ID GetLibId() const override
Definition: lib_symbol.h:135
Class to handle modifications to the symbol libraries.
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
LIB_TREE_NODE * m_Parent
bool IsSymbolModified(const wxString &aAlias, const wxString &aLibrary) const
Return true if symbol has unsaved modifications.
SYMBOL_LIBRARY_MANAGER & GetLibManager()
Node type: library.
#define _(s)
static LIB_TREE_NODE * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> #SYM_TREE_NODE.
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:90
bool GetAttr(wxDataViewItem const &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
SYMBOL_LIBRARY_MANAGER * m_libMgr
Hashes to decide whether a library needs an update.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
wxString UnescapeString(const wxString &aSource)
wxArrayString GetLibraryNames() const
Return the array of library names.
const wxString GetPinningSymbol() const
bool IsLibraryModified(const wxString &aLibrary) const
Return true if library has unsaved modifications.
LIB_TREE_NODE::PTR_VECTOR::iterator deleteLibrary(LIB_TREE_NODE::PTR_VECTOR::iterator &aLibNodeIt)
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
const char * name
Definition: DXF_plotter.cpp:56
enum TYPE m_Type
void GetValue(wxVariant &aVariant, wxDataViewItem const &aItem, unsigned int aCol) const override
bool IsLibraryLoaded(const wxString &aLibrary) const
Return true if the library was successfully loaded.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:54
PTR_VECTOR m_Children
wxString GetDescription() override
Definition: lib_symbol.h:142
Handle actions for the various symbol editor and viewers.
std::list< LIB_SYMBOL * > GetAliases(const wxString &aLibrary) const
The symbol library editor main window.