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.

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