You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
7.4 KiB

7 years ago
7 years ago
7 years ago
12 years ago
12 years ago
7 years ago
  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. #include <footprint_filter.h>
  25. #include <tool/tool_manager.h>
  26. #include <trace_helpers.h>
  27. #include <wx/wupdlock.h>
  28. #include <cvpcb_id.h>
  29. #include <cvpcb_mainframe.h>
  30. #include <listboxes.h>
  31. #include <tools/cvpcb_actions.h>
  32. FOOTPRINTS_LISTBOX::FOOTPRINTS_LISTBOX( CVPCB_MAINFRAME* parent, wxWindowID id ) :
  33. ITEMS_LISTBOX_BASE( parent, id, wxDefaultPosition, wxDefaultSize, wxLC_SINGLE_SEL|wxNO_BORDER )
  34. {
  35. }
  36. FOOTPRINTS_LISTBOX::~FOOTPRINTS_LISTBOX()
  37. {
  38. }
  39. int FOOTPRINTS_LISTBOX::GetCount()
  40. {
  41. return m_footprintList.Count();
  42. }
  43. void FOOTPRINTS_LISTBOX::SetString( unsigned linecount, const wxString& text )
  44. {
  45. unsigned count = m_footprintList.Count();
  46. if( count > 0 )
  47. {
  48. if( linecount >= count )
  49. linecount = count - 1;
  50. m_footprintList[linecount] = text;
  51. }
  52. UpdateWidth( linecount );
  53. }
  54. wxString FOOTPRINTS_LISTBOX::GetSelectedFootprint()
  55. {
  56. wxString footprintName;
  57. int ii = GetFirstSelected();
  58. if( ii >= 0 )
  59. {
  60. wxString msg = m_footprintList[ii];
  61. msg.Trim( true );
  62. msg.Trim( false );
  63. footprintName = msg.AfterFirst( wxChar( ' ' ) );
  64. }
  65. return footprintName;
  66. }
  67. void FOOTPRINTS_LISTBOX::AppendLine( const wxString& text )
  68. {
  69. m_footprintList.Add( text );
  70. int lines = m_footprintList.Count();
  71. SetItemCount( lines );
  72. UpdateWidth( lines - 1 );
  73. }
  74. wxString FOOTPRINTS_LISTBOX::OnGetItemText( long item, long column ) const
  75. {
  76. if( item < 0 || item >= (long)m_footprintList.GetCount() )
  77. return wxEmptyString;
  78. return m_footprintList.Item( item );
  79. }
  80. void FOOTPRINTS_LISTBOX::SetSelection( int index, bool State )
  81. {
  82. if( index >= GetCount() )
  83. index = GetCount() - 1;
  84. if( (index >= 0) && (GetCount() > 0) )
  85. {
  86. Select( index, State );
  87. EnsureVisible( index );
  88. Refresh();
  89. }
  90. }
  91. void FOOTPRINTS_LISTBOX::SetSelectedFootprint( const LIB_ID& aFPID )
  92. {
  93. wxString id = aFPID.Format().wx_str();
  94. for( int i = 0; i < GetCount(); ++i )
  95. {
  96. wxString candidate = m_footprintList.Item( i ).substr( 4 );
  97. if( candidate.CmpNoCase( id ) == 0 )
  98. {
  99. SetSelection( i, true );
  100. return;
  101. }
  102. }
  103. }
  104. void FOOTPRINTS_LISTBOX::SetFootprints( FOOTPRINT_LIST& aList, const wxString& aLibName,
  105. COMPONENT* aComponent,
  106. const wxString &aFootPrintFilterPattern,
  107. int aFilterType )
  108. {
  109. wxArrayString newList;
  110. wxString msg;
  111. wxString oldSelection;
  112. FOOTPRINT_FILTER filter( aList );
  113. if( aFilterType & FILTERING_BY_COMPONENT_FP_FILTERS && aComponent )
  114. filter.FilterByFootprintFilters( aComponent->GetFootprintFilters() );
  115. if( aFilterType & FILTERING_BY_PIN_COUNT && aComponent )
  116. filter.FilterByPinCount( aComponent->GetPinCount() );
  117. if( aFilterType & FILTERING_BY_LIBRARY )
  118. filter.FilterByLibrary( aLibName );
  119. if( !aFootPrintFilterPattern.IsEmpty() )
  120. filter.FilterByTextPattern( aFootPrintFilterPattern );
  121. if( GetSelection() >= 0 && GetSelection() < (int)m_footprintList.GetCount() )
  122. oldSelection = m_footprintList[ GetSelection() ];
  123. for( auto& i: filter )
  124. {
  125. msg.Printf( "%3d %s:%s",
  126. int( newList.GetCount() + 1 ),
  127. i.GetLibNickname(),
  128. i.GetFootprintName() );
  129. newList.Add( msg );
  130. }
  131. if( newList == m_footprintList )
  132. return;
  133. m_footprintList = newList;
  134. int selection = m_footprintList.Index( oldSelection );
  135. if( selection == wxNOT_FOUND )
  136. selection = 0;
  137. wxWindowUpdateLocker freeze( this );
  138. DeleteAllItems();
  139. if( m_footprintList.GetCount() )
  140. {
  141. SetItemCount( m_footprintList.GetCount() );
  142. SetSelection( selection, true );
  143. RefreshItems( 0L, m_footprintList.GetCount()-1 );
  144. UpdateWidth();
  145. }
  146. }
  147. BEGIN_EVENT_TABLE( FOOTPRINTS_LISTBOX, ITEMS_LISTBOX_BASE )
  148. EVT_CHAR( FOOTPRINTS_LISTBOX::OnChar )
  149. EVT_LIST_ITEM_SELECTED( ID_CVPCB_FOOTPRINT_LIST, FOOTPRINTS_LISTBOX::OnLeftClick )
  150. EVT_LIST_ITEM_ACTIVATED( ID_CVPCB_FOOTPRINT_LIST, FOOTPRINTS_LISTBOX::OnLeftDClick )
  151. END_EVENT_TABLE()
  152. void FOOTPRINTS_LISTBOX::OnLeftClick( wxListEvent& event )
  153. {
  154. if( m_footprintList.IsEmpty() )
  155. return;
  156. // On some plateforms (OSX) the focus is lost when the viewers (fp and 3D viewers)
  157. // are opened and refreshed when a new footprint is selected.
  158. // If the listbox has the focus before selecting a new footprint, it will be forced
  159. // after selection.
  160. bool hasFocus = HasFocus();
  161. // If the footprint view window is displayed, update the footprint.
  162. if( GetParent()->GetFootprintViewerFrame() )
  163. GetParent()->GetToolManager()->RunAction( CVPCB_ACTIONS::showFootprintViewer, true );
  164. GetParent()->DisplayStatus();
  165. if( hasFocus )
  166. SetFocus();
  167. }
  168. void FOOTPRINTS_LISTBOX::OnLeftDClick( wxListEvent& event )
  169. {
  170. GetParent()->GetToolManager()->RunAction( CVPCB_ACTIONS::associate, true );
  171. }
  172. void FOOTPRINTS_LISTBOX::OnChar( wxKeyEvent& event )
  173. {
  174. wxLogTrace( kicadTraceKeyEvent, "FOOTPRINTS_LISTBOX::OnChar %s", dump( event ) );
  175. int key = event.GetKeyCode();
  176. switch( key )
  177. {
  178. case WXK_HOME:
  179. case WXK_END:
  180. case WXK_UP:
  181. case WXK_DOWN:
  182. case WXK_PAGEUP:
  183. case WXK_PAGEDOWN:
  184. event.Skip();
  185. return;
  186. default:
  187. break;
  188. }
  189. // Search for an item name starting by the key code:
  190. key = toupper( key );
  191. for( unsigned ii = 0; ii < m_footprintList.GetCount(); ii++ )
  192. {
  193. wxString text = m_footprintList.Item( ii );
  194. // Search for the start char of the footprint name. Skip the line number.
  195. text.Trim( false ); // Remove leading spaces in line
  196. unsigned jj = 0;
  197. for( ; jj < text.Len(); jj++ )
  198. {
  199. // skip line number
  200. if( text[jj] == ' ' )
  201. break;
  202. }
  203. for( ; jj < text.Len(); jj++ )
  204. { // skip blanks
  205. if( text[jj] != ' ' )
  206. break;
  207. }
  208. int start_char = toupper( text[jj] );
  209. if( key == start_char )
  210. {
  211. SetSelection( ii, true ); // Ensure visible
  212. break;
  213. }
  214. }
  215. event.Skip();
  216. }