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.

176 lines
4.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017-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 <eda_draw_frame.h>
  20. #include <kiway.h>
  21. #include <kiway_player.h>
  22. #include <project.h>
  23. #include <widgets/footprint_choice.h>
  24. #include <widgets/footprint_select_widget.h>
  25. #include <widgets/progress_reporter.h>
  26. #include <footprint_info_impl.h>
  27. #include <wx/wupdlock.h>
  28. extern FOOTPRINT_LIST_IMPL GFootprintList; // KIFACE scope.
  29. wxDEFINE_EVENT( EVT_FOOTPRINT_SELECTED, wxCommandEvent );
  30. FOOTPRINT_SELECT_WIDGET::FOOTPRINT_SELECT_WIDGET( EDA_DRAW_FRAME* aFrame, wxWindow* aParent,
  31. FOOTPRINT_LIST* aFpList, bool aUpdate,
  32. int aMaxItems ) :
  33. wxPanel( aParent ),
  34. m_kiway( nullptr ),
  35. m_update( aUpdate ),
  36. m_max_items( aMaxItems ),
  37. m_fp_list( aFpList )
  38. {
  39. m_zero_filter = true;
  40. m_sizer = new wxBoxSizer( wxVERTICAL );
  41. m_fp_sel_ctrl = new FOOTPRINT_CHOICE( this, wxID_ANY );
  42. m_sizer->Add( m_fp_sel_ctrl, 1, wxEXPAND, 5 );
  43. SetSizer( m_sizer );
  44. Layout();
  45. m_sizer->Fit( this );
  46. m_fp_sel_ctrl->Bind( wxEVT_COMBOBOX, &FOOTPRINT_SELECT_WIDGET::OnComboBox, this );
  47. }
  48. void FOOTPRINT_SELECT_WIDGET::Load( KIWAY& aKiway, PROJECT& aProject )
  49. {
  50. m_kiway = &aKiway;
  51. try
  52. {
  53. m_fp_list = FOOTPRINT_LIST::GetInstance( aKiway );
  54. if( m_fp_list->GetCount() == 0 )
  55. {
  56. // If the fp-info-cache is empty (or, more likely, hasn't been created in a new
  57. // project yet), load footprints the hard way.
  58. FP_LIB_TABLE* fpTable = aProject.PcbFootprintLibs( aKiway );
  59. static_cast<FOOTPRINT_LIST_IMPL*>( m_fp_list )->ReadFootprintFiles( fpTable );
  60. }
  61. m_fp_filter.SetList( *m_fp_list );
  62. }
  63. catch( ... )
  64. {
  65. // no footprint libraries available
  66. }
  67. if( m_update )
  68. UpdateList();
  69. }
  70. void FOOTPRINT_SELECT_WIDGET::OnComboBox( wxCommandEvent& aEvent )
  71. {
  72. wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );
  73. int sel = m_fp_sel_ctrl->GetSelection();
  74. if( sel == wxNOT_FOUND )
  75. return;
  76. wxStringClientData* clientdata =
  77. static_cast<wxStringClientData*>( m_fp_sel_ctrl->GetClientObject( sel ) );
  78. wxASSERT( clientdata );
  79. evt.SetString( clientdata->GetData() );
  80. wxPostEvent( this, evt );
  81. }
  82. void FOOTPRINT_SELECT_WIDGET::ClearFilters()
  83. {
  84. m_fp_filter.ClearFilters();
  85. m_default_footprint.Clear();
  86. m_zero_filter = false;
  87. }
  88. void FOOTPRINT_SELECT_WIDGET::FilterByPinCount( int aPinCount )
  89. {
  90. m_fp_filter.FilterByPinCount( aPinCount );
  91. }
  92. void FOOTPRINT_SELECT_WIDGET::FilterByFootprintFilters( wxArrayString const& aFilters,
  93. bool aZeroFilters )
  94. {
  95. m_zero_filter = ( aZeroFilters && aFilters.size() == 0 );
  96. m_fp_filter.FilterByFootprintFilters( aFilters );
  97. }
  98. void FOOTPRINT_SELECT_WIDGET::SetDefaultFootprint( wxString const& aFp )
  99. {
  100. m_default_footprint = aFp;
  101. }
  102. bool FOOTPRINT_SELECT_WIDGET::UpdateList()
  103. {
  104. int n_items = 0;
  105. if( !m_fp_list )
  106. return false;
  107. wxWindowUpdateLocker lock( m_fp_sel_ctrl );
  108. m_fp_sel_ctrl->Clear();
  109. // Be careful adding items! "Default" must occupy POS_DEFAULT,
  110. // "Other" must occupy POS_OTHER, and the separator must occupy POS_SEPARATOR.
  111. m_fp_sel_ctrl->Append( m_default_footprint.IsEmpty() ?
  112. _( "No default footprint" ) :
  113. "[" + _( "Default" ) + "] " + m_default_footprint,
  114. new wxStringClientData( m_default_footprint ) );
  115. if( !m_zero_filter )
  116. {
  117. for( FOOTPRINT_INFO& fpinfo : m_fp_filter )
  118. {
  119. wxString display_name( fpinfo.GetLibNickname() + ":" + fpinfo.GetFootprintName() );
  120. m_fp_sel_ctrl->Append( display_name, new wxStringClientData( display_name ) );
  121. ++n_items;
  122. if( n_items >= m_max_items )
  123. break;
  124. }
  125. }
  126. SelectDefault();
  127. return true;
  128. }
  129. void FOOTPRINT_SELECT_WIDGET::SelectDefault()
  130. {
  131. m_fp_sel_ctrl->SetSelection( 0 );
  132. }
  133. bool FOOTPRINT_SELECT_WIDGET::Enable( bool aEnable )
  134. {
  135. return m_fp_sel_ctrl->Enable( aEnable );
  136. }