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 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 <kiface_ids.h>
25#include <nlohmann/json.hpp>
26#include <wx/wupdlock.h>
27
28
29wxDEFINE_EVENT( EVT_FOOTPRINT_SELECTED, wxCommandEvent );
30
32 int aMaxItems ) :
33 wxPanel( aParent ),
34 m_max_items( aMaxItems ),
35 m_pin_count( 0 ),
36 m_zero_filter( true ),
37 m_kiway( nullptr )
38{
39 m_sizer = new wxBoxSizer( wxVERTICAL );
40 m_fp_sel_ctrl = new FOOTPRINT_CHOICE( this, wxID_ANY );
41 m_sizer->Add( m_fp_sel_ctrl, 1, wxEXPAND, 5 );
42
43 SetSizer( m_sizer );
44 Layout();
45 m_sizer->Fit( this );
46
47 m_fp_sel_ctrl->Bind( wxEVT_COMBOBOX, &FOOTPRINT_SELECT_WIDGET::OnComboBox, this );
48}
49
50
52{
53 m_kiway = &aKiway;
54}
55
56
57void FOOTPRINT_SELECT_WIDGET::OnComboBox( wxCommandEvent& aEvent )
58{
59 wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );
60 int sel = m_fp_sel_ctrl->GetSelection();
61
62 if( sel == wxNOT_FOUND )
63 return;
64
65 wxStringClientData* clientdata =
66 static_cast<wxStringClientData*>( m_fp_sel_ctrl->GetClientObject( sel ) );
67 wxASSERT( clientdata );
68
69 evt.SetString( clientdata->GetData() );
70 wxPostEvent( this, evt );
71}
72
73
75{
76 m_pin_count = 0;
77 m_filters.Clear();
78 m_default_footprint.Clear();
79 m_zero_filter = false;
80}
81
82
84{
85 m_pin_count = aPinCount;
86}
87
88
89void FOOTPRINT_SELECT_WIDGET::FilterByFootprintFilters( wxArrayString const& aFilters,
90 bool aZeroFilters )
91{
92 m_zero_filter = ( aZeroFilters && aFilters.size() == 0 );
93 m_filters = aFilters;
94}
95
96
98{
100}
101
102
104{
105 if( !m_kiway )
106 return false;
107
108 wxWindowUpdateLocker lock( m_fp_sel_ctrl );
109 m_fp_sel_ctrl->Clear();
110
111 // Add the default footprint entry at the top
112 wxString defaultLabel = m_default_footprint.IsEmpty()
113 ? _( "No default footprint" )
114 : wxS( "[" ) + _( "Default" ) + wxS( "] " ) + m_default_footprint;
115
116 m_fp_sel_ctrl->Append( defaultLabel, new wxStringClientData( m_default_footprint ) );
117
118 // If zero_filter is set and we have no filters, show no footprints
119 if( m_zero_filter && m_filters.IsEmpty() && m_pin_count == 0 )
120 {
122 return true;
123 }
124
125 // Build JSON request for pcbnew
126 using json = nlohmann::json;
127 json request;
128 request["pin_count"] = m_pin_count;
129 request["zero_filters"] = m_zero_filter;
130 request["max_results"] = m_max_items;
131
132 json filtersArray = json::array();
133
134 for( const wxString& filter : m_filters )
135 filtersArray.push_back( filter.ToStdString() );
136
137 request["filters"] = filtersArray;
138
139 // Get the filter function from pcbnew via KIWAY
140 try
141 {
142 KIFACE* kiface = m_kiway->KiFACE( KIWAY::FACE_PCB );
143
144 if( !kiface )
145 {
147 return true;
148 }
149
150 void* funcPtr = kiface->IfaceOrAddress( KIFACE_FILTER_FOOTPRINTS );
151
152 if( !funcPtr )
153 {
155 return true;
156 }
157
158 // Call the filter function
159 using FilterFunc = wxString ( * )( const wxString& );
160 FilterFunc filterFootprints = reinterpret_cast<FilterFunc>( funcPtr );
161
162 wxString requestStr = wxString::FromUTF8( request.dump() );
163 wxString responseStr = filterFootprints( requestStr );
164
165 // Parse the response
166 json response = json::parse( responseStr.ToStdString() );
167
168 if( response.is_array() )
169 {
170 for( const auto& item : response )
171 {
172 if( item.is_string() )
173 {
174 wxString fpName = wxString::FromUTF8( item.get<std::string>() );
175 m_fp_sel_ctrl->Append( fpName, new wxStringClientData( fpName ) );
176 }
177 }
178 }
179 }
180 catch( const std::exception& e )
181 {
182 // JSON parsing or other error - just show default
183 }
184
186 return true;
187}
188
189
191{
192 m_fp_sel_ctrl->SetSelection( 0 );
193}
194
195
197{
198 return m_fp_sel_ctrl->Enable( aEnable );
199}
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.
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, 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:294
@ FACE_PCB
pcbnew DSO
Definition kiway.h:302
Container for project specific data.
Definition project.h:65
#define _(s)
wxDEFINE_EVENT(EVT_FOOTPRINT_SELECTED, wxCommandEvent)
nlohmann::json json
Definition gerbview.cpp:50
@ KIFACE_FILTER_FOOTPRINTS
Function pointer type: wxString (*)(const wxString& aFilterJson) Input JSON: {"pin_count": N,...
Definition kiface_ids.h:39
static wxString filterFootprints(const wxString &aFilterJson)
Filter footprints based on criteria passed as JSON.
Definition pcbnew.cpp:105
Implement a participant in the KIWAY alchemy.
Definition kiway.h:155
IFACE KIFACE_BASE kiface("pcb_test_frame", KIWAY::FACE_PCB)