KiCad PCB EDA Suite
FOOTPRINT_SELECT_WIDGET Class Reference

#include <footprint_select_widget.h>

Inheritance diagram for FOOTPRINT_SELECT_WIDGET:

Public Member Functions

 FOOTPRINT_SELECT_WIDGET (wxWindow *aParent, FOOTPRINT_LIST *aFpList, bool aUpdate=true, int aMaxItems=400)
 Construct a footprint selector widget. More...
 
virtual ~FOOTPRINT_SELECT_WIDGET ()
 
void Load (KIWAY &aKiway, PROJECT &aProject)
 Start loading. More...
 
void ClearFilters ()
 Clear all filters. More...
 
void FilterByPinCount (int aPinCount)
 Filter by pin count. More...
 
void FilterByFootprintFilters (wxArrayString const &aFilters, bool aZeroFilters)
 Filter by footprint filter list. More...
 
void SetDefaultFootprint (wxString const &aFp)
 Set the default footprint for a part. More...
 
bool UpdateList ()
 Update the contents of the list to match the filters. More...
 
void SelectDefault ()
 Set current selection to the default footprint. More...
 
virtual bool Enable (bool aEnable=true) override
 Enable or disable the control for input. More...
 

Private Member Functions

void FootprintsLoaded ()
 
void OnComboBox (wxCommandEvent &aEvent)
 
void OnComboInteractive (wxCommandEvent &aEvent)
 
wxString ShowPicker ()
 Show the component picker and return the selected component. Used by DoOther() More...
 
void DoOther ()
 Handle activation of the "Other..." item. More...
 

Private Attributes

KIWAYm_kiway
 
FOOTPRINT_CHOICEm_fp_sel_ctrl
 
wxSizer * m_sizer
 
bool m_update
 
bool m_finished_loading
 
int m_max_items
 
wxString m_default_footprint
 
wxString m_other_footprint
 
int m_last_item
 
FOOTPRINT_LISTm_fp_list
 
FOOTPRINT_FILTER m_fp_filter
 
bool m_zero_filter
 

Detailed Description

Definition at line 40 of file footprint_select_widget.h.

Constructor & Destructor Documentation

◆ FOOTPRINT_SELECT_WIDGET()

FOOTPRINT_SELECT_WIDGET::FOOTPRINT_SELECT_WIDGET ( wxWindow *  aParent,
FOOTPRINT_LIST aFpList,
bool  aUpdate = true,
int  aMaxItems = 400 
)

Construct a footprint selector widget.

This requires references to an external footprint loader, and an external unique_ptr-to-FOOTPRINT_LIST. The latter will be populated with a FOOTPRINT_LIST instance the first time Load() is called.

The reason for this is that footprint loading tends to be very expensive, especially when using online libraries. The caller is expected to keep these objects around (e.g. they may be statics on the dialog this FOOTPRINT_SELECT_WIDGET is created in) so footprints do not have to be loaded more than once.

Parameters
aParent- parent window
aFpList- FOOTPRINT_LIST container
aUpdate- whether to call UpdateList() automatically when finished loading
aMaxItems- maximum number of filter items to display, in addition to Default and Other

Definition at line 44 of file footprint_select_widget.cpp.

45  :
46  wxPanel( aParent ),
47  m_kiway( nullptr ),
48  m_update( aUpdate ),
49  m_finished_loading( false ),
50  m_max_items( aMaxItems ),
51  m_last_item( 0 ),
52  m_fp_list( aFpList )
53 {
54  m_zero_filter = true;
55  m_sizer = new wxBoxSizer( wxVERTICAL );
56  m_fp_sel_ctrl = new FOOTPRINT_CHOICE( this, wxID_ANY );
57  m_sizer->Add( m_fp_sel_ctrl, 1, wxEXPAND, 5 );
58 
59  SetSizer( m_sizer );
60  Layout();
61  m_sizer->Fit( this );
62 
63  m_fp_sel_ctrl->Bind( wxEVT_COMBOBOX, &FOOTPRINT_SELECT_WIDGET::OnComboBox, this );
64  m_fp_sel_ctrl->Bind( EVT_INTERACTIVE_CHOICE, &FOOTPRINT_SELECT_WIDGET::OnComboInteractive, this );
65 }
void OnComboInteractive(wxCommandEvent &aEvent)
Customized combo box for footprint selection.
void OnComboBox(wxCommandEvent &aEvent)

