You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
11 KiB

  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. #include <symbol_tree_synchronizing_adapter.h>
  26. #include <symbol_library_manager.h>
  27. #include <symbol_lib_table.h>
  28. #include <tools/symbol_editor_control.h>
  29. wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
  30. SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Create( SYMBOL_EDIT_FRAME* aParent,
  31. SYMBOL_LIBRARY_MANAGER* aLibMgr )
  32. {
  33. auto* adapter = new SYMBOL_TREE_SYNCHRONIZING_ADAPTER( aParent, aLibMgr );
  34. return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
  35. }
  36. SYMBOL_TREE_SYNCHRONIZING_ADAPTER::SYMBOL_TREE_SYNCHRONIZING_ADAPTER( SYMBOL_EDIT_FRAME* aParent,
  37. SYMBOL_LIBRARY_MANAGER* aLibMgr ) :
  38. LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
  39. m_frame( aParent ),
  40. m_libMgr( aLibMgr ),
  41. m_lastSyncHash( -1 )
  42. {
  43. }
  44. TOOL_INTERACTIVE* SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetContextMenuTool()
  45. {
  46. return m_frame->GetToolManager()->GetTool<SYMBOL_EDITOR_CONTROL>();
  47. }
  48. bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::IsContainer( const wxDataViewItem& aItem ) const
  49. {
  50. const LIB_TREE_NODE* node = ToNode( aItem );
  51. return node ? node->m_Type == LIB_TREE_NODE::LIB : true;
  52. }
  53. #define PROGRESS_INTERVAL_MILLIS 120
  54. void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::Sync( const wxString& aForceRefresh,
  55. std::function<void( int, int, const wxString& )> aProgressCallback )
  56. {
  57. wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
  58. m_lastSyncHash = m_libMgr->GetHash();
  59. int i = 0, max = GetLibrariesCount();
  60. // Process already stored libraries
  61. for( auto it = m_tree.m_Children.begin(); it != m_tree.m_Children.end(); /* iteration inside */ )
  62. {
  63. const wxString& name = it->get()->m_Name;
  64. if( wxGetUTCTimeMillis() > nextUpdate )
  65. {
  66. aProgressCallback( i, max, name );
  67. nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
  68. }
  69. // There is a bug in SYMBOL_LIBRARY_MANAGER::LibraryExists() that uses the buffered
  70. // modified libraries before the symbol library table which prevents the library from
  71. // being removed from the tree control.
  72. if( !m_libMgr->LibraryExists( name, true )
  73. || !m_frame->Prj().SchSymbolLibTable()->HasLibrary( name, true )
  74. || m_frame->Prj().SchSymbolLibTable()->FindRow( name, true ) !=
  75. m_frame->Prj().SchSymbolLibTable()->FindRow( name, false )
  76. || name == aForceRefresh )
  77. {
  78. it = deleteLibrary( it );
  79. continue;
  80. }
  81. else
  82. {
  83. updateLibrary( *(LIB_TREE_NODE_LIB*) it->get() );
  84. }
  85. ++it;
  86. ++i;
  87. }
  88. // Look for new libraries
  89. for( const wxString& libName : m_libMgr->GetLibraryNames() )
  90. {
  91. if( m_libHashes.count( libName ) == 0 )
  92. {
  93. if( wxGetUTCTimeMillis() > nextUpdate )
  94. {
  95. aProgressCallback( i++, max, libName );
  96. nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
  97. }
  98. SYMBOL_LIB_TABLE_ROW* library = m_libMgr->GetLibrary( libName );
  99. LIB_TREE_NODE_LIB& lib_node = DoAddLibraryNode( libName, library->GetDescr() );
  100. updateLibrary( lib_node );
  101. }
  102. }
  103. m_tree.AssignIntrinsicRanks();
  104. }
  105. int SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetLibrariesCount() const
  106. {
  107. int count = LIB_TREE_MODEL_ADAPTER::GetLibrariesCount();
  108. for( const wxString& libName : m_libMgr->GetLibraryNames() )
  109. {
  110. if( m_libHashes.count( libName ) == 0 )
  111. ++count;
  112. }
  113. return count;
  114. }
  115. void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::updateLibrary( LIB_TREE_NODE_LIB& aLibNode )
  116. {
  117. auto hashIt = m_libHashes.find( aLibNode.m_Name );
  118. if( hashIt == m_libHashes.end() )
  119. {
  120. // add a new library
  121. for( LIB_PART* alias : m_libMgr->GetAliases( aLibNode.m_Name ) )
  122. aLibNode.AddItem( alias );
  123. }
  124. else if( hashIt->second != m_libMgr->GetLibraryHash( aLibNode.m_Name ) )
  125. {
  126. // update an existing library
  127. std::list<LIB_PART*> aliases = m_libMgr->GetAliases( aLibNode.m_Name );
  128. // remove the common part from the aliases list
  129. for( auto nodeIt = aLibNode.m_Children.begin(); nodeIt != aLibNode.m_Children.end(); /**/ )
  130. {
  131. auto aliasIt = std::find_if( aliases.begin(), aliases.end(),
  132. [&] ( const LIB_PART* a )
  133. {
  134. return a->GetName() == (*nodeIt)->m_Name;
  135. } );
  136. if( aliasIt != aliases.end() )
  137. {
  138. // alias exists both in the component tree and the library manager,
  139. // update only the node data
  140. static_cast<LIB_TREE_NODE_LIB_ID*>( nodeIt->get() )->Update( *aliasIt );
  141. aliases.erase( aliasIt );
  142. ++nodeIt;
  143. }
  144. else
  145. {
  146. // node does not exist in the library manager, remove the corresponding node
  147. nodeIt = aLibNode.m_Children.erase( nodeIt );
  148. }
  149. }
  150. // now the aliases list contains only new aliases that need to be added to the tree
  151. for( LIB_PART* alias : aliases )
  152. aLibNode.AddItem( alias );
  153. }
  154. aLibNode.AssignIntrinsicRanks();
  155. m_libHashes[aLibNode.m_Name] = m_libMgr->GetLibraryHash( aLibNode.m_Name );
  156. }
  157. LIB_TREE_NODE::PTR_VECTOR::iterator SYMBOL_TREE_SYNCHRONIZING_ADAPTER::deleteLibrary(
  158. LIB_TREE_NODE::PTR_VECTOR::iterator& aLibNodeIt )
  159. {
  160. LIB_TREE_NODE* node = aLibNodeIt->get();
  161. m_libHashes.erase( node->m_Name );
  162. auto it = m_tree.m_Children.erase( aLibNodeIt );
  163. return it;
  164. }
  165. void SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
  166. unsigned int aCol ) const
  167. {
  168. if( IsFrozen() )
  169. {
  170. aVariant = wxEmptyString;
  171. return;
  172. }
  173. LIB_TREE_NODE* node = ToNode( aItem );
  174. wxASSERT( node );
  175. switch( aCol )
  176. {
  177. case 0:
  178. if( m_frame->GetCurPart() && m_frame->GetCurPart()->GetLibId() == node->m_LibId )
  179. node->m_Name = m_frame->GetCurPart()->GetLibId().GetLibItemName();
  180. if( node->m_Pinned )
  181. aVariant = GetPinningSymbol() + node->m_Name;
  182. else
  183. aVariant = node->m_Name;
  184. // mark modified items with an asterisk
  185. if( node->m_Type == LIB_TREE_NODE::LIB )
  186. {
  187. if( m_libMgr->IsLibraryModified( node->m_Name ) )
  188. aVariant = aVariant.GetString() + " *";
  189. }
  190. else if( node->m_Type == LIB_TREE_NODE::LIBID )
  191. {
  192. if( m_libMgr->IsPartModified( node->m_Name, node->m_Parent->m_Name ) )
  193. aVariant = aVariant.GetString() + " *";
  194. }
  195. break;
  196. case 1:
  197. if( m_frame->GetCurPart() && m_frame->GetCurPart()->GetLibId() == node->m_LibId )
  198. node->m_Desc = m_frame->GetCurPart()->GetDescription();
  199. aVariant = node->m_Desc;
  200. // Annotate that the library failed to load in the description column
  201. if( node->m_Type == LIB_TREE_NODE::LIB )
  202. {
  203. if( !m_libMgr->IsLibraryLoaded( node->m_Name ) )
  204. aVariant = _( "(failed to load)" ) + wxS( " " ) + aVariant.GetString();
  205. }
  206. break;
  207. default: // column == -1 is used for default Compare function
  208. aVariant = node->m_Name;
  209. break;
  210. }
  211. }
  212. bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
  213. wxDataViewItemAttr& aAttr ) const
  214. {
  215. if( IsFrozen() )
  216. return false;
  217. LIB_TREE_NODE* node = ToNode( aItem );
  218. wxCHECK( node, false );
  219. // Mark both columns of unloaded libraries using grey text color (to look disabled)
  220. if( node->m_Type == LIB_TREE_NODE::LIB && !m_libMgr->IsLibraryLoaded( node->m_Name ) )
  221. {
  222. aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
  223. return true;
  224. }
  225. // The remaining attributes are only for the name column
  226. if( aCol != 0 )
  227. return false;
  228. LIB_PART* curPart = m_frame->GetCurPart();
  229. switch( node->m_Type )
  230. {
  231. case LIB_TREE_NODE::LIB:
  232. // mark modified libs with bold font
  233. aAttr.SetBold( m_libMgr->IsLibraryModified( node->m_Name ) );
  234. // mark the current library with background color
  235. if( curPart && curPart->GetLibId().GetLibNickname() == node->m_LibId.GetLibNickname() )
  236. {
  237. #ifdef __WXGTK__
  238. // The native wxGTK+ impl ignores background colour, so set the text colour instead.
  239. // This works reasonably well in dark themes, and quite poorly in light ones....
  240. aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
  241. #else
  242. aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
  243. aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
  244. #endif
  245. }
  246. break;
  247. case LIB_TREE_NODE::LIBID:
  248. // mark modified part with bold font
  249. aAttr.SetBold( m_libMgr->IsPartModified( node->m_Name, node->m_Parent->m_Name ) );
  250. // mark aliases with italic font
  251. aAttr.SetItalic( !node->m_IsRoot );
  252. // mark the current part with background color
  253. if( curPart && curPart->GetLibId() == node->m_LibId )
  254. {
  255. #ifdef __WXGTK__
  256. // The native wxGTK+ impl ignores background colour, so set the text colour instead.
  257. // This works reasonably well in dark themes, and quite poorly in light ones....
  258. aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
  259. #else
  260. aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
  261. aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
  262. #endif
  263. }
  264. break;
  265. default:
  266. return false;
  267. }
  268. return true;
  269. }