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_ASYNC_LOADER &aLoader, std::unique_ptr< 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 OnProgressTimer (wxTimerEvent &aEvent)
 
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
 
wxGauge * m_progress_ctrl
 
FOOTPRINT_CHOICEm_fp_sel_ctrl
 
wxSizer * m_sizer
 
wxSimplebook * m_book
 
std::unique_ptr< wxTimer > m_progress_timer
 
bool m_update
 
bool m_finished_loading
 
int m_max_items
 
wxString m_default_footprint
 
wxString m_other_footprint
 
int m_last_item
 
FOOTPRINT_ASYNC_LOADERm_fp_loader
 
std::unique_ptr< FOOTPRINT_LIST > & m_fp_list
 
FOOTPRINT_FILTER m_fp_filter
 
bool m_zero_filter
 

Detailed Description

Definition at line 45 of file footprint_select_widget.h.

Constructor & Destructor Documentation

FOOTPRINT_SELECT_WIDGET::FOOTPRINT_SELECT_WIDGET ( wxWindow *  aParent,
FOOTPRINT_ASYNC_LOADER aLoader,
std::unique_ptr< 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
aLoader- FOOTPRINT_ASYNC_LOADER instance
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 63 of file footprint_select_widget.cpp.

References m_book, m_fp_sel_ctrl, m_progress_ctrl, m_progress_timer, m_sizer, OnComboBox(), OnComboInteractive(), and OnProgressTimer().

66  : wxPanel( aParent ),
67  m_kiway( nullptr ),
68  m_update( aUpdate ),
69  m_finished_loading( false ),
70  m_max_items( aMaxItems ),
71  m_last_item( 0 ),
72  m_fp_loader( aLoader ),
73  m_fp_list( aFpList )
74 {
75  m_sizer = new wxBoxSizer( wxVERTICAL );
76  m_progress_timer = std::make_unique<wxTimer>( this );
77  m_book = new wxSimplebook( this, wxID_ANY );
78  m_progress_ctrl = new wxGauge( m_book, wxID_ANY, 100 );
79  m_fp_sel_ctrl = new FOOTPRINT_CHOICE( m_book, wxID_ANY );
80 
81  m_book->SetEffect( wxSHOW_EFFECT_BLEND );
82  m_book->AddPage( m_progress_ctrl, "", true );
83  m_book->AddPage( m_fp_sel_ctrl, "", false );
84  m_sizer->Add( m_book, 1, wxEXPAND | wxALL, 5 );
85 
86  SetSizer( m_sizer );
87  Layout();
88  m_sizer->Fit( this );
89 
90  Bind( wxEVT_TIMER, &FOOTPRINT_SELECT_WIDGET::OnProgressTimer, this, m_progress_timer->GetId() );
91  m_fp_sel_ctrl->Bind( wxEVT_COMBOBOX, &FOOTPRINT_SELECT_WIDGET::OnComboBox, this );
92  m_fp_sel_ctrl->Bind(
93  EVT_INTERACTIVE_CHOICE, &FOOTPRINT_SELECT_WIDGET::OnComboInteractive, this );
94 }
void OnComboInteractive(wxCommandEvent &aEvent)
FOOTPRINT_ASYNC_LOADER & m_fp_loader
std::unique_ptr< FOOTPRINT_LIST > & m_fp_list
Customized combo box for footprint selection.
void OnComboBox(wxCommandEvent &aEvent)
void OnProgressTimer(wxTimerEvent &aEvent)
std::unique_ptr< wxTimer > m_progress_timer
virtual FOOTPRINT_SELECT_WIDGET::~FOOTPRINT_SELECT_WIDGET ( )
inlinevirtual

Definition at line 71 of file footprint_select_widget.h.

72  {
73  }

Member Function Documentation

void FOOTPRINT_SELECT_WIDGET::ClearFilters ( )

Clear all filters.

Does not update the list.

Definition at line 226 of file footprint_select_widget.cpp.

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

Referenced by DIALOG_CHOOSE_COMPONENT::PopulateFootprintSelector().

227 {
229  m_default_footprint.Clear();
230  m_other_footprint.Clear();
231  m_zero_filter = false;
232 }
void ClearFilters()
Clear all filter criteria.
void FOOTPRINT_SELECT_WIDGET::DoOther ( )
private

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

Definition at line 188 of file footprint_select_widget.cpp.

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

Referenced by OnComboInteractive().

189 {
190  wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );
191 
192  wxString fpname = ShowPicker();
193  m_other_footprint = fpname;
194  UpdateList();
195  m_fp_sel_ctrl->SetSelection( POS_OTHER );
197 
198  evt.SetString( m_other_footprint );
199  wxPostEvent( this, evt );
200 }
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.
bool FOOTPRINT_SELECT_WIDGET::Enable ( bool  aEnable = true)
overridevirtual

