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.

84 lines
2.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018-2021 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 <wx/tokenzr.h>
  20. #include <kicad_string.h>
  21. #include <eda_pattern_match.h>
  22. #include <fp_lib_table.h>
  23. #include <footprint_info.h>
  24. #include <footprint_info_impl.h>
  25. #include <generate_footprint_info.h>
  26. #include <wx/settings.h>
  27. #include "fp_tree_model_adapter.h"
  28. wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
  29. FP_TREE_MODEL_ADAPTER::Create( EDA_BASE_FRAME* aParent, LIB_TABLE* aLibs )
  30. {
  31. auto* adapter = new FP_TREE_MODEL_ADAPTER( aParent, aLibs );
  32. return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
  33. }
  34. FP_TREE_MODEL_ADAPTER::FP_TREE_MODEL_ADAPTER( EDA_BASE_FRAME* aParent, LIB_TABLE* aLibs ) :
  35. LIB_TREE_MODEL_ADAPTER( aParent, "pinned_footprint_libs" ),
  36. m_libs( (FP_LIB_TABLE*) aLibs )
  37. {}
  38. void FP_TREE_MODEL_ADAPTER::AddLibraries()
  39. {
  40. for( const wxString& libName : m_libs->GetLogicalLibs() )
  41. {
  42. const FP_LIB_TABLE_ROW* library = m_libs->FindRow( libName, true );
  43. DoAddLibrary( libName, library->GetDescr(), getFootprints( libName ), true );
  44. }
  45. m_tree.AssignIntrinsicRanks();
  46. }
  47. std::vector<LIB_TREE_ITEM*> FP_TREE_MODEL_ADAPTER::getFootprints( const wxString& aLibName )
  48. {
  49. std::vector<LIB_TREE_ITEM*> libList;
  50. auto fullListStart = GFootprintList.GetList().begin();
  51. auto fullListEnd = GFootprintList.GetList().end();
  52. std::unique_ptr<FOOTPRINT_INFO> dummy = std::make_unique<FOOTPRINT_INFO_IMPL>( aLibName, wxEmptyString );
  53. // List is sorted, so use a binary search to find the range of footnotes for our library
  54. auto libBounds = std::equal_range( fullListStart, fullListEnd, dummy,
  55. []( const std::unique_ptr<FOOTPRINT_INFO>& a,
  56. const std::unique_ptr<FOOTPRINT_INFO>& b )
  57. {
  58. return StrNumCmp( a->GetLibNickname(), b->GetLibNickname(), false ) < 0;
  59. } );
  60. for( auto i = libBounds.first; i != libBounds.second; ++i )
  61. libList.push_back( i->get() );
  62. return libList;
  63. }
  64. wxString FP_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
  65. {
  66. return GenerateFootprintInfo( m_libs, aLibId );
  67. }