KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_design_block_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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU 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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <pgm_base.h>
25#include <design_block.h>
26#include <design_block_pane.h>
30#include <kiface_base.h>
31#include <kiway_holder.h>
32#include <eda_draw_frame.h>
33#include <widgets/lib_tree.h>
38#include <string_utils.h>
39#include <wx/log.h>
40#include <wx/panel.h>
41#include <wx/sizer.h>
42#include <wx/splitter.h>
43#include <wx/timer.h>
44#include <wx/wxhtml.h>
45#include <wx/msgdlg.h>
47
48
50
51
53 std::vector<LIB_ID>& aHistoryList,
54 std::function<void()> aSelectHandler,
55 TOOL_INTERACTIVE* aContextMenuTool ) :
56
57 wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize ),
58 m_dbl_click_timer( nullptr ),
59 m_open_libs_timer( nullptr ),
60 m_vsplitter( nullptr ),
61 m_tree( nullptr ),
62 m_preview( nullptr ),
63 m_parent( aParent ),
64 m_frame( aFrame ),
65 m_selectHandler( std::move( aSelectHandler ) ),
66 m_historyList( aHistoryList )
67{
69
70 // Load design block files:
71 auto* progressReporter = new WX_PROGRESS_REPORTER( aParent, _( "Load Design Block Libraries" ), 1,
73
74 DESIGN_BLOCK_LIB_TABLE::GetGlobalList().ReadDesignBlockFiles( libs, nullptr, progressReporter );
75
76 // Force immediate deletion of the WX_PROGRESS_REPORTER. Do not use Destroy(), or use
77 // Destroy() followed by wxSafeYield() because on Windows, APP_PROGRESS_DIALOG and
78 // WX_PROGRESS_REPORTER have some side effects on the event loop manager. For instance, a
79 // subsequent call to ShowModal() or ShowQuasiModal() for a dialog following the use of a
80 // WX_PROGRESS_REPORTER results in incorrect modal or quasi modal behavior.
81 delete progressReporter;
82
83 if( DESIGN_BLOCK_LIB_TABLE::GetGlobalList().GetErrorCount() )
84 displayErrors( aFrame );
85
87 m_frame, libs, m_frame->config()->m_DesignBlockChooserPanel.tree, aContextMenuTool );
88
89 // -------------------------------------------------------------------------------------
90 // Construct the actual panel
91 //
92
93 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
94
95 m_vsplitter = new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
96 wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH );
97
98
99 // Avoid the splitter window being assigned as the parent to additional windows.
100 m_vsplitter->SetExtraStyle( wxWS_EX_TRANSIENT );
101
102 wxPanel* treePanel = new wxPanel( m_vsplitter );
103 wxBoxSizer* treeSizer = new wxBoxSizer( wxVERTICAL );
104 treePanel->SetSizer( treeSizer );
105
106 m_detailsPanel = new wxPanel( m_vsplitter );
107 m_detailsSizer = new wxBoxSizer( wxVERTICAL );
108 m_detailsPanel->SetSizer( m_detailsSizer );
109
110 m_detailsPanel->Layout();
112
113 m_vsplitter->SetSashGravity( 0.5 );
114 m_vsplitter->SetMinimumPaneSize( 20 );
115 m_vsplitter->SplitHorizontally( treePanel, m_detailsPanel );
116
117 sizer->Add( m_vsplitter, 1, wxEXPAND, 5 );
118
119
120 m_tree = new LIB_TREE( treePanel, wxT( "design_blocks" ), libs, m_adapter, LIB_TREE::FLAGS::ALL_WIDGETS, nullptr );
121
122 treeSizer->Add( m_tree, 1, wxEXPAND, 5 );
123 treePanel->Layout();
124 treeSizer->Fit( treePanel );
125
126 RefreshLibs();
127 m_adapter->FinishTreeInitialization();
128
130
131 m_dbl_click_timer = new wxTimer( this );
132 m_open_libs_timer = new wxTimer( this );
133
134 SetSizer( sizer );
135
136 Layout();
137
138 Bind( wxEVT_TIMER, &PANEL_DESIGN_BLOCK_CHOOSER::onCloseTimer, this, m_dbl_click_timer->GetId() );
139 Bind( wxEVT_TIMER, &PANEL_DESIGN_BLOCK_CHOOSER::onOpenLibsTimer, this, m_open_libs_timer->GetId() );
140 Bind( EVT_LIBITEM_CHOSEN, &PANEL_DESIGN_BLOCK_CHOOSER::onDesignBlockChosen, this );
141 Bind( wxEVT_CHAR_HOOK, &PANEL_DESIGN_BLOCK_CHOOSER::OnChar, this );
142
143 // Open the user's previously opened libraries on timer expiration.
144 // This is done on a timer because we need a gross hack to keep GTK from garbling the
145 // display. Must be longer than the search debounce timer.
146 m_open_libs_timer->StartOnce( 300 );
147}
148
149
151{
152 Unbind( wxEVT_TIMER, &PANEL_DESIGN_BLOCK_CHOOSER::onCloseTimer, this );
153 Unbind( EVT_LIBITEM_SELECTED, &PANEL_DESIGN_BLOCK_CHOOSER::onDesignBlockSelected, this );
154 Unbind( EVT_LIBITEM_CHOSEN, &PANEL_DESIGN_BLOCK_CHOOSER::onDesignBlockChosen, this );
155 Unbind( wxEVT_CHAR_HOOK, &PANEL_DESIGN_BLOCK_CHOOSER::OnChar, this );
156
157 // Stop the timer during destruction early to avoid potential race conditions (that do happen)
158 m_dbl_click_timer->Stop();
159 m_open_libs_timer->Stop();
160 delete m_dbl_click_timer;
161 delete m_open_libs_timer;
162}
163
164
166{
168
169 if( APP_SETTINGS_BASE* cfg = m_frame->config() )
170 {
171 // Save any changes to column widths, etc.
172 m_adapter->SaveSettings();
173
174 cfg->m_DesignBlockChooserPanel.width = GetParent()->GetSize().x;
175 cfg->m_DesignBlockChooserPanel.height = GetParent()->GetSize().y;
176 cfg->m_DesignBlockChooserPanel.sash_pos_v = m_vsplitter->GetSashPosition();
177 cfg->m_DesignBlockChooserPanel.sort_mode = m_tree->GetSortMode();
178 }
179}
180
181
183{
184 if( m_tree )
186}
187
188
190{
191 m_preview = aPreview;
192 m_detailsSizer->Add( m_preview, 1, wxEXPAND, 5 );
193 Layout();
194}
195
196
197void PANEL_DESIGN_BLOCK_CHOOSER::OnChar( wxKeyEvent& aEvent )
198{
199 if( aEvent.GetKeyCode() == WXK_ESCAPE )
200 {
201 wxObject* eventSource = aEvent.GetEventObject();
202
203 if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( eventSource ) )
204 {
205 // First escape cancels search string value
206 if( textCtrl->GetValue() == m_tree->GetSearchString() && !m_tree->GetSearchString().IsEmpty() )
207 {
208 m_tree->SetSearchString( wxEmptyString );
209 return;
210 }
211 }
212 }
213 else
214 {
215 aEvent.Skip();
216 }
217}
218
219
221{
222 if( APP_SETTINGS_BASE* cfg = m_frame->config() )
223 {
224 auto horizPixelsFromDU =
225 [&]( int x ) -> int
226 {
227 wxSize sz( x, 0 );
228 return GetParent()->ConvertDialogToPixels( sz ).x;
229 };
230
231 APP_SETTINGS_BASE::PANEL_DESIGN_BLOCK_CHOOSER& panelCfg = cfg->m_DesignBlockChooserPanel;
232
233 int w = panelCfg.width > 40 ? panelCfg.width : horizPixelsFromDU( 440 );
234 int h = panelCfg.height > 40 ? panelCfg.height : horizPixelsFromDU( 340 );
235
236 GetParent()->SetSize( wxSize( w, h ) );
237 GetParent()->Layout();
238
239 // We specify the width of the right window (m_design_block_view_panel), because specify
240 // the width of the left window does not work as expected when SetSashGravity() is called
241
242 if( panelCfg.sash_pos_h < 0 )
243 panelCfg.sash_pos_h = horizPixelsFromDU( 220 );
244
245 if( panelCfg.sash_pos_v < 0 )
246 panelCfg.sash_pos_v = horizPixelsFromDU( 230 );
247
248 if( m_vsplitter )
249 m_vsplitter->SetSashPosition( panelCfg.sash_pos_v );
250
251 m_adapter->SetSortMode( (LIB_TREE_MODEL_ADAPTER::SORT_MODE) panelCfg.sort_mode );
252 }
253}
254
255
257{
258 // Unselect before syncing to avoid null reference in the adapter
259 // if a selected item is removed during the sync
260 m_tree->Unselect();
261
263
264 // Clear all existing libraries then re-add
265 adapter->ClearLibraries();
266
267 // Read the libraries from disk if they've changed
269 adapter->SetLibTable( fpTable );
270
271 // Sync FOOTPRINT_INFO list to the libraries on disk
272 if( aProgress )
273 {
274 WX_PROGRESS_REPORTER progressReporter( this, _( "Update Design Block Libraries" ), 2,
275 PR_CAN_ABORT );
276
277 DESIGN_BLOCK_LIB_TABLE::GetGlobalList().ReadDesignBlockFiles( fpTable, nullptr, &progressReporter );
278 progressReporter.Show( false );
279 }
280 else
281 {
282 DESIGN_BLOCK_LIB_TABLE::GetGlobalList().ReadDesignBlockFiles( fpTable, nullptr, nullptr );
283 }
284
286
287 if( !m_historyList.empty() )
288 adapter->SetPreselectNode( m_historyList[0], 0 );
289
290 adapter->AddLibraries( m_frame );
291
292 m_tree->Regenerate( true );
293}
294
295
297{
298 m_adapter->SetPreselectNode( aPreselect, 0 );
299}
300
301
303{
304 return m_tree->GetSelectedLibId( aUnit );
305}
306
307
309{
310 m_tree->CenterLibId( aLibId );
311 m_tree->SelectLibId( aLibId );
312}
313
314
316{
317 // Hack because of eaten MouseUp event. See PANEL_DESIGN_BLOCK_CHOOSER::onDesignBlockChosen
318 // for the beginning of this spaghetti noodle.
319
320 wxMouseState 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 {
333 }
334}
335
336
338{
339 if( APP_SETTINGS_BASE* cfg = m_frame->config() )
340 m_adapter->OpenLibs( cfg->m_LibTree.open_libs );
341
342 // Bind this now se we don't spam the event queue with EVT_LIBITEM_SELECTED events during
343 // the initial load.
344 Bind( EVT_LIBITEM_SELECTED, &PANEL_DESIGN_BLOCK_CHOOSER::onDesignBlockSelected, this );
345}
346
347
349{
350 if( GetSelectedLibId().IsValid() )
351 {
352 std::unique_ptr<DESIGN_BLOCK> designBlock( m_parent->GetDesignBlock( GetSelectedLibId(), true, true ) );
353 m_preview->DisplayDesignBlock( designBlock.get() );
354 }
355}
356
357
359{
361 {
362 // Got a selection. We can't just end the modal dialog here, because wx leaks some events
363 // back to the parent window (in particular, the MouseUp following a double click).
364 //
365 // NOW, here's where it gets really fun. wxTreeListCtrl eats MouseUp. This isn't really
366 // feasible to bypass without a fully custom wxDataViewCtrl implementation, and even then
367 // might not be fully possible (docs are vague). To get around this, we use a one-shot
368 // timer to schedule the dialog close.
369 //
370 // See PANEL_DESIGN_BLOCK_CHOOSER::onCloseTimer for the other end of this spaghetti noodle.
372 }
373}
374
376{
377 LIB_ID savedId = GetSelectedLibId();
378
379 m_tree->Unselect();
380
381 // Remove duplicates
382 for( int i = (int) m_historyList.size() - 1; i >= 0; --i )
383 {
384 if( m_historyList[i] == aLibId )
385 m_historyList.erase( m_historyList.begin() + i );
386 }
387
388 // Add the new name at the beginning of the history list
389 m_historyList.insert( m_historyList.begin(), aLibId );
390
391 // Remove extra names
392 while( m_historyList.size() >= 8 )
393 m_historyList.pop_back();
394
396 m_tree->Regenerate( true );
397
398 SelectLibId( savedId );
399}
400
401
403{
404 m_adapter->RemoveGroup( true, false );
405
406 // Build the history list
407 std::vector<LIB_TREE_ITEM*> historyInfos;
408
409 for( const LIB_ID& lib : m_historyList )
410 {
412 lib.GetLibItemName() );
413
414 // this can be null, for example, if the design block has been deleted from a library.
415 if( fp_info != nullptr )
416 historyInfos.push_back( fp_info );
417 }
418
419 m_adapter->DoAddLibrary( wxT( "-- " ) + _( "Recently Used" ) + wxT( " --" ), wxEmptyString,
420 historyInfos, false, true )
421 .m_IsRecentlyUsedGroup = true;
422}
423
424
425void PANEL_DESIGN_BLOCK_CHOOSER::displayErrors( wxTopLevelWindow* aWindow )
426{
427 // @todo: go to a more HTML !<table>! ? centric output, possibly with recommendations
428 // for remedy of errors. Add numeric error codes to PARSE_ERROR, and switch on them for
429 // remedies, etc. Full access is provided to everything in every exception!
430
431 HTML_MESSAGE_BOX dlg( aWindow, _( "Load Error" ) );
432
433 dlg.MessageSet( _( "Errors were encountered loading design blocks:" ) );
434
435 wxString msg;
436
437 while( std::unique_ptr<IO_ERROR> error = DESIGN_BLOCK_LIB_TABLE::GetGlobalList().PopError() )
438 {
439 wxString tmp = EscapeHTML( error->Problem() );
440
441 // Preserve new lines in error messages so queued errors don't run together.
442 tmp.Replace( wxS( "\n" ), wxS( "<BR>" ) );
443 msg += wxT( "<p>" ) + tmp + wxT( "</p>" );
444 }
445
446 dlg.AddHTML_Text( msg );
447
448 dlg.ShowModal();
449}
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:108
PANEL_DESIGN_BLOCK_CHOOSER m_DesignBlockChooserPanel
Definition: app_settings.h:216
static DESIGN_BLOCK_LIST_IMPL & GetGlobalList()
bool ReadDesignBlockFiles(DESIGN_BLOCK_LIB_TABLE *aTable, const wxString *aNickname=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr) override
Read all the design blocks provided by the combination of aTable and aNickname.
DESIGN_BLOCK_INFO * GetDesignBlockInfo(const wxString &aDesignBlockName)
Get info for a design block by id.
DESIGN_BLOCK * GetDesignBlock(const LIB_ID &aLibId, bool aUseCacheLib, bool aShowErrorMsg)
Load design block from design block library table.
virtual void DisplayDesignBlock(DESIGN_BLOCK *aDesignBlock)=0
Set the currently displayed design block.
static wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > Create(EDA_BASE_FRAME *aParent, LIB_TABLE *aLibs, APP_SETTINGS_BASE::LIB_TREE &aSettings, TOOL_INTERACTIVE *aContextMenuTool)
Factory function: create a model adapter in a reference-counting container.
void SetLibTable(DESIGN_BLOCK_LIB_TABLE *aLibs)
int ShowModal() override
virtual APP_SETTINGS_BASE * config() const
Return the settings object used in SaveSettings(), and is overloaded in KICAD_MANAGER_FRAME.
The base class for create windows for drawing purpose.
virtual EDA_DRAW_PANEL_GAL * GetCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
void SetFocus() override
void MessageSet(const wxString &message)
Add a message (in bold) to message list.
void AddHTML_Text(const wxString &message)
Add HTML text (without any change) to message list.
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:172
A mix-in to provide polymorphism between items stored in libraries (symbols, aliases and footprints).
Definition: lib_tree_item.h:41
void SetPreselectNode(const LIB_ID &aLibId, int aUnit)
Set the symbol name to be selected if there are no search results.
Widget displaying a tree of symbols with optional search text control and description panel.
Definition: lib_tree.h:49
void CenterLibId(const LIB_ID &aLibId)
Ensure that an item is visible (preferably centered).
Definition: lib_tree.cpp:365
void ShowChangedLanguage()
Definition: lib_tree.cpp:288
void SelectLibId(const LIB_ID &aLibId)
Select an item in the tree widget.
Definition: lib_tree.cpp:359
LIB_TREE_MODEL_ADAPTER::SORT_MODE GetSortMode() const
Definition: lib_tree.h:155
wxString GetSearchString() const
Definition: lib_tree.cpp:403
void Unselect()
Unselect currently selected item in wxDataViewCtrl.
Definition: lib_tree.cpp:371
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
For multi-unit symbols, if the user selects the symbol itself rather than picking an individual unit,...
Definition: lib_tree.cpp:298
@ ALL_WIDGETS
Definition: lib_tree.h:58
void SetSearchString(const wxString &aSearchString)
Save/restore search string.
Definition: lib_tree.cpp:397
void Regenerate(bool aKeepState)
Regenerate the tree.
Definition: lib_tree.cpp:438
DESIGN_BLOCK_PREVIEW_WIDGET * m_preview
std::function< void()> m_selectHandler
void onOpenLibsTimer(wxTimerEvent &aEvent)
void addDesignBlockToHistory(const LIB_ID &aLibId)
PANEL_DESIGN_BLOCK_CHOOSER(EDA_DRAW_FRAME *aFrame, DESIGN_BLOCK_PANE *aParent, std::vector< LIB_ID > &aHistoryList, std::function< void()> aSelectHandler, TOOL_INTERACTIVE *aContextMenuTool)
Panel for using design blocks.
void SetPreselect(const LIB_ID &aPreselect)
void onCloseTimer(wxTimerEvent &aEvent)
void displayErrors(wxTopLevelWindow *aWindow)
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
To be called after this dialog returns from ShowModal().
void SetPreviewWidget(DESIGN_BLOCK_PREVIEW_WIDGET *aPreview)
void RefreshLibs(bool aProgress=false)
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > m_adapter
void onDesignBlockSelected(wxCommandEvent &aEvent)
void onDesignBlockChosen(wxCommandEvent &aEvent)
Handle the selection of an item.
void SelectLibId(const LIB_ID &aLibId)
virtual DESIGN_BLOCK_LIB_TABLE * DesignBlockLibs()
Return the table of design block libraries.
Definition: project.cpp:431
Multi-thread safe progress reporter dialog, intended for use of tasks that parallel reporting back of...
#define _(s)
STL namespace.
see class PGM_BASE
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
#define PR_CAN_ABORT