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.

256 lines
6.6 KiB

6 years ago
6 years ago
6 years ago
2 years ago
2 years ago
11 years ago
2 years ago
11 years ago
11 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 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 The 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/log.h>
  28. #include <wx/wupdlock.h>
  29. #include <cvpcb_id.h>
  30. #include <cvpcb_mainframe.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,
  34. wxLC_SINGLE_SEL | wxNO_BORDER )
  35. {
  36. }
  37. int FOOTPRINTS_LISTBOX::GetCount()
  38. {
  39. return (int) m_footprintList.Count();
  40. }
  41. void FOOTPRINTS_LISTBOX::SetString( unsigned linecount, const wxString& text )
  42. {
  43. unsigned count = m_footprintList.Count();
  44. if( count > 0 )
  45. {
  46. if( linecount >= count )
  47. linecount = count - 1;
  48. m_footprintList[linecount] = text;
  49. }
  50. UpdateWidth( linecount );
  51. }
  52. wxString FOOTPRINTS_LISTBOX::GetSelectedFootprint()
  53. {
  54. wxString footprintName;
  55. int ii = GetFirstSelected();
  56. if( ii >= 0 )
  57. {
  58. wxString msg = m_footprintList[ii];
  59. msg.Trim( true );
  60. msg.Trim( false );
  61. footprintName = msg.AfterFirst( wxChar( ' ' ) );
  62. }
  63. return footprintName;
  64. }
  65. wxString FOOTPRINTS_LISTBOX::OnGetItemText( long item, long column ) const
  66. {
  67. if( item < 0 || item >= (long)m_footprintList.GetCount() )
  68. return wxEmptyString;
  69. return m_footprintList.Item( item );
  70. }
  71. void FOOTPRINTS_LISTBOX::SetSelection( int aIndex, bool aState )
  72. {
  73. if( aIndex >= GetCount() )
  74. aIndex = GetCount() - 1;
  75. if( aIndex >= 0 && GetCount() > 0)
  76. {
  77. Select( aIndex, aState );
  78. EnsureVisible( aIndex );
  79. Refresh();
  80. }
  81. }
  82. void FOOTPRINTS_LISTBOX::SetSelectedFootprint( const LIB_ID& aFPID )
  83. {
  84. wxString id = aFPID.Format().wx_str();
  85. for( int i = 0; i < GetCount(); ++i )
  86. {
  87. wxString candidate = m_footprintList.Item( i ).substr( 4 );
  88. if( candidate.CmpNoCase( id ) == 0 )
  89. {
  90. SetSelection( i, true );
  91. return;
  92. }
  93. }
  94. }
  95. void FOOTPRINTS_LISTBOX::SetFootprints( FOOTPRINT_LIST& aList, const wxString& aLibName,
  96. COMPONENT* aComponent,
  97. const wxString &aFootPrintFilterPattern,
  98. int aFilterType )
  99. {
  100. wxArrayString newList;
  101. wxString msg;
  102. wxString oldSelection;
  103. FOOTPRINT_FILTER filter( aList );
  104. if( aFilterType & FILTERING_BY_COMPONENT_FP_FILTERS && aComponent )
  105. filter.FilterByFootprintFilters( aComponent->GetFootprintFilters() );
  106. if( aFilterType & FILTERING_BY_PIN_COUNT && aComponent )
  107. filter.FilterByPinCount( aComponent->GetPinCount() );
  108. if( aFilterType & FILTERING_BY_LIBRARY )
  109. filter.FilterByLibrary( aLibName );
  110. if( !aFootPrintFilterPattern.IsEmpty() )
  111. filter.FilterByTextPattern( aFootPrintFilterPattern );
  112. if( GetSelection() >= 0 && GetSelection() < (int)m_footprintList.GetCount() )
  113. oldSelection = m_footprintList[ GetSelection() ];
  114. for( const FOOTPRINT_INFO& i : filter )
  115. {
  116. msg.Printf( wxS( "%3d %s:%s" ),
  117. int( newList.GetCount() + 1 ),
  118. i.GetLibNickname(),
  119. i.GetFootprintName() );
  120. newList.Add( msg );
  121. }
  122. if( newList == m_footprintList )
  123. return;
  124. m_footprintList = newList;
  125. int selection = m_footprintList.Index( oldSelection );
  126. if( selection == wxNOT_FOUND )
  127. selection = 0;
  128. DeselectAll();
  129. wxSafeYield();
  130. wxWindowUpdateLocker freeze( this );
  131. DeleteAllItems();
  132. if( m_footprintList.GetCount() )
  133. {
  134. SetItemCount( m_footprintList.GetCount() );
  135. SetSelection( selection, true );
  136. RefreshItems( 0L, m_footprintList.GetCount() - 1 );
  137. UpdateWidth();
  138. }
  139. }
  140. BEGIN_EVENT_TABLE( FOOTPRINTS_LISTBOX, ITEMS_LISTBOX_BASE )
  141. EVT_CHAR( FOOTPRINTS_LISTBOX::OnChar )
  142. EVT_LIST_ITEM_SELECTED( ID_CVPCB_FOOTPRINT_LIST, FOOTPRINTS_LISTBOX::OnLeftClick )
  143. EVT_LIST_ITEM_ACTIVATED( ID_CVPCB_FOOTPRINT_LIST, FOOTPRINTS_LISTBOX::OnLeftDClick )
  144. END_EVENT_TABLE()
  145. void FOOTPRINTS_LISTBOX::OnLeftClick( wxListEvent& event )
  146. {
  147. GetParent()->RefreshFootprintViewer();
  148. }
  149. void FOOTPRINTS_LISTBOX::OnLeftDClick( wxListEvent& event )
  150. {
  151. GetParent()->GetToolManager()->RunAction( CVPCB_ACTIONS::associate );
  152. }
  153. void FOOTPRINTS_LISTBOX::OnChar( wxKeyEvent& event )
  154. {
  155. wxLogTrace( kicadTraceKeyEvent, wxS( "FOOTPRINTS_LISTBOX::OnChar %s" ), dump( event ) );
  156. int key = event.GetKeyCode();
  157. switch( key )
  158. {
  159. case WXK_HOME:
  160. case WXK_END:
  161. case WXK_UP:
  162. case WXK_DOWN:
  163. case WXK_PAGEUP:
  164. case WXK_PAGEDOWN:
  165. event.Skip();
  166. return;
  167. default:
  168. break;
  169. }
  170. // Search for an item name starting by the key code:
  171. key = toupper( key );
  172. for( unsigned ii = 0; ii < m_footprintList.GetCount(); ii++ )
  173. {
  174. wxString text = m_footprintList.Item( ii );
  175. // Search for the start char of the footprint name. Skip the line number.
  176. text.Trim( false ); // Remove leading spaces in line
  177. unsigned jj = 0;
  178. for( ; jj < text.Len(); jj++ )
  179. {
  180. // skip line number
  181. if( text[jj] == ' ' )
  182. break;
  183. }
  184. for( ; jj < text.Len(); jj++ )
  185. {
  186. // skip blanks
  187. if( text[jj] != ' ' )
  188. break;
  189. }
  190. int start_char = toupper( text[jj] );
  191. if( key == start_char )
  192. {
  193. SetSelection( ii, true ); // Ensure visible
  194. break;
  195. }
  196. }
  197. event.Skip();
  198. }