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.

241 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, _( "Load Symbol Libraries" ),
  61. aNicknames.size(), PR_CAN_ABORT );
  62. }
  63. // Disable KIID generation: not needed for library parts; sometimes very slow
  64. KIID::CreateNilUuids( true );
  65. std::unordered_map<wxString, std::vector<LIB_SYMBOL*>> loadedSymbolMap;
  66. SYMBOL_ASYNC_LOADER loader( aNicknames, m_libs, GetFilter() != nullptr, &loadedSymbolMap,
  67. progressReporter.get() );
  68. LOCALE_IO toggle;
  69. loader.Start();
  70. while( !loader.Done() )
  71. {
  72. if( progressReporter && !progressReporter->KeepRefreshing() )
  73. break;
  74. wxMilliSleep( PROGRESS_INTERVAL_MILLIS );
  75. }
  76. loader.Join();
  77. bool cancelled = false;
  78. if( progressReporter )
  79. cancelled = progressReporter->IsCancelled();
  80. if( !loader.GetErrors().IsEmpty() )
  81. {
  82. HTML_MESSAGE_BOX dlg( aFrame, _( "Load Error" ) );
  83. dlg.MessageSet( _( "Errors loading symbols:" ) );
  84. wxString msg = loader.GetErrors();
  85. msg.Replace( "\n", "<BR>" );
  86. dlg.AddHTML_Text( msg );
  87. dlg.ShowModal();
  88. }
  89. if( loadedSymbolMap.size() > 0 )
  90. {
  91. COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
  92. PROJECT_FILE& project = aFrame->Prj().GetProjectFile();
  93. auto addFunc =
  94. [&]( const wxString& aLibName, const std::vector<LIB_SYMBOL*>& aSymbolList,
  95. const wxString& aDescription )
  96. {
  97. std::vector<LIB_TREE_ITEM*> treeItems( aSymbolList.begin(), aSymbolList.end() );
  98. bool pinned = alg::contains( cfg->m_Session.pinned_symbol_libs, aLibName )
  99. || alg::contains( project.m_PinnedSymbolLibs, aLibName );
  100. DoAddLibrary( aLibName, aDescription, treeItems, pinned, false );
  101. };
  102. for( const auto& [libNickname, libSymbols] : loadedSymbolMap )
  103. {
  104. SYMBOL_LIB_TABLE_ROW* row = m_libs->FindRow( libNickname );
  105. wxCHECK2( row, continue );
  106. if( !row->GetIsVisible() )
  107. continue;
  108. std::vector<wxString> additionalColumns;
  109. row->GetAvailableSymbolFields( additionalColumns );
  110. for( const wxString& column : additionalColumns )
  111. addColumnIfNecessary( column );
  112. if( row->SupportsSubLibraries() )
  113. {
  114. std::vector<wxString> subLibraries;
  115. row->GetSubLibraryNames( subLibraries );
  116. wxString parentDesc = m_libs->GetDescription( libNickname );
  117. for( const wxString& lib : subLibraries )
  118. {
  119. wxString suffix = lib.IsEmpty() ? wxString( wxT( "" ) )
  120. : wxString::Format( wxT( " - %s" ), lib );
  121. wxString name = wxString::Format( wxT( "%s%s" ), libNickname, suffix );
  122. wxString desc = row->GetSubLibraryDescription( lib );
  123. if( !parentDesc.IsEmpty() )
  124. {
  125. desc = wxString::Format( wxT( "%s (%s)" ),
  126. parentDesc,
  127. desc.IsEmpty() ? lib : desc );
  128. }
  129. UTF8 utf8Lib( lib );
  130. std::vector<LIB_SYMBOL*> symbols;
  131. std::copy_if( libSymbols.begin(), libSymbols.end(),
  132. std::back_inserter( symbols ),
  133. [&utf8Lib]( LIB_SYMBOL* aSym )
  134. {
  135. return utf8Lib == aSym->GetLibId().GetSubLibraryName();
  136. } );
  137. addFunc( name, symbols, desc );
  138. }
  139. }
  140. else
  141. {
  142. addFunc( libNickname, libSymbols, m_libs->GetDescription( libNickname ) );
  143. }
  144. }
  145. }
  146. KIID::CreateNilUuids( false );
  147. m_tree.AssignIntrinsicRanks();
  148. if( progressReporter )
  149. {
  150. // Force immediate deletion of the APP_PROGRESS_DIALOG. Do not use Destroy(), or Destroy()
  151. // followed by wxSafeYield() because on Windows, APP_PROGRESS_DIALOG has some side effects
  152. // on the event loop manager.
  153. // One in particular is the call of ShowModal() following SYMBOL_TREE_MODEL_ADAPTER
  154. // creating a APP_PROGRESS_DIALOG (which has incorrect modal behaviour).
  155. progressReporter.reset();
  156. m_show_progress = false;
  157. }
  158. return !cancelled;
  159. }
  160. void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname, bool pinned )
  161. {
  162. bool onlyPowerSymbols = ( GetFilter() != nullptr );
  163. std::vector<LIB_SYMBOL*> symbols;
  164. std::vector<LIB_TREE_ITEM*> comp_list;
  165. try
  166. {
  167. m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
  168. }
  169. catch( const IO_ERROR& ioe )
  170. {
  171. wxLogError( _( "Error loading symbol library '%s'." ) + wxS( "\n%s" ),
  172. aLibNickname,
  173. ioe.What() );
  174. return;
  175. }
  176. if( symbols.size() > 0 )
  177. {
  178. comp_list.assign( symbols.begin(), symbols.end() );
  179. DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, pinned,
  180. false );
  181. }
  182. }
  183. wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
  184. {
  185. return GenerateAliasInfo( m_libs, aLibId, aUnit );
  186. }