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 <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  // Add/move the search string to the top of the list if it isn't already there
133  searchString = m_searchCombo->GetValue();
134  index = m_searchCombo->FindString( searchString, true );
135 
136  if( index == wxNOT_FOUND )
137  {
138  m_searchCombo->Insert( searchString, 0 );
139  m_searchCombo->SetSelection( 0 );
140  m_upToDate = false;
141  m_frame->GetFindHistoryList().Insert( searchString, 0 );
142 
143  if( m_searchCombo->GetCount() > 10 )
144  {
145  m_frame->GetFindHistoryList().pop_back();
146  m_searchCombo->Delete( 10 );
147  }
148  }
149  else if( index != 0 )
150  {
151  m_searchCombo->Delete( index );
152  m_searchCombo->Insert( searchString, 0 );
153  m_searchCombo->SetSelection( 0 );
154  m_upToDate = false;
155 
156  if( m_frame->GetFindHistoryList().Index( searchString ) )
157  m_frame->GetFindHistoryList().Remove( searchString );
158 
159  m_frame->GetFindHistoryList().Insert( searchString, 0 );
160  }
161 
162  // Update search flags
163  flags = 0;
164 
165  if( FindOptionCase != m_matchCase->GetValue() )
166  {
167  FindOptionCase = m_matchCase->GetValue();
168  m_upToDate = false;
169  }
170 
171  if( FindOptionWords != m_matchWords->GetValue() )
172  {
173  FindOptionWords = m_matchWords->GetValue();
174  m_upToDate = false;
175  }
176 
177  if( FindOptionWildcards != m_wildcards->GetValue() )
178  {
179  FindOptionWildcards = m_wildcards->GetValue();
180  m_upToDate = false;
181  }
182 
183  FindOptionWrap = m_wrap->GetValue();
184 
185  if( FindIncludeTexts != m_includeTexts->GetValue() )
186  {
187  FindIncludeTexts = m_includeTexts->GetValue();
188  m_upToDate = false;
189  }
190 
191  if( FindIncludeValues != m_includeValues->GetValue() )
192  {
193  FindIncludeValues = m_includeValues->GetValue();
194  m_upToDate = false;
195  }
196 
197  if( FindIncludeReferences != m_includeReferences->GetValue() )
198  {
200  m_upToDate = false;
201  }
202 
203  if( FindIncludeMarkers != m_includeMarkers->GetValue() )
204  {
205  FindIncludeMarkers = m_includeMarkers->GetValue();
206  m_upToDate = false;
207  }
208 
209  if( FindOptionCase )
210  flags |= wxFR_MATCHCASE;
211 
212  if( FindOptionWords )
213  flags |= wxFR_WHOLEWORD;
214 
215  if( FindOptionWildcards )
216  flags |= FR_MATCH_WILDCARD;
217 
218  // Search parameters
219  m_frame->GetFindReplaceData().SetFindString( searchString );
220  m_frame->GetFindReplaceData().SetFlags( flags );
221 
223  m_frame->GetCanvas()->GetViewStart( &screen->m_StartVisu.x, &screen->m_StartVisu.y );
224 
225  // Refresh the list of results
226  if( !m_upToDate )
227  {
228  m_status->SetLabel( _( "Searching..." ) );
229  m_hitList.clear();
230 
232  {
233  for( FOOTPRINT* fp : m_frame->GetBoard()->Footprints() )
234  {
235  if( ( fp->Reference().Matches( m_frame->GetFindReplaceData(), nullptr )
237  || ( fp->Value().Matches( m_frame->GetFindReplaceData(), nullptr )
238  && FindIncludeValues ) )
239  {
240  m_hitList.push_back( fp );
241  }
242 
243  if( m_includeTexts->GetValue() )
244  {
245  for( BOARD_ITEM* item : fp->GraphicalItems() )
246  {
247  FP_TEXT* textItem = dynamic_cast<FP_TEXT*>( item );
248 
249  if( textItem && textItem->Matches( m_frame->GetFindReplaceData(),
250  nullptr ) )
251  {
252  m_hitList.push_back( fp );
253  }
254  }
255  }
256  }
257 
258  if( FindIncludeTexts )
259  {
260  for( BOARD_ITEM* item : m_frame->GetBoard()->Drawings() )
261  {
262  PCB_TEXT* textItem = dynamic_cast<PCB_TEXT*>( item );
263 
264  if( textItem && textItem->Matches( m_frame->GetFindReplaceData(), nullptr ) )
265  {
266  m_hitList.push_back( textItem );
267  }
268  }
269 
270  for( BOARD_ITEM* item : m_frame->GetBoard()->Zones() )
271  {
272  ZONE* zoneItem = dynamic_cast<ZONE*>( item );
273 
274  if( zoneItem && zoneItem->Matches( m_frame->GetFindReplaceData(), nullptr ) )
275  {
276  m_hitList.push_back( zoneItem );
277  }
278  }
279  }
280  }
281 
282  if( FindIncludeMarkers )
283  {
284  for( PCB_MARKER* marker : m_frame->GetBoard()->Markers() )
285  {
286  if( marker->Matches( m_frame->GetFindReplaceData(), nullptr ) )
287  m_hitList.push_back( marker );
288  }
289  }
290 
291  m_upToDate = true;
292  isFirstSearch = true;
293 
294  if( aDirection )
295  m_it = m_hitList.begin();
296  else
297  m_it = m_hitList.end();
298  }
299 
300  // Do we want a sorting algorithm ? If so, implement it here.
301 
302  // Get the item to display
303  if( m_hitList.empty() )
304  {
305  m_frame->SetStatusText( wxEmptyString );
306  }
307  else
308  {
309  if( aDirection )
310  {
311  if( m_it != m_hitList.end() && !isFirstSearch )
312  m_it++;
313 
314  if( m_it == m_hitList.end() )
315  {
316  if( m_wrap->GetValue() )
317  {
318  m_it = m_hitList.begin();
319  }
320  else
321  {
322  endIsReached = true;
323  m_it--; // point to the last REAL result
324  }
325  }
326  }
327  else
328  {
329  if( m_it == m_hitList.begin() )
330  {
331  if( m_wrap->GetValue() )
332  m_it = m_hitList.end();
333  else
334  endIsReached = true;
335  }
336 
337  if( m_it != m_hitList.begin() )
338  m_it--;
339  }
340  }
341 
342  // Display the item
343  if( m_hitList.empty() )
344  {
345  m_frame->SetStatusText( wxEmptyString );
346  msg.Printf( _( "'%s' not found" ), searchString );
347  m_frame->ShowInfoBarMsg( msg );
348 
349  m_status->SetLabel( msg );
350  }
351  else if( endIsReached )
352  {
353  m_frame->SetStatusText( wxEmptyString );
354  m_frame->ShowInfoBarMsg( _( "No more items to show" ) );
355 
356  m_status->SetLabel( _( "No hits" ) );
357  }
358  else
359  {
361  m_frame->FocusOnLocation( ( *m_it )->GetPosition() );
362 
363  msg.Printf( _( "'%s' found" ), searchString );
364  m_frame->SetStatusText( msg );
365 
366  msg.Printf( _( "Hit(s): %ld / %lu" ),
367  std::distance( m_hitList.begin(), m_it ) + 1,
368  m_hitList.size() );
369  m_status->SetLabel( msg );
370  }
371 
372  if( m_highlightCallback )
374 }
375 
376 
377 void DIALOG_FIND::OnCloseButtonClick( wxCommandEvent& aEvent )
378 {
379  wxCloseEvent tmp;
380 
381  OnClose( tmp );
382 
383  aEvent.Skip();
384 }
385 
386 
387 void DIALOG_FIND::OnClose( wxCloseEvent& aEvent )
388 {
389  FindOptionCase = m_matchCase->GetValue();
390  FindOptionWords = m_matchWords->GetValue();
391  FindOptionWildcards = m_wildcards->GetValue();
392  FindOptionWrap = m_wrap->GetValue();
393 
394  FindIncludeTexts = m_includeTexts->GetValue();
395  FindIncludeValues = m_includeValues->GetValue();
396  FindIncludeMarkers = m_includeMarkers->GetValue();
398 
399  aEvent.Skip();
400 }
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:49
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: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:82
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:87
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
wxCheckBox * m_includeMarkers
wxCheckBox * m_matchWords
bool m_upToDate
Definition: dialog_find.h:85
std::deque< BOARD_ITEM * >::iterator m_it
Definition: dialog_find.h:84
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:83
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