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 <tool/tool_manager.h>
31 #include <tools/lib_control.h>
32 
33 
35  LIB_MANAGER* aLibMgr )
36 {
37  return PTR( new SYMBOL_TREE_SYNCHRONIZING_ADAPTER( aParent, aLibMgr ) );
38 }
39 
40 
42  LIB_MANAGER* aLibMgr ) :
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->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.Children.begin(); it != m_tree.Children.end(); /* iteration inside */ )
80  {
81  const wxString& name = it->get()->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( 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 auto& 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 
120  auto& lib_node = m_tree.AddLib( libName, library->GetDescr() );
121  updateLibrary( lib_node );
122  }
123  }
124 
126 }
127 
128 
130 {
132 
133  for( const auto& 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.Name );
146 
147  if( hashIt == m_libHashes.end() )
148  {
149  // add a new library
150  for( auto alias : m_libMgr->GetAliases( aLibNode.Name ) )
151  aLibNode.AddItem( alias );
152  }
153  else if( hashIt->second != m_libMgr->GetLibraryHash( aLibNode.Name ) )
154  {
155  // update an existing library
156  std::list<LIB_ALIAS*> aliases = m_libMgr->GetAliases( aLibNode.Name );
157 
158  // remove the common part from the aliases list
159  for( auto nodeIt = aLibNode.Children.begin(); nodeIt != aLibNode.Children.end(); )
160  {
161  auto aliasIt = std::find_if( aliases.begin(), aliases.end(),
162  [&] ( const LIB_ALIAS* a ) {
163  return a->GetName() == (*nodeIt)->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.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( auto alias : aliases )
183  aLibNode.AddItem( alias );
184  }
185 
186  aLibNode.AssignIntrinsicRanks();
187  m_libHashes[aLibNode.Name] = m_libMgr->GetLibraryHash( aLibNode.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->Name );
196  auto it = m_tree.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  auto node = ToNode( aItem );
211  wxASSERT( node );
212 
213  switch( aCol )
214  {
215  case 0:
216  aVariant = node->Name;
217 
218  // mark modified libs with an asterisk
219  if( node->Type == LIB_TREE_NODE::LIB && m_libMgr->IsLibraryModified( node->Name ) )
220  aVariant = node->Name + " *";
221 
222  // mark modified parts with an aster-ix
223  if( node->Type == LIB_TREE_NODE::LIBID
224  && m_libMgr->IsPartModified( node->Name, node->Parent->Name ) )
225  aVariant = node->Name + " *";
226 
227  break;
228 
229  case 1:
230  if( node->LibId == m_libMgr->GetCurrentLibId() )
231  {
232  LIB_ALIAS* alias = nullptr;
233 
234  // When the node parent name is empty, the node is a lib name, not a symbol name
235  if( !node->Parent->Name.IsEmpty() )
236  alias = m_libMgr->GetAlias( node->Name, node->Parent->Name );
237 
238  if( alias )
239  aVariant = alias->GetDescription();
240  else
241  aVariant = node->Desc;
242  }
243  else
244  aVariant = node->Desc;
245  break;
246 
247  default: // column == -1 is used for default Compare function
248  aVariant = node->Name;
249  break;
250  }
251 }
252 
253 
254 bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
255  wxDataViewItemAttr& aAttr ) const
256 {
257  if( IsFrozen() )
258  return false;
259 
260  // change attributes only for the name field
261  if( aCol != 0 )
262  return false;
263 
264  auto node = ToNode( aItem );
265  wxCHECK( node, false );
266 
267  switch( node->Type )
268  {
269  case LIB_TREE_NODE::LIB:
270  // mark modified libs with bold font
271  aAttr.SetBold( m_libMgr->IsLibraryModified( node->Name ) );
272 
273 #ifdef __WXGTK__
274  // The native wxGTK+ impl ignores background colour, so set the text colour instead.
275  // This works reasonably well in dark themes, and quite poorly in light ones....
276  if( node->Name == m_libMgr->GetCurrentLib() )
277  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
278 #else
279  // mark the current library with background color
280  if( node->Name == m_libMgr->GetCurrentLib() )
281  {
282  aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
283  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
284  }
285 #endif
286  break;
287 
289  // mark modified part with bold font
290  aAttr.SetBold( m_libMgr->IsPartModified( node->Name, node->Parent->Name ) );
291 
292  // mark aliases with italic font
293  aAttr.SetItalic( !node->IsRoot );
294 
295 #ifdef __WXGTK__
296  // The native wxGTK+ impl ignores background colour, so set the text colour instead.
297  // This works reasonably well in dark themes, and quite poorly in light ones....
298  if( node->LibId == m_libMgr->GetCurrentLibId() )
299  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
300 #else
301  // mark the current part with background color
302  if( node->LibId == m_libMgr->GetCurrentLibId() )
303  {
304  aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
305  aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
306  }
307 #endif
308  break;
309 
310  default:
311  return false;
312  }
313 
314  return true;
315 }
int m_lastSyncHash
LIB_MANAGER hash value returned in the last synchronization
static PTR Create(LIB_EDIT_FRAME *aParent, LIB_MANAGER *aLibs)
Part library alias object definition.
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
PTR_VECTOR Children
List of child nodes.
virtual int GetLibrariesCount() const
Return the number of libraries loaded in the tree.
enum TYPE Type
Node type.
bool IsContainer(const wxDataViewItem &aItem) const override
std::map< wxString, int > m_libHashes
Hashes to decide whether a library needs an update
std::list< LIB_ALIAS * > GetAliases(const wxString &aLibrary) const
const wxString & GetCurrentLib() const
Returns the currently modified library name.
Definition: lib_manager.h:278
LIB_ALIAS * GetAlias(const wxString &aAlias, const wxString &aLibrary) const
Returns either an alias of a working LIB_PART copy, or alias of the original part if there is no work...
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.
Class LIB_CONTROL.
Definition: lib_control.h:38
int GetLibraryHash(const wxString &aLibrary) const
Returns a library hash value to determine if it has changed.
Definition: lib_manager.cpp:86
#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.
Class LIB_PIN definition.
Node type: library.
int GetHash() const
Definition: lib_manager.cpp:75
static LIB_TREE_NODE const * ToNode(wxDataViewItem aItem)
Convert wxDataViewItem -> CMP_TREE_NODE.
wxArrayString GetLibraryNames() const
Returns the array of library names.
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.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
LIB_TREE_NODE_LIB & AddLib(wxString const &aName, wxString const &aDesc)
Construct an empty library node, add it to the root, and return it.
Class to handle modifications to the symbol libraries.
Definition: lib_manager.h:89
const wxString & GetDescription() override
wxString Name
Actual name of the part.
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:61
#define max(a, b)
Definition: auxiliary.h:86
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
size_t i
Definition: json11.cpp:597
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.
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:310
SYMBOL_TREE_SYNCHRONIZING_ADAPTER(LIB_EDIT_FRAME *aParent, LIB_MANAGER *aLibMgr)