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