KiCad PCB EDA Suite
dialog_find.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) 2012 Marco Mattila <marcom99@gmail.com>
5  * Copyright (C) 2018 Jean-Pierre Charras jp.charras at wanadoo.fr
6  * Copyright (C) 1992-2021 Kicad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <board.h>
27 #include <pcb_marker.h>
28 #include <footprint.h>
29 #include <pcb_text.h>
30 #include <fp_text.h>
31 #include <zone.h>
32 #include <dialog_find.h>
33 #include <kicad_string.h>
34 #include <pcb_edit_frame.h>
35 #include <string>
36 #include <tool/tool_manager.h>
37 #include <tools/pcb_actions.h>
38 #include <wx/fdrepdlg.h>
39 
40 
41 //Defined as global because these values have to survive the destructor
42 
43 bool FindOptionCase = false;
44 bool FindOptionWords = false;
45 bool FindOptionWildcards = false;
46 bool FindOptionWrap = true;
47 
48 bool FindIncludeTexts = true;
49 bool FindIncludeValues = true;
51 bool FindIncludeMarkers = true;
52 
53 
55 {
56  m_frame = aFrame;
57  GetSizer()->SetSizeHints( this );
58 
60 
61  while( m_searchCombo->GetCount() > 10 )
62  {
63  m_frame->GetFindHistoryList().pop_back();
64  m_searchCombo->Delete( 9 );
65  }
66 
67  if( m_searchCombo->GetCount() )
68  {
69  m_searchCombo->SetSelection( 0 );
70  m_searchCombo->SelectAll();
71  }
72 
73  m_matchCase->SetValue( FindOptionCase );
74  m_matchWords->SetValue( FindOptionWords );
75  m_wildcards->SetValue( FindOptionWildcards );
76  m_wrap->SetValue( FindOptionWrap );
77 
78  m_includeTexts->SetValue( FindIncludeTexts );
82 
83  m_status->SetLabel( wxEmptyString);
84  m_upToDate = false;
85 
86  m_hitList.clear();
87  m_it = m_hitList.begin();
88 
89  m_findNext->SetDefault();
91 
92  Center();
93 }
94 
95 
96 void DIALOG_FIND::onTextEnter( wxCommandEvent& aEvent )
97 {
98  search( true );
99 }
100 
101 
102 void DIALOG_FIND::onFindNextClick( wxCommandEvent& aEvent )
103 {
104  search( true );
105 }
106 
107 
108 void DIALOG_FIND::onFindPreviousClick( wxCommandEvent& aEvent )
109 {
110  search( false );
111 }
112 
113 
114 void DIALOG_FIND::onSearchAgainClick( wxCommandEvent& aEvent )
115 {
116  m_upToDate = false;
117  search( true );
118 }
119 
120 
121 void DIALOG_FIND::search( bool aDirection )
122 {
123  PCB_SCREEN* screen = m_frame->GetScreen();
124  int flags;
125  int index;
126  wxString msg;
127  wxString searchString;
128  bool endIsReached = false;
129  bool isFirstSearch = false;
130 
131  // Add/move the search string to the top of the list if it isn't already there
132  searchString = m_searchCombo->GetValue();
133  index = m_searchCombo->FindString( searchString, true );
134 
135  if( index == wxNOT_FOUND )
136  {
137  m_searchCombo->Insert( searchString, 0 );
138  m_searchCombo->SetSelection( 0 );
139  m_upToDate = false;
140  m_frame->GetFindHistoryList().Insert( searchString, 0 );
141 
142  if( m_searchCombo->GetCount() > 10 )
143  {
144  m_frame->GetFindHistoryList().pop_back();
145  m_searchCombo->Delete( 10 );
146  }
147  }
148  else if( index != 0 )
149  {
150  m_searchCombo->Delete( index );
151  m_searchCombo->Insert( searchString, 0 );
152  m_searchCombo->SetSelection( 0 );
153  m_upToDate = false;
154 
155  if( m_frame->GetFindHistoryList().Index( searchString ) )
156  m_frame->GetFindHistoryList().Remove( searchString );
157 
158  m_frame->GetFindHistoryList().Insert( searchString, 0 );
159  }
160 
161  // Update search flags
162  flags = 0;
163 
164  if( FindOptionCase != m_matchCase->GetValue() )
165  {
166  FindOptionCase = m_matchCase->GetValue();
167  m_upToDate = false;
168  }
169 
170  if( FindOptionWords != m_matchWords->GetValue() )
171  {
172  FindOptionWords = m_matchWords->GetValue();
173  m_upToDate = false;
174  }
175 
176  if( FindOptionWildcards != m_wildcards->GetValue() )
177  {
178  FindOptionWildcards = m_wildcards->GetValue();
179  m_upToDate = false;
180  }
181 
182  FindOptionWrap = m_wrap->GetValue();
183 
184  if( FindIncludeTexts != m_includeTexts->GetValue() )
185  {
186  FindIncludeTexts = m_includeTexts->GetValue();
187  m_upToDate = false;
188  }
189 
190  if( FindIncludeValues != m_includeValues->GetValue() )
191  {
192  FindIncludeValues = m_includeValues->GetValue();
193  m_upToDate = false;
194  }
195 
196  if( FindIncludeReferences != m_includeReferences->GetValue() )
197  {
199  m_upToDate = false;
200  }
201 
202  if( FindIncludeMarkers != m_includeMarkers->GetValue() )
203  {
204  FindIncludeMarkers = m_includeMarkers->GetValue();
205  m_upToDate = false;
206  }
207 
208  if( FindOptionCase )
209  flags |= wxFR_MATCHCASE;
210 
211  if( FindOptionWords )
212  flags |= wxFR_WHOLEWORD;
213 
214  if( FindOptionWildcards )
215  flags |= FR_MATCH_WILDCARD;
216 
217  // Search parameters
218  m_frame->GetFindReplaceData().SetFindString( searchString );
219  m_frame->GetFindReplaceData().SetFlags( flags );
220 
222  m_frame->GetCanvas()->GetViewStart( &screen->m_StartVisu.x, &screen->m_StartVisu.y );
223 
224  // Refresh the list of results
225  if( !m_upToDate )
226  {
227  m_status->SetLabel( _( "Searching..." ) );
228  m_hitList.clear();
229 
231  {
232  for( FOOTPRINT* fp : m_frame->GetBoard()->Footprints() )
233  {
234  if( ( fp->Reference().Matches( m_frame->GetFindReplaceData(), nullptr )
236  || ( fp->Value().Matches( m_frame->GetFindReplaceData(), nullptr )
237  && FindIncludeValues ) )
238  {
239  m_hitList.push_back( fp );
240  }
241 
242  if( m_includeTexts->GetValue() )
243  {
244  for( BOARD_ITEM* item : fp->GraphicalItems() )
245  {
246  FP_TEXT* textItem = dynamic_cast<FP_TEXT*>( item );
247 
248  if( textItem && textItem->Matches( m_frame->GetFindReplaceData(),
249  nullptr ) )
250  {
251  m_hitList.push_back( fp );
252  }
253  }
254  }
255  }
256 
257  if( FindIncludeTexts )
258  {
259  for( BOARD_ITEM* item : m_frame->GetBoard()->Drawings() )
260  {
261  PCB_TEXT* textItem = dynamic_cast<PCB_TEXT*>( item );
262 
263  if( textItem && textItem->Matches( m_frame->GetFindReplaceData(), nullptr ) )
264  {
265  m_hitList.push_back( textItem );
266  }
267  }
268 
269  for( BOARD_ITEM* item : m_frame->GetBoard()->Zones() )
270  {
271  ZONE* zoneItem = dynamic_cast<ZONE*>( item );
272 
273  if( zoneItem && zoneItem->Matches( m_frame->GetFindReplaceData(), nullptr ) )
274  {
275  m_hitList.push_back( zoneItem );
276  }
277  }
278  }
279  }
280 
281  if( FindIncludeMarkers )
282  {
283  for( PCB_MARKER* marker : m_frame->GetBoard()->Markers() )
284  {
285  if( marker->Matches( m_frame->GetFindReplaceData(), nullptr ) )
286  m_hitList.push_back( marker );
287  }
288  }
289 
290  m_upToDate = true;
291  isFirstSearch = true;
292 
293  if( aDirection )
294  m_it = m_hitList.begin();
295  else
296  m_it = m_hitList.end();
297  }
298 
299  // Do we want a sorting algorithm ? If so, implement it here.
300 
301  // Get the item to display
302  if( m_hitList.empty() )
303  {
304  m_frame->SetStatusText( wxEmptyString );
305  }
306  else
307  {
308  if( aDirection )
309  {
310  if( m_it != m_hitList.end() && !isFirstSearch )
311  m_it++;
312 
313  if( m_it == m_hitList.end() )
314  {
315  if( m_wrap->GetValue() )
316  {
317  m_it = m_hitList.begin();
318  }
319  else
320  {
321  endIsReached = true;
322  m_it--; // point to the last REAL result
323  }
324  }
325  }
326  else
327  {
328  if( m_it == m_hitList.begin() )
329  {
330  if( m_wrap->GetValue() )
331  m_it = m_hitList.end();
332  else
333  endIsReached = true;
334  }
335 
336  if( m_it != m_hitList.begin() )
337  m_it--;
338  }
339  }
340 
341  // Display the item
342  if( m_hitList.empty() )
343  {
344  m_frame->SetStatusText( wxEmptyString );
345  msg.Printf( _( "'%s' not found" ), searchString );
346  m_frame->ShowInfoBarMsg( msg );
347 
348  m_status->SetLabel( msg );
349  }
350  else if( endIsReached )
351  {
352  m_frame->SetStatusText( wxEmptyString );
353  m_frame->ShowInfoBarMsg( _( "No more items to show" ) );
354 
355  m_status->SetLabel( _( "No hits" ) );
356  }
357  else
358  {
360  m_frame->FocusOnLocation( ( *m_it )->GetPosition() );
361 
362  msg.Printf( _( "'%s' found" ), searchString );
363  m_frame->SetStatusText( msg );
364 
365  msg.Printf( _( "Hit(s): %ld / %lu" ),
366  std::distance( m_hitList.begin(), m_it ) + 1,
367  m_hitList.size() );
368  m_status->SetLabel( msg );
369  }
370 
371  if( m_highlightCallback )
373 }
374 
375 
376 void DIALOG_FIND::OnCloseButtonClick( wxCommandEvent& aEvent )
377 {
378  wxCloseEvent tmp;
379 
380  OnClose( tmp );
381 
382  aEvent.Skip();
383 }
384 
385 
386 void DIALOG_FIND::OnClose( wxCloseEvent& aEvent )
387 {
388  FindOptionCase = m_matchCase->GetValue();
389  FindOptionWords = m_matchWords->GetValue();
390  FindOptionWildcards = m_wildcards->GetValue();
391  FindOptionWrap = m_wrap->GetValue();
392 
393  FindIncludeTexts = m_includeTexts->GetValue();
394  FindIncludeValues = m_includeValues->GetValue();
395  FindIncludeMarkers = m_includeMarkers->GetValue();
397 
398  aEvent.Skip();
399 }
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:59
ZONES & Zones()
Definition: board.h:239
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:80
MARKERS & Markers()
Definition: board.h:242
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
wxCheckBox * m_includeReferences
bool Matches(const wxFindReplaceData &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition: zone.h:133
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:143
bool FindOptionWords
Definition: dialog_find.cpp:44
bool FindIncludeTexts
Definition: dialog_find.cpp:48
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:97
bool FindIncludeMarkers
Definition: dialog_find.cpp:51
wxStaticText * m_status
wxFindReplaceData & GetFindReplaceData()
void onTextEnter(wxCommandEvent &event) override
Definition: dialog_find.cpp:96
BOARD_ITEM * GetItem() const
Return the currently found item or nullptr in the case of no items found.
Definition: dialog_find.h:48
PCB_BASE_FRAME * m_frame
Definition: dialog_find.h:77
void search(bool direction)
void ShowInfoBarMsg(const wxString &aMsg, bool aShowCloseButton=false)
Show the WX_INFOBAR displayed on the top of the canvas with a message and an info icon on the left of...
wxPoint m_StartVisu
Coordinates in drawing units of the current view position (upper left corner of device)
Definition: base_screen.h:93
bool FindIncludeValues
Definition: dialog_find.cpp:49
void onFindNextClick(wxCommandEvent &event) override
wxComboBox * m_searchCombo
void onFindPreviousClick(wxCommandEvent &event) override
wxCheckBox * m_includeTexts
wxCheckBox * m_matchCase
FOOTPRINTS & Footprints()
Definition: board.h:233
std::function< void(BOARD_ITEM *)> m_highlightCallback
Definition: dialog_find.h:82
bool Matches(const wxFindReplaceData &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition: fp_text.h:83
DIALOG_FIND(PCB_BASE_FRAME *aParent)
Definition: dialog_find.cpp:54
void OnCloseButtonClick(wxCommandEvent &aEvent) override
#define _(s)
Handle a list of polygons defining a copper zone.
Definition: zone.h:57
wxCheckBox * m_wildcards
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
bool FindIncludeReferences
Definition: dialog_find.cpp:50
wxCheckBox * m_includeMarkers
wxCheckBox * m_matchWords
bool m_upToDate
Definition: dialog_find.h:80
std::deque< BOARD_ITEM * >::iterator m_it
Definition: dialog_find.h:79
wxArrayString & GetFindHistoryList()
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition: pcb_actions.h:62
std::deque< BOARD_ITEM * > m_hitList
Definition: dialog_find.h:78
PCB_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
bool FindOptionWrap
Definition: dialog_find.cpp:46
bool FindOptionWildcards
Definition: dialog_find.cpp:45
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:54
bool Matches(const wxFindReplaceData &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition: pcb_text.h:71
void OnClose(wxCloseEvent &event) override
bool FindOptionCase
Definition: dialog_find.cpp:43
BOARD * GetBoard() const
void onSearchAgainClick(wxCommandEvent &event) override
void FocusOnLocation(const wxPoint &aPos)
Useful to focus on a particular location, in find functions.
wxCheckBox * m_wrap
DRAWINGS & Drawings()
Definition: board.h:236
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
wxCheckBox * m_includeValues
Class DIALOG_FIND_BASE.
wxButton * m_findNext