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.

350 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. default:
  205. if( m_colIdxMap.count( aCol ) )
  206. {
  207. if( node->m_Type == LIB_TREE_NODE::LIB )
  208. {
  209. LIB_SYMBOL_LIBRARY_MANAGER& libMgr = m_frame->GetLibManager();
  210. SYMBOL_LIB_TABLE_ROW* lib = libMgr.GetLibrary( node->m_LibId.GetLibNickname() );
  211. if( lib )
  212. node->m_Desc = lib->GetDescr();
  213. if( !m_libMgr->IsLibraryLoaded( node->m_Name ) )
  214. aVariant = _( "(failed to load)" ) + wxS( " " ) + aVariant.GetString();
  215. }
  216. const wxString& key = m_colIdxMap.at( aCol );
  217. if( m_frame->GetCurSymbol() && m_frame->GetCurSymbol()->GetLibId() == node->m_LibId )
  218. {
  219. node->m_Desc = m_frame->GetCurSymbol()->GetDescription();
  220. }
  221. if( node->m_Fields.count( key ) )
  222. aVariant = node->m_Fields.at( key );
  223. else if( key == wxT( "Description" ) )
  224. aVariant = node->m_Desc;
  225. else
  226. aVariant = wxEmptyString;
  227. }
  228. break;
  229. }
  230. }
  231. bool SYMBOL_TREE_SYNCHRONIZING_ADAPTER::GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
  232. wxDataViewItemAttr& aAttr ) const
  233. {
  234. if( IsFrozen() )
  235. return false;
  236. LIB_TREE_NODE* node = ToNode( aItem );
  237. wxCHECK( node, false );
  238. // Mark both columns of unloaded libraries using grey text color (to look disabled)
  239. if( node->m_Type == LIB_TREE_NODE::LIB && !m_libMgr->IsLibraryLoaded( node->m_Name ) )
  240. {
  241. aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
  242. return true;
  243. }
  244. // The remaining attributes are only for the name column
  245. if( aCol != NAME_COL )
  246. return false;
  247. LIB_SYMBOL* curSymbol = m_frame->GetCurSymbol();
  248. switch( node->m_Type )
  249. {
  250. case LIB_TREE_NODE::LIB:
  251. // mark modified libs with bold font
  252. aAttr.SetBold( m_libMgr->IsLibraryModified( node->m_Name ) );
  253. // mark the current library with background color
  254. if( curSymbol && curSymbol->GetLibId().GetLibNickname() == node->m_LibId.GetLibNickname() )
  255. {
  256. #ifdef __WXGTK__
  257. // The native wxGTK+ impl ignores background colour, so set the text colour instead.
  258. // This works reasonably well in dark themes, and quite poorly in light ones....
  259. aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
  260. #else
  261. aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
  262. aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
  263. #endif
  264. }
  265. break;
  266. case LIB_TREE_NODE::LIBID:
  267. // mark modified part with bold font
  268. aAttr.SetBold( m_libMgr->IsSymbolModified( node->m_Name, node->m_Parent->m_Name ) );
  269. // mark aliases with italic font
  270. aAttr.SetItalic( !node->m_IsRoot );
  271. // mark the current part with background color
  272. if( curSymbol && curSymbol->GetLibId() == node->m_LibId )
  273. {
  274. #ifdef __WXGTK__
  275. // The native wxGTK+ impl ignores background colour, so set the text colour instead.
  276. // This works reasonably well in dark themes, and quite poorly in light ones....
  277. aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
  278. #else
  279. aAttr.SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
  280. aAttr.SetColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT ) );
  281. #endif
  282. }
  283. break;
  284. default:
  285. return false;
  286. }
  287. return true;
  288. }