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