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.

239 lines
7.9 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 (C) 2014-2022 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 <wx/log.h>
  22. #include <wx/tokenzr.h>
  23. #include <wx/window.h>
  24. #include <core/kicad_algo.h>
  25. #include <pgm_base.h>
  26. #include <project/project_file.h>
  27. #include <widgets/wx_progress_reporters.h>
  28. #include <dialogs/html_message_box.h>
  29. #include <eda_pattern_match.h>
  30. #include <generate_alias_info.h>
  31. #include <sch_base_frame.h>
  32. #include <locale_io.h>
  33. #include <lib_symbol.h>
  34. #include <symbol_async_loader.h>
  35. #include <symbol_lib_table.h>
  36. #include <symbol_tree_model_adapter.h>
  37. #include <string_utils.h>
  38. bool SYMBOL_TREE_MODEL_ADAPTER::m_show_progress = true;
  39. #define PROGRESS_INTERVAL_MILLIS 33 // 30 FPS refresh rate
  40. wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
  41. SYMBOL_TREE_MODEL_ADAPTER::Create( EDA_BASE_FRAME* aParent, LIB_TABLE* aLibs )
  42. {
  43. auto* adapter = new SYMBOL_TREE_MODEL_ADAPTER( aParent, aLibs );
  44. return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
  45. }
  46. SYMBOL_TREE_MODEL_ADAPTER::SYMBOL_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent, LIB_TABLE* aLibs ) :
  47. LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
  48. m_libs( (SYMBOL_LIB_TABLE*) aLibs )
  49. {
  50. // Symbols may have different value from name
  51. m_availableColumns.emplace_back( wxT( "Value" ) );
  52. }
  53. SYMBOL_TREE_MODEL_ADAPTER::~SYMBOL_TREE_MODEL_ADAPTER()
  54. {}
  55. bool SYMBOL_TREE_MODEL_ADAPTER::AddLibraries( const std::vector<wxString>& aNicknames,
  56. SCH_BASE_FRAME* aFrame )
  57. {
  58. std::unique_ptr<WX_PROGRESS_REPORTER> progressReporter = nullptr;
  59. if( m_show_progress )
  60. {
  61. progressReporter = std::make_unique<WX_PROGRESS_REPORTER>( aFrame,
  62. _( "Loading Symbol Libraries" ),
  63. aNicknames.size(), true );
  64. }
  65. // Disable KIID generation: not needed for library parts; sometimes very slow
  66. KIID::CreateNilUuids( true );
  67. std::unordered_map<wxString, std::vector<LIB_SYMBOL*>> loadedSymbolMap;
  68. SYMBOL_ASYNC_LOADER loader( aNicknames, m_libs, GetFilter() != nullptr, &loadedSymbolMap,
  69. progressReporter.get() );
  70. LOCALE_IO toggle;
  71. loader.Start();
  72. while( !loader.Done() )
  73. {
  74. if( progressReporter && !progressReporter->KeepRefreshing() )
  75. break;
  76. wxMilliSleep( PROGRESS_INTERVAL_MILLIS );
  77. }
  78. loader.Join();
  79. bool cancelled = false;
  80. if( progressReporter )
  81. cancelled = progressReporter->IsCancelled();
  82. if( !loader.GetErrors().IsEmpty() )
  83. {
  84. HTML_MESSAGE_BOX dlg( aFrame, _( "Load Error" ) );
  85. dlg.MessageSet( _( "Errors loading symbols:" ) );
  86. wxString msg = loader.GetErrors();
  87. msg.Replace( "\n", "<BR>" );
  88. dlg.AddHTML_Text( msg );
  89. dlg.ShowModal();
  90. }
  91. if( loadedSymbolMap.size() > 0 )
  92. {
  93. COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
  94. PROJECT_FILE& project = aFrame->Prj().GetProjectFile();
  95. auto addFunc =
  96. [&]( const wxString& aLibName, const std::vector<LIB_SYMBOL*>& aSymbolList,
  97. const wxString& aDescription )
  98. {
  99. std::vector<LIB_TREE_ITEM*> treeItems( aSymbolList.begin(), aSymbolList.end() );
  100. bool pinned = alg::contains( cfg->m_Session.pinned_symbol_libs, aLibName )
  101. || alg::contains( project.m_PinnedSymbolLibs, aLibName );
  102. DoAddLibrary( aLibName, aDescription, treeItems, pinned, false );
  103. };
  104. for( const auto& [libNickname, libSymbols] : loadedSymbolMap )
  105. {
  106. SYMBOL_LIB_TABLE_ROW* row = m_libs->FindRow( libNickname );
  107. wxCHECK2( row, continue );
  108. if( !row->GetIsVisible() )
  109. continue;
  110. std::vector<wxString> additionalColumns;
  111. row->GetAvailableSymbolFields( additionalColumns );
  112. for( const wxString& column : additionalColumns )
  113. addColumnIfNecessary( column );
  114. if( row->SupportsSubLibraries() )
  115. {
  116. std::vector<wxString> subLibraries;
  117. row->GetSubLibraryNames( subLibraries );
  118. wxString parentDesc = m_libs->GetDescription( libNickname );
  119. for( const wxString& lib : subLibraries )
  120. {
  121. wxString suffix = lib.IsEmpty() ? wxString( wxT( "" ) )
  122. : wxString::Format( wxT( " - %s" ), lib );
  123. wxString name = wxString::Format( wxT( "%s%s" ), libNickname, suffix );
  124. wxString desc = row->GetSubLibraryDescription( lib );
  125. if( !parentDesc.IsEmpty() )
  126. desc = wxString::Format( wxT( "%s (%s)" ), parentDesc,
  127. ( desc.IsEmpty() ) ? lib : desc );
  128. UTF8 utf8Lib( lib );
  129. std::vector<LIB_SYMBOL*> symbols;
  130. std::copy_if( libSymbols.begin(), libSymbols.end(),
  131. std::back_inserter( symbols ),
  132. [&utf8Lib]( LIB_SYMBOL* aSym )
  133. {
  134. return utf8Lib == aSym->GetLibId().GetSubLibraryName();
  135. } );
  136. addFunc( name, symbols, desc );
  137. }
  138. }
  139. else
  140. {
  141. addFunc( libNickname, libSymbols, m_libs->GetDescription( libNickname ) );
  142. }
  143. }
  144. }
  145. KIID::CreateNilUuids( false );
  146. m_tree.AssignIntrinsicRanks();
  147. if( progressReporter )
  148. {
  149. // Force immediate deletion of the APP_PROGRESS_DIALOG. Do not use Destroy(), or Destroy()
  150. // followed by wxSafeYield() because on Windows, APP_PROGRESS_DIALOG has some side effects
  151. // on the event loop manager.
  152. // One in particular is the call of ShowModal() following SYMBOL_TREE_MODEL_ADAPTER
  153. // creating a APP_PROGRESS_DIALOG (which has incorrect modal behaviour).
  154. progressReporter.reset();
  155. m_show_progress = false;
  156. }
  157. return !cancelled;
  158. }
  159. void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname, bool pinned )
  160. {
  161. bool onlyPowerSymbols = ( GetFilter() != nullptr );
  162. std::vector<LIB_SYMBOL*> symbols;
  163. std::vector<LIB_TREE_ITEM*> comp_list;
  164. try
  165. {
  166. m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
  167. }
  168. catch( const IO_ERROR& ioe )
  169. {
  170. wxLogError( _( "Error loading symbol library '%s'." ) + wxS( "\n%s" ),
  171. aLibNickname,
  172. ioe.What() );
  173. return;
  174. }
  175. if( symbols.size() > 0 )
  176. {
  177. comp_list.assign( symbols.begin(), symbols.end() );
  178. DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, pinned,
  179. false );
  180. }
  181. }
  182. wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
  183. {
  184. return GenerateAliasInfo( m_libs, aLibId, aUnit );
  185. }