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.

104 lines
3.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018-2023 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <pgm_base.h>
  20. #include <eda_base_frame.h>
  21. #include <core/kicad_algo.h>
  22. #include <settings/common_settings.h>
  23. #include <project/project_file.h>
  24. #include <wx/tokenzr.h>
  25. #include <string_utils.h>
  26. #include <eda_pattern_match.h>
  27. #include <fp_lib_table.h>
  28. #include <footprint_info.h>
  29. #include <footprint_info_impl.h>
  30. #include <generate_footprint_info.h>
  31. #include <wx/settings.h>
  32. #include "fp_tree_model_adapter.h"
  33. wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
  34. FP_TREE_MODEL_ADAPTER::Create( EDA_BASE_FRAME* aParent, LIB_TABLE* aLibs )
  35. {
  36. auto* adapter = new FP_TREE_MODEL_ADAPTER( aParent, aLibs );
  37. return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
  38. }
  39. FP_TREE_MODEL_ADAPTER::FP_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent, LIB_TABLE* aLibs ) :
  40. LIB_TREE_MODEL_ADAPTER( aParent, wxT( "pinned_footprint_libs" ) ),
  41. m_libs( (FP_LIB_TABLE*) aLibs )
  42. {}
  43. void FP_TREE_MODEL_ADAPTER::AddLibraries( EDA_BASE_FRAME* aParent )
  44. {
  45. COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
  46. PROJECT_FILE& project = aParent->Prj().GetProjectFile();
  47. for( const wxString& libName : m_libs->GetLogicalLibs() )
  48. {
  49. const FP_LIB_TABLE_ROW* library = nullptr;
  50. try
  51. {
  52. library = m_libs->FindRow( libName, true );
  53. }
  54. catch( ... )
  55. {
  56. // Skip loading this library, if not exists/ not found
  57. continue;
  58. }
  59. bool pinned = alg::contains( cfg->m_Session.pinned_fp_libs, libName )
  60. || alg::contains( project.m_PinnedFootprintLibs, libName );
  61. DoAddLibrary( libName, library->GetDescr(), getFootprints( libName ), pinned, true );
  62. }
  63. m_tree.AssignIntrinsicRanks();
  64. }
  65. std::vector<LIB_TREE_ITEM*> FP_TREE_MODEL_ADAPTER::getFootprints( const wxString& aLibName )
  66. {
  67. std::vector<LIB_TREE_ITEM*> libList;
  68. auto fullListStart = GFootprintList.GetList().begin();
  69. auto fullListEnd = GFootprintList.GetList().end();
  70. std::unique_ptr<FOOTPRINT_INFO> dummy = std::make_unique<FOOTPRINT_INFO_IMPL>( aLibName, wxEmptyString );
  71. // List is sorted, so use a binary search to find the range of footnotes for our library
  72. auto libBounds = std::equal_range( fullListStart, fullListEnd, dummy,
  73. []( const std::unique_ptr<FOOTPRINT_INFO>& a,
  74. const std::unique_ptr<FOOTPRINT_INFO>& b )
  75. {
  76. return StrNumCmp( a->GetLibNickname(), b->GetLibNickname(), false ) < 0;
  77. } );
  78. for( auto i = libBounds.first; i != libBounds.second; ++i )
  79. libList.push_back( i->get() );
  80. return libList;
  81. }
  82. wxString FP_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
  83. {
  84. return GenerateFootprintInfo( m_libs, aLibId );
  85. }