References m_fp_sel_ctrl, m_sizer, m_zero_filter, OnComboBox(), and OnComboInteractive().

◆ ~FOOTPRINT_SELECT_WIDGET()

virtual FOOTPRINT_SELECT_WIDGET::~FOOTPRINT_SELECT_WIDGET ( )
inlinevirtual

Definition at line 65 of file footprint_select_widget.h.

66  {
67  }

Member Function Documentation

◆ ClearFilters()

void FOOTPRINT_SELECT_WIDGET::ClearFilters ( )

Clear all filters.

Does not update the list.

Definition at line 182 of file footprint_select_widget.cpp.

183 {
185  m_default_footprint.Clear();
186  m_other_footprint.Clear();
187  m_zero_filter = false;
188 }
void ClearFilters()
Clear all filter criteria.

References FOOTPRINT_FILTER::ClearFilters(), m_default_footprint, m_fp_filter, m_other_footprint, and m_zero_filter.

Referenced by DIALOG_CHOOSE_COMPONENT::PopulateFootprintSelector().

◆ DoOther()

void FOOTPRINT_SELECT_WIDGET::DoOther ( )
private

Handle activation of the "Other..." item.

Definition at line 144 of file footprint_select_widget.cpp.

145 {
146  wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );
147 
148  wxString fpname = ShowPicker();
149  m_other_footprint = fpname;
150  UpdateList();
151  m_fp_sel_ctrl->SetSelection( POS_OTHER );
153 
154  evt.SetString( m_other_footprint );
155  wxPostEvent( this, evt );
156 }
wxString ShowPicker()
Show the component picker and return the selected component. Used by DoOther()
bool UpdateList()
Update the contents of the list to match the filters.

References m_fp_sel_ctrl, m_last_item, m_other_footprint, POS_OTHER, ShowPicker(), and UpdateList().

Referenced by OnComboInteractive().

◆ Enable()

bool FOOTPRINT_SELECT_WIDGET::Enable ( bool  aEnable = true)
overridevirtual

Enable or disable the control for input.

Definition at line 261 of file footprint_select_widget.cpp.

262 {
263  return m_fp_sel_ctrl->Enable( aEnable );
264 }

References m_fp_sel_ctrl.

Referenced by DIALOG_CHOOSE_COMPONENT::PopulateFootprintSelector().

◆ FilterByFootprintFilters()

void FOOTPRINT_SELECT_WIDGET::FilterByFootprintFilters ( wxArrayString const &  aFilters,
bool  aZeroFilters 
)

Filter by footprint filter list.

Does not update the list.

Parameters
aFiltersis a wxArrayString of strings used to filter the list of available footprints (can be empty). The final fp list is the list of footprint names matching at least one filtering string. A filtering string is something like sm0402 or DIP*
aZeroFilters- if true, zero filters = zero footprints. If false, zero filters = not filtering.

Definition at line 197 of file footprint_select_widget.cpp.

199 {
200  m_zero_filter = ( aZeroFilters && aFilters.size() == 0 );
202 }
void FilterByFootprintFilters(wxArrayString const &aFilters)
Set a list of footprint filters to filter by.

References FOOTPRINT_FILTER::FilterByFootprintFilters(), m_fp_filter, and m_zero_filter.

Referenced by DIALOG_CHOOSE_COMPONENT::PopulateFootprintSelector().

◆ FilterByPinCount()

