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.

129 lines
4.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-2019 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/tokenzr.h>
  22. #include <wx/window.h>
  23. #include <widgets/app_progress_dialog.h>
  24. #include <eda_pattern_match.h>
  25. #include <symbol_lib_table.h>
  26. #include <class_libentry.h>
  27. #include <generate_alias_info.h>
  28. #include <symbol_tree_model_adapter.h>
  29. bool SYMBOL_TREE_MODEL_ADAPTER::m_show_progress = true;
  30. #define PROGRESS_INTERVAL_MILLIS 66
  31. wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
  32. SYMBOL_TREE_MODEL_ADAPTER::Create( EDA_BASE_FRAME* aParent, LIB_TABLE* aLibs )
  33. {
  34. auto* adapter = new SYMBOL_TREE_MODEL_ADAPTER( aParent, aLibs );
  35. return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
  36. }
  37. SYMBOL_TREE_MODEL_ADAPTER::SYMBOL_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent, LIB_TABLE* aLibs ) :
  38. LIB_TREE_MODEL_ADAPTER( aParent, "pinned_symbol_libs" ),
  39. m_libs( (SYMBOL_LIB_TABLE*) aLibs )
  40. {}
  41. SYMBOL_TREE_MODEL_ADAPTER::~SYMBOL_TREE_MODEL_ADAPTER()
  42. {}
  43. void SYMBOL_TREE_MODEL_ADAPTER::AddLibraries( const std::vector<wxString>& aNicknames,
  44. wxWindow* aParent )
  45. {
  46. APP_PROGRESS_DIALOG* prg = nullptr;
  47. wxLongLong nextUpdate = wxGetUTCTimeMillis() + (PROGRESS_INTERVAL_MILLIS / 2);
  48. if( m_show_progress )
  49. {
  50. prg = new APP_PROGRESS_DIALOG( _( "Loading Symbol Libraries" ), wxEmptyString,
  51. aNicknames.size(), aParent );
  52. }
  53. unsigned int ii = 0;
  54. for( const auto& nickname : aNicknames )
  55. {
  56. if( prg && wxGetUTCTimeMillis() > nextUpdate )
  57. {
  58. prg->Update( ii, wxString::Format( _( "Loading library \"%s\"" ), nickname ) );
  59. nextUpdate = wxGetUTCTimeMillis() + PROGRESS_INTERVAL_MILLIS;
  60. }
  61. AddLibrary( nickname );
  62. ii++;
  63. }
  64. m_tree.AssignIntrinsicRanks();
  65. if( prg )
  66. {
  67. // Force immediate deletion of the APP_PROGRESS_DIALOG
  68. // ( do not use Destroy(), or use Destroy() followed by wxSafeYield() )
  69. // because on Windows, APP_PROGRESS_DIALOG has some side effects on the event loop
  70. // manager. A side effect is the call of ShowModal() of a dialog following
  71. // the use of SYMBOL_TREE_MODEL_ADAPTER creating a APP_PROGRESS_DIALOG
  72. // has a broken behavior (incorrect modal behavior).
  73. delete prg;
  74. m_show_progress = false;
  75. }
  76. }
  77. void SYMBOL_TREE_MODEL_ADAPTER::AddLibrary( wxString const& aLibNickname )
  78. {
  79. bool onlyPowerSymbols = ( GetFilter() == CMP_FILTER_POWER );
  80. std::vector<LIB_PART*> symbols;
  81. std::vector<LIB_TREE_ITEM*> comp_list;
  82. try
  83. {
  84. m_libs->LoadSymbolLib( symbols, aLibNickname, onlyPowerSymbols );
  85. }
  86. catch( const IO_ERROR& ioe )
  87. {
  88. wxLogError( wxString::Format( _( "Error loading symbol library %s.\n\n%s" ),
  89. aLibNickname,
  90. ioe.What() ) );
  91. return;
  92. }
  93. if( symbols.size() > 0 )
  94. {
  95. comp_list.assign( symbols.begin(), symbols.end() );
  96. DoAddLibrary( aLibNickname, m_libs->GetDescription( aLibNickname ), comp_list, false );
  97. }
  98. }
  99. wxString SYMBOL_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
  100. {
  101. return GenerateAliasInfo( m_libs, aLibId, aUnit );
  102. }