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.

180 lines
5.2 KiB

3 years ago
3 years ago
  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/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. 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. WX_PROGRESS_REPORTER* progressReporter =
  60. new WX_PROGRESS_REPORTER( m_frame, _( "Loading Footprint Libraries" ), 3 );
  61. static_cast<FOOTPRINT_LIST_IMPL*>( m_fp_list )
  62. ->ReadFootprintFiles( fpTable, nullptr, progressReporter );
  63. delete progressReporter;
  64. }
  65. m_fp_filter.SetList( *m_fp_list );
  66. }
  67. catch( ... )
  68. {
  69. // no footprint libraries available
  70. }
  71. if( m_update )
  72. UpdateList();
  73. }
  74. void FOOTPRINT_SELECT_WIDGET::OnComboBox( wxCommandEvent& aEvent )
  75. {
  76. wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );
  77. int sel = m_fp_sel_ctrl->GetSelection();
  78. if( sel == wxNOT_FOUND )
  79. return;
  80. wxStringClientData* clientdata =
  81. static_cast<wxStringClientData*>( m_fp_sel_ctrl->GetClientObject( sel ) );
  82. wxASSERT( clientdata );
  83. evt.SetString( clientdata->GetData() );
  84. wxPostEvent( this, evt );
  85. }
  86. void FOOTPRINT_SELECT_WIDGET::ClearFilters()
  87. {
  88. m_fp_filter.ClearFilters();
  89. m_default_footprint.Clear();
  90. m_zero_filter = false;
  91. }
  92. void FOOTPRINT_SELECT_WIDGET::FilterByPinCount( int aPinCount )
  93. {
  94. m_fp_filter.FilterByPinCount( aPinCount );
  95. }
  96. void FOOTPRINT_SELECT_WIDGET::FilterByFootprintFilters( wxArrayString const& aFilters,
  97. bool aZeroFilters )
  98. {
  99. m_zero_filter = ( aZeroFilters && aFilters.size() == 0 );
  100. m_fp_filter.FilterByFootprintFilters( aFilters );
  101. }
  102. void FOOTPRINT_SELECT_WIDGET::SetDefaultFootprint( wxString const& aFp )
  103. {
  104. m_default_footprint = aFp;
  105. }
  106. bool FOOTPRINT_SELECT_WIDGET::UpdateList()
  107. {
  108. int n_items = 0;
  109. if( !m_fp_list )
  110. return false;
  111. wxWindowUpdateLocker lock( m_fp_sel_ctrl );
  112. m_fp_sel_ctrl->Clear();
  113. // Be careful adding items! "Default" must occupy POS_DEFAULT,
  114. // "Other" must occupy POS_OTHER, and the separator must occupy POS_SEPARATOR.
  115. m_fp_sel_ctrl->Append( m_default_footprint.IsEmpty() ?
  116. _( "No default footprint" ) :
  117. wxS( "[" ) + _( "Default" ) + wxS( "] " ) + m_default_footprint,
  118. new wxStringClientData( m_default_footprint ) );
  119. if( !m_zero_filter )
  120. {
  121. for( FOOTPRINT_INFO& fpinfo : m_fp_filter )
  122. {
  123. wxString display_name( fpinfo.GetLibNickname() + wxS( ":" ) + fpinfo.GetFootprintName() );
  124. m_fp_sel_ctrl->Append( display_name, new wxStringClientData( display_name ) );
  125. ++n_items;
  126. if( n_items >= m_max_items )
  127. break;
  128. }
  129. }
  130. SelectDefault();
  131. return true;
  132. }
  133. void FOOTPRINT_SELECT_WIDGET::SelectDefault()
  134. {
  135. m_fp_sel_ctrl->SetSelection( 0 );
  136. }
  137. bool FOOTPRINT_SELECT_WIDGET::Enable( bool aEnable )
  138. {
  139. return m_fp_sel_ctrl->Enable( aEnable );
  140. }