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.

283 lines
7.4 KiB

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