KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_footprint_chooser.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) 2014 Henner Zeller <[email protected]>
5 * Copyright (C) 2023 CERN
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
23#include <algorithm>
24#include <wx/button.h>
25#include <wx/clipbrd.h>
26#include <wx/display.h>
27#include <wx/log.h>
28#include <wx/panel.h>
29#include <wx/sizer.h>
30#include <wx/splitter.h>
31#include <wx/timer.h>
32#include <wx/wxhtml.h>
33#include <pcb_base_frame.h>
34#include <pcbnew_settings.h>
35#include <pgm_base.h>
38#include <widgets/lib_tree.h>
40#include <footprint.h>
41#include <project_pcb.h>
42#include <kiface_base.h>
43#include <tool/actions.h>
44#include <tool/tool_manager.h>
45#include <widgets/kistatusbar.h>
46
47// When a new footprint is selected, a custom event is sent, for instance to update
48// 3D viewer. So define a FP_SELECTION_EVENT event
49wxDEFINE_EVENT( FP_SELECTION_EVENT, wxCommandEvent );
50
52 const wxArrayString& aFootprintHistoryList,
53 std::function<bool( LIB_TREE_NODE& )> aFilter,
54 std::function<void()> aAcceptHandler,
55 std::function<void()> aEscapeHandler ) :
56 wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize ),
57 m_hsplitter( nullptr ),
58 m_vsplitter( nullptr ),
59 m_frame( aFrame ),
60 m_filter( std::move( aFilter ) ),
61 m_acceptHandler( std::move( aAcceptHandler ) ),
62 m_escapeHandler( std::move( aEscapeHandler ) )
63{
64 m_CurrFootprint = nullptr;
66
67 // Ensure libraries are loaded before building the tree. This is necessary when the footprint
68 // chooser is opened from contexts that don't preload libraries (e.g., from Eeschema).
69 footprints->AsyncLoad();
70 footprints->BlockUntilLoaded();
71
72 m_adapter = FP_TREE_MODEL_ADAPTER::Create( aFrame, footprints );
73 FP_TREE_MODEL_ADAPTER* adapter = static_cast<FP_TREE_MODEL_ADAPTER*>( m_adapter.get() );
74
75 std::vector<LIB_TREE_ITEM*> historyInfos;
76
77 for( const wxString& item : aFootprintHistoryList )
78 {
79 LIB_ID fpid;
80
81 if( fpid.Parse( item ) >= 0 )
82 continue;
83
84 FOOTPRINT* fp = footprints->LoadFootprint( fpid, false );
85
86 if( fp != nullptr )
87 {
88 historyInfos.push_back( fp );
89 m_historyFootprints.push_back( std::unique_ptr<FOOTPRINT>( fp ) );
90 }
91 }
92
93 adapter->DoAddLibrary( wxT( "-- " ) + _( "Recently Used" ) + wxT( " --" ), wxEmptyString,
94 historyInfos, false, true )
96
97 if( historyInfos.size() )
98 adapter->SetPreselectNode( historyInfos[0]->GetLIB_ID(), 0 );
99
100 adapter->SetFilter( &m_filter );
101 adapter->AddLibraries( m_frame );
102
103 // -------------------------------------------------------------------------------------
104 // Construct the actual panel
105 //
106
107 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
108
109 m_vsplitter = new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
110 wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH );
111
112 m_hsplitter = new wxSplitterWindow( m_vsplitter, wxID_ANY, wxDefaultPosition, wxDefaultSize,
113 wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH );
114
115 // Avoid the splitter window being assigned as the Parent to additional windows
116 m_vsplitter->SetExtraStyle( wxWS_EX_TRANSIENT );
117 m_hsplitter->SetExtraStyle( wxWS_EX_TRANSIENT );
118
119 m_detailsPanel = new wxPanel( m_vsplitter );
120 auto detailsSizer = new wxBoxSizer( wxVERTICAL );
121 m_detailsPanel->SetSizer( detailsSizer );
122
123 m_details = new HTML_WINDOW( m_detailsPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize );
124 detailsSizer->Add( m_details, 1, wxEXPAND, 5 );
125 m_detailsPanel->Layout();
126 detailsSizer->Fit( m_detailsPanel );
127
128 m_vsplitter->SetSashGravity( 0.5 );
129
130 // Ensure splitted areas are always shown (i.e. 0 size not allowed) when m_detailsPanel is shown
131 m_vsplitter->SetMinimumPaneSize( 80 ); // arbitrary value but reasonable min size
132 m_vsplitter->SplitHorizontally( m_hsplitter, m_detailsPanel );
133
134 sizer->Add( m_vsplitter, 1, wxEXPAND, 5 );
135
136 m_tree = new LIB_TREE( m_hsplitter, wxT( "footprints" ), m_adapter,
138
139 m_hsplitter->SetSashGravity( 0.8 );
140 m_hsplitter->SetMinimumPaneSize( 20 );
141
142 m_RightPanel = new wxPanel( m_hsplitter );
143 m_RightPanelSizer = new wxBoxSizer( wxVERTICAL );
144
146 m_preview_ctrl->SetUserUnits( m_frame->GetUserUnits() );
147 m_RightPanelSizer->Add( m_preview_ctrl, 1, wxEXPAND, 5 );
148
149 m_RightPanel->SetSizer( m_RightPanelSizer );
150 m_RightPanel->Layout();
152
153 m_hsplitter->SplitVertically( m_tree, m_RightPanel );
154
155 m_dbl_click_timer = new wxTimer( this );
156 m_open_libs_timer = new wxTimer( this );
157
158 SetSizer( sizer );
159
160 m_adapter->FinishTreeInitialization();
161
162 Bind( wxEVT_TIMER, &PANEL_FOOTPRINT_CHOOSER::onCloseTimer, this, m_dbl_click_timer->GetId() );
163 Bind( wxEVT_TIMER, &PANEL_FOOTPRINT_CHOOSER::onOpenLibsTimer, this, m_open_libs_timer->GetId() );
164 Bind( EVT_LIBITEM_SELECTED, &PANEL_FOOTPRINT_CHOOSER::onFootprintSelected, this );
165 Bind( EVT_LIBITEM_CHOSEN, &PANEL_FOOTPRINT_CHOOSER::onFootprintChosen, this );
166 m_frame->Bind( wxEVT_MENU_OPEN, &PANEL_FOOTPRINT_CHOOSER::onMenuOpen, this );
167 m_frame->Bind( wxEVT_MENU_CLOSE, &PANEL_FOOTPRINT_CHOOSER::onMenuClose, this );
168
169 m_details->Connect( wxEVT_CHAR_HOOK,
171 nullptr, this );
172
173 Layout();
174
175 // Open the user's previously opened libraries on timer expiration.
176 // This is done on a timer because we need a gross hack to keep GTK from garbling the
177 // display. Must be longer than the search debounce timer.
178 m_open_libs_timer->StartOnce( 300 );
179}
180
181
183{
184 m_frame->Unbind( wxEVT_MENU_OPEN, &PANEL_FOOTPRINT_CHOOSER::onMenuOpen, this );
185 m_frame->Unbind( wxEVT_MENU_CLOSE, &PANEL_FOOTPRINT_CHOOSER::onMenuClose, this );
186 Unbind( wxEVT_TIMER, &PANEL_FOOTPRINT_CHOOSER::onCloseTimer, this );
187 Unbind( EVT_LIBITEM_SELECTED, &PANEL_FOOTPRINT_CHOOSER::onFootprintSelected, this );
188 Unbind( EVT_LIBITEM_CHOSEN, &PANEL_FOOTPRINT_CHOOSER::onFootprintChosen, this );
189
190 m_details->Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( PANEL_FOOTPRINT_CHOOSER::OnDetailsCharHook ),
191 nullptr, this );
192
193 // I am not sure the following two lines are necessary, but they will not hurt anyone
194 m_dbl_click_timer->Stop();
195 m_open_libs_timer->Stop();
196 delete m_dbl_click_timer;
197 delete m_open_libs_timer;
198
199 if( PCBNEW_SETTINGS* cfg = GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" ) )
200 {
201 // Save any changes to column widths, etc.
202 m_adapter->SaveSettings();
203
204 cfg->m_FootprintChooser.width = GetParent()->ToDIP( GetParent()->GetSize().x );
205 cfg->m_FootprintChooser.height = GetParent()->ToDIP( GetParent()->GetSize().y );
206 cfg->m_FootprintChooser.sash_h = m_hsplitter->GetSashPosition();
207
208 if( m_vsplitter )
209 cfg->m_FootprintChooser.sash_v = m_vsplitter->GetSashPosition();
210
211 cfg->m_FootprintChooser.sort_mode = m_tree->GetSortMode();
212 }
213}
214
215
216void PANEL_FOOTPRINT_CHOOSER::OnChar( wxKeyEvent& aEvent )
217{
218 if( aEvent.GetKeyCode() == WXK_ESCAPE )
219 {
220 // First escape clears search string, second escape closes
221 if( !m_tree->GetSearchString().IsEmpty() )
222 {
223 m_tree->SetSearchString( wxEmptyString );
224 GetFocusTarget()->SetFocus();
225 return;
226 }
227
229 }
230 else
231 {
232 aEvent.Skip();
233 }
234}
235
236
237void PANEL_FOOTPRINT_CHOOSER::onMenuOpen( wxMenuEvent& aEvent )
238{
239 m_tree->BlockPreview( true );
240 aEvent.Skip();
241}
242
243
244void PANEL_FOOTPRINT_CHOOSER::onMenuClose( wxMenuEvent& aEvent )
245{
246 m_tree->BlockPreview( false );
247 aEvent.Skip();
248}
249
250
252{
253 if( PCBNEW_SETTINGS* settings = GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" ) )
254 {
255 auto horizPixelsFromDU =
256 [&]( int x ) -> int
257 {
258 wxSize sz( x, 0 );
259 return GetParent()->ConvertDialogToPixels( sz ).x;
260 };
261
262 PCBNEW_SETTINGS::FOOTPRINT_CHOOSER& cfg = settings->m_FootprintChooser;
263
264 // The persisted size is stored in DIP so it is independent of the monitor it was saved
265 // on. Restoring raw pixels would scale the window by the DPI ratio when reopened on a
266 // different-scale display, producing a window that spills across monitors.
267 int w = cfg.width > 40 ? GetParent()->FromDIP( cfg.width ) : horizPixelsFromDU( 440 );
268 int h = cfg.height > 40 ? GetParent()->FromDIP( cfg.height ) : horizPixelsFromDU( 340 );
269
270 // Cap to the work area so a stale pre-DIP setting cannot reopen the window across monitors.
271 if( int display = wxDisplay::GetFromWindow( GetParent() ); display != wxNOT_FOUND )
272 {
273 wxRect workArea = wxDisplay( display ).GetClientArea();
274 w = std::min( w, workArea.GetWidth() );
275 h = std::min( h, workArea.GetHeight() );
276 }
277
278 GetParent()->SetSize( wxSize( w, h ) );
279 GetParent()->Layout();
280
281 // We specify the width of the right window (m_symbol_view_panel), because specify
282 // the width of the left window does not work as expected when SetSashGravity() is called
283 if( cfg.sash_h < 0 )
284 cfg.sash_h = horizPixelsFromDU( 220 );
285
286 m_hsplitter->SetSashPosition( cfg.sash_h );
287
288 if( cfg.sash_v < 0 )
289 cfg.sash_v = horizPixelsFromDU( 230 );
290
291 if( m_vsplitter )
292 m_vsplitter->SetSashPosition( cfg.sash_v );
293
295 }
296}
297
298
300{
301 m_preselect = aPreselect;
302 m_adapter->SetPreselectNode( aPreselect, 0 );
303
304 if( m_tree && aPreselect.IsValid() )
305 m_tree->SelectLibId( aPreselect );
306}
307
308
310{
311 return m_tree->GetSelectedLibId();
312}
313
314
315void PANEL_FOOTPRINT_CHOOSER::onCloseTimer( wxTimerEvent& aEvent )
316{
317 // Hack because of eaten MouseUp event. See PANEL_FOOTPRINT_CHOOSER::onFootprintChosen
318 // for the beginning of this spaghetti noodle.
319
320 auto state = wxGetMouseState();
321
322 if( state.LeftIsDown() )
323 {
324 // Mouse hasn't been raised yet, so fire the timer again. Otherwise the
325 // purpose of this timer is defeated.
327 }
328 else
329 {
331 }
332}
333
334
336
338{
339 // Freeze across both steps so the expand and the re-scroll paint as a single final
340 // state, otherwise the selected row visibly shifts and snaps back.
341 m_tree->Freeze();
342
343 if( PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
344 m_adapter->OpenLibs( cfg->m_LibTree.open_libs );
345
346 // OpenLibs reshuffles the tree, so re-assert the preselected footprint.
347 if( m_preselect.IsValid() )
348 m_tree->SelectLibId( m_preselect );
349
350 m_tree->Thaw();
351}
352
353
355{
356 if( !m_preview_ctrl || !m_preview_ctrl->IsInitialized() )
357 return;
358
359 LIB_ID lib_id = m_tree->GetSelectedLibId();
360
361 if( !lib_id.IsValid() )
362 {
363 m_preview_ctrl->SetStatusText( _( "No footprint selected" ) );
364 }
365 else
366 {
367 m_preview_ctrl->ClearStatus();
368 m_preview_ctrl->DisplayFootprint( lib_id );
369 }
370
371 m_CurrFootprint = static_cast<FOOTPRINT_PREVIEW_PANEL*>( m_preview_ctrl->GetPreviewPanel() )->GetCurrentFootprint();
372
373 // Send a FP_SELECTION_EVENT event after a footprint change
374 wxCommandEvent event( FP_SELECTION_EVENT, GetId() );
375 event.SetEventObject( this );
376
377 ProcessWindowEvent( event );
378}
379
380
381void PANEL_FOOTPRINT_CHOOSER::onFootprintChosen( wxCommandEvent& aEvent )
382{
383 if( m_tree->GetSelectedLibId().IsValid() )
384 {
385 // Got a selection. We can't just end the modal dialog here, because wx leaks some
386 // events back to the parent window (in particular, the MouseUp following a double click).
387 //
388 // NOW, here's where it gets really fun. wxTreeListCtrl eats MouseUp. This isn't really
389 // feasible to bypass without a fully custom wxDataViewCtrl implementation, and even then
390 // might not be fully possible (docs are vague). To get around this, we use a one-shot
391 // timer to schedule the dialog close.
392 //
393 // See PANEL_FOOTPRINT_CHOOSER::onCloseTimer for the other end of this spaghetti noodle.
395 }
396}
397
398
400{
401 if( m_details && e.GetKeyCode() == 'C' && e.ControlDown() &&
402 !e.AltDown() && !e.ShiftDown() && !e.MetaDown() )
403 {
404 wxString txt = m_details->SelectionToText();
405 wxLogNull doNotLog; // disable logging of failed clipboard actions
406
407 if( wxTheClipboard->Open() )
408 {
409 wxTheClipboard->SetData( new wxTextDataObject( txt ) );
410 wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
411 wxTheClipboard->Close();
412 }
413 }
414 else
415 {
416 e.Skip();
417 }
418}
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
An interface to the global shared library manager that is schematic-specific and linked to one projec...
FOOTPRINT * LoadFootprint(const wxString &aNickname, const wxString &aName, bool aKeepUUID)
Load a FOOTPRINT having aName from the library given by aNickname.
Panel that renders a single footprint via Cairo GAL, meant to be exported through Kiface.
FOOTPRINT * GetCurrentFootprint() const
void AddLibraries(EDA_BASE_FRAME *aParent)
static wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > Create(PCB_BASE_FRAME *aParent, FOOTPRINT_LIBRARY_ADAPTER *aLibs)
Factory function: create a model adapter in a reference-counting container.
Add dark theme support to wxHtmlWindow.
Definition html_window.h:31
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
void AsyncLoad()
Loads all available libraries for this adapter type in the background.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:65
bool IsValid() const
Check if this LID_ID is valid.
Definition lib_id.h:168
void SetPreselectNode(const LIB_ID &aLibId, int aUnit)
Set the symbol name to be selected if there are no search results.
LIB_TREE_NODE_LIBRARY & DoAddLibrary(const wxString &aNodeName, const wxString &aDesc, const std::vector< LIB_TREE_ITEM * > &aItemList, bool pinned, bool presorted)
Add the given list of symbols by alias.
void SetFilter(std::function< bool(LIB_TREE_NODE &aNode)> *aFilter)
Set the filter.
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
Widget displaying a tree of symbols with optional search text control and description panel.
Definition lib_tree.h:46
@ ALL_WIDGETS
Definition lib_tree.h:55
void onFootprintSelected(wxCommandEvent &aEvent)
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
void onCloseTimer(wxTimerEvent &aEvent)
std::function< void()> m_acceptHandler
FOOTPRINT_PREVIEW_WIDGET * m_preview_ctrl
void OnChar(wxKeyEvent &aEvent)
std::function< void()> m_escapeHandler
void onMenuOpen(wxMenuEvent &aEvent)
Handle parent frame menu events to block tree preview.
static constexpr int DblClickDelay
std::function< bool(LIB_TREE_NODE &)> m_filter
void OnDetailsCharHook(wxKeyEvent &aEvt)
void onOpenLibsTimer(wxTimerEvent &aEvent)
void SetPreselect(const LIB_ID &aPreselect)
PANEL_FOOTPRINT_CHOOSER(PCB_BASE_FRAME *aFrame, wxTopLevelWindow *aParent, const wxArrayString &aFootprintHistoryList, std::function< bool(LIB_TREE_NODE &)> aFilter, std::function< void()> aAcceptHandler, std::function< void()> aEscapeHandler)
Create dialog to choose component.
std::vector< std::unique_ptr< FOOTPRINT > > m_historyFootprints
void onMenuClose(wxMenuEvent &aEvent)
void onFootprintChosen(wxCommandEvent &aEvent)
Handle the selection of an item.
LIB_ID GetSelectedLibId() const
To be called after this dialog returns from ShowModal().
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
static FOOTPRINT_LIBRARY_ADAPTER * FootprintLibAdapter(PROJECT *aProject)
#define _(s)
STL namespace.
wxDEFINE_EVENT(FP_SELECTION_EVENT, wxCommandEvent)
see class PGM_BASE
T * GetAppSettings(const char *aFilename)