Enable or disable the control for input.

Definition at line 309 of file footprint_select_widget.cpp.

References m_fp_sel_ctrl.

Referenced by DIALOG_CHOOSE_COMPONENT::PopulateFootprintSelector().

310 {
311  return m_fp_sel_ctrl->Enable( aEnable );
312 }
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 241 of file footprint_select_widget.cpp.

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

Referenced by DIALOG_CHOOSE_COMPONENT::PopulateFootprintSelector().

243 {
244  if( aZeroFilters && aFilters.size() == 0 )
245  m_zero_filter = true;
246  else
247  m_zero_filter = false;
248 
250 }
void FilterByFootprintFilters(wxArrayString const &aFilters)
Set a list of footprint filters to filter by.
void FOOTPRINT_SELECT_WIDGET::FilterByPinCount ( int  aPinCount)

Filter by pin count.

Does not update the list.

Definition at line 235 of file footprint_select_widget.cpp.

References FOOTPRINT_FILTER::FilterByPinCount(), and m_fp_filter.

Referenced by DIALOG_CHOOSE_COMPONENT::PopulateFootprintSelector().

236 {
237  m_fp_filter.FilterByPinCount( aPinCount );
238 }
void FilterByPinCount(int aPinCount)
Set a pin count to filter by.
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 97 of file footprint_select_widget.cpp.

References FOOTPRINT_LIST::GetInstance(), FOOTPRINT_ASYNC_LOADER::GetProgress(), FOOTPRINT_ASYNC_LOADER::IsSameTable(), m_fp_list, m_fp_loader, m_kiway, m_progress_timer, PROJECT::PcbFootprintLibs(), FOOTPRINT_ASYNC_LOADER::SetList(), and FOOTPRINT_ASYNC_LOADER::Start().

Referenced by DIALOG_CHOOSE_COMPONENT::OnInitDialog().

98 {
99  m_kiway = &aKiway;
100 
101  try
102  {
103  auto fp_lib_table = aProject.PcbFootprintLibs( aKiway );
104 
105  if( m_fp_loader.GetProgress() == 0 || !m_fp_loader.IsSameTable( fp_lib_table ) )
106  {
109  m_fp_loader.Start( fp_lib_table );
110  }
111 
112  m_progress_timer->Start( 200 );
113  }
114  catch( ... )
115  {
116  // no footprint libraries available
117  }
118 }
FOOTPRINT_ASYNC_LOADER & m_fp_loader
std::unique_ptr< FOOTPRINT_LIST > & m_fp_list
static std::unique_ptr< FOOTPRINT_LIST > GetInstance(KIWAY &aKiway)
Factory function to return a new FOOTPRINT_LIST via Kiway.
void Start(FP_LIB_TABLE *aTable, wxString const *aNickname=nullptr, unsigned aNThreads=DEFAULT_THREADS)
Launch the worker threads.
VTBL_ENTRY FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
int GetProgress() const
Get the current completion percentage.
void SetList(FOOTPRINT_LIST *aList)
Assign a FOOTPRINT_LIST to the loader.
std::unique_ptr< wxTimer > m_progress_timer
bool IsSameTable(FP_LIB_TABLE *aOther)
Return true if the given table is the same as the last table loaded.
void FOOTPRINT_SELECT_WIDGET::OnComboBox ( wxCommandEvent &  aEvent)
private

Definition at line 143 of file footprint_select_widget.cpp.

References m_fp_sel_ctrl, m_last_item, POS_OTHER, and POS_SEPARATOR.

Referenced by FOOTPRINT_SELECT_WIDGET().

144 {
145  wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );
146  int sel = m_fp_sel_ctrl->GetSelection();
147 
148  switch( sel )
149  {
150  case wxNOT_FOUND: return;
151 
152  case POS_SEPARATOR:
153  // User somehow managed to select the separator. This should not be
154  // possible, but just in case... deselect it
155  m_fp_sel_ctrl->SetSelection( m_last_item );
156  break;
157 
158  case POS_OTHER:
159  // When POS_OTHER is selected, a dialog should be shown. However, we don't want to
160  // do this ALL the time, as some times (e.g. when moving around with the arrow keys)
161  // it could be very annoying. Therefore showing the picker is done from the custom
162  // "interactive select" event on FOOTPRINT_CHOICE, which only fires for more direct
163  // choice actions.
164  break;
165 
166  default:
167  {
168  wxStringClientData* clientdata =
169  static_cast<wxStringClientData*>( m_fp_sel_ctrl->GetClientObject( sel ) );
170  wxASSERT( clientdata );
171 
172  evt.SetString( clientdata->GetData() );
173  wxPostEvent( this, evt );
174  }
175  }
176 }
void FOOTPRINT_SELECT_WIDGET::OnComboInteractive ( wxCommandEvent &  aEvent)
private

