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_kiway( nullptr ),
40  m_update( aUpdate ),
41  m_max_items( aMaxItems ),
42  m_fp_list( aFpList )
43 {
44  m_zero_filter = true;
45  m_sizer = new wxBoxSizer( wxVERTICAL );
46  m_fp_sel_ctrl = new FOOTPRINT_CHOICE( this, wxID_ANY );
47  m_sizer->Add( m_fp_sel_ctrl, 1, wxEXPAND, 5 );
48 
49  SetSizer( m_sizer );
50  Layout();
51  m_sizer->Fit( this );
52 
53  m_fp_sel_ctrl->Bind( wxEVT_COMBOBOX, &FOOTPRINT_SELECT_WIDGET::OnComboBox, this );
54 }
55 
56 
57 void FOOTPRINT_SELECT_WIDGET::Load( KIWAY& aKiway, PROJECT& aProject )
58 {
59  m_kiway = &aKiway;
60 
61  try
62  {
64 
65  if( m_fp_list->GetCount() == 0 )
66  {
67  // If the fp-info-cache is empty (or, more likely, hasn't been created in a new
68  // project yet), load footprints the hard way.
69  FP_LIB_TABLE* fpTable = aProject.PcbFootprintLibs( aKiway );
70  static_cast<FOOTPRINT_LIST_IMPL*>( m_fp_list )->ReadFootprintFiles( fpTable );
71  }
72 
74  }
75  catch( ... )
76  {
77  // no footprint libraries available
78  }
79 
80  if( m_update )
81  UpdateList();
82 }
83 
84 
85 void FOOTPRINT_SELECT_WIDGET::OnComboBox( wxCommandEvent& aEvent )
86 {
87  wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );
88  int sel = m_fp_sel_ctrl->GetSelection();
89 
90  if( sel == wxNOT_FOUND )
91  return;
92 
93  wxStringClientData* clientdata =
94  static_cast<wxStringClientData*>( m_fp_sel_ctrl->GetClientObject( sel ) );
95  wxASSERT( clientdata );
96 
97  evt.SetString( clientdata->GetData() );
98  wxPostEvent( this, evt );
99 }
100 
101 
103 {
105  m_default_footprint.Clear();
106  m_zero_filter = false;
107 }
108 
109 
111 {
112  m_fp_filter.FilterByPinCount( aPinCount );
113 }
114 
115 
116 void FOOTPRINT_SELECT_WIDGET::FilterByFootprintFilters( wxArrayString const& aFilters,
117  bool aZeroFilters )
118 {
119  m_zero_filter = ( aZeroFilters && aFilters.size() == 0 );
121 }
122 
123 
125 {
126  m_default_footprint = aFp;
127 }
128 
129 
131 {
132  int n_items = 0;
133 
134  if( !m_fp_list )
135  return false;
136 
137  wxWindowUpdateLocker lock( m_fp_sel_ctrl );
138  m_fp_sel_ctrl->Clear();
139 
140  // Be careful adding items! "Default" must occupy POS_DEFAULT,
141  // "Other" must occupy POS_OTHER, and the separator must occupy POS_SEPARATOR.
142 
143  m_fp_sel_ctrl->Append( m_default_footprint.IsEmpty() ?
144  _( "No default footprint" ) :
145  "[" + _( "Default" ) + "] " + m_default_footprint,
146  new wxStringClientData( m_default_footprint ) );
147 
148  if( !m_zero_filter )
149  {
150  for( FOOTPRINT_INFO& fpinfo : m_fp_filter )
151  {
152  wxString display_name( fpinfo.GetLibNickname() + ":" + fpinfo.GetFootprintName() );
153 
154  m_fp_sel_ctrl->Append( display_name, new wxStringClientData( display_name ) );
155  ++n_items;
156 
157  if( n_items >= m_max_items )
158  break;
159  }
160  }
161 
162  SelectDefault();
163  return true;
164 }
165 
166 
168 {
169  m_fp_sel_ctrl->SetSelection( 0 );
170 }
171 
172 
174 {
175  return m_fp_sel_ctrl->Enable( aEnable );
176 }
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.