void FOOTPRINT_SELECT_WIDGET::FilterByPinCount ( int  aPinCount)

Filter by pin count.

Does not update the list.

Definition at line 191 of file footprint_select_widget.cpp.

192 {
193  m_fp_filter.FilterByPinCount( aPinCount );
194 }
void FilterByPinCount(int aPinCount)
Set a pin count to filter by.

References FOOTPRINT_FILTER::FilterByPinCount(), and m_fp_filter.

Referenced by DIALOG_CHOOSE_COMPONENT::PopulateFootprintSelector().

◆ FootprintsLoaded()

void FOOTPRINT_SELECT_WIDGET::FootprintsLoaded ( )
private

Definition at line 88 of file footprint_select_widget.cpp.

89 {
91 
92  m_finished_loading = true;
93 
94  if( m_update )
95  UpdateList();
96 }
void SetList(FOOTPRINT_LIST &aList)
Set the list to filter.
bool UpdateList()
Update the contents of the list to match the filters.

References m_finished_loading, m_fp_filter, m_fp_list, m_update, FOOTPRINT_FILTER::SetList(), and UpdateList().

Referenced by Load().

◆ Load()

void FOOTPRINT_SELECT_WIDGET::Load ( KIWAY aKiway,
PROJECT aProject 
)

Start loading.

This function returns immediately; footprints will continue to load in the background.

Parameters
aKiway- active kiway instance. This is cached for use when "Other" is selected.
aProject- current project

Definition at line 68 of file footprint_select_widget.cpp.

69 {
70  m_kiway = &aKiway;
71 
72  try
73  {
74  auto fp_lib_table = aProject.PcbFootprintLibs( aKiway );
76 
77  WX_PROGRESS_REPORTER progressReporter( this, _( "Loading Footprint Libraries" ), 2 );
78  m_fp_list->ReadFootprintFiles( fp_lib_table, nullptr, &progressReporter );
80  }
81  catch( ... )
82  {
83  // no footprint libraries available
84  }
85 }
static FOOTPRINT_LIST * GetInstance(KIWAY &aKiway)
Factory function to return a FOOTPRINT_LIST via Kiway.
virtual bool ReadFootprintFiles(FP_LIB_TABLE *aTable, const wxString *aNickname=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)=0
Read all the footprints provided by the combination of aTable and aNickname.
VTBL_ENTRY FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:285
#define _(s)
Definition: 3d_actions.cpp:33

References _, FootprintsLoaded(), FOOTPRINT_LIST::GetInstance(), m_fp_list, m_kiway, PROJECT::PcbFootprintLibs(), and FOOTPRINT_LIST::ReadFootprintFiles().

Referenced by DIALOG_CHOOSE_COMPONENT::OnInitDialog().

◆ OnComboBox()

void FOOTPRINT_SELECT_WIDGET::OnComboBox ( wxCommandEvent &  aEvent)
private

Definition at line 99 of file footprint_select_widget.cpp.

100 {
101  wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );
102  int sel = m_fp_sel_ctrl->GetSelection();
103 
104  switch( sel )
105  {
106  case wxNOT_FOUND: return;
107 
108  case POS_SEPARATOR:
109  // User somehow managed to select the separator. This should not be
110  // possible, but just in case... deselect it
111  m_fp_sel_ctrl->SetSelection( m_last_item );
112  break;
113 
114  case POS_OTHER:
115  // When POS_OTHER is selected, a dialog should be shown. However, we don't want to
116  // do this ALL the time, as some times (e.g. when moving around with the arrow keys)
117  // it could be very annoying. Therefore showing the picker is done from the custom
118  // "interactive select" event on FOOTPRINT_CHOICE, which only fires for more direct
119  // choice actions.
120  break;
121 
122  default:
123  {
124  wxStringClientData* clientdata =
125  static_cast<wxStringClientData*>( m_fp_sel_ctrl->GetClientObject( sel ) );
126  wxASSERT( clientdata );
127 
128  evt.SetString( clientdata->GetData() );
129  wxPostEvent( this, evt );
130  }
131  }
132 }