Definition at line 179 of file footprint_select_widget.cpp.

References DoOther(), m_fp_sel_ctrl, and POS_OTHER.

Referenced by FOOTPRINT_SELECT_WIDGET().

180 {
181  if( aEvent.GetInt() == POS_OTHER && !m_fp_sel_ctrl->IsPopupShown() )
182  {
183  DoOther();
184  }
185 }
void DoOther()
Handle activation of the "Other..." item.
void FOOTPRINT_SELECT_WIDGET::OnProgressTimer ( wxTimerEvent &  aEvent)
private

Definition at line 121 of file footprint_select_widget.cpp.

References FOOTPRINT_ASYNC_LOADER::GetProgress(), FOOTPRINT_ASYNC_LOADER::Join(), m_book, m_finished_loading, m_fp_filter, m_fp_list, m_fp_loader, m_progress_ctrl, m_progress_timer, m_update, PAGE_SELECT, FOOTPRINT_FILTER::SetList(), and UpdateList().

Referenced by FOOTPRINT_SELECT_WIDGET().

122 {
123  int prog = m_fp_loader.GetProgress();
124  m_progress_ctrl->SetValue( prog );
125 
126  if( prog == 100 )
127  {
128  wxBusyCursor busy;
129 
130  m_fp_loader.Join();
132  m_progress_timer->Stop();
133 
134  m_book->SetSelection( PAGE_SELECT );
135  m_finished_loading = true;
136 
137  if( m_update )
138  UpdateList();
139  }
140 }
void SetList(FOOTPRINT_LIST &aList)
Set the list to filter.
FOOTPRINT_ASYNC_LOADER & m_fp_loader
std::unique_ptr< FOOTPRINT_LIST > & m_fp_list
bool UpdateList()
Update the contents of the list to match the filters.
bool Join()
Wait until the worker threads are finished, and then perform any required single-threaded finishing o...
int GetProgress() const
Get the current completion percentage.
std::unique_ptr< wxTimer > m_progress_timer
void FOOTPRINT_SELECT_WIDGET::SelectDefault ( )

Set current selection to the default footprint.

Definition at line 303 of file footprint_select_widget.cpp.

References m_fp_sel_ctrl, and POS_DEFAULT.

Referenced by UpdateList().

304 {
305  m_fp_sel_ctrl->SetSelection( POS_DEFAULT );
306 }
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 253 of file footprint_select_widget.cpp.

References m_default_footprint.

Referenced by DIALOG_CHOOSE_COMPONENT::PopulateFootprintSelector().

254 {
255  m_default_footprint = aFp;
256 }
wxString FOOTPRINT_SELECT_WIDGET::ShowPicker ( )
private

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

Definition at line 203 of file footprint_select_widget.cpp.

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

Referenced by DoOther().

204 {
205  wxString fpname;
206  wxWindow* parent = ::wxGetTopLevelParent( this );
207  DIALOG_SHIM* dsparent = dynamic_cast<DIALOG_SHIM*>( parent );
208 
209  // Only quasimodal dialogs can launch modal kiface dialogs. Otherwise the
210  // event loop goes all silly.
211  wxASSERT( !dsparent || dsparent->IsQuasiModal() );
212 
213  auto frame = m_kiway->Player( FRAME_PCB_MODULE_VIEWER_MODAL, true );
214 
215  if( !frame->ShowModal( &fpname, parent ) )
216  {
217  fpname = wxEmptyString;
218  }
219 
220  frame->Destroy();
221 
222  return fpname;
223 }
Class DIALOG_SHIM may sit in the inheritance tree between wxDialog and any class written by wxFormBui...
Definition: dialog_shim.h:60
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, KIWAY_PLAYER *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:302
bool IsQuasiModal()
Definition: dialog_shim.h:93
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
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 259 of file footprint_select_widget.cpp.

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(), OnProgressTimer(), and DIALOG_CHOOSE_COMPONENT::PopulateFootprintSelector().

