KiCad PCB EDA Suite
Loading...
Searching...
No Matches
footprint_chooser_frame.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) 2023 CERN
5 * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <pgm_base.h>
26#include <kiface_base.h>
27#include <kiway.h>
28#include <kiway_express.h>
29#include <wx/button.h>
30#include <wx/checkbox.h>
31#include <kiplatform/ui.h>
36#include "wx/display.h"
37
38
39static wxArrayString s_FootprintHistoryList;
40static unsigned s_FootprintHistoryMaxCount = 8;
41
42static void AddFootprintToHistory( const wxString& aName )
43{
44 // Remove duplicates
45 for( int ii = (int) s_FootprintHistoryList.GetCount() - 1; ii >= 0; --ii )
46 {
47 if( s_FootprintHistoryList[ ii ] == aName )
48 s_FootprintHistoryList.RemoveAt( (size_t) ii );
49 }
50
51 // Add the new name at the beginning of the history list
52 s_FootprintHistoryList.Insert( aName, 0 );
53
54 // Remove extra names
56 s_FootprintHistoryList.RemoveAt( s_FootprintHistoryList.GetCount() - 1 );
57}
58
59
60BEGIN_EVENT_TABLE( FOOTPRINT_CHOOSER_FRAME, PCB_BASE_FRAME )
62 EVT_BUTTON( wxID_OK, FOOTPRINT_CHOOSER_FRAME::OnOK )
63 EVT_BUTTON( wxID_CANCEL, FOOTPRINT_CHOOSER_FRAME::closeFootprintChooser )
65END_EVENT_TABLE()
66
67
68#define MODAL_FRAME ( wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN \
69 | wxWANTS_CHARS | wxFRAME_NO_TASKBAR | wxSTAY_ON_TOP )
70
71
73 PCB_BASE_FRAME( aKiway, aParent, FRAME_FOOTPRINT_CHOOSER, _( "Footprint Chooser" ),
74 wxDefaultPosition, wxDefaultSize, MODAL_FRAME,
76 m_filterByPinCount( nullptr ),
77 m_filterByFPFilters( nullptr ),
78 m_pinCount( 0 ),
79 m_firstPaintEvent( true )
80{
81 SetModal( true );
82
83 m_filterByFPFilters = new wxCheckBox( this, wxID_ANY, _( "Apply footprint filters" ) );
84 m_filterByPinCount = new wxCheckBox( this, wxID_ANY, _( "Filter by pin count" ) );
85
86 m_filterByFPFilters->Show( false );
87 m_filterByPinCount->Show( false );
88
89 if( PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
90 {
91 m_filterByFPFilters->SetValue( cfg->m_FootprintChooser.use_fp_filters );
92 m_filterByPinCount->SetValue( cfg->m_FootprintChooser.filter_on_pin_count );
93 }
94
95 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
97 // Filter
98 [this]( LIB_TREE_NODE& aNode ) -> bool
99 {
100 return filterFootprint( aNode );
101 },
102 // Close handler
103 [this]()
104 {
105 wxCommandEvent dummy;
106 OnOK( dummy );
107 } );
108
109 sizer->Add( m_chooserPanel, 1, wxEXPAND | wxBOTTOM, 2 );
110
111 wxBoxSizer* fpFilterSizer = new wxBoxSizer( wxVERTICAL );
112 fpFilterSizer->Add( m_filterByFPFilters, 0, wxTOP | wxEXPAND, 5 );
113 sizer->Add( fpFilterSizer, 0, wxEXPAND | wxLEFT, 10 );
114
115 wxBoxSizer* buttonsSizer = new wxBoxSizer( wxHORIZONTAL );
116 buttonsSizer->Add( m_filterByPinCount, 0, wxLEFT | wxTOP | wxALIGN_TOP, 5 );
117
118 wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
119 wxButton* okButton = new wxButton( this, wxID_OK );
120 wxButton* cancelButton = new wxButton( this, wxID_CANCEL );
121
122 sdbSizer->AddButton( okButton );
123 sdbSizer->AddButton( cancelButton );
124 sdbSizer->Realize();
125
126 buttonsSizer->Add( sdbSizer, 1, wxALL, 5 );
127
128 sizer->Add( buttonsSizer, 0, wxEXPAND | wxLEFT, 5 );
129 SetSizer( sizer );
130
131 SetTitle( GetTitle() + wxString::Format( _( " (%d items loaded)" ),
133
134 Layout();
136
137 m_filterByPinCount->Bind( wxEVT_CHECKBOX,
138 [&]( wxCommandEvent& evt )
139 {
141 } );
142
143 m_filterByFPFilters->Bind( wxEVT_CHECKBOX,
144 [&]( wxCommandEvent& evt )
145 {
147 } );
148}
149
150
152{
153 if( PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
154 {
155 cfg->m_FootprintChooser.use_fp_filters = m_filterByFPFilters->GetValue();
156 cfg->m_FootprintChooser.filter_on_pin_count = m_filterByPinCount->GetValue();
157 }
158}
159
161{
162 if( aNode.m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
163 {
164 // Normally lib nodes get scored by the max of their children's scores. However, if a
165 // lib node *has* no children then the scorer will call the filter on the lib node itself,
166 // and we just want to return true if we're not filtering at all.
167 return !m_filterByPinCount->GetValue() && !m_filterByFPFilters->GetValue();
168 }
169
170 auto patternMatch =
171 []( LIB_ID& id, std::vector<std::unique_ptr<EDA_PATTERN_MATCH>>& filters ) -> bool
172 {
173 // The matching is case insensitive
174 wxString name;
175
176 for( const std::unique_ptr<EDA_PATTERN_MATCH>& filter : filters )
177 {
178 name.Empty();
179
180 // If the filter contains a ':' then include the library name in the pattern
181 if( filter->GetPattern().Contains( wxS( ":" ) ) )
182 name = id.GetUniStringLibNickname().Lower() + wxS( ":" );
183
184 name += id.GetUniStringLibItemName().Lower();
185
186 if( filter->Find( name ) )
187 return true;
188 }
189
190 return false;
191 };
192
193 if( m_pinCount > 0 && m_filterByPinCount->GetValue() )
194 {
195 if( aNode.m_PinCount != m_pinCount )
196 return false;
197 }
198
199 if( !m_fpFilters.empty() && m_filterByFPFilters->GetValue() )
200 {
201 if( !patternMatch( aNode.m_LibId, m_fpFilters ) )
202 return false;
203 }
204
205 return true;
206}
207
208
210{
211 // Only dismiss a modal frame once, so that the return values set by
212 // the prior DismissModal() are not bashed for ShowModal().
213 if( !IsDismissed() )
214 DismissModal( false );
215
216 // window to be destroyed by the caller of KIWAY_PLAYER::ShowModal()
217}
218
219
221{
222 PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( aCfg );
223 wxCHECK_MSG( cfg, nullptr, wxT( "config not existing" ) );
224
225 return &cfg->m_FootprintViewer;
226}
227
228
230{
231 auto* settings = Pgm().GetSettingsManager().GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>();
232
233 if( settings )
234 return Pgm().GetSettingsManager().GetColorSettings( settings->m_ColorTheme );
235 else
236 return Pgm().GetSettingsManager().GetColorSettings();
237}
238
239
241{
242 const std::string& payload = mail.GetPayload();
243
244 switch( mail.Command() )
245 {
247 {
248 m_pinCount = 0;
249 m_fpFilters.clear();
250
251 /*
252 * Symbol netlist format:
253 * pinCount
254 * fpFilters
255 */
256 std::vector<std::string> strings = split( payload, "\r" );
257
258 if( strings.size() >= 1 )
259 {
260 wxString pinCountStr( strings[0] );
261 pinCountStr.ToInt( &m_pinCount );
262
263 if( m_pinCount > 0 )
264 {
265 m_filterByPinCount->SetLabel( m_filterByPinCount->GetLabel()
266 + wxString::Format( wxS( " (%d)" ), m_pinCount ) );
267 m_filterByPinCount->Show( true );
268 }
269 }
270
271 if( strings.size() >= 2 && !strings[1].empty() )
272 {
273 for( const wxString& filter : wxSplit( strings[1], ' ' ) )
274 {
275 m_fpFilters.push_back( std::make_unique<EDA_PATTERN_MATCH_WILDCARD_ANCHORED>() );
276 m_fpFilters.back()->SetPattern( filter.Lower() );
277 }
278
279 m_filterByFPFilters->SetLabel( m_filterByFPFilters->GetLabel()
280 + wxString::Format( wxS( " (%s)" ), strings[1] ) );
281 m_filterByFPFilters->Show( true );
282 }
283
284 break;
285 }
286
287 default:
288 break;
289 }
290}
291
292
293bool FOOTPRINT_CHOOSER_FRAME::ShowModal( wxString* aFootprint, wxWindow* aParent )
294{
295 if( aFootprint && !aFootprint->IsEmpty() )
296 {
297 LIB_ID fpid;
298
299 fpid.Parse( *aFootprint, true );
300
301 if( fpid.IsValid() )
303 }
304
305 return KIWAY_PLAYER::ShowModal( aFootprint, aParent );
306}
307
308
309static wxRect s_dialogRect( 0, 0, 0, 0 );
310
311
312void FOOTPRINT_CHOOSER_FRAME::SetPosition( const wxPoint& aNewPosition )
313{
314 PCB_BASE_FRAME::SetPosition( aNewPosition );
315
316 s_dialogRect.SetPosition( aNewPosition );
317}
318
319
321{
322 bool ret;
323
324 // Show or hide the window. If hiding, save current position and size.
325 // If showing, use previous position and size.
326 if( show )
327 {
328#ifndef __WINDOWS__
329 PCB_BASE_FRAME::Raise(); // Needed on OS X and some other window managers (i.e. Unity)
330#endif
331 ret = PCB_BASE_FRAME::Show( show );
332
333 // returns a zeroed-out default wxRect if none existed before.
334 wxRect savedDialogRect = s_dialogRect;
335
336 if( savedDialogRect.GetSize().x != 0 && savedDialogRect.GetSize().y != 0 )
337 {
338 SetSize( savedDialogRect.GetPosition().x, savedDialogRect.GetPosition().y,
339 std::max( wxWindow::GetSize().x, savedDialogRect.GetSize().x ),
340 std::max( wxWindow::GetSize().y, savedDialogRect.GetSize().y ),
341 0 );
342 }
343
344 // Be sure that the dialog appears in a visible area
345 // (the dialog position might have been stored at the time when it was
346 // shown on another display)
347 if( wxDisplay::GetFromWindow( this ) == wxNOT_FOUND )
348 Centre();
349 }
350 else
351 {
352 s_dialogRect = wxRect( wxWindow::GetPosition(), wxWindow::GetSize() );
353 ret = PCB_BASE_FRAME::Show( show );
354 }
355
356 return ret;
357}
358
359
360void FOOTPRINT_CHOOSER_FRAME::OnPaint( wxPaintEvent& aEvent )
361{
363 {
366
367 m_firstPaintEvent = false;
368 }
369
370 aEvent.Skip();
371}
372
373
374void FOOTPRINT_CHOOSER_FRAME::OnOK( wxCommandEvent& aEvent )
375{
377
378 if( fpID.IsValid() )
379 {
380 wxString footprint = fpID.Format();
381
382 AddFootprintToHistory( footprint );
383 DismissModal( true, footprint );
384 }
385 else
386 {
387 DismissModal( false );
388 }
389}
390
391
393{
394 Close( false );
395}
396
397
const char * name
Definition: DXF_plotter.cpp:57
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:92
Color settings are a bit different than most of the settings objects in that there can be more than o...
WINDOW_SETTINGS * GetWindowSettings(APP_SETTINGS_BASE *aCfg) override
Return a pointer to the window settings for this frame.
void closeFootprintChooser(wxCommandEvent &aEvent)
bool filterFootprint(LIB_TREE_NODE &aNode)
FOOTPRINT_CHOOSER_FRAME(KIWAY *aKiway, wxWindow *aParent)
bool ShowModal(wxString *aFootprint, wxWindow *aParent) override
void OnPaint(wxPaintEvent &aEvent)
bool Show(bool show) override
void OnOK(wxCommandEvent &aEvent)
void SetPosition(const wxPoint &aNewPosition)
Force the position of the dialog to a new position.
void KiwayMailIn(KIWAY_EXPRESS &mail) override
Receive KIWAY_EXPRESS messages from other players.
std::vector< std::unique_ptr< EDA_PATTERN_MATCH > > m_fpFilters
COLOR_SETTINGS * GetColorSettings(bool aForceRefresh) const override
Helper to retrieve the current color settings.
PANEL_FOOTPRINT_CHOOSER * m_chooserPanel
Carry a payload from one KIWAY_PLAYER to another within a PROJECT.
Definition: kiway_express.h:39
std::string & GetPayload()
Return the payload, which can be any text but it typically self identifying s-expression.
Definition: kiway_express.h:57
MAIL_T Command()
Returns the MAIL_T associated with this mail.
Definition: kiway_express.h:49
virtual bool ShowModal(wxString *aResult=nullptr, wxWindow *aResultantFocusWindow=nullptr)
Show this wxFrame as if it were a modal dialog, with all other instantiated wxFrames disabled until t...
bool IsDismissed()
void SetModal(bool aIsModal)
Definition: kiway_player.h:157
void DismissModal(bool aRetVal, const wxString &aResult=wxEmptyString)
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:279
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:51
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:172
UTF8 Format() const
Definition: lib_id.cpp:118
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
enum TYPE m_Type
wxWindow * GetFocusTarget() const
void SetPreselect(const LIB_ID &aPreselect)
LIB_ID GetSelectedLibId() const
To be called after this dialog returns from ShowModal().
WINDOW_SETTINGS m_FootprintViewer
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
#define _(s)
#define FOOTPRINT_CHOOSER_FRAME_NAME
static wxRect s_dialogRect(0, 0, 0, 0)
static wxArrayString s_FootprintHistoryList
static unsigned s_FootprintHistoryMaxCount
static void AddFootprintToHistory(const wxString &aName)
#define MODAL_FRAME
@ FRAME_FOOTPRINT_CHOOSER
Definition: frame_type.h:44
@ MAIL_SYMBOL_NETLIST
Definition: mail_type.h:45
void FixupCancelButtonCmdKeyCollision(wxWindow *aWindow)
Definition: gtk/ui.cpp:94
void ForceFocus(wxWindow *aWindow)
Pass the current focus to the window.
Definition: gtk/ui.cpp:67
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:119
std::vector< FAB_LAYER_COLOR > dummy
static std::vector< std::string > split(const std::string &aStr, const std::string &aDelim)
Split the input string into a vector of output strings.
Definition: string_utils.h:310
Stores the common settings that are saved and loaded for each window / frame.
Definition: app_settings.h:74