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 <[email protected]>
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 <string_utils.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  DIALOG_FIND_BASE( aFrame, wxID_ANY, _( "Find" ) )
56 {
57  m_frame = aFrame;
58  GetSizer()->SetSizeHints( this );
59 
61 
62  while( m_searchCombo->GetCount() > 10 )
63  {
64  m_frame->GetFindHistoryList().pop_back();
65  m_searchCombo->Delete( 9 );
66  }
67 
68  if( m_searchCombo->GetCount() )
69  {
70  m_searchCombo->SetSelection( 0 );
71  m_searchCombo->SelectAll();
72  }
73 
74  m_matchCase->SetValue( FindOptionCase );
75  m_matchWords->SetValue( FindOptionWords );
76  m_wildcards->SetValue( FindOptionWildcards );
77  m_wrap->SetValue( FindOptionWrap );
78 
79  m_includeTexts->SetValue( FindIncludeTexts );
83 
84  m_status->SetLabel( wxEmptyString);
85  m_upToDate = false;
86 
87  m_hitList.clear();
88  m_it = m_hitList.begin();
89 
90  m_findNext->SetDefault();
92 
93  Center();
94 }
95 
96 
97 void DIALOG_FIND::onTextEnter( wxCommandEvent& aEvent )
98 {
99  search( true );
100 }
101 
102 
103 void DIALOG_FIND::onFindNextClick( wxCommandEvent& aEvent )
104 {
105  search( true );
106 }
107 
108 
109 void DIALOG_FIND::onFindPreviousClick( wxCommandEvent& aEvent )
110 {
111  search( false );
112 }
113 
114 
115 void DIALOG_FIND::onSearchAgainClick( wxCommandEvent& aEvent )
116 {
117  m_upToDate = false;
118  search( true );
119 }
120 
121 
122 void DIALOG_FIND::search( bool aDirection )
123 {
124  PCB_SCREEN* screen = m_frame->GetScreen();
125  int flags;
126  int index;
127  wxString msg;
128  wxString searchString;
129  bool endIsReached = false;
130  bool isFirstSearch = false;
131 
132  searchString = m_searchCombo->GetValue();
133 
134  if( searchString.IsEmpty() )
135  {
136  Show();
137  return;
138  }
139 
140  // Add/move the search string to the top of the list if it isn't already there
141  index = m_searchCombo->FindString( searchString, true );
142 
143  if( index == wxNOT_FOUND )
144  {
145  m_searchCombo->Insert( searchString, 0 );
146  m_searchCombo->SetSelection( 0 );
147  m_upToDate = false;
148  m_frame->GetFindHistoryList().Insert( searchString, 0 );
149 
150  if( m_searchCombo->GetCount() > 10 )
151  {
152  m_frame->GetFindHistoryList().pop_back();
153  m_searchCombo->Delete( 10 );
154  }
155  }
156  else if( index != 0 )
157  {
158  m_searchCombo->Delete( index );
159  m_searchCombo->Insert( searchString, 0 );
160  m_searchCombo->SetSelection( 0 );
161  m_upToDate = false;
162 
163  if( m_frame->GetFindHistoryList().Index( searchString ) )
164  m_frame->GetFindHistoryList().Remove( searchString );
165 
166  m_frame->GetFindHistoryList().Insert( searchString, 0 );
167  }
168 
169  // Update search flags
170  flags = 0;
171 
172  if( FindOptionCase != m_matchCase->GetValue() )
173  {
174  FindOptionCase = m_matchCase->GetValue();
175  m_upToDate = false;
176  }
177 
178  if( FindOptionWords != m_matchWords->GetValue() )
179  {
180  FindOptionWords = m_matchWords->GetValue();
181  m_upToDate = false;
182  }
183 
184  if( FindOptionWildcards != m_wildcards->GetValue() )
185  {
186  FindOptionWildcards = m_wildcards->GetValue();
187  m_upToDate = false;
188  }
189 
190  FindOptionWrap = m_wrap->GetValue();
191 
192  if( FindIncludeTexts != m_includeTexts->GetValue() )
193  {
194  FindIncludeTexts = m_includeTexts->GetValue();
195  m_upToDate = false;
196  }
197 
198  if( FindIncludeValues != m_includeValues->GetValue() )
199  {
200  FindIncludeValues = m_includeValues->GetValue();
201  m_upToDate = false;
202  }
203 
204  if( FindIncludeReferences != m_includeReferences->GetValue() )
205  {
207  m_upToDate = false;
208  }
209 
210  if( FindIncludeMarkers != m_includeMarkers->GetValue() )
211  {
212  FindIncludeMarkers = m_includeMarkers->GetValue();
213  m_upToDate = false;
214  }
215 
216  if( FindOptionCase )
217  flags |= wxFR_MATCHCASE;
218 
219  if( FindOptionWords )
220  flags |= wxFR_WHOLEWORD;
221 
222  if( FindOptionWildcards )
223  flags |= FR_MATCH_WILDCARD;
224 
225  // Search parameters
226  m_frame->GetFindReplaceData().SetFindString( searchString );
227  m_frame->GetFindReplaceData().SetFlags( flags );
228 
230  m_frame->GetCanvas()->GetViewStart( &screen->m_StartVisu.x, &screen->m_StartVisu.y );
231 
232  // Refresh the list of results
233  if( !m_upToDate )
234  {
235  m_status->SetLabel( _( "Searching..." ) );
236  m_hitList.clear();
237 
239  {
240  for( FOOTPRINT* fp : m_frame->GetBoard()->Footprints() )
241  {
242  if( ( fp->Reference().Matches( m_frame->GetFindReplaceData(), nullptr )
244  || ( fp->Value().Matches( m_frame->GetFindReplaceData(), nullptr )
245  && FindIncludeValues ) )
246  {
247  m_hitList.push_back( fp );
248  }
249 
250  if( m_includeTexts->GetValue() )
251  {
252  for( BOARD_ITEM* item : fp->GraphicalItems() )
253  {
254  FP_TEXT* textItem = dynamic_cast<FP_TEXT*>( item );
255 
256  if( textItem && textItem->Matches( m_frame->GetFindReplaceData(),
257  nullptr ) )
258  {
259  m_hitList.push_back( fp );
260  }
261  }
262  }
263  }
264 
265  if( FindIncludeTexts )
266  {
267  for( BOARD_ITEM* item : m_frame->GetBoard()->Drawings() )
268  {
269  PCB_TEXT* textItem = dynamic_cast<PCB_TEXT*>( item );
270 
271  if( textItem && textItem->Matches( m_frame->GetFindReplaceData(), nullptr ) )
272  {
273  m_hitList.push_back( textItem );
274  }
275  }
276 
277  for( BOARD_ITEM* item : m_frame->GetBoard()->Zones() )
278  {
279  ZONE* zoneItem = dynamic_cast<ZONE*>( item );
280 
281  if( zoneItem && zoneItem->Matches( m_frame->GetFindReplaceData(), nullptr ) )
282  {
283  m_hitList.push_back( zoneItem );
284  }
285  }
286  }
287  }
288 
289  if( FindIncludeMarkers )
290  {
291  for( PCB_MARKER* marker : m_frame->GetBoard()->Markers() )
292  {
293  if( marker->Matches( m_frame->GetFindReplaceData(), nullptr ) )
294  m_hitList.push_back( marker );
295  }
296  }
297 
298  m_upToDate = true;
299  isFirstSearch = true;
300 
301  if( aDirection )
302  m_it = m_hitList.begin();
303  else
304  m_it = m_hitList.end();
305  }
306 
307  // Do we want a sorting algorithm ? If so, implement it here.
308 
309  // Get the item to display
310  if( m_hitList.empty() )
311  {
312  m_frame->SetStatusText( wxEmptyString );
313  }
314  else
315  {
316  if( aDirection )
317  {
318  if( m_it != m_hitList.end() && !isFirstSearch )
319  m_it++;
320 
321  if( m_it == m_hitList.end() )
322  {
323  if( m_wrap->GetValue() )
324  {
325  m_it = m_hitList.begin();
326  }
327  else
328  {
329  endIsReached = true;
330  m_it--; // point to the last REAL result
331  }
332  }
333  }
334  else
335  {
336  if( m_it == m_hitList.begin() )
337  {
338  if( m_wrap->GetValue() )
339  m_it = m_hitList.end();
340  else
341  endIsReached = true;
342  }
343 
344  if( m_it != m_hitList.begin() )
345  m_it--;
346  }
347  }
348 
349  // Display the item
350  if( m_hitList.empty() )
351  {
352  m_frame->SetStatusText( wxEmptyString );
353  msg.Printf( _( "'%s' not found" ), searchString );
354  m_frame->ShowInfoBarMsg( msg );
355 
356  m_status->SetLabel( msg );
357  }
358  else if( endIsReached )
359  {
360  m_frame->SetStatusText( wxEmptyString );
361  m_frame->ShowInfoBarMsg( _( "No more items to show" ) );
362 
363  m_status->SetLabel( _( "No hits" ) );
364  }
365  else
366  {
368  m_frame->FocusOnLocation( ( *m_it )->GetPosition() );
369 
370  msg.Printf( _( "'%s' found" ), searchString );
371  m_frame->SetStatusText( msg );
372 
373  msg.Printf( _( "Hit(s): %ld / %lu" ),
374  std::distance( m_hitList.begin(), m_it ) + 1,
375  m_hitList.size() );
376  m_status->SetLabel( msg );
377  }
378 
379  if( m_highlightCallback )
381 }
382 
383 
384 void DIALOG_FIND::OnCloseButtonClick( wxCommandEvent& aEvent )
385 {
386  wxCloseEvent tmp;
387 
388  OnClose( tmp );
389 
390  aEvent.Skip();
391 }
392 
393 bool DIALOG_FIND::Show( bool show )
394 {
395  bool ret = DIALOG_FIND_BASE::Show( show );
396 
397  if( show )
398  m_searchCombo->SetFocus();
399 
400  return ret;
401 }
402 
403 
404 void DIALOG_FIND::OnClose( wxCloseEvent& aEvent )
405 {
406  FindOptionCase = m_matchCase->GetValue();
407  FindOptionWords = m_matchWords->GetValue();
408  FindOptionWildcards = m_wildcards->GetValue();
409  FindOptionWrap = m_wrap->GetValue();
410 
411  FindIncludeTexts = m_includeTexts->GetValue();
412  FindIncludeValues = m_includeValues->GetValue();
413  FindIncludeMarkers = m_includeMarkers->GetValue();
415 
416  aEvent.Skip();
417 }
bool Show(bool show=true) override
The Show method is overridden to make the search combobox focused by default.
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:59
ZONES & Zones()
Definition: board.h:240
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
MARKERS & Markers()
Definition: board.h:243
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:132
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:97
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:90
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:234
std::function< void(BOARD_ITEM *)> m_highlightCallback
Definition: dialog_find.h:95
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:56
wxCheckBox * m_wildcards
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
bool FindIncludeReferences
Definition: dialog_find.cpp:50
bool Show(bool show) override
wxCheckBox * m_includeMarkers
wxCheckBox * m_matchWords
bool m_upToDate
Definition: dialog_find.h:93
std::deque< BOARD_ITEM * >::iterator m_it
Definition: dialog_find.h:92
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:91
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:237
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
wxCheckBox * m_includeValues
Class DIALOG_FIND_BASE.
wxButton * m_findNext