KiCad PCB EDA Suite
footprints_listbox.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) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2020 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 <footprint_filter.h>
26 #include <tool/tool_manager.h>
27 #include <trace_helpers.h>
28 #include <wx/wupdlock.h>
29 
30 #include <cvpcb_id.h>
31 #include <cvpcb_mainframe.h>
32 #include <listboxes.h>
33 #include <tools/cvpcb_actions.h>
34 
36  ITEMS_LISTBOX_BASE( parent, id, wxDefaultPosition, wxDefaultSize, wxLC_SINGLE_SEL|wxNO_BORDER )
37 {
38 }
39 
40 
42 {
43 }
44 
45 
47 {
48  return m_footprintList.Count();
49 }
50 
51 
52 void FOOTPRINTS_LISTBOX::SetString( unsigned linecount, const wxString& text )
53 {
54  unsigned count = m_footprintList.Count();
55  if( count > 0 )
56  {
57  if( linecount >= count )
58  linecount = count - 1;
59 
60  m_footprintList[linecount] = text;
61  }
62  UpdateWidth( linecount );
63 }
64 
65 
67 {
68  wxString footprintName;
69  int ii = GetFirstSelected();
70 
71  if( ii >= 0 )
72  {
73  wxString msg = m_footprintList[ii];
74  msg.Trim( true );
75  msg.Trim( false );
76  footprintName = msg.AfterFirst( wxChar( ' ' ) );
77  }
78 
79  return footprintName;
80 }
81 
82 
83 void FOOTPRINTS_LISTBOX::AppendLine( const wxString& text )
84 {
85  m_footprintList.Add( text );
86  int lines = m_footprintList.Count();
87  SetItemCount( lines );
88  UpdateWidth( lines - 1 );
89 }
90 
91 
92 wxString FOOTPRINTS_LISTBOX::OnGetItemText( long item, long column ) const
93 {
94  if( item < 0 || item >= (long)m_footprintList.GetCount() )
95  return wxEmptyString;
96 
97  return m_footprintList.Item( item );
98 }
99 
100 
101 void FOOTPRINTS_LISTBOX::SetSelection( int index, bool State )
102 {
103  if( index >= GetCount() )
104  index = GetCount() - 1;
105 
106  if( (index >= 0) && (GetCount() > 0) )
107  {
108  Select( index, State );
109  EnsureVisible( index );
110  Refresh();
111  }
112 }
113 
114 
116 {
117  wxString id = aFPID.Format().wx_str();
118 
119  for( int i = 0; i < GetCount(); ++i )
120  {
121  wxString candidate = m_footprintList.Item( i ).substr( 4 );
122 
123  if( candidate.CmpNoCase( id ) == 0 )
124  {
125  SetSelection( i, true );
126  return;
127  }
128  }
129 }
130 
131 
132 void FOOTPRINTS_LISTBOX::SetFootprints( FOOTPRINT_LIST& aList, const wxString& aLibName,
133  COMPONENT* aComponent,
134  const wxString &aFootPrintFilterPattern,
135  int aFilterType )
136 {
137  wxArrayString newList;
138  wxString msg;
139  wxString oldSelection;
140 
141  FOOTPRINT_FILTER filter( aList );
142 
143  if( aFilterType & FILTERING_BY_COMPONENT_FP_FILTERS && aComponent )
144  filter.FilterByFootprintFilters( aComponent->GetFootprintFilters() );
145 
146  if( aFilterType & FILTERING_BY_PIN_COUNT && aComponent )
147  filter.FilterByPinCount( aComponent->GetPinCount() );
148 
149  if( aFilterType & FILTERING_BY_LIBRARY )
150  filter.FilterByLibrary( aLibName );
151 
152  if( !aFootPrintFilterPattern.IsEmpty() )
153  filter.FilterByTextPattern( aFootPrintFilterPattern );
154 
155  if( GetSelection() >= 0 && GetSelection() < (int)m_footprintList.GetCount() )
156  oldSelection = m_footprintList[ GetSelection() ];
157 
158  for( auto& i: filter )
159  {
160  msg.Printf( "%3d %s:%s",
161  int( newList.GetCount() + 1 ),
162  i.GetLibNickname(),
163  i.GetFootprintName() );
164  newList.Add( msg );
165  }
166 
167  if( newList == m_footprintList )
168  return;
169 
170  m_footprintList = newList;
171 
172  int selection = m_footprintList.Index( oldSelection );
173 
174  if( selection == wxNOT_FOUND )
175  selection = 0;
176 
177  wxWindowUpdateLocker freeze( this );
178  DeleteAllItems();
179 
180  if( m_footprintList.GetCount() )
181  {
182  SetItemCount( m_footprintList.GetCount() );
183  SetSelection( selection, true );
184  RefreshItems( 0L, m_footprintList.GetCount()-1 );
185  UpdateWidth();
186  }
187 }
188 
189 
190 BEGIN_EVENT_TABLE( FOOTPRINTS_LISTBOX, ITEMS_LISTBOX_BASE )
191  EVT_CHAR( FOOTPRINTS_LISTBOX::OnChar )
194 END_EVENT_TABLE()
195 
196 
197 void FOOTPRINTS_LISTBOX::OnLeftClick( wxListEvent& event )
198 {
199  if( m_footprintList.IsEmpty() )
200  return;
201 
202  // On some plateforms (OSX) the focus is lost when the viewers (fp and 3D viewers)
203  // are opened and refreshed when a new footprint is selected.
204  // If the listbox has the focus before selecting a new footprint, it will be forced
205  // after selection.
206  bool hasFocus = HasFocus();
207 
208  // If the footprint view window is displayed, update the footprint.
209  if( GetParent()->GetFootprintViewerFrame() )
210  GetParent()->GetToolManager()->RunAction( CVPCB_ACTIONS::showFootprintViewer, true );
211 
212  GetParent()->DisplayStatus();
213 
214  if( hasFocus )
215  SetFocus();
216 }
217 
218 
219 void FOOTPRINTS_LISTBOX::OnLeftDClick( wxListEvent& event )
220 {
222 }
223 
224 
225 void FOOTPRINTS_LISTBOX::OnChar( wxKeyEvent& event )
226 {
227  wxLogTrace( kicadTraceKeyEvent, "FOOTPRINTS_LISTBOX::OnChar %s", dump( event ) );
228 
229  int key = event.GetKeyCode();
230 
231  switch( key )
232  {
233  case WXK_HOME:
234  case WXK_END:
235  case WXK_UP:
236  case WXK_DOWN:
237  case WXK_PAGEUP:
238  case WXK_PAGEDOWN:
239  event.Skip();
240  return;
241 
242  default:
243  break;
244  }
245 
246  // Search for an item name starting by the key code:
247  key = toupper( key );
248 
249  for( unsigned ii = 0; ii < m_footprintList.GetCount(); ii++ )
250  {
251  wxString text = m_footprintList.Item( ii );
252 
253  // Search for the start char of the footprint name. Skip the line number.
254  text.Trim( false ); // Remove leading spaces in line
255  unsigned jj = 0;
256 
257  for( ; jj < text.Len(); jj++ )
258  {
259  // skip line number
260  if( text[jj] == ' ' )
261  break;
262  }
263 
264  for( ; jj < text.Len(); jj++ )
265  { // skip blanks
266  if( text[jj] != ' ' )
267  break;
268  }
269 
270  int start_char = toupper( text[jj] );
271 
272  if( key == start_char )
273  {
274  SetSelection( ii, true ); // Ensure visible
275  break;
276  }
277  }
278 
279  event.Skip();
280 }
static TOOL_ACTION showFootprintViewer
Open the footprint viewer.
Definition: cvpcb_actions.h:52
void OnChar(wxKeyEvent &event)
void SetSelectedFootprint(const LIB_ID &aFPID)
void FilterByFootprintFilters(wxArrayString const &aFilters)
Set a list of footprint filters to filter by.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:140
void OnLeftDClick(wxListEvent &event)
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
void FilterByLibrary(wxString const &aLibName)
Add library name to filter criteria.
void SetString(unsigned linecount, const wxString &text)
void FilterByPinCount(int aPinCount)
Set a pin count to filter by.
void UpdateWidth(int aLine=-1)
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
wxArrayString m_footprintList
Definition: listboxes.h:86
wxLogTrace helper definitions.
Footprint display filter.
wxString OnGetItemText(long item, long column) const override
Function OnGetItemText this overloaded function MUST be provided for the wxLC_VIRTUAL mode because re...
UTF8 Format() const
Definition: lib_id.cpp:237
COMPONENT is used to store components and all of their related information found in a netlist.
Definition: pcb_netlist.h:80
void SetSelection(int index, bool State=true)
FOOTPRINTS_LISTBOX(CVPCB_MAINFRAME *parent, wxWindowID id)
void AppendLine(const wxString &text)
Holds a list of FOOTPRINT_INFO objects, along with a list of IO_ERRORs or PARSE_ERRORs that were thro...
void FilterByTextPattern(wxString const &aPattern)
Add a pattern to filter by name, including wildcards and optionally a colon-delimited library name.
wxString wx_str() const
Definition: utf8.cpp:51
int GetPinCount() const
Definition: pcb_netlist.h:160
const wxChar *const kicadTraceKeyEvent
Flag to enable wxKeyEvent debug tracing.
void SetFootprints(FOOTPRINT_LIST &aList, const wxString &aLibName, COMPONENT *aComponent, const wxString &aFootPrintFilterPattern, int aFilterType)
Function SetFootprints populates the wxListCtrl with the footprints from aList that meet the filter c...
const wxArrayString & GetFootprintFilters() const
Definition: pcb_netlist.h:157
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:74
static TOOL_ACTION associate
Definition: cvpcb_actions.h:64
void OnLeftClick(wxListEvent &event)
The CvPcb application main window.
virtual CVPCB_MAINFRAME * GetParent() const