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.

242 lines
8.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 Chris Pavlina <pavlina.chris@gmail.com>
  5. * Copyright (C) 2014 Henner Zeller <h.zeller@acm.org>
  6. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software: you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation, either version 3 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "symbol_tree_model_adapter.h"
  22. #include <wx/log.h>
  23. #include <wx/tokenzr.h>
  24. #include <wx/window.h>
  25. #include <core/kicad_algo.h>
  26. #include <pgm_base.h>
  27. #include <project/project_file.h>
  28. #include <widgets/wx_progress_reporters.h>
  29. #include <dialogs/html_message_box.h>
  30. #include <generate_alias_info.h>
  31. #include <sch_base_frame.h>
  32. #include <locale_io.h>
  33. #include <symbol_async_loader.h>
  34. #include <symbol_lib_table.h>
  35. #include <string_utils.h>
  36. bool SYMBOL_TREE_MODEL_ADAPTER::m_show_progress = true;
  37. #define PROGRESS_INTERVAL_MILLIS 33 // 30 FPS refresh rate
  38. wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
  39. SYMBOL_TREE_MODEL_ADAPTER::Create( SCH_BASE_FRAME* aParent, LIB_TABLE* aLibs )
  40. {
  41. auto* adapter = new SYMBOL_TREE_MODEL_ADAPTER( aParent, aLibs );
  42. return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
  43. }
  44. SYMBOL_TREE_MODEL_ADAPTER::SYMBOL_TREE_MODEL_ADAPTER( SCH_BASE_FRAME* aParent, LIB_TABLE* aLibs ) :
  45. LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs",
  46. aParent->GetViewerSettingsBase()->m_LibTree ),
  47. m_libs( (SYMBOL_LIB_TABLE*) aLibs )
  48. {
  49. // Symbols may have different value from name
  50. m_availableColumns.emplace_back( wxT( "Value" ) );
  51. }
  52. SYMBOL_TREE_MODEL_ADAPTER::~SYMBOL_TREE_MODEL_ADAPTER()
  53. {}
  54. bool SYMBOL_TREE_MODEL_ADAPTER::AddLibraries( const std::vector<wxString>& aNicknames,
  55. SCH_BASE_FRAME* aFrame )
  56. {
  57. std::unique_ptr<WX_PROGRESS_REPORTER> progressReporter = nullptr;
  58. if( m_show_progress )
  59. {
  60. progressReporter = std::make_unique<WX_PROGRESS_REPORTER>( aFrame,
  61. _( "Loading Symbol Libraries" ),
  62. aNicknames.size(), true );
  63. }
  64. // Disable KIID generation: not needed for library parts; sometimes very slow
  65. KIID::CreateNilUuids( true );
  66. std::unordered_map<wxString, std::vector<LIB_SYMBOL*>> loadedSymbolMap;
  67. SYMBOL_ASYNC_LOADER loader( aNicknames, m_libs, GetFilter() != nullptr, &loadedSymbolMap,
  68. progressReporter.get() );
  69. LOCALE_IO toggle;
  70. loader.Start();
  71. while( !loader.Done() )
  72. {
  73. if( progressReporter && !progressReporter->KeepRefreshing() )
  74. break;
  75. wxMilliSleep( PROGRESS_INTERVAL_MILLIS );
  76. }
  77. loader.Join();
  78. bool cancelled = false;
  79. if( progressReporter )
  80. cancelled = progressReporter->IsCancelled();
  81. if( !loader.GetErrors().IsEmpty() )
  82. {
  83. HTML_MESSAGE_BOX dlg( aFrame, _( "Load Error" ) );
  84. dlg.MessageSet( _( "Errors loading symbols:" ) );
  85. wxString msg = loader.GetErrors();
  86. msg.Replace( "\n", "<BR>" );
  87. dlg.AddHTML_Text( msg );
  88. dlg.ShowModal();
  89. }
  90. if( loadedSymbolMap.size() > 0 )
  91. {
  92. COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
  93. PROJECT_FILE& project = aFrame->Prj().GetProjectFile();
  94. auto addFunc =
  95. [&]( const wxString& aLibName, const std::vector<LIB_SYMBOL*>& aSymbolList,
  96. const wxString& aDescription )
  97. {
  98. std::vector<LIB_TREE_ITEM*> treeItems( aSymbolList.begin(), aSymbolList.end() );
  99. bool pinned = alg::contains( cfg->m_Session.pinned_symbol_libs, aLibName )
  100. || alg::contains( project.m_PinnedSymbolLibs, aLibName );
  101. DoAddLibrary( aLibName, aDescription, treeItems, pinned, false );
  102. };
  103. for( const auto& [libNickname, libSymbols] : loadedSymbolMap )
  104. {
  105. SYMBOL_LIB_TABLE_ROW* row = m_libs->FindRow( libNickname );
  106. wxCHECK2( row, continue );
  107. if( !row->GetIsVisible() )
  108. continue;
  109. std::vector<wxString> additionalColumns;
  110. row->GetAvailableSymbolFields( additionalColumns );
  111. for( const wxString& column : additionalColumns )
  112. addColumnIfNecessary( column );
  113. if( row->SupportsSubLibraries() )
  114. {
  115. std::vector<wxString> subLibraries;
  116. row->GetSubLibraryNames( subLibraries );
  117. wxString parentDesc = m_libs->GetDescription( libNickname );
  118. for( const wxString& lib : subLibraries )
  119. {
  120. wxString suffix = lib.IsEmpty() ? wxString( wxT( "" ) )
  121. : wxString::Format( wxT( " - %s" ), lib );
  122. wxString name = wxString::Format( wxT( "%s%s" ), libNickname, suffix );
  123. wxString desc = row->GetSubLibraryDescription( lib );
  124. if( !parentDesc.IsEmpty() )
  125. {
  126. desc = wxString::Format( wxT( "%s (%s)" ),
  127. parentDesc,
  128. desc.IsEmpty() ? lib : desc );
  129. }
  130. UTF8 utf8Lib( lib );
  131. std::vector<LIB_SYMBOL*> symbols;
  132. std::copy_if( libSymbols.begin(), libSymbols.end(),
  133. std::back_inserter( symbols ),
  134. [&utf8Lib]( LIB_SYMBOL* aSym )
  135. {
  136. return utf8Lib == aSym->GetLibId().GetSubLibraryName();
  137. } );
  138. addFunc( name, symbols, desc );
  139. }
  140. }
  141. else
  142. {
  143. addFunc( libNickname, libSymbols, m_libs->GetDescription( libNickname ) );
  144. }
  145. }
  146. }
  147. KIID::CreateNilUuids( false );
  148. m_tree.AssignIntrinsicRanks();
  149. if( progressReporter )
  150. {
  151. // Force immediate deletion of the APP_PROGRESS_DIALOG. Do not use Destroy(), or Destroy()
  152. // followed by wxSafeYield() because on Windows, APP_PROGRESS_DIALOG has some side effects
  153. // on the event loop manager.
  154. // One in particular is the call of ShowModal() following SYMBOL_TREE_MODEL_ADAPTER
  155. // creating a APP_PROGRESS_DIALOG (which has incorrect modal behaviour).
  156. progressReporter.reset();
  157. m_show_progress = false;
  158. }
  159. return !cancelled;
  160. }
  161. void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname, bool pinned )
  162. {
  163. bool onlyPowerSymbols = ( GetFilter() != nullptr );
  164. std::vector<LIB_SYMBOL*> symbols;
  165. std::vector<LIB_TREE_ITEM*> comp_list;
  166. try
  167. {
  168. m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
  169. }
  170. catch( const IO_ERROR& ioe )
  171. {
  172. wxLogError( _( "Error loading symbol library '%s'." ) + wxS( "\n%s" ),
  173. aLibNickname,
  174. ioe.What() );
  175. return;
  176. }
  177. if( symbols.size() > 0 )
  178. {
  179. comp_list.assign( symbols.begin(), symbols.end() );
  180. DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, pinned,
  181. false );
  182. }
  183. }
  184. wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
  185. {
  186. return GenerateAliasInfo( m_libs, aLibId, aUnit );
  187. }