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 <symbol_library_manager.h>
28 #include <symbol_lib_table.h>
29 #include <class_libentry.h>
30 #include <tools/lib_control.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 
68  std::function<void( int, int, const wxString& )> aProgressCallback )
69 {
70  wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
71 
72  int libMgrHash = m_libMgr->GetHash();
73 
74  if( !aForce && m_lastSyncHash == libMgrHash )
75  return;
76 
77  m_lastSyncHash = libMgrHash;
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 modified
92  // libraries before the symbol library table which prevents the library from being
93  // removed from the tree control.
94  if( !m_libMgr->LibraryExists( name, true )
95  || !m_frame->Prj().SchSymbolLibTable()->HasLibrary( name, true ) )
96  {
97  it = deleteLibrary( it );
98  continue;
99  }
100  else if( aForce || m_libMgr->GetLibraryHash( name ) != m_libHashes[name] )
101  {
102  updateLibrary( *(LIB_TREE_NODE_LIB*) it->get() );
103  }
104 
105  ++it;
106  ++i;
107  }
108 
109  // Look for new libraries
110  for( const wxString& libName : m_libMgr->GetLibraryNames() )
111  {
112  if( m_libHashes.count( libName ) == 0 )
113  {
114  if( wxGetUTCTimeMillis() > nextUpdate )
115  {
116  aProgressCallback( i++, max, libName );
117  nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
118  }
119 
120  SYMBOL_LIB_TABLE_ROW* library = m_libMgr->GetLibrary( libName );
121  LIB_TREE_NODE_LIB& lib_node = DoAddLibraryNode( libName, library->GetDescr() );
122 
123  updateLibrary( lib_node );
124  }
125  }
126 
128 }
129 
130 
132 {
134 
135  for( const wxString& libName : m_libMgr->GetLibraryNames() )
136  {
137  if( m_libHashes.count( libName ) == 0 )
138  ++count;
139  }
140 
141  return count;
142 }
143 
144 
146 {
147  auto hashIt = m_libHashes.find( aLibNode.m_Name );
148 
149  if( hashIt == m_libHashes.end() )
150  {
151  // add a new library
152  for( LIB_PART* alias : m_libMgr->GetAliases( aLibNode.m_Name ) )
153  aLibNode.AddItem( alias );
154  }
155  else if( hashIt->second != m_libMgr->GetLibraryHash( aLibNode.m_Name ) )
156  {
157  // update an existing library
158  std::list<LIB_PART*> aliases = m_libMgr->GetAliases( aLibNode.m_Name );
159 
160  // remove the common part from the aliases list
161  for( auto nodeIt = aLibNode.m_Children.begin(); nodeIt != aLibNode.m_Children.end(); )
162  {
163  auto aliasIt = std::find_if( aliases.begin(), aliases.end(),
164  [&] ( const LIB_PART* a )
165  {
166  return a->GetName() == (*nodeIt)->m_Name;
167  } );
168 
169  if( aliasIt != aliases.end() )
170  {
171  // alias exists both in the component tree and the library manager,
172  // update only the node data
173  static_cast<LIB_TREE_NODE_LIB_ID*>( nodeIt->get() )->Update( *aliasIt );
174  aliases.erase( aliasIt );
175  ++nodeIt;
176  }
177  else
178  {
179  // node does not exist in the library manager, remove the corresponding node
180  nodeIt = aLibNode.m_Children.erase( nodeIt );
181  }
182  }
183 
184  // now the aliases list contains only new aliases that need to be added to the tree
185  for( LIB_PART* alias : aliases )
186  aLibNode.AddItem( alias );
187  }
188 
189  aLibNode.AssignIntrinsicRanks();
190  m_libHashes[aLibNode.m_Name] = m_libMgr->GetLibraryHash( aLibNode.m_Name );
191 }
192 
193 
194 LIB_TREE_NODE::PTR_VECTOR::iterator SYMBOL_TREE_SYNCHRONIZING_ADAPTER::deleteLibrary(
195  LIB_TREE_NODE::PTR_VECTOR::iterator& aLibNodeIt )
196 {
197  LIB_TREE_NODE* node = aLibNodeIt->get();
198  m_libHashes.erase( node->m_Name );
199  auto it = m_tree.m_Children.erase( aLibNodeIt );
200  return it;
201 }
202 
203 
204 void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
205  unsigned int aCol ) const
206 {
207  if( IsFrozen() )
208  {
209  aVariant = wxEmptyString;
210  return;
211  }
212 
213  LIB_TREE_NODE* node = ToNode( aItem );
214  wxASSERT( node );
215 
216  switch( aCol )
217  {
218  case 0:
219  if( m_frame->GetCurPart() && m_frame->GetCurPart()->GetLibId() == node->m_LibId )
221 
222  if( node->m_Pinned )
223  aVariant = GetPinningSymbol() + node->m_Name;
224  else
225  aVariant = node->m_Name;
226 
227  // mark modified items with an asterisk
228  if( node->m_Type == LIB_TREE_NODE::LIB )
229  {
230  if( m_libMgr->IsLibraryModified( node->m_Name ) )
231  aVariant = aVariant.GetString() + " *";
232  }
233  else if( node->m_Type == LIB_TREE_NODE::LIBID )
234  {
235  if( m_libMgr->IsPartModified( node->m_Name, node->m_Parent->m_Name ) )
236  aVariant = aVariant.GetString() + " *";
237  }
238 
239  break;
240 
241  case 1:
242  if( m_frame->GetCurPart() && m_frame->GetCurPart()->GetLibId() == node->m_LibId )
243  node->m_Desc = m_frame->GetCurPart()->GetDescription();
244 
245  aVariant = node->m_Desc;
246 
247  // Annotate that the library failed to load in the description column
248  if( node->m_Type == LIB_TREE_NODE::LIB )
249  {
250  if( !m_libMgr->IsLibraryLoaded( node->m_Name ) )
251  aVariant = _( "(failed to load)" ) + wxS( " " ) + aVariant.GetString();
252  }
253 
254  break;
255 
256  default: // column == -1 is used for default Compare function
257  aVariant = node->m_Name;
258  break;
259  }
260 }
261 
262 
263 bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
264  wxDataViewItemAttr& aAttr ) const
265 {
266  if( IsFrozen() )
267  return false;
268 
269  LIB_TREE_NODE* node = ToNode( aItem );
270  wxCHECK( node, false );
271 
272  // Mark both columns of unloaded libraries using grey text color (to look disabled)
273  if( node->m_Type == LIB_TREE_NODE::LIB && !m_libMgr->IsLibraryLoaded( node->m_Name ) )
274  {
275  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
276  return true;
277  }
278 
279  // The remaining attributes are only for the name column
280  if( aCol != 0 )
281  return false;
282 
283  switch( node->m_Type )
284  {
285  case LIB_TREE_NODE::LIB:
286  // mark modified libs with bold font
287  aAttr.SetBold( m_libMgr->IsLibraryModified( node->m_Name ) );
288 
289 #ifdef __WXGTK__
290  // The native wxGTK+ impl ignores background colour, so set the text colour instead.
291  // This works reasonably well in dark themes, and quite poorly in light ones....
292  if( node->m_Name == m_libMgr->GetCurrentLib() )
293  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
294 #else
295  // mark the current library with background color
296  if( node->m_Name == m_libMgr->GetCurrentLib() )
297  {
298  aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
299  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
300  }
301 #endif
302  break;
303 
305  // mark modified part with bold font
306  aAttr.SetBold( m_libMgr->IsPartModified( node->m_Name, node->m_Parent->m_Name ) );
307 
308  // mark aliases with italic font
309  aAttr.SetItalic( !node->m_IsRoot );
310 
311 #ifdef __WXGTK__
312  // The native wxGTK+ impl ignores background colour, so set the text colour instead.
313  // This works reasonably well in dark themes, and quite poorly in light ones....
314  if( node->m_LibId == m_libMgr->GetCurrentLibId() )
315  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
316 #else
317  // mark the current part with background color
318  if( node->m_LibId == m_libMgr->GetCurrentLibId() )
319  {
320  aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
321  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
322  }
323 #endif
324  break;
325 
326  default:
327  return false;
328  }
329 
330  return true;
331 }
int m_lastSyncHash
SYMBOL_LIBRARY_MANAGER hash value returned in the last synchronization
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
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.
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
Hashes to decide whether a library needs an update
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
#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.
SYMBOL_LIB_TABLE_ROW * GetLibrary(const wxString &aLibrary) const
Finds a single library within the (aggregate) library table.
int GetLibraryHash(const wxString &aLibrary) const
Returns a library hash value to determine if it has changed.
Class to handle modifications to the symbol libraries.
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
LIB_TREE_NODE * m_Parent
LIB_ID GetCurrentLibId() const
Returns the current library and part name as LIB_ID.
Node type: library.
Define a library symbol object.
static LIB_TREE_NODE * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
bool GetAttr(wxDataViewItem const &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
wxString GetDescription() override
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
LIB_PART * GetCurPart()
Return the current part being edited or NULL if none selected.
LIB_TREE_NODE_LIB & DoAddLibraryNode(wxString const &aNodeName, wxString const &aDesc)
wxArrayString GetLibraryNames() const
Returns the array of library names.
const wxString & GetCurrentLib() const
Returns the currently modified library name.
const wxString GetPinningSymbol() const
bool IsLibraryModified(const wxString &aLibrary) const
Returns 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
Returns 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:59
#define _(s)
Definition: 3d_actions.cpp:33
enum TYPE m_Type
void GetValue(wxVariant &aVariant, wxDataViewItem const &aItem, unsigned int aCol) const override
bool IsLibraryLoaded(const wxString &aLibrary) const
Returns true if the library was successfully loaded.
wxString m_Name
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:76
void Sync(bool aForce=false, std::function< void(int, int, const wxString &)> aProgressCallback=[](int, int, const wxString &){})
std::list< LIB_PART * > GetAliases(const wxString &aLibrary) const
PTR_VECTOR m_Children
wxString m_Desc
The symbol library editor main window.
bool IsPartModified(const wxString &aAlias, const wxString &aLibrary) const
Returns true if part has unsaved modifications.