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.

230 lines
7.5 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. SYMBOL_TREE_MODEL_ADAPTER::~SYMBOL_TREE_MODEL_ADAPTER()
  51. {}
  52. bool SYMBOL_TREE_MODEL_ADAPTER::AddLibraries( const std::vector<wxString>& aNicknames,
  53. SCH_BASE_FRAME* aFrame )
  54. {
  55. std::unique_ptr<WX_PROGRESS_REPORTER> progressReporter = nullptr;
  56. if( m_show_progress )
  57. {
  58. progressReporter = std::make_unique<WX_PROGRESS_REPORTER>( aFrame,
  59. _( "Loading Symbol Libraries" ),
  60. aNicknames.size(), true );
  61. }
  62. // Disable KIID generation: not needed for library parts; sometimes very slow
  63. KIID::CreateNilUuids( true );
  64. std::unordered_map<wxString, std::vector<LIB_SYMBOL*>> loadedSymbols;
  65. SYMBOL_ASYNC_LOADER loader( aNicknames, m_libs,
  66. GetFilter() == LIB_TREE_MODEL_ADAPTER::SYM_FILTER_POWER,
  67. &loadedSymbols, 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( loadedSymbols.size() > 0 )
  90. {
  91. COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
  92. PROJECT_FILE& project = aFrame->Prj().GetProjectFile();
  93. auto addFunc =
  94. [&]( const wxString& aLibName, 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 std::pair<const wxString, std::vector<LIB_SYMBOL*>>& pair : loadedSymbols )
  103. {
  104. SYMBOL_LIB_TABLE_ROW* row = m_libs->FindRow( pair.first );
  105. if( !row->GetIsVisible() )
  106. continue;
  107. std::vector<wxString> additionalColumns;
  108. row->GetAvailableSymbolFields( additionalColumns );
  109. for( const wxString& column : additionalColumns )
  110. addColumnIfNecessary( column );
  111. if( row->SupportsSubLibraries() )
  112. {
  113. std::vector<wxString> subLibraries;
  114. row->GetSubLibraryNames( subLibraries );
  115. wxString parentDesc = m_libs->GetDescription( pair.first );
  116. for( const wxString& lib : subLibraries )
  117. {
  118. wxString name = wxString::Format( wxT( "%s - %s" ), pair.first, lib );
  119. wxString desc;
  120. if( !parentDesc.IsEmpty() )
  121. desc = wxString::Format( wxT( "%s (%s)" ), parentDesc, lib );
  122. std::vector<LIB_SYMBOL*> symbols;
  123. std::copy_if( pair.second.begin(), pair.second.end(),
  124. std::back_inserter( symbols ),
  125. [&lib]( LIB_SYMBOL* aSym )
  126. {
  127. return lib.IsSameAs( aSym->GetLibId().GetSubLibraryName() );
  128. } );
  129. addFunc( name, symbols, desc );
  130. }
  131. }
  132. else
  133. {
  134. addFunc( pair.first, pair.second, m_libs->GetDescription( pair.first ) );
  135. }
  136. }
  137. }
  138. KIID::CreateNilUuids( false );
  139. m_tree.AssignIntrinsicRanks();
  140. if( progressReporter )
  141. {
  142. // Force immediate deletion of the APP_PROGRESS_DIALOG. Do not use Destroy(), or Destroy()
  143. // followed by wxSafeYield() because on Windows, APP_PROGRESS_DIALOG has some side effects
  144. // on the event loop manager.
  145. // One in particular is the call of ShowModal() following SYMBOL_TREE_MODEL_ADAPTER
  146. // creating a APP_PROGRESS_DIALOG (which has incorrect modal behaviour).
  147. progressReporter.reset();
  148. m_show_progress = false;
  149. }
  150. return !cancelled;
  151. }
  152. void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname, bool pinned )
  153. {
  154. bool onlyPowerSymbols = ( GetFilter() == SYM_FILTER_POWER );
  155. std::vector<LIB_SYMBOL*> symbols;
  156. std::vector<LIB_TREE_ITEM*> comp_list;
  157. try
  158. {
  159. m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
  160. }
  161. catch( const IO_ERROR& ioe )
  162. {
  163. wxLogError( _( "Error loading symbol library '%s'." ) + wxS( "\n%s" ),
  164. aLibNickname,
  165. ioe.What() );
  166. return;
  167. }
  168. if( symbols.size() > 0 )
  169. {
  170. comp_list.assign( symbols.begin(), symbols.end() );
  171. DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, pinned,
  172. false );
  173. }
  174. }
  175. wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
  176. {
  177. return GenerateAliasInfo( m_libs, aLibId, aUnit );
  178. }