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.

78 lines
2.5 KiB

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