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 The 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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <eda_draw_frame.h>
22#include <kiway.h>
25#include <set>
26#include <wx/wupdlock.h>
27
28
29wxDEFINE_EVENT( EVT_FOOTPRINT_SELECTED, wxCommandEvent );
30
31FOOTPRINT_SELECT_WIDGET::FOOTPRINT_SELECT_WIDGET( EDA_DRAW_FRAME* aFrame, wxWindow* aParent, int aMaxItems ) :
32 wxPanel( aParent ),
33 m_max_items( aMaxItems ),
34 m_pin_count( 0 ),
35 m_zero_filter( true ),
36 m_kiway( nullptr )
37{
38 m_sizer = new wxBoxSizer( wxVERTICAL );
39 m_fp_sel_ctrl = new FOOTPRINT_CHOICE( this, wxID_ANY );
40 m_sizer->Add( m_fp_sel_ctrl, 1, wxEXPAND, 5 );
41
42 SetSizer( m_sizer );
43 Layout();
44 m_sizer->Fit( this );
45
46 m_fp_sel_ctrl->Bind( wxEVT_COMBOBOX, &FOOTPRINT_SELECT_WIDGET::OnComboBox, this );
47}
48
49
51{
52 m_kiway = &aKiway;
53}
54
55
56void FOOTPRINT_SELECT_WIDGET::OnComboBox( wxCommandEvent& aEvent )
57{
58 wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );
59 int sel = m_fp_sel_ctrl->GetSelection();
60
61 if( sel == wxNOT_FOUND )
62 return;
63
64 wxStringClientData* clientdata = static_cast<wxStringClientData*>( m_fp_sel_ctrl->GetClientObject( sel ) );
65 wxASSERT( clientdata );
66
67 evt.SetString( clientdata->GetData() );
68 wxPostEvent( this, evt );
69}
70
71
73{
74 m_pin_count = 0;
75 m_filters.Clear();
76 m_default_footprint.Clear();
77 m_zero_filter = false;
78 m_always_included.clear();
79}
80
81
83{
84 if( !aFootprintId.IsValid() )
85 return;
86
87 for( const LIB_ID& existing : m_always_included )
88 {
89 if( existing == aFootprintId )
90 return;
91 }
92
93 m_always_included.push_back( aFootprintId );
94}
95
96
98{
99 m_pin_count = aPinCount;
100}
101
102
103void FOOTPRINT_SELECT_WIDGET::FilterByFootprintFilters( wxArrayString const& aFilters, bool aZeroFilters )
104{
105 m_zero_filter = ( aZeroFilters && aFilters.size() == 0 );
106 m_filters = aFilters;
107}
108
109
111{
113}
114
115
117{
118 if( !m_kiway )
119 return false;
120
121 wxWindowUpdateLocker lock( m_fp_sel_ctrl );
122 m_fp_sel_ctrl->Clear();
123
124 // Add the default footprint entry at the top
125 wxString defaultLabel = m_default_footprint.IsEmpty()
126 ? _( "No default footprint" )
127 : wxS( "[" ) + _( "Default" ) + wxS( "] " ) + m_default_footprint;
128
129 m_fp_sel_ctrl->Append( defaultLabel, new wxStringClientData( m_default_footprint ) );
130
131 // Explicitly associated footprints are listed first and always shown, bypassing the
132 // pin-count and glob filters (issue #2282). Track their names so the filtered results below
133 // can be deduplicated against them.
134 std::set<wxString> alwaysIncludedNames;
135
136 for( const LIB_ID& fpId : m_always_included )
137 {
138 wxString fpName = fpId.Format().wx_str();
139
140 if( alwaysIncludedNames.insert( fpName ).second )
141 m_fp_sel_ctrl->Append( fpName, new wxStringClientData( fpName ) );
142 }
143
144 // If zero_filter is set and we have no filters, show only the always-included footprints
145 if( m_zero_filter && m_filters.IsEmpty() && m_pin_count == 0 )
146 {
148 return true;
149 }
150
152 query.m_Patterns.assign( m_filters.begin(), m_filters.end() );
153 query.m_PinCount = m_pin_count;
156
158
159 // The query fails when the PCB kiface is unavailable. Then the list is just the default.
160 if( matchResult.m_Success )
161 {
162 const auto& matches = matchResult.m_MatchingNames;
163
164 for( const wxString& fpName : matches )
165 {
166 if( alwaysIncludedNames.count( fpName ) )
167 continue;
168
169 m_fp_sel_ctrl->Append( fpName, new wxStringClientData( fpName ) );
170 }
171 }
172
174 return true;
175}
176
177
179{
180 m_fp_sel_ctrl->SetSelection( 0 );
181}
182
183
185{
186 return m_fp_sel_ctrl->Enable( aEnable );
187}
The base class for create windows for drawing purpose.
Customized combo box for footprint selection.
virtual bool Enable(bool aEnable=true) override
Enable or disable the control for input.
std::vector< LIB_ID > m_always_included
void OnComboBox(wxCommandEvent &aEvent)
void AddAlwaysIncludedFootprint(const LIB_ID &aFootprintId)
Add a footprint that is always listed regardless of the pin-count and glob filters (issue #2282).
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, 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 by fetching filtered footprints from pcbnew.
void ClearFilters()
Clear all filters.
void Load(KIWAY &aKiway, PROJECT &aProject)
Initialize the widget with a KIWAY for cross-module communication.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:348
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
bool IsValid() const
Check if this LID_ID is valid.
Definition lib_id.h:168
Container for project specific data.
Definition project.h:63
#define _(s)
FOOTPRINT_MATCH_RESULT QueryMatchingFootprints(KIWAY &aKiway, const FOOTPRINT_MATCH_QUERY &aQuery)
Ask the footprint libraries, for the footprints matching a query (over the Pcbnew kiface).
wxDEFINE_EVENT(EVT_FOOTPRINT_SELECTED, wxCommandEvent)
All the information needed to query the footprint libraries for matching footprints.
std::vector< wxString > m_Patterns
Wildcard patterns matched against the footprint name, or against "library:footprint" when a pattern c...
bool m_ZeroFilters
When true, a query with no patterns (and no pin count) matches nothing rather than everything.
std::optional< int > m_PinCount
When greater than zero, only footprints with this many pads are returned.
int m_MaxResults
Limits the number of returned footprints; zero means no limit.
The outcome of a footprint match query, and the JSON encoding used to move it across the Pcbnew kifac...
std::vector< wxString > m_MatchingNames
The matching "library:footprint" names.
bool m_Success
False if the query could not be performed, in which case MatchingNames is empty.