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-2017 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 
30 #include <fctsys.h>
31 #include <wx/wupdlock.h>
32 
33 #include <cvpcb.h>
34 #include <cvpcb_mainframe.h>
35 #include <listboxes.h>
36 #include <cvpcb_id.h>
37 #include <eda_pattern_match.h>
38 #include <footprint_filter.h>
39 
40 
42  wxWindowID id, const wxPoint& loc,
43  const wxSize& size ) :
44  ITEMS_LISTBOX_BASE( parent, id, loc, size, wxLC_SINGLE_SEL | wxNO_BORDER )
45 {
46 }
47 
48 
50 {
51 }
52 
53 
55 {
56  return m_footprintList.Count();
57 }
58 
59 
60 void FOOTPRINTS_LISTBOX::SetString( unsigned linecount, const wxString& text )
61 {
62  unsigned count = m_footprintList.Count();
63  if( count > 0 )
64  {
65  if( linecount >= count )
66  linecount = count - 1;
67  m_footprintList[linecount] = text;
68  }
69  UpdateWidth( linecount );
70 }
71 
72 
74 {
75  wxString footprintName;
76  int ii = GetFirstSelected();
77 
78  if( ii >= 0 )
79  {
80  wxString msg = m_footprintList[ii];
81  msg.Trim( true );
82  msg.Trim( false );
83  footprintName = msg.AfterFirst( wxChar( ' ' ) );
84  }
85 
86  return footprintName;
87 }
88 
89 
90 void FOOTPRINTS_LISTBOX::AppendLine( const wxString& text )
91 {
92  m_footprintList.Add( text );
93  int lines = m_footprintList.Count();
94  SetItemCount( lines );
95  UpdateWidth( lines - 1 );
96 }
97 
98 
99 wxString FOOTPRINTS_LISTBOX::OnGetItemText( long item, long column ) const
100 {
101  if( item < 0 || item >= (long)m_footprintList.GetCount() )
102  return wxEmptyString;
103 
104  return m_footprintList.Item( item );
105 }
106 
107 
108 void FOOTPRINTS_LISTBOX::SetSelection( int index, bool State )
109 {
110  if( index >= GetCount() )
111  index = GetCount() - 1;
112 
113  if( (index >= 0) && (GetCount() > 0) )
114  {
115  Select( index, State );
116  EnsureVisible( index );
117  Refresh();
118  }
119 }
120 
121 
123 {
124  wxString id = wxString::Format( "%s:%s",
125  GetChars( aFPID.GetLibNickname() ),
126  GetChars( aFPID.GetLibItemName() ) );
127 
128  for( int i = 0; i < GetCount(); ++i )
129  {
130  wxString candidate = m_footprintList.Item( i ).substr( 4 );
131 
132  if( candidate.CmpNoCase( id ) == 0 )
133  {
134  SetSelection( i, true );
135  return;
136  }
137  }
138 }
139 
140 
141 void FOOTPRINTS_LISTBOX::SetFootprints( FOOTPRINT_LIST& aList, const wxString& aLibName,
142  COMPONENT* aComponent,
143  const wxString &aFootPrintFilterPattern,
144  int aFilterType )
145 {
146  wxArrayString newList;
147  wxString msg;
148  wxString oldSelection;
149 
150  FOOTPRINT_FILTER filter( aList );
151 
152  if( aFilterType & FILTERING_BY_COMPONENT_KEYWORD && aComponent )
153  filter.FilterByFootprintFilters( aComponent->GetFootprintFilters() );
154 
155  if( aFilterType & FILTERING_BY_PIN_COUNT && aComponent )
156  filter.FilterByPinCount( aComponent->GetPinCount() );
157 
158  if( aFilterType & FILTERING_BY_LIBRARY )
159  filter.FilterByLibrary( aLibName );
160 
161  if( aFilterType & FILTERING_BY_NAME )
162  filter.FilterByPattern( aFootPrintFilterPattern );
163 
164  if( GetSelection() >= 0 && GetSelection() < (int)m_footprintList.GetCount() )
165  oldSelection = m_footprintList[ GetSelection() ];
166 
167  for( auto& i: filter )
168  {
169  msg.Printf( "%3d %s:%s", int( newList.GetCount() + 1 ),
170  GetChars( i.GetLibNickname() ),
171  GetChars( i.GetFootprintName() ) );
172  newList.Add( msg );
173  }
174 
175  if( newList == m_footprintList )
176  return;
177 
178  m_footprintList = newList;
179 
180  int selection = m_footprintList.Index( oldSelection );
181 
182  if( selection == wxNOT_FOUND )
183  selection = 0;
184 
185  wxWindowUpdateLocker freeze( this );
186  DeleteAllItems();
187 
188  if( m_footprintList.GetCount() )
189  {
190  SetItemCount( m_footprintList.GetCount() );
191  SetSelection( selection, true );
192  RefreshItems( 0L, m_footprintList.GetCount()-1 );
193  UpdateWidth();
194  }
195 }
196 
197 
198 BEGIN_EVENT_TABLE( FOOTPRINTS_LISTBOX, ITEMS_LISTBOX_BASE )
199  EVT_CHAR( FOOTPRINTS_LISTBOX::OnChar )
202 END_EVENT_TABLE()
203 
204 
205 void FOOTPRINTS_LISTBOX::OnLeftClick( wxListEvent& event )
206 {
207  if( m_footprintList.IsEmpty() )
208  return;
209 
210  // On some plateforms (OSX) the focus is lost when the viewers (fp and 3D viewers)
211  // are opened and refreshed when a new footprint is selected.
212  // If the listbox has the focus before selecting a new footprint, it will be forced
213  // after selection.
214  bool hasFocus = HasFocus();
215 
216  // If the footprint view window is displayed, update the footprint.
217  if( GetParent()->GetFootprintViewerFrame() )
219 
221 
222  if( hasFocus )
223  SetFocus();
224 }
225 
226 
227 void FOOTPRINTS_LISTBOX::OnLeftDClick( wxListEvent& event )
228 {
229  wxString footprintName = GetSelectedFootprint();
230 
231  GetParent()->SetNewPkg( footprintName );
232 }
233 
234 
235 void FOOTPRINTS_LISTBOX::OnChar( wxKeyEvent& event )
236 {
237  int key = event.GetKeyCode();
238 
239  switch( key )
240  {
241  case WXK_TAB:
242  case WXK_RIGHT:
243  case WXK_NUMPAD_RIGHT:
244  GetParent()->ChangeFocus( true );
245  return;
246 
247  case WXK_LEFT:
248  case WXK_NUMPAD_LEFT:
249  GetParent()->ChangeFocus( false );
250  return;
251 
252  case WXK_HOME:
253  case WXK_END:
254  case WXK_UP:
255  case WXK_DOWN:
256  case WXK_PAGEUP:
257  case WXK_PAGEDOWN:
258  event.Skip();
259  return;
260 
261  default:
262  break;
263  }
264 
265  // Search for an item name starting by the key code:
266  key = toupper( key );
267 
268  for( unsigned ii = 0; ii < m_footprintList.GetCount(); ii++ )
269  {
270  wxString text = m_footprintList.Item( ii );
271 
272  // Search for the start char of the footprint name. Skip the line number.
273  text.Trim( false ); // Remove leading spaces in line
274  unsigned jj = 0;
275 
276  for( ; jj < text.Len(); jj++ )
277  {
278  // skip line number
279  if( text[jj] == ' ' )
280  break;
281  }
282 
283  for( ; jj < text.Len(); jj++ )
284  { // skip blanks
285  if( text[jj] != ' ' )
286  break;
287  }
288 
289  int start_char = toupper( text[jj] );
290 
291  if( key == start_char )
292  {
293  SetSelection( ii, true ); // Ensure visible
294  break;
295  }
296  }
297 
298  event.Skip();
299 }
void OnChar(wxKeyEvent &event)
void FilterByPattern(wxString const &aPattern)
Add a pattern to filter by name, including wildcards and optionally a colon-delimited library name...
int GetPinCount() const
Definition: pcb_netlist.h:184
void SetSelectedFootprint(const LIB_ID &aFPID)
const wxArrayString & GetFootprintFilters() const
Definition: pcb_netlist.h:177
void FilterByFootprintFilters(wxArrayString const &aFilters)
Set a list of footprint filters to filter by.
void SetNewPkg(const wxString &aFootprintName)
Function SetNewPkg set the footprint name for all selected components in component list and selects t...
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 ChangeFocus(bool aMoveRight)
void CreateScreenCmp()
Create or Update the frame showing the current highlighted footprint and (if showed) the 3D display f...
Abstract pattern-matching tool and implementations.
void FilterByPinCount(int aPinCount)
Set a pin count to filter by.
void UpdateWidth(int aLine=-1)
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
wxArrayString m_footprintList
Definition: listboxes.h:90
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...
Class COMPONENT is used to store components and all of their related information found in a netlist...
Definition: pcb_netlist.h:83
void SetSelection(int index, bool State=true)
void AppendLine(const wxString &text)
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
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
size_t i
Definition: json11.cpp:597
FOOTPRINTS_LISTBOX(CVPCB_MAINFRAME *parent, wxWindowID id, const wxPoint &loc, const wxSize &size)
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...
virtual CVPCB_MAINFRAME * GetParent() const
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
void OnLeftClick(wxListEvent &event)
void DisplayStatus()
Function DisplayStatus updates the information displayed on the status bar at bottom of the main fram...
The CvPcb application main window.