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 
27 #include <wx/wupdlock.h>
29 
30 
31 wxDEFINE_EVENT( EVT_FOOTPRINT_SELECTED, wxCommandEvent );
32 
33 
35  FOOTPRINT_LIST* aFpList, bool aUpdate,
36  int aMaxItems ) :
37  wxPanel( aParent ),
38  m_kiway( nullptr ),
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  m_kiway = &aKiway;
59 
60  try
61  {
64  }
65  catch( ... )
66  {
67  // no footprint libraries available
68  }
69 
70  if( m_update )
71  UpdateList();
72 }
73 
74 
75 void FOOTPRINT_SELECT_WIDGET::OnComboBox( wxCommandEvent& aEvent )
76 {
77  wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );
78  int sel = m_fp_sel_ctrl->GetSelection();
79 
80  if( sel == wxNOT_FOUND )
81  return;
82 
83  wxStringClientData* clientdata =
84  static_cast<wxStringClientData*>( m_fp_sel_ctrl->GetClientObject( sel ) );
85  wxASSERT( clientdata );
86 
87  evt.SetString( clientdata->GetData() );
88  wxPostEvent( this, evt );
89 }
90 
91 
93 {
95  m_default_footprint.Clear();
96  m_zero_filter = false;
97 }
98 
99 
101 {
102  m_fp_filter.FilterByPinCount( aPinCount );
103 }
104 
105 
106 void FOOTPRINT_SELECT_WIDGET::FilterByFootprintFilters( wxArrayString const& aFilters,
107  bool aZeroFilters )
108 {
109  m_zero_filter = ( aZeroFilters && aFilters.size() == 0 );
111 }
112 
113 
115 {
116  m_default_footprint = aFp;
117 }
118 
119 
121 {
122  int n_items = 0;
123 
124  if( !m_fp_list )
125  return false;
126 
127  wxWindowUpdateLocker lock( m_fp_sel_ctrl );
128  m_fp_sel_ctrl->Clear();
129 
130  // Be careful adding items! "Default" must occupy POS_DEFAULT,
131  // "Other" must occupy POS_OTHER, and the separator must occupy POS_SEPARATOR.
132 
133  m_fp_sel_ctrl->Append( m_default_footprint.IsEmpty() ?
134  _( "No default footprint" ) :
135  "[" + _( "Default" ) + "] " + m_default_footprint,
136  new wxStringClientData( m_default_footprint ) );
137 
138  if( !m_zero_filter )
139  {
140  for( auto& fpinfo : m_fp_filter )
141  {
142  wxString display_name( fpinfo.GetLibNickname() + ":" + fpinfo.GetFootprintName() );
143 
144  m_fp_sel_ctrl->Append( display_name, new wxStringClientData( display_name ) );
145  ++n_items;
146 
147  if( n_items >= m_max_items )
148  break;
149  }
150  }
151 
152  SelectDefault();
153  return true;
154 }
155 
156 
158 {
159  m_fp_sel_ctrl->SetSelection( 0 );
160 }
161 
162 
164 {
165  return m_fp_sel_ctrl->Enable( aEnable );
166 }
void SetList(FOOTPRINT_LIST &aList)
Set the list to filter.
Container for project specific data.
Definition: project.h:62
void FilterByFootprintFilters(wxArrayString const &aFilters, bool aZeroFilters)
Filter by footprint filter list.
void FilterByFootprintFilters(wxArrayString const &aFilters)
Set a list of footprint filters to filter by.
void ClearFilters()
Clear all filter criteria.
void SetDefaultFootprint(wxString const &aFp)
Set the default footprint for a part.
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.
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.
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...
#define _(s)
Definition: 3d_actions.cpp:33
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.