KiCad PCB EDA Suite
footprint_select_widget.cpp
Go to the documentation of this file.
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 
20 #include <eda_draw_frame.h>
21 #include <kiway.h>
22 #include <kiway_player.h>
23 #include <project.h>
26 #include <progress_reporter.h>
27 #include <footprint_info_impl.h>
28 #include <wx/wupdlock.h>
29 
30 
31 extern FOOTPRINT_LIST_IMPL GFootprintList; // KIFACE scope.
32 
33 wxDEFINE_EVENT( EVT_FOOTPRINT_SELECTED, wxCommandEvent );
34 
36  FOOTPRINT_LIST* aFpList, bool aUpdate,
37  int aMaxItems ) :
38  wxPanel( aParent ),
39  m_update( aUpdate ),
40  m_max_items( aMaxItems ),
41  m_fp_list( aFpList )
42 {
43  m_zero_filter = true;
44  m_sizer = new wxBoxSizer( wxVERTICAL );
45  m_fp_sel_ctrl = new FOOTPRINT_CHOICE( this, wxID_ANY );
46  m_sizer->Add( m_fp_sel_ctrl, 1, wxEXPAND, 5 );
47 
48  SetSizer( m_sizer );
49  Layout();
50  m_sizer->Fit( this );
51 
52  m_fp_sel_ctrl->Bind( wxEVT_COMBOBOX, &FOOTPRINT_SELECT_WIDGET::OnComboBox, this );
53 }
54 
55 
56 void FOOTPRINT_SELECT_WIDGET::Load( KIWAY& aKiway, PROJECT& aProject )
57 {
58  try
59  {
61 
62  if( m_fp_list->GetCount() == 0 )
63  {
64  // If the fp-info-cache is empty (or, more likely, hasn't been created in a new
65  // project yet), load footprints the hard way.
66  FP_LIB_TABLE* fpTable = aProject.PcbFootprintLibs( aKiway );
67  static_cast<FOOTPRINT_LIST_IMPL*>( m_fp_list )->ReadFootprintFiles( fpTable );
68  }
69 
71  }
72  catch( ... )
73  {
74  // no footprint libraries available
75  }
76 
77  if( m_update )
78  UpdateList();
79 }
80 
81 
82 void FOOTPRINT_SELECT_WIDGET::OnComboBox( wxCommandEvent& aEvent )
83 {
84  wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );
85  int sel = m_fp_sel_ctrl->GetSelection();
86 
87  if( sel == wxNOT_FOUND )
88  return;
89 
90  wxStringClientData* clientdata =
91  static_cast<wxStringClientData*>( m_fp_sel_ctrl->GetClientObject( sel ) );
92  wxASSERT( clientdata );
93 
94  evt.SetString( clientdata->GetData() );
95  wxPostEvent( this, evt );
96 }
97 
98 
100 {
102  m_default_footprint.Clear();
103  m_zero_filter = false;
104 }
105 
106 
108 {
109  m_fp_filter.FilterByPinCount( aPinCount );
110 }
111 
112 
113 void FOOTPRINT_SELECT_WIDGET::FilterByFootprintFilters( wxArrayString const& aFilters,
114  bool aZeroFilters )
115 {
116  m_zero_filter = ( aZeroFilters && aFilters.size() == 0 );
118 }
119 
120 
122 {
123  m_default_footprint = aFp;
124 }
125 
126 
128 {
129  int n_items = 0;
130 
131  if( !m_fp_list )
132  return false;
133 
134  wxWindowUpdateLocker lock( m_fp_sel_ctrl );
135  m_fp_sel_ctrl->Clear();
136 
137  // Be careful adding items! "Default" must occupy POS_DEFAULT,
138  // "Other" must occupy POS_OTHER, and the separator must occupy POS_SEPARATOR.
139 
140  m_fp_sel_ctrl->Append( m_default_footprint.IsEmpty() ?
141  _( "No default footprint" ) :
142  wxT( "[" ) + _( "Default" ) + "] " + m_default_footprint,
143  new wxStringClientData( m_default_footprint ) );
144 
145  if( !m_zero_filter )
146  {
147  for( FOOTPRINT_INFO& fpinfo : m_fp_filter )
148  {
149  wxString display_name( fpinfo.GetLibNickname() + wxT( ":" ) + fpinfo.GetFootprintName() );
150 
151  m_fp_sel_ctrl->Append( display_name, new wxStringClientData( display_name ) );
152  ++n_items;
153 
154  if( n_items >= m_max_items )
155  break;
156  }
157  }
158 
159  SelectDefault();
160  return true;
161 }
162 
163 
165 {
166  m_fp_sel_ctrl->SetSelection( 0 );
167 }
168 
169 
171 {
172  return m_fp_sel_ctrl->Enable( aEnable );
173 }
void SetList(FOOTPRINT_LIST &aList)
Set the list to filter.
Container for project specific data.
Definition: project.h:62
void ClearFilters()
Clear all filter criteria.
FOOTPRINT_LIST_IMPL GFootprintList
The global footprint info table.
Definition: cvpcb.cpp:140
Customized combo box for footprint selection.
wxDEFINE_EVENT(EVT_FOOTPRINT_SELECTED, wxCommandEvent)
The base class for create windows for drawing purpose.
void ClearFilters()
Clear all filters.
bool UpdateList()
Update the contents of the list to match the filters.
void FilterByPinCount(int aPinCount)
Set a pin count to filter by.
static FOOTPRINT_LIST * GetInstance(KIWAY &aKiway)
Factory function to return a FOOTPRINT_LIST via Kiway.
unsigned GetCount() const
void FilterByFootprintFilters(const wxArrayString &aFilters, bool aZeroFilters)
Filter by footprint filter list.
void SetDefaultFootprint(const wxString &aFp)
Set the default footprint for a part.
void OnComboBox(wxCommandEvent &aEvent)
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:260
virtual bool Enable(bool aEnable=true) override
Enable or disable the control for input.
#define _(s)
virtual FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:283
void SelectDefault()
Set current selection to the default footprint.
Holds a list of FOOTPRINT_INFO objects, along with a list of IO_ERRORs or PARSE_ERRORs that were thro...
void FilterByFootprintFilters(const wxArrayString &aFilters)
Set a list of footprint filters to filter by.
FOOTPRINT_SELECT_WIDGET(EDA_DRAW_FRAME *aFrame, wxWindow *aParent, FOOTPRINT_LIST *aFpList, bool aUpdate=true, int aMaxItems=400)
Construct a footprint selector widget.
void Load(KIWAY &aKiway, PROJECT &aProject)
Start loading.
void FilterByPinCount(int aPinCount)
Filter by pin count.