KiCad PCB EDA Suite
dialog_choose_component.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 <h.zeller@acm.org>
5  * Copyright (C) 2016-2019 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 
26 
27 #include <algorithm>
28 #include <set>
29 #include <wx/utils.h>
30 
31 #include <wx/button.h>
32 #include <wx/dataview.h>
33 #include <wx/panel.h>
34 #include <wx/sizer.h>
35 #include <wx/splitter.h>
36 #include <wx/timer.h>
37 #include <wx/utils.h>
38 
39 #include <class_library.h>
40 #include <sch_base_frame.h>
41 #include <template_fieldnames.h>
42 #include <symbol_lib_table.h>
43 #include <widgets/lib_tree.h>
47 #include <wx/clipbrd.h>
48 
52 
54 
55 
58  int aDeMorganConvert, bool aAllowFieldEdits,
59  bool aShowFootprints, bool aAllowBrowser )
60  : DIALOG_SHIM( aParent, wxID_ANY, aTitle, wxDefaultPosition, wxDefaultSize,
61  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ),
62  m_browser_button( nullptr ),
63  m_hsplitter( nullptr ),
64  m_vsplitter( nullptr ),
65  m_fp_sel_ctrl( nullptr ),
66  m_fp_preview( nullptr ),
67  m_tree( nullptr ),
68  m_details( nullptr ),
69  m_parent( aParent ),
70  m_deMorganConvert( aDeMorganConvert >= 0 ? aDeMorganConvert : 0 ),
71  m_allow_field_edits( aAllowFieldEdits ),
72  m_show_footprints( aShowFootprints ),
73  m_external_browser_requested( false )
74 {
75  auto sizer = new wxBoxSizer( wxVERTICAL );
76 
77  // Use a slightly different layout, with a details pane spanning the entire window,
78  // if we're not showing footprints.
79  if( aShowFootprints )
80  {
81  m_hsplitter = new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
82  wxSP_LIVE_UPDATE );
83 
84  //Avoid the splitter window being assigned as the Parent to additional windows
85  m_hsplitter->SetExtraStyle( wxWS_EX_TRANSIENT );
86 
87  sizer->Add( m_hsplitter, 1, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 5 );
88  }
89  else
90  {
91  m_vsplitter = new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
92  wxSP_LIVE_UPDATE );
93 
94  m_hsplitter = new wxSplitterWindow( m_vsplitter, wxID_ANY, wxDefaultPosition, wxDefaultSize,
95  wxSP_LIVE_UPDATE );
96 
97  //Avoid the splitter window being assigned as the Parent to additional windows
98  m_hsplitter->SetExtraStyle( wxWS_EX_TRANSIENT );
99 
100  auto detailsPanel = new wxPanel( m_vsplitter );
101  auto detailsSizer = new wxBoxSizer( wxVERTICAL );
102  detailsPanel->SetSizer( detailsSizer );
103 
104  m_details = new wxHtmlWindow( detailsPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize,
105  wxHW_SCROLLBAR_AUTO );
106  detailsSizer->Add( m_details, 1, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 5 );
107  detailsPanel->Layout();
108  detailsSizer->Fit( detailsPanel );
109 
110  m_vsplitter->SetSashGravity( 0.5 );
111  m_vsplitter->SetMinimumPaneSize( 20 );
112  m_vsplitter->SplitHorizontally( m_hsplitter, detailsPanel );
113 
114  sizer->Add( m_vsplitter, 1, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 5 );
115  }
116 
117  m_tree = new LIB_TREE( m_hsplitter, Prj().SchSymbolLibTable(), aAdapter,
119 
120  m_hsplitter->SetSashGravity( 0.8 );
121  m_hsplitter->SetMinimumPaneSize( 20 );
122  m_hsplitter->SplitVertically( m_tree, ConstructRightPanel( m_hsplitter ) );
123 
124  m_dbl_click_timer = new wxTimer( this );
125 
126  auto buttonsSizer = new wxBoxSizer( wxHORIZONTAL );
127 
128  if( aAllowBrowser )
129  {
130  m_browser_button = new wxButton( this, wxID_ANY, _( "Select with Browser" ) );
131  buttonsSizer->Add( m_browser_button, 0, wxALL | wxALIGN_CENTER_VERTICAL, 5 );
132  }
133 
134  auto sdbSizer = new wxStdDialogButtonSizer();
135  auto okButton = new wxButton( this, wxID_OK );
136  auto cancelButton = new wxButton( this, wxID_CANCEL );
137  sdbSizer->AddButton( okButton );
138  sdbSizer->AddButton( cancelButton );
139  sdbSizer->Realize();
140 
141  buttonsSizer->Add( sdbSizer, 1, wxALL, 5 );
142 
143  sizer->Add( buttonsSizer, 0, wxEXPAND | wxLEFT, 5 );
144  SetSizer( sizer );
145 
146  Layout();
147 
148  // We specify the width of the right window (m_symbol_view_panel), because specify
149  // the width of the left window does not work as expected when SetSashGravity() is called
150  m_hsplitter->SetSashPosition( m_h_sash_pos ? m_h_sash_pos : HorizPixelsFromDU( 240 ) );
151 
152  if( m_vsplitter )
153  m_vsplitter->SetSashPosition( m_v_sash_pos ? m_v_sash_pos : VertPixelsFromDU( 170 ) );
154 
155  if( m_last_dlg_size == wxSize( -1, -1 ) )
156  SetSizeInDU( 360, 280 );
157  else
158  SetSize( m_last_dlg_size );
159 
161  okButton->SetDefault();
162 
163  Bind( wxEVT_INIT_DIALOG, &DIALOG_CHOOSE_COMPONENT::OnInitDialog, this );
164  Bind( wxEVT_TIMER, &DIALOG_CHOOSE_COMPONENT::OnCloseTimer, this, m_dbl_click_timer->GetId() );
165  Bind( COMPONENT_PRESELECTED, &DIALOG_CHOOSE_COMPONENT::OnComponentPreselected, this );
166  Bind( COMPONENT_SELECTED, &DIALOG_CHOOSE_COMPONENT::OnComponentSelected, this );
167 
168  if( m_browser_button )
169  m_browser_button->Bind( wxEVT_COMMAND_BUTTON_CLICKED,
171 
172  if( m_fp_sel_ctrl )
173  m_fp_sel_ctrl->Bind( EVT_FOOTPRINT_SELECTED,
175 
176  if( m_details )
177  m_details->Connect( wxEVT_CHAR_HOOK,
178  wxKeyEventHandler( DIALOG_CHOOSE_COMPONENT::OnCharHook ),
179  NULL, this );
180 }
181 
182 
184 {
185  Unbind( wxEVT_INIT_DIALOG, &DIALOG_CHOOSE_COMPONENT::OnInitDialog, this );
186  Unbind( wxEVT_TIMER, &DIALOG_CHOOSE_COMPONENT::OnCloseTimer, this );
187  Unbind( COMPONENT_PRESELECTED, &DIALOG_CHOOSE_COMPONENT::OnComponentPreselected, this );
188  Unbind( COMPONENT_SELECTED, &DIALOG_CHOOSE_COMPONENT::OnComponentSelected, this );
189 
190  if( m_browser_button )
191  m_browser_button->Unbind( wxEVT_COMMAND_BUTTON_CLICKED,
193 
194  if( m_fp_sel_ctrl )
195  m_fp_sel_ctrl->Unbind( EVT_FOOTPRINT_SELECTED,
197 
198  if( m_details )
199  m_details->Disconnect( wxEVT_CHAR_HOOK,
200  wxKeyEventHandler( DIALOG_CHOOSE_COMPONENT::OnCharHook ),
201  NULL, this );
202 
203  // I am not sure the following two lines are necessary, but they will not hurt anyone
204  m_dbl_click_timer->Stop();
205  delete m_dbl_click_timer;
206 
207  m_last_dlg_size = GetSize();
208  m_h_sash_pos = m_hsplitter->GetSashPosition();
209 
210  if( m_vsplitter )
211  m_v_sash_pos = m_vsplitter->GetSashPosition();
212 }
213 
214 
215 wxPanel* DIALOG_CHOOSE_COMPONENT::ConstructRightPanel( wxWindow* aParent )
216 {
217  auto panel = new wxPanel( aParent );
218  auto sizer = new wxBoxSizer( wxVERTICAL );
219 
221  m_parent->GetCanvas()->GetBackend() );
222  m_symbol_preview->SetLayoutDirection( wxLayout_LeftToRight );
223 
224  if( m_show_footprints )
225  {
227 
228  sizer->Add( m_symbol_preview, 1, wxEXPAND | wxTOP | wxBOTTOM | wxRIGHT, 5 );
229 
230  if ( fp_list )
231  {
232 
233  if( m_allow_field_edits )
234  m_fp_sel_ctrl = new FOOTPRINT_SELECT_WIDGET( panel, fp_list, true );
235 
236  m_fp_preview = new FOOTPRINT_PREVIEW_WIDGET( panel, Kiway() );
237  }
238 
239  if( m_fp_sel_ctrl )
240  sizer->Add( m_fp_sel_ctrl, 0, wxEXPAND | wxBOTTOM | wxTOP | wxRIGHT, 5 );
241 
242  if( m_fp_preview )
243  sizer->Add( m_fp_preview, 1, wxEXPAND | wxBOTTOM | wxRIGHT, 5 );
244  }
245  else
246  {
247  sizer->Add( m_symbol_preview, 1, wxEXPAND | wxTOP | wxRIGHT, 5 );
248  }
249 
250  panel->SetSizer( sizer );
251  panel->Layout();
252  sizer->Fit( panel );
253 
254  return panel;
255 }
256 
257 
258 void DIALOG_CHOOSE_COMPONENT::OnInitDialog( wxInitDialogEvent& aEvent )
259 {
261  {
262  // This hides the GAL panel and shows the status label
263  m_fp_preview->SetStatusText( wxEmptyString );
264  }
265 
266  if( m_fp_sel_ctrl )
267  m_fp_sel_ctrl->Load( Kiway(), Prj() );
268 }
269 
270 
272 {
273  if( m_details && e.GetKeyCode() == 'C' && e.ControlDown() &&
274  !e.AltDown() && !e.ShiftDown() && !e.MetaDown() )
275  {
276  wxString txt = m_details->SelectionToText();
277 
278  if( wxTheClipboard->Open() )
279  {
280  wxTheClipboard->SetData( new wxTextDataObject( txt ) );
281  wxTheClipboard->Close();
282  }
283  }
284  else
285  {
286  e.Skip();
287  }
288 }
289 
290 
292 {
293  return m_tree->GetSelectedLibId( aUnit );
294 }
295 
296 
297 void DIALOG_CHOOSE_COMPONENT::OnUseBrowser( wxCommandEvent& aEvent )
298 {
300  EndQuasiModal( wxID_OK );
301 }
302 
303 
304 void DIALOG_CHOOSE_COMPONENT::OnCloseTimer( wxTimerEvent& aEvent )
305 {
306  // Hack handler because of eaten MouseUp event. See
307  // DIALOG_CHOOSE_COMPONENT::OnComponentSelected for the beginning
308  // of this spaghetti noodle.
309 
310  auto state = wxGetMouseState();
311 
312  if( state.LeftIsDown() )
313  {
314  // Mouse hasn't been raised yet, so fire the timer again. Otherwise the
315  // purpose of this timer is defeated.
317  }
318  else
319  {
320  EndQuasiModal( wxID_OK );
321  }
322 }
323 
324 
326 {
328  return;
329 
330  LIB_ALIAS* alias = nullptr;
331 
332  try
333  {
334  alias = Prj().SchSymbolLibTable()->LoadSymbol( aLibId );
335  }
336  catch( const IO_ERROR& ioe )
337  {
338  wxLogError( wxString::Format( _( "Error loading symbol %s from library %s.\n\n%s" ),
339  aLibId.GetLibItemName().wx_str(),
340  aLibId.GetLibNickname().wx_str(),
341  ioe.What() ) );
342  }
343 
344  if( !alias )
345  return;
346 
347  LIB_FIELD* fp_field = alias->GetPart()->GetField( FOOTPRINT );
348  wxString fp_name = fp_field ? fp_field->GetFullText() : wxString( "" );
349 
350  ShowFootprint( fp_name );
351 }
352 
353 
354 void DIALOG_CHOOSE_COMPONENT::ShowFootprint( wxString const& aName )
355 {
357  return;
358 
359  if( aName == wxEmptyString )
360  {
361  m_fp_preview->SetStatusText( _( "No footprint specified" ) );
362  }
363  else
364  {
365  LIB_ID lib_id;
366 
367  if( lib_id.Parse( aName, LIB_ID::ID_PCB ) == -1 && lib_id.IsValid() )
368  {
370  m_fp_preview->CacheFootprint( lib_id );
371  m_fp_preview->DisplayFootprint( lib_id );
372  }
373  else
374  {
375  m_fp_preview->SetStatusText( _( "Invalid footprint specified" ) );
376  }
377  }
378 }
379 
380 
382 {
383  if( !m_fp_sel_ctrl )
384  return;
385 
387 
388  LIB_ALIAS* alias = nullptr;
389 
390  if( aLibId.IsValid() )
391  {
392  try
393  {
394  alias = Prj().SchSymbolLibTable()->LoadSymbol( aLibId );
395  }
396  catch( const IO_ERROR& ioe )
397  {
398  wxLogError( wxString::Format( _( "Error occurred loading symbol %s from library %s."
399  "\n\n%s" ),
400  aLibId.GetLibItemName().wx_str(),
401  aLibId.GetLibNickname().wx_str(),
402  ioe.What() ) );
403  }
404  }
405 
406  if( alias != nullptr )
407  {
408  LIB_PINS temp_pins;
409  LIB_FIELD* fp_field = alias->GetPart()->GetField( FOOTPRINT );
410  wxString fp_name = fp_field ? fp_field->GetFullText() : wxString( "" );
411 
412  alias->GetPart()->GetPins( temp_pins );
413 
414  m_fp_sel_ctrl->FilterByPinCount( temp_pins.size() );
419  }
420  else
421  {
423  m_fp_sel_ctrl->Disable();
424  }
425 }
426 
427 
428 void DIALOG_CHOOSE_COMPONENT::OnFootprintSelected( wxCommandEvent& aEvent )
429 {
430  m_fp_override = aEvent.GetString();
431 
432  m_field_edits.erase(
433  std::remove_if( m_field_edits.begin(), m_field_edits.end(),
434  []( std::pair<int, wxString> const& i ) { return i.first == FOOTPRINT; } ),
435  m_field_edits.end() );
436 
437  m_field_edits.push_back( std::make_pair( FOOTPRINT, m_fp_override ) );
438 
440 }
441 
442 
444 {
445  int unit = 0;
446 
447  LIB_ID id = m_tree->GetSelectedLibId( &unit );
448 
449 
450  if( id.IsValid() )
451  {
452  m_symbol_preview->DisplaySymbol( id, unit );
453 
454  ShowFootprintFor( id );
456  }
457  else
458  {
459  m_symbol_preview->SetStatusText( _( "No symbol selected" ) );
460 
462  m_fp_preview->SetStatusText( wxEmptyString );
463 
465  }
466 }
467 
468 
469 void DIALOG_CHOOSE_COMPONENT::OnComponentSelected( wxCommandEvent& aEvent )
470 {
471  if( m_tree->GetSelectedLibId().IsValid() )
472  {
473  // Got a selection. We can't just end the modal dialog here, because
474  // wx leaks some events back to the parent window (in particular, the
475  // MouseUp following a double click).
476  //
477  // NOW, here's where it gets really fun. wxTreeListCtrl eats MouseUp.
478  // This isn't really feasible to bypass without a fully custom
479  // wxDataViewCtrl implementation, and even then might not be fully
480  // possible (docs are vague). To get around this, we use a one-shot
481  // timer to schedule the dialog close.
482  //
483  // See DIALOG_CHOOSE_COMPONENT::OnCloseTimer for the other end of this
484  // spaghetti noodle.
486  }
487 }
488 
489 
FOOTPRINT_SELECT_WIDGET * m_fp_sel_ctrl
LIB_FIELD * GetField(int aId)
Return pointer to the requested field.
Part library alias object definition.
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_holder.h:56
void FilterByFootprintFilters(wxArrayString const &aFilters, bool aZeroFilters)
Filter by footprint filter list.
std::vector< std::pair< int, wxString > > m_field_edits
void PopulateFootprintSelector(LIB_ID const &aLibId)
Populate the footprint selector for a given alias.
void OnFootprintSelected(wxCommandEvent &aEvent)
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:55
Field object used in symbol libraries.
Definition: lib_field.h:59
SYMBOL_PREVIEW_WIDGET * m_symbol_preview
void SetDefaultFootprint(wxString const &aFp)
Set the default footprint for a part.
void ShowFootprint(wxString const &aFootprint)
Display the given footprint by name.
Class DIALOG_SHIM may sit in the inheritance tree between wxDialog and any class written by wxFormBui...
Definition: dialog_shim.h:83
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
bool IsValid() const
Definition: lib_id.h:171
void SetStatusText(wxString const &aText)
Set the contents of the status label and display it.
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:118
Field Name Module PCB, i.e. "16DIP300".
void OnComponentPreselected(wxCommandEvent &aEvent)
void ClearFilters()
Clear all filters.
void OnCharHook(wxKeyEvent &aEvt)
bool UpdateList()
Update the contents of the list to match the filters.
void DisplaySymbol(const LIB_ID &aSymbolID, int aUnit)
Set the currently displayed symbol.
void GetPins(LIB_PINS &aList, int aUnit=0, int aConvert=0)
Return a list of pin object pointers from the draw item list.
static FOOTPRINT_LIST * GetInstance(KIWAY &aKiway)
Factory function to return a FOOTPRINT_LIST via Kiway.
DIALOG_CHOOSE_COMPONENT(SCH_BASE_FRAME *aParent, const wxString &aTitle, SYMBOL_TREE_MODEL_ADAPTER::PTR &aAdapter, int aDeMorganConvert, bool aAllowFieldEdits, bool aShowFootprints, bool aAllowBrowser)
Create dialog to choose component.
void OnInitDialog(wxInitDialogEvent &aEvent)
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
void ClearStatus()
Clear the contents of the status label and hide it.
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
For multi-unit components, if the user selects the component itself rather than picking an individual...
Definition: lib_tree.cpp:141
void SetSizeInDU(int x, int y)
Set the dialog to the given dimensions in "dialog units".
void OnCloseTimer(wxTimerEvent &aEvent)
virtual bool Enable(bool aEnable=true) override
Enable or disable the control for input.
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
wxPanel * ConstructRightPanel(wxWindow *aParent)
Definition: hash_eda.h:46
LIB_PART * GetPart() const
Get the shared LIB_PART.
void SetStatusText(wxString const &aText)
Set the contents of the status label and display it.
void OnUseBrowser(wxCommandEvent &aEvent)
#define _(s)
wxArrayString & GetFootprints()
int VertPixelsFromDU(int y)
Convert an integer number of dialog units to pixels, vertically.
void EndQuasiModal(int retCode)
void CacheFootprint(const LIB_ID &aFPID)
Preload a footprint into the cache.
Holds a list of FOOTPRINT_INFO objects, along with a list of IO_ERRORs or PARSE_ERRORs that were thro...
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
void DisplayFootprint(const LIB_ID &aFPID)
Set the currently displayed footprint.
static constexpr int DblClickDelay
size_t i
Definition: json11.cpp:597
wxString wx_str() const
Definition: utf8.cpp:51
wxString GetFullText(int unit=1) const
Return the text of a field.
Definition: lib_field.cpp:301
FOOTPRINT_PREVIEW_WIDGET * m_fp_preview
void Load(KIWAY &aKiway, PROJECT &aProject)
Start loading.
int HorizPixelsFromDU(int x)
Convert an integer number of dialog units to pixels, horizontally.
void ShowFootprintFor(LIB_ID const &aLibId)
Look up the footprint for a given symbol specified in the LIB_ID and display it.
GAL_TYPE GetBackend() const
Function GetBackend Returns the type of backend currently used by GAL canvas.
wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > PTR
Reference-counting container for a pointer to CMP_TREE_MODEL_ADAPTER_BASE.
Definition for part library class.
int Parse(const UTF8 &aId, LIB_ID_TYPE aType, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:122
bool IsInitialized() const
Return whether the widget initialized properly.
A shim class between EDA_DRAW_FRAME and several derived classes: LIB_EDIT_FRAME, LIB_VIEW_FRAME,...
void OnComponentSelected(wxCommandEvent &aEvent)
Handle the selection of an item.
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
To be called after this dialog returns from ShowModal().
void FilterByPinCount(int aPinCount)
Filter by pin count.
Widget displaying a tree of components with optional search text control and description panel.
Definition: lib_tree.h:42