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 
31 
32 wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
34  SYMBOL_LIBRARY_MANAGER* aLibMgr )
35 {
36  auto* adapter = new SYMBOL_TREE_SYNCHRONIZING_ADAPTER( aParent, aLibMgr );
37  return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
38 }
39 
40 
42  SYMBOL_LIBRARY_MANAGER* aLibMgr ) :
43  LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
44  m_frame( aParent ),
45  m_libMgr( aLibMgr ),
46  m_lastSyncHash( -1 )
47 {
48 }
49 
50 
52 {
54 }
55 
56 
57 bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
58 {
59  const LIB_TREE_NODE* node = ToNode( aItem );
60  return node ? node->m_Type == LIB_TREE_NODE::LIB : true;
61 }
62 
63 
64 #define PROGRESS_INTERVAL_MILLIS 120
65 
66 void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync( const wxString& aForceRefresh,
67  std::function<void( int, int, const wxString& )> aProgressCallback )
68 {
69  wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
70 
72  int i = 0, max = GetLibrariesCount();
73 
74  // Process already stored libraries
75  for( auto it = m_tree.m_Children.begin(); it != m_tree.m_Children.end(); /* iteration inside */ )
76  {
77  const wxString& name = it->get()->m_Name;
78 
79  if( wxGetUTCTimeMillis() > nextUpdate )
80  {
81  aProgressCallback( i, max, name );
82  nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
83  }
84 
85  // There is a bug in SYMBOL_LIBRARY_MANAGER::LibraryExists() that uses the buffered
86  // modified libraries before the symbol library table which prevents the library from
87  // being removed from the tree control.
88  if( !m_libMgr->LibraryExists( name, true )
89  || !m_frame->Prj().SchSymbolLibTable()->HasLibrary( name, true )
90  || m_frame->Prj().SchSymbolLibTable()->FindRow( name, true ) !=
91  m_frame->Prj().SchSymbolLibTable()->FindRow( name, false )
92  || name == aForceRefresh )
93  {
94  it = deleteLibrary( it );
95  continue;
96  }
97  else
98  {
99  updateLibrary( *(LIB_TREE_NODE_LIB*) it->get() );
100  }
101 
102  ++it;
103  ++i;
104  }
105 
106  // Look for new libraries
107  for( const wxString& libName : m_libMgr->GetLibraryNames() )
108  {
109  if( m_libHashes.count( libName ) == 0 )
110  {
111  if( wxGetUTCTimeMillis() > nextUpdate )
112  {
113  aProgressCallback( i++, max, libName );
114  nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
115  }
116 
117  SYMBOL_LIB_TABLE_ROW* library = m_libMgr->GetLibrary( libName );
118  LIB_TREE_NODE_LIB& lib_node = DoAddLibraryNode( libName, library->GetDescr() );
119 
120  updateLibrary( lib_node );
121  }
122  }
123 
125 }
126 
127 
129 {
131 
132  for( const wxString& libName : m_libMgr->GetLibraryNames() )
133  {
134  if( m_libHashes.count( libName ) == 0 )
135  ++count;
136  }
137 
138  return count;
139 }
140 
141 
143 {
144  auto hashIt = m_libHashes.find( aLibNode.m_Name );
145 
146  if( hashIt == m_libHashes.end() )
147  {
148  // add a new library
149  for( LIB_PART* alias : m_libMgr->GetAliases( aLibNode.m_Name ) )
150  aLibNode.AddItem( alias );
151  }
152  else if( hashIt->second != m_libMgr->GetLibraryHash( aLibNode.m_Name ) )
153  {
154  // update an existing library
155  std::list<LIB_PART*> aliases = m_libMgr->GetAliases( aLibNode.m_Name );
156 
157  // remove the common part from the aliases list
158  for( auto nodeIt = aLibNode.m_Children.begin(); nodeIt != aLibNode.m_Children.end(); )
159  {
160  auto aliasIt = std::find_if( aliases.begin(), aliases.end(),
161  [&] ( const LIB_PART* a )
162  {
163  return a->GetName() == (*nodeIt)->m_Name;
164  } );
165 
166  if( aliasIt != aliases.end() )
167  {
168  // alias exists both in the component tree and the library manager,
169  // update only the node data
170  static_cast<LIB_TREE_NODE_LIB_ID*>( nodeIt->get() )->Update( *aliasIt );
171  aliases.erase( aliasIt );
172  ++nodeIt;
173  }
174  else
175  {
176  // node does not exist in the library manager, remove the corresponding node
177  nodeIt = aLibNode.m_Children.erase( nodeIt );
178  }
179  }
180 
181  // now the aliases list contains only new aliases that need to be added to the tree
182  for( LIB_PART* alias : aliases )
183  aLibNode.AddItem( alias );
184  }
185 
186  aLibNode.AssignIntrinsicRanks();
187  m_libHashes[aLibNode.m_Name] = m_libMgr->GetLibraryHash( aLibNode.m_Name );
188 }
189 
190 
191 LIB_TREE_NODE::PTR_VECTOR::iterator SYMBOL_TREE_SYNCHRONIZING_ADAPTER::deleteLibrary(
192  LIB_TREE_NODE::PTR_VECTOR::iterator& aLibNodeIt )
193 {
194  LIB_TREE_NODE* node = aLibNodeIt->get();
195  m_libHashes.erase( node->m_Name );
196  auto it = m_tree.m_Children.erase( aLibNodeIt );
197  return it;
198 }
199 
200 
201 void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
202  unsigned int aCol ) const
203 {
204  if( IsFrozen() )
205  {
206  aVariant = wxEmptyString;
207  return;
208  }
209 
210  LIB_TREE_NODE* node = ToNode( aItem );
211  wxASSERT( node );
212 
213  switch( aCol )
214  {
215  case 0:
216  if( m_frame->GetCurPart() && m_frame->GetCurPart()->GetLibId() == node->m_LibId )
218 
219  if( node->m_Pinned )
220  aVariant = GetPinningSymbol() + node->m_Name;
221  else
222  aVariant = node->m_Name;
223 
224  // mark modified items with an asterisk
225  if( node->m_Type == LIB_TREE_NODE::LIB )
226  {
227  if( m_libMgr->IsLibraryModified( node->m_Name ) )
228  aVariant = aVariant.GetString() + " *";
229  }
230  else if( node->m_Type == LIB_TREE_NODE::LIBID )
231  {
232  if( m_libMgr->IsPartModified( node->m_Name, node->m_Parent->m_Name ) )
233  aVariant = aVariant.GetString() + " *";
234  }
235 
236  break;
237 
238  case 1:
239  if( m_frame->GetCurPart() && m_frame->GetCurPart()->GetLibId() == node->m_LibId )
240  node->m_Desc = m_frame->GetCurPart()->GetDescription();
241 
242  aVariant = node->m_Desc;
243 
244  // Annotate that the library failed to load in the description column
245  if( node->m_Type == LIB_TREE_NODE::LIB )
246  {
247  if( !m_libMgr->IsLibraryLoaded( node->m_Name ) )
248  aVariant = _( "(failed to load)" ) + wxS( " " ) + aVariant.GetString();
249  }
250 
251  break;
252 
253  default: // column == -1 is used for default Compare function
254  aVariant = node->m_Name;
255  break;
256  }
257 }
258 
259 
260 bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
261  wxDataViewItemAttr& aAttr ) const
262 {
263  if( IsFrozen() )
264  return false;
265 
266  LIB_TREE_NODE* node = ToNode( aItem );
267  wxCHECK( node, false );
268 
269  // Mark both columns of unloaded libraries using grey text color (to look disabled)
270  if( node->m_Type == LIB_TREE_NODE::LIB && !m_libMgr->IsLibraryLoaded( node->m_Name ) )
271  {
272  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
273  return true;
274  }
275 
276  // The remaining attributes are only for the name column
277  if( aCol != 0 )
278  return false;
279 
280  LIB_PART* curPart = m_frame->GetCurPart();
281 
282  switch( node->m_Type )
283  {
284  case LIB_TREE_NODE::LIB:
285  // mark modified libs with bold font
286  aAttr.SetBold( m_libMgr->IsLibraryModified( node->m_Name ) );
287 
288  // mark the current library with background color
289  if( curPart && curPart->GetLibId().GetLibNickname() == node->m_LibId.GetLibNickname() )
290  {
291 #ifdef __WXGTK__
292  // The native wxGTK+ impl ignores background colour, so set the text colour instead.
293  // This works reasonably well in dark themes, and quite poorly in light ones....
294  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
295 #else
296  aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
297  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
298 #endif
299  }
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  // mark the current part with background color
310  if( curPart && curPart->GetLibId() == node->m_LibId )
311  {
312 #ifdef __WXGTK__
313  // The native wxGTK+ impl ignores background colour, so set the text colour instead.
314  // This works reasonably well in dark themes, and quite poorly in light ones....
315  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
316 #else
317  aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
318  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
319 #endif
320  }
321  break;
322 
323  default:
324  return false;
325  }
326 
327  return true;
328 }
const UTF8 & GetLibItemName() const
Definition: lib_id.h:106
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
LIB_ID GetLibId() const override
Definition: lib_symbol.h:131
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
SYMBOL_LIBRARY_MANAGER hash value returned in the last synchronization.
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_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.
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
Node type: library.
Define a library symbol object.
Definition: lib_symbol.h:93
static LIB_TREE_NODE * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:92
bool GetAttr(wxDataViewItem const &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
wxString GetDescription() override
Definition: lib_symbol.h:138
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.
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
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: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
Return true if the library was successfully loaded.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
std::list< LIB_PART * > GetAliases(const wxString &aLibrary) const
PTR_VECTOR m_Children
Handle actions for the various symbol editor and viewers.
The symbol library editor main window.
bool IsPartModified(const wxString &aAlias, const wxString &aLibrary) const
Return true if part has unsaved modifications.