References m_fp_sel_ctrl, m_last_item, POS_OTHER, and POS_SEPARATOR.

Referenced by FOOTPRINT_SELECT_WIDGET().

◆ OnComboInteractive()

void FOOTPRINT_SELECT_WIDGET::OnComboInteractive ( wxCommandEvent &  aEvent)
private

Definition at line 135 of file footprint_select_widget.cpp.

136 {
137  if( aEvent.GetInt() == POS_OTHER && !m_fp_sel_ctrl->IsPopupShown() )
138  {
139  DoOther();
140  }
141 }
void DoOther()
Handle activation of the "Other..." item.

References DoOther(), m_fp_sel_ctrl, and POS_OTHER.

Referenced by FOOTPRINT_SELECT_WIDGET().

◆ SelectDefault()

void FOOTPRINT_SELECT_WIDGET::SelectDefault ( )

Set current selection to the default footprint.

Definition at line 255 of file footprint_select_widget.cpp.

256 {
257  m_fp_sel_ctrl->SetSelection( POS_DEFAULT );
258 }

References m_fp_sel_ctrl, and POS_DEFAULT.

Referenced by UpdateList().

◆ SetDefaultFootprint()

void FOOTPRINT_SELECT_WIDGET::SetDefaultFootprint ( wxString const &  aFp)

Set the default footprint for a part.

This will be listed at the top. May be an empty string.

Definition at line 205 of file footprint_select_widget.cpp.

206 {
207  m_default_footprint = aFp;
208 }

References m_default_footprint.

Referenced by DIALOG_CHOOSE_COMPONENT::PopulateFootprintSelector().

◆ ShowPicker()

wxString FOOTPRINT_SELECT_WIDGET::ShowPicker ( )
private

Show the component picker and return the selected component. Used by DoOther()

Definition at line 159 of file footprint_select_widget.cpp.

160 {
161  wxString fpname;
162  wxWindow* parent = ::wxGetTopLevelParent( this );
163  DIALOG_SHIM* dsparent = dynamic_cast<DIALOG_SHIM*>( parent );
164 
165  // Only quasimodal dialogs can launch modal kiface dialogs. Otherwise the
166  // event loop goes all silly.
167  wxASSERT( !dsparent || dsparent->IsQuasiModal() );
168 
169  auto frame = m_kiway->Player( FRAME_FOOTPRINT_VIEWER_MODAL, true );
170 
171  if( !frame->ShowModal( &fpname, parent ) )
172  {
173  fpname = wxEmptyString;
174  }
175 
176  frame->Destroy();
177 
178  return fpname;
179 }
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:83
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:342
bool IsQuasiModal()
Definition: dialog_shim.h:123
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.

References KIWAY_PLAYER::Destroy(), FRAME_FOOTPRINT_VIEWER_MODAL, DIALOG_SHIM::IsQuasiModal(), m_kiway, and KIWAY::Player().

Referenced by DoOther().

◆ UpdateList()

bool FOOTPRINT_SELECT_WIDGET::UpdateList ( )

Update the contents of the list to match the filters.

Has no effect if the footprint list has not been loaded yet. The "default" footprint will be selected.

Returns
true if the footprint list has been loaded (and the list was updated)

Definition at line 211 of file footprint_select_widget.cpp.