260 {
261  int n_items = 0;
262 
263  if( !m_fp_list || !m_finished_loading )
264  return false;
265 
266  wxWindowUpdateLocker lock( m_fp_sel_ctrl );
267  m_fp_sel_ctrl->Clear();
268 
269  // Be careful adding items! "Default" must occupy POS_DEFAULT,
270  // "Other" must occupy POS_OTHER, and the separator must occupy POS_SEPARATOR.
271 
272  m_fp_sel_ctrl->Append( m_default_footprint.IsEmpty() ?
273  _( "No default footprint" ) :
274  "[" + _( "Default" ) + "] " + m_default_footprint,
275  new wxStringClientData( m_default_footprint ) );
276 
277  m_fp_sel_ctrl->Append( m_other_footprint.IsEmpty() ?
278  _( "Other..." ) :
279  "[" + _( "Other..." ) + "] " + m_other_footprint,
280  new wxStringClientData( m_other_footprint ) );
281 
282  m_fp_sel_ctrl->Append( "", new wxStringClientData( "" ) );
283 
284  if( !m_zero_filter )
285  {
286  for( auto& fpinfo : m_fp_filter )
287  {
288  wxString display_name( fpinfo.GetNickname() + ":" + fpinfo.GetFootprintName() );
289 
290  m_fp_sel_ctrl->Append( display_name, new wxStringClientData( display_name ) );
291  ++n_items;
292 
293  if( n_items >= m_max_items )
294  break;
295  }
296  }
297 
298  SelectDefault();
299  return true;
300 }
std::unique_ptr< FOOTPRINT_LIST > & m_fp_list
void SelectDefault()
Set current selection to the default footprint.

Member Data Documentation

wxSimplebook* FOOTPRINT_SELECT_WIDGET::m_book
private

Definition at line 137 of file footprint_select_widget.h.

Referenced by FOOTPRINT_SELECT_WIDGET(), and OnProgressTimer().

wxString FOOTPRINT_SELECT_WIDGET::m_default_footprint
private

Definition at line 144 of file footprint_select_widget.h.

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

bool FOOTPRINT_SELECT_WIDGET::m_finished_loading
private

Definition at line 142 of file footprint_select_widget.h.

Referenced by OnProgressTimer(), and UpdateList().

FOOTPRINT_FILTER FOOTPRINT_SELECT_WIDGET::m_fp_filter
private
std::unique_ptr<FOOTPRINT_LIST>& FOOTPRINT_SELECT_WIDGET::m_fp_list
private

Definition at line 149 of file footprint_select_widget.h.

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

FOOTPRINT_ASYNC_LOADER& FOOTPRINT_SELECT_WIDGET::m_fp_loader
private

Definition at line 148 of file footprint_select_widget.h.

Referenced by Load(), and OnProgressTimer().

FOOTPRINT_CHOICE* FOOTPRINT_SELECT_WIDGET::m_fp_sel_ctrl
private
KIWAY* FOOTPRINT_SELECT_WIDGET::m_kiway
private

Definition at line 133 of file footprint_select_widget.h.

Referenced by Load(), and ShowPicker().

int FOOTPRINT_SELECT_WIDGET::m_last_item
private

Definition at line 146 of file footprint_select_widget.h.

Referenced by DoOther(), and OnComboBox().

int FOOTPRINT_SELECT_WIDGET::m_max_items
private

Definition at line 143 of file footprint_select_widget.h.

Referenced by UpdateList().

wxString FOOTPRINT_SELECT_WIDGET::m_other_footprint
private

Definition at line 145 of file footprint_select_widget.h.

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

wxGauge* FOOTPRINT_SELECT_WIDGET::m_progress_ctrl
private

Definition at line 134 of file footprint_select_widget.h.

Referenced by FOOTPRINT_SELECT_WIDGET(), and OnProgressTimer().

std::unique_ptr<wxTimer> FOOTPRINT_SELECT_WIDGET::m_progress_timer
private

Definition at line 139 of file footprint_select_widget.h.

Referenced by FOOTPRINT_SELECT_WIDGET(), Load(), and OnProgressTimer().

wxSizer* FOOTPRINT_SELECT_WIDGET::m_sizer
private

Definition at line 136 of file footprint_select_widget.h.

Referenced by FOOTPRINT_SELECT_WIDGET().

bool FOOTPRINT_SELECT_WIDGET::m_update
private

Definition at line 141 of file footprint_select_widget.h.

Referenced by OnProgressTimer().

bool FOOTPRINT_SELECT_WIDGET::m_zero_filter
private

Definition at line 151 of file footprint_select_widget.h.

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


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