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 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 <lib_manager.h>
28 #include <symbol_lib_table.h>
29 #include <class_libentry.h>
30 #include <tools/lib_control.h>
31 
32 
34  LIB_MANAGER* aLibMgr )
35 {
36  return PTR( new SYMBOL_TREE_SYNCHRONIZING_ADAPTER( aParent, aLibMgr ) );
37 }
38 
39 
41  LIB_MANAGER* aLibMgr ) :
42  LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
43  m_frame( aParent ),
44  m_libMgr( aLibMgr ),
45  m_lastSyncHash( -1 )
46 {
47 }
48 
49 
51 {
53 }
54 
55 
56 bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
57 {
58  const LIB_TREE_NODE* node = ToNode( aItem );
59  return node ? node->m_Type == LIB_TREE_NODE::LIB : true;
60 }
61 
62 
63 #define PROGRESS_INTERVAL_MILLIS 120
64 
66  std::function<void( int, int, const wxString& )> aProgressCallback )
67 {
68  wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
69 
70  int libMgrHash = m_libMgr->GetHash();
71 
72  if( !aForce && m_lastSyncHash == libMgrHash )
73  return;
74 
75  m_lastSyncHash = libMgrHash;
76  int i = 0, max = GetLibrariesCount();
77 
78  // Process already stored libraries
79  for( auto it = m_tree.m_Children.begin(); it != m_tree.m_Children.end(); /* iteration inside */ )
80  {
81  const wxString& name = it->get()->m_Name;
82 
83  if( wxGetUTCTimeMillis() > nextUpdate )
84  {
85  aProgressCallback( i, max, name );
86  nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
87  }
88 
89  // There is a bug in LIB_MANAGER::LibraryExists() that uses the buffered modified
90  // libraries before the symbol library table which prevents the library from being
91  // removed from the tree control.
92  if( !m_libMgr->LibraryExists( name, true )
93  || !m_frame->Prj().SchSymbolLibTable()->HasLibrary( name, true ) )
94  {
95  it = deleteLibrary( it );
96  continue;
97  }
98  else if( aForce || m_libMgr->GetLibraryHash( name ) != m_libHashes[name] )
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 
118  SYMBOL_LIB_TABLE_ROW* library = m_libMgr->GetLibrary( libName );
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_PART* 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_PART*> 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_PART* a )
163  {
164  return a->GetName() == (*nodeIt)->m_Name;
165  } );
166 
167  if( aliasIt != aliases.end() )
168  {
169  // alias exists both in the component 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_PART* 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->GetCurPart() && m_frame->GetCurPart()->GetLibId() == node->m_LibId )
219 
220  if( node->m_Pinned )
221  aVariant = GetPinningSymbol() + node->m_Name;
222  else
223  aVariant = 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->IsPartModified( node->m_Name, node->m_Parent->m_Name ) )
234  aVariant = aVariant.GetString() + " *";
235  }
236 
237  break;
238 
239  case 1:
240  if( m_frame->GetCurPart() && m_frame->GetCurPart()->GetLibId() == node->m_LibId )
241  node->m_Desc = m_frame->GetCurPart()->GetDescription();
242 
243  aVariant = node->m_Desc;
244 
245  // Annotate that the library failed to load in the description column
246  if( node->m_Type == LIB_TREE_NODE::LIB )
247  {
248  if( !m_libMgr->IsLibraryLoaded( node->m_Name ) )
249  aVariant = _( "(failed to load) " ) + aVariant.GetString();
250  }
251 
252  break;
253 
254  default: // column == -1 is used for default Compare function
255  aVariant = node->m_Name;
256  break;
257  }
258 }
259 
260 
261 bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
262  wxDataViewItemAttr& aAttr ) const
263 {
264  if( IsFrozen() )
265  return false;
266 
267  LIB_TREE_NODE* node = ToNode( aItem );
268  wxCHECK( node, false );
269 
270  // Mark both columns of unloaded libraries using grey text color (to look disabled)
271  if( node->m_Type == LIB_TREE_NODE::LIB && !m_libMgr->IsLibraryLoaded( node->m_Name ) )
272  {
273  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
274  return true;
275  }
276 
277  // The remaining attributes are only for the name column
278  if( aCol != 0 )
279  return false;
280 
281  switch( node->m_Type )
282  {
283  case LIB_TREE_NODE::LIB:
284  // mark modified libs with bold font
285  aAttr.SetBold( m_libMgr->IsLibraryModified( node->m_Name ) );
286 
287 #ifdef __WXGTK__
288  // The native wxGTK+ impl ignores background colour, so set the text colour instead.
289  // This works reasonably well in dark themes, and quite poorly in light ones....
290  if( node->m_Name == m_libMgr->GetCurrentLib() )
291  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
292 #else
293  // mark the current library with background color
294  if( node->m_Name == m_libMgr->GetCurrentLib() )
295  {
296  aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
297  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
298  }
299 #endif
300  break;
301 
303  // mark modified part with bold font
304  aAttr.SetBold( m_libMgr->IsPartModified( node->m_Name, node->m_Parent->m_Name ) );
305 
306  // mark aliases with italic font
307  aAttr.SetItalic( !node->m_IsRoot );
308 
309 #ifdef __WXGTK__
310  // The native wxGTK+ impl ignores background colour, so set the text colour instead.
311  // This works reasonably well in dark themes, and quite poorly in light ones....
312  if( node->m_LibId == m_libMgr->GetCurrentLibId() )
313  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
314 #else
315  // mark the current part with background color
316  if( node->m_LibId == m_libMgr->GetCurrentLibId() )
317  {
318  aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
319  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
320  }
321 #endif
322  break;
323 
324  default:
325  return false;
326  }
327 
328  return true;
329 }
int m_lastSyncHash
LIB_MANAGER hash value returned in the last synchronization
static PTR Create(LIB_EDIT_FRAME *aParent, LIB_MANAGER *aLibs)
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
LIB_ID GetLibId() const override
virtual int GetLibrariesCount() const
Return the number of libraries loaded in the tree.
std::list< LIB_PART * > GetAliases(const wxString &aLibrary) const
bool IsContainer(const wxDataViewItem &aItem) const override
bool IsLibraryLoaded(const wxString &aLibrary) const
Returns true if the library was successfully loaded.
LIB_PART * GetCurPart()
Return the current part being edited or NULL if none selected.
std::map< wxString, int > m_libHashes
Hashes to decide whether a library needs an update
const wxString & GetCurrentLib() const
Returns the currently modified library name.
Definition: lib_manager.h:281
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.
Handle actions for the various symbol editor and viewers.
Definition: lib_control.h:37
int GetLibraryHash(const wxString &aLibrary) const
Returns a library hash value to determine if it has changed.
Definition: lib_manager.cpp:89
#define PROGRESS_INTERVAL_MILLIS
LIB_TREE_NODE_LIB_ID & AddItem(LIB_TREE_ITEM *aItem)
Construct a new alias node, add it to this library, and return it.
bool IsPartModified(const wxString &aAlias, const wxString &aLibrary) const
Returns true if part has unsaved modifications.
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
LIB_TREE_NODE * m_Parent
Node type: library.
int GetHash() const
Definition: lib_manager.cpp:78
Define a library symbol object.
wxArrayString GetLibraryNames() const
Returns the array of library names.
static LIB_TREE_NODE * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
bool GetAttr(wxDataViewItem const &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Finds a single library within the (aggregate) library table.
wxString GetDescription() override
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
LIB_TREE_NODE_LIB & DoAddLibraryNode(wxString const &aNodeName, wxString const &aDesc)
Class to handle modifications to the symbol libraries.
Definition: lib_manager.h:99
const wxString GetPinningSymbol() const
LIB_TREE_NODE::PTR_VECTOR::iterator deleteLibrary(LIB_TREE_NODE::PTR_VECTOR::iterator &aLibNodeIt)
void AssignIntrinsicRanks(bool presorted=false)
Store intrinsic ranks on all children of this node.
The symbol library editor main window.
const char * name
Definition: DXF_plotter.cpp:60
#define _(s)
Definition: 3d_actions.cpp:33
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
enum TYPE m_Type
void GetValue(wxVariant &aVariant, wxDataViewItem const &aItem, unsigned int aCol) const override
bool IsLibraryModified(const wxString &aLibrary) const
Returns true if library has unsaved modifications.
wxString m_Name
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:78
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > PTR
Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER_BASE.
void Sync(bool aForce=false, std::function< void(int, int, const wxString &)> aProgressCallback=[](int, int, const wxString &){})
LIB_ID GetCurrentLibId() const
Returns the current library and part name as LIB_ID.
Definition: lib_manager.h:293
PTR_VECTOR m_Children
wxString m_Desc
SYMBOL_TREE_SYNCHRONIZING_ADAPTER(LIB_EDIT_FRAME *aParent, LIB_MANAGER *aLibMgr)