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{
60 try
61 {
63
64 if( m_fp_list->GetCount() == 0 )
65 {
66 // If the fp-info-cache is empty (or, more likely, hasn't been created in a new
67 // project yet), load footprints the hard way.
68 FP_LIB_TABLE* fpTable = aProject.PcbFootprintLibs( aKiway );
69 WX_PROGRESS_REPORTER* progressReporter =
70 new WX_PROGRESS_REPORTER( m_frame, _( "Loading Footprint Libraries" ), 3 );
71 static_cast<FOOTPRINT_LIST_IMPL*>( m_fp_list )
72 ->ReadFootprintFiles( fpTable, nullptr, progressReporter );
73
74 delete progressReporter;
75 }
76
78 }
79 catch( ... )
80 {
81 // no footprint libraries available
82 }
83
84 if( m_update )
85 UpdateList();
86}
87
88
89void FOOTPRINT_SELECT_WIDGET::OnComboBox( wxCommandEvent& aEvent )
90{
91 wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );
92 int sel = m_fp_sel_ctrl->GetSelection();
93
94 if( sel == wxNOT_FOUND )
95 return;
96
97 wxStringClientData* clientdata =
98 static_cast<wxStringClientData*>( m_fp_sel_ctrl->GetClientObject( sel ) );
99 wxASSERT( clientdata );
100
101 evt.SetString( clientdata->GetData() );
102 wxPostEvent( this, evt );
103}
104
105
107{
109 m_default_footprint.Clear();
110 m_zero_filter = false;
111}
112
113
115{
116 m_fp_filter.FilterByPinCount( aPinCount );
117}
118
119
120void FOOTPRINT_SELECT_WIDGET::FilterByFootprintFilters( wxArrayString const& aFilters,
121 bool aZeroFilters )
122{
123 m_zero_filter = ( aZeroFilters && aFilters.size() == 0 );
125}
126
127
129{
131}
132
133
135{
136 int n_items = 0;
137
138 if( !m_fp_list )
139 return false;
140
141 wxWindowUpdateLocker lock( m_fp_sel_ctrl );
142 m_fp_sel_ctrl->Clear();
143
144 // Be careful adding items! "Default" must occupy POS_DEFAULT,
145 // "Other" must occupy POS_OTHER, and the separator must occupy POS_SEPARATOR.
146
147 m_fp_sel_ctrl->Append( m_default_footprint.IsEmpty() ?
148 _( "No default footprint" ) :
149 wxS( "[" ) + _( "Default" ) + wxS( "] " ) + m_default_footprint,
150 new wxStringClientData( m_default_footprint ) );
151
152 if( !m_zero_filter )
153 {
154 for( FOOTPRINT_INFO& fpinfo : m_fp_filter )
155 {
156 wxString display_name( fpinfo.GetLibNickname() + wxS( ":" ) + fpinfo.GetFootprintName() );
157
158 m_fp_sel_ctrl->Append( display_name, new wxStringClientData( display_name ) );
159 ++n_items;
160
161 if( n_items >= m_max_items )
162 break;
163 }
164 }
165
167 return true;
168}
169
170
172{
173 m_fp_sel_ctrl->SetSelection( 0 );
174}
175
176
178{
179 return m_fp_sel_ctrl->Enable( aEnable );
180}
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.
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:279
Container for project specific data.
Definition: project.h:62
virtual FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:339
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