212 {
213  int n_items = 0;
214 
215  if( !m_fp_list || !m_finished_loading )
216  return false;
217 
218  wxWindowUpdateLocker lock( m_fp_sel_ctrl );
219  m_fp_sel_ctrl->Clear();
220 
221  // Be careful adding items! "Default" must occupy POS_DEFAULT,
222  // "Other" must occupy POS_OTHER, and the separator must occupy POS_SEPARATOR.
223 
224  m_fp_sel_ctrl->Append( m_default_footprint.IsEmpty() ?
225  _( "No default footprint" ) :
226  "[" + _( "Default" ) + "] " + m_default_footprint,
227  new wxStringClientData( m_default_footprint ) );
228 
229  m_fp_sel_ctrl->Append( m_other_footprint.IsEmpty() ?
230  _( "Other..." ) :
231  "[" + _( "Other..." ) + "] " + m_other_footprint,
232  new wxStringClientData( m_other_footprint ) );
233 
234  m_fp_sel_ctrl->Append( "", new wxStringClientData( "" ) );
235 
236  if( !m_zero_filter )
237  {
238  for( auto& fpinfo : m_fp_filter )
239  {
240  wxString display_name( fpinfo.GetLibNickname() + ":" + fpinfo.GetFootprintName() );
241 
242  m_fp_sel_ctrl->Append( display_name, new wxStringClientData( display_name ) );
243  ++n_items;
244 
245  if( n_items >= m_max_items )
246  break;
247  }
248  }
249 
250  SelectDefault();
251  return true;
252 }
void SelectDefault()
Set current selection to the default footprint.
#define _(s)
Definition: 3d_actions.cpp:33

References _, m_default_footprint, m_finished_loading, m_fp_filter, m_fp_list, m_fp_sel_ctrl, m_max_items, m_other_footprint, m_zero_filter, and SelectDefault().

Referenced by DoOther(), FootprintsLoaded(), and DIALOG_CHOOSE_COMPONENT::PopulateFootprintSelector().

Member Data Documentation

◆ m_default_footprint

wxString FOOTPRINT_SELECT_WIDGET::m_default_footprint
private

Definition at line 134 of file footprint_select_widget.h.

Referenced by ClearFilters(), SetDefaultFootprint(), and UpdateList().

◆ m_finished_loading

bool FOOTPRINT_SELECT_WIDGET::m_finished_loading
private

Definition at line 132 of file footprint_select_widget.h.

Referenced by FootprintsLoaded(), and UpdateList().

◆ m_fp_filter

FOOTPRINT_FILTER FOOTPRINT_SELECT_WIDGET::m_fp_filter
private

◆ m_fp_list

FOOTPRINT_LIST* FOOTPRINT_SELECT_WIDGET::m_fp_list
private

Definition at line 138 of file footprint_select_widget.h.

Referenced by FootprintsLoaded(), Load(), and UpdateList().

◆ m_fp_sel_ctrl

FOOTPRINT_CHOICE* FOOTPRINT_SELECT_WIDGET::m_fp_sel_ctrl
private

◆ m_kiway

KIWAY* FOOTPRINT_SELECT_WIDGET::m_kiway
private

Definition at line 127 of file footprint_select_widget.h.

Referenced by Load(), and ShowPicker().

◆ m_last_item

int FOOTPRINT_SELECT_WIDGET::m_last_item
private

Definition at line 136 of file footprint_select_widget.h.

Referenced by DoOther(), and OnComboBox().

◆ m_max_items

int FOOTPRINT_SELECT_WIDGET::m_max_items
private

Definition at line 133 of file footprint_select_widget.h.

Referenced by UpdateList().

◆ m_other_footprint

wxString FOOTPRINT_SELECT_WIDGET::m_other_footprint
private

Definition at line 135 of file footprint_select_widget.h.

Referenced by ClearFilters(), DoOther(), and UpdateList().

◆ m_sizer

wxSizer* FOOTPRINT_SELECT_WIDGET::m_sizer
private

Definition at line 129 of file footprint_select_widget.h.

Referenced by FOOTPRINT_SELECT_WIDGET().

◆ m_update

bool FOOTPRINT_SELECT_WIDGET::m_update
private

Definition at line 131 of file footprint_select_widget.h.

Referenced by FootprintsLoaded().

◆ m_zero_filter

bool FOOTPRINT_SELECT_WIDGET::m_zero_filter
private

The documentation for this class was generated from the following files: