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.

170 lines
5.1 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-2021 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 <widgets/progress_reporter.h>
  25. #include <dialogs/html_messagebox.h>
  26. #include <eda_pattern_match.h>
  27. #include <generate_alias_info.h>
  28. #include <lib_symbol.h>
  29. #include <locale_io.h>
  30. #include <symbol_async_loader.h>
  31. #include <symbol_lib_table.h>
  32. #include <symbol_tree_model_adapter.h>
  33. bool SYMBOL_TREE_MODEL_ADAPTER::m_show_progress = true;
  34. #define PROGRESS_INTERVAL_MILLIS 66
  35. wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
  36. SYMBOL_TREE_MODEL_ADAPTER::Create( EDA_BASE_FRAME* aParent, LIB_TABLE* aLibs )
  37. {
  38. auto* adapter = new SYMBOL_TREE_MODEL_ADAPTER( aParent, aLibs );
  39. return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
  40. }
  41. SYMBOL_TREE_MODEL_ADAPTER::SYMBOL_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent, LIB_TABLE* aLibs ) :
  42. LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
  43. m_libs( (SYMBOL_LIB_TABLE*) aLibs )
  44. {}
  45. SYMBOL_TREE_MODEL_ADAPTER::~SYMBOL_TREE_MODEL_ADAPTER()
  46. {}
  47. void SYMBOL_TREE_MODEL_ADAPTER::AddLibraries( const std::vector<wxString>& aNicknames,
  48. wxWindow* aParent )
  49. {
  50. std::unique_ptr<WX_PROGRESS_REPORTER> prg = nullptr;
  51. if( m_show_progress )
  52. {
  53. prg = std::make_unique<WX_PROGRESS_REPORTER>( aParent, _( "Loading Symbol Libraries" ),
  54. aNicknames.size(), true );
  55. }
  56. // Disable KIID generation: not needed for library parts; sometimes very slow
  57. KIID::CreateNilUuids( true );
  58. std::unordered_map<wxString, std::vector<LIB_SYMBOL*>> loadedSymbols;
  59. SYMBOL_ASYNC_LOADER loader( aNicknames, m_libs,
  60. GetFilter() == LIB_TREE_MODEL_ADAPTER::SYM_FILTER_POWER,
  61. &loadedSymbols, prg.get() );
  62. LOCALE_IO toggle;
  63. loader.Start();
  64. while( !loader.Done() )
  65. {
  66. if( prg )
  67. prg->KeepRefreshing();
  68. wxMilliSleep( PROGRESS_INTERVAL_MILLIS );
  69. }
  70. if( prg && prg->IsCancelled() )
  71. {
  72. loader.Abort();
  73. }
  74. else
  75. {
  76. loader.Join();
  77. }
  78. if( !loader.GetErrors().IsEmpty() )
  79. {
  80. HTML_MESSAGE_BOX dlg( aParent, _( "Load Error" ) );
  81. dlg.MessageSet( _( "Errors loading symbols:" ) );
  82. wxString msg = loader.GetErrors();
  83. msg.Replace( "\n", "<BR>" );
  84. dlg.AddHTML_Text( msg );
  85. dlg.ShowModal();
  86. }
  87. if( loadedSymbols.size() > 0 )
  88. {
  89. for( const std::pair<const wxString, std::vector<LIB_SYMBOL*>>& pair : loadedSymbols )
  90. {
  91. std::vector<LIB_TREE_ITEM*> treeItems( pair.second.begin(), pair.second.end() );
  92. DoAddLibrary( pair.first, m_libs->GetDescription( pair.first ), treeItems, false );
  93. }
  94. }
  95. KIID::CreateNilUuids( false );
  96. m_tree.AssignIntrinsicRanks();
  97. if( prg )
  98. {
  99. // Force immediate deletion of the APP_PROGRESS_DIALOG
  100. // ( do not use Destroy(), or use Destroy() followed by wxSafeYield() )
  101. // because on Windows, APP_PROGRESS_DIALOG has some side effects on the event loop
  102. // manager. A side effect is the call of ShowModal() of a dialog following
  103. // the use of SYMBOL_TREE_MODEL_ADAPTER creating a APP_PROGRESS_DIALOG
  104. // has a broken behavior (incorrect modal behavior).
  105. prg.reset();
  106. m_show_progress = false;
  107. }
  108. }
  109. void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname )
  110. {
  111. bool onlyPowerSymbols = ( GetFilter() == SYM_FILTER_POWER );
  112. std::vector<LIB_SYMBOL*> symbols;
  113. std::vector<LIB_TREE_ITEM*> comp_list;
  114. try
  115. {
  116. m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
  117. }
  118. catch( const IO_ERROR& ioe )
  119. {
  120. wxLogError( _( "Error loading symbol library '%s'." ) + wxS( "\n%s" ),
  121. aLibNickname,
  122. ioe.What() );
  123. return;
  124. }
  125. if( symbols.size() > 0 )
  126. {
  127. comp_list.assign( symbols.begin(), symbols.end() );
  128. DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, false );
  129. }
  130. }
  131. wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
  132. {
  133. return GenerateAliasInfo( m_libs, aLibId, aUnit );
  134. }