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.

174 lines
5.2 KiB

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