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-2018 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 <class_board.h>
27 #include <class_marker_pcb.h>
28 #include <class_module.h>
29 #include <class_pcb_text.h>
30 #include <class_text_mod.h>
31 #include <confirm.h>
32 #include <dialog_find.h>
33 #include <fctsys.h>
34 #include <kicad_string.h>
35 #include <pcb_edit_frame.h>
36 #include <pcbnew.h>
37 #include <string>
38 #include <tool/tool_manager.h>
39 #include <tools/pcb_actions.h>
40 #include <wx/fdrepdlg.h>
41 
42 //Defined as global because these values have to survive the destructor
43 
44 bool FindOptionCase = false;
45 bool FindOptionWords = false;
46 bool FindOptionWildcards = false;
47 bool FindOptionWrap = true;
48 
49 bool FindIncludeTexts = true;
50 bool FindIncludeValues = true;
52 bool FindIncludeMarkers = true;
53 
54 
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 void DIALOG_FIND::onTextEnter( wxCommandEvent& aEvent )
97 {
98  search( true );
99 }
100 
101 void DIALOG_FIND::onFindNextClick( wxCommandEvent& aEvent )
102 {
103  search( true );
104 }
105 
106 void DIALOG_FIND::onFindPreviousClick( wxCommandEvent& aEvent )
107 {
108  search( false );
109 }
110 
111 void DIALOG_FIND::onSearchAgainClick( wxCommandEvent& aEvent )
112 {
113  m_upToDate = false;
114  search( true );
115 }
116 
117 void DIALOG_FIND::search( bool aDirection )
118 {
119  PCB_SCREEN* screen = m_frame->GetScreen();
120  int flags;
121  int index;
122  wxString msg;
123  wxString searchString;
124 
125  // Add/move the search string to the top of the list if it isn't already there
126  searchString = m_searchCombo->GetValue();
127  index = m_searchCombo->FindString( searchString, true );
128 
129  if( index == wxNOT_FOUND )
130  {
131  m_searchCombo->Insert( searchString, 0 );
132  m_searchCombo->SetSelection( 0 );
133  m_upToDate = false;
134  m_frame->GetFindHistoryList().Insert( searchString, 0 );
135 
136  if( m_searchCombo->GetCount() > 10 )
137  {
138  m_frame->GetFindHistoryList().pop_back();
139  m_searchCombo->Delete( 10 );
140  }
141  }
142  else if( index != 0 )
143  {
144  m_searchCombo->Delete( index );
145  m_searchCombo->Insert( searchString, 0 );
146  m_searchCombo->SetSelection( 0 );
147  m_upToDate = false;
148 
149  if( m_frame->GetFindHistoryList().Index( searchString ) )
150  m_frame->GetFindHistoryList().Remove( searchString );
151 
152  m_frame->GetFindHistoryList().Insert( searchString, 0 );
153  }
154 
155  // Update search flags
156  flags = 0;
157 
158  if( FindOptionCase != m_matchCase->GetValue() )
159  {
160  FindOptionCase = m_matchCase->GetValue();
161  m_upToDate = false;
162  }
163 
164  if( FindOptionWords != m_matchWords->GetValue() )
165  {
166  FindOptionWords = m_matchWords->GetValue();
167  m_upToDate = false;
168  }
169 
170  if( FindOptionWildcards != m_wildcards->GetValue() )
171  {
172  FindOptionWildcards = m_wildcards->GetValue();
173  m_upToDate = false;
174  }
175 
176  FindOptionWrap = m_wrap->GetValue();
177 
178  if( FindIncludeTexts != m_includeTexts->GetValue() )
179  {
180  FindIncludeTexts = m_includeTexts->GetValue();
181  m_upToDate = false;
182  }
183 
184  if( FindIncludeValues != m_includeValues->GetValue() )
185  {
186  FindIncludeValues = m_includeValues->GetValue();
187  m_upToDate = false;
188  }
189 
190  if( FindIncludeReferences != m_includeReferences->GetValue() )
191  {
193  m_upToDate = false;
194  }
195 
196  if( FindIncludeMarkers != m_includeMarkers->GetValue() )
197  {
198  FindIncludeMarkers = m_includeMarkers->GetValue();
199  m_upToDate = false;
200  }
201 
202  if( FindOptionCase )
203  flags |= wxFR_MATCHCASE;
204 
205  if( FindOptionWords )
206  flags |= wxFR_WHOLEWORD;
207 
208  if( FindOptionWildcards )
209  flags |= FR_MATCH_WILDCARD;
210 
211  // Search parameters
212  m_frame->GetFindReplaceData().SetFindString( searchString );
213  m_frame->GetFindReplaceData().SetFlags( flags );
214 
216  m_frame->GetCanvas()->GetViewStart( &screen->m_StartVisu.x, &screen->m_StartVisu.y );
217 
218  // Refresh the list of results
219  if( !m_upToDate )
220  {
221  m_status->SetLabel( _( "Searching..." ) );
222  m_hitList.clear();
223 
225  {
226  for( MODULE* module : m_frame->GetBoard()->Modules() )
227  {
228  if( ( module->Reference().Matches( m_frame->GetFindReplaceData(), nullptr )
230  || ( module->Value().Matches( m_frame->GetFindReplaceData(), nullptr )
231  && FindIncludeValues ) )
232  {
233  m_hitList.push_back( module );
234  }
235 
236  if( m_includeTexts->GetValue() )
237  {
238  for( BOARD_ITEM* item : module->GraphicalItems() )
239  {
240  TEXTE_MODULE* textItem = dynamic_cast<TEXTE_MODULE*>( item );
241 
242  if( textItem
243  && textItem->Matches( m_frame->GetFindReplaceData(), nullptr ) )
244  {
245  m_hitList.push_back( module );
246  }
247  }
248  }
249  }
250 
251  if( FindIncludeTexts )
252  {
253  for( BOARD_ITEM* item : m_frame->GetBoard()->Drawings() )
254  {
255  TEXTE_PCB* textItem = dynamic_cast<TEXTE_PCB*>( item );
256 
257  if( textItem && textItem->Matches( m_frame->GetFindReplaceData(), nullptr ) )
258  {
259  m_hitList.push_back( textItem );
260  }
261  }
262  }
263  }
264 
265  if( FindIncludeMarkers )
266  {
267  for( MARKER_PCB* marker : m_frame->GetBoard()->Markers() )
268  {
269  if( marker->Matches( m_frame->GetFindReplaceData(), nullptr ) )
270  m_hitList.push_back( marker );
271  }
272  }
273 
274  m_upToDate = true;
275 
276  if( aDirection )
277  m_it = m_hitList.begin();
278  else
279  m_it = m_hitList.end();
280  }
281 
282  // Do we want a sorting algorithm ? If so, implement it here.
283 
284  // Get the item to display
285  if( m_hitList.empty() )
286  {
287  m_frame->SetStatusText( wxEmptyString );
288  }
289  else
290  {
291  if( aDirection )
292  {
293  m_it++;
294 
295  if( m_it == m_hitList.end() )
296  {
297  if( m_wrap->GetValue() )
298  m_it = m_hitList.begin();
299  else
300  {
301  m_frame->SetStatusText( wxEmptyString );
302  DisplayError( this, _( "No more item to show" ), 10 );
303  return;
304  }
305  }
306  }
307  else
308  {
309  if( m_it == m_hitList.begin() )
310  {
311  if( m_wrap->GetValue() )
312  {
313  m_it = m_hitList.end();
314  }
315  else
316  {
317  m_frame->SetStatusText( wxEmptyString );
318  DisplayError( this, _( "No more item to show" ), 10 );
319  return;
320  }
321  }
322 
323  m_it--;
324  }
325  }
326 
327  // Display the item
328  if( m_it != m_hitList.end() )
329  {
331  m_frame->FocusOnLocation( ( *m_it )->GetPosition() );
332 
333  msg.Printf( _( "\"%s\" found" ), searchString );
334  m_frame->SetStatusText( msg );
335 
336  msg.Printf( _( "Hit(s): %ld / %lu" ), std::distance( m_hitList.begin(), m_it ),
337  m_hitList.size() );
338  m_status->SetLabel( msg );
339  }
340  else
341  {
342  m_frame->SetStatusText( wxEmptyString );
343 
344  msg.Printf( _( "\"%s\" not found" ), searchString );
345  DisplayError( this, msg, 10 );
346 
347  m_status->SetLabel( _( "No hits" ) );
348  }
349 
350  if( m_highlightCallback )
352 }
353 
354 void DIALOG_FIND::onClose( wxCommandEvent& aEvent )
355 {
356  FindOptionCase = m_matchCase->GetValue();
357  FindOptionWords = m_matchWords->GetValue();
358  FindOptionWildcards = m_wildcards->GetValue();
359  FindOptionWrap = m_wrap->GetValue();
360 
361  FindIncludeTexts = m_includeTexts->GetValue();
362  FindIncludeValues = m_includeValues->GetValue();
363  FindIncludeMarkers = m_includeMarkers->GetValue();
365 
366  EndModal( 1 );
367 }
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:62
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:239
bool Matches(wxFindReplaceData &aSearchData, void *aAuxData) override
Function Matches compares the item against the search criteria in aSearchData.
TEXTE_PCB class definition.
This file is part of the common library.
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
MARKERS & Markers()
Definition: class_board.h:285
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
wxCheckBox * m_includeReferences
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:140
bool FindOptionWords
Definition: dialog_find.cpp:45
bool FindIncludeTexts
Definition: dialog_find.cpp:49
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:114
bool FindIncludeMarkers
Definition: dialog_find.cpp:52
wxStaticText * m_status
wxFindReplaceData & GetFindReplaceData()
void onTextEnter(wxCommandEvent &event) override
Definition: dialog_find.cpp:96
BOARD_ITEM * GetItem() const
Returns the currently found item or nullptr in the case of no items found.
Definition: dialog_find.h:49
PCB_BASE_FRAME * m_frame
Definition: dialog_find.h:69
void search(bool direction)
wxPoint m_StartVisu
Coordinates in drawing units of the current view position (upper left corner of device)
Definition: base_screen.h:67
Markers used to show a drc problem on boards.
bool FindIncludeValues
Definition: dialog_find.cpp:50
Footprint text class description.
void onFindNextClick(wxCommandEvent &event) override
wxComboBox * m_searchCombo
void onFindPreviousClick(wxCommandEvent &event) override
MODULES & Modules()
Definition: class_board.h:266
wxCheckBox * m_includeTexts
wxCheckBox * m_matchCase
DIALOG_FIND(PCB_BASE_FRAME *aParent)
Definition: dialog_find.cpp:55
wxCheckBox * m_wildcards
bool FindIncludeReferences
Definition: dialog_find.cpp:51
void onClose(wxCommandEvent &event) override
wxCheckBox * m_includeMarkers
wxCheckBox * m_matchWords
bool m_upToDate
Definition: dialog_find.h:72
std::deque< BOARD_ITEM * >::iterator m_it
Definition: dialog_find.h:71
#define _(s)
Definition: 3d_actions.cpp:33
wxArrayString & GetFindHistoryList()
static TOOL_ACTION selectItem
Selects an item (specified as the event parameter).
Definition: pcb_actions.h:65
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
std::deque< BOARD_ITEM * > m_hitList
Definition: dialog_find.h:70
PCB_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
bool FindOptionWrap
Definition: dialog_find.cpp:47
bool FindOptionWildcards
Definition: dialog_find.cpp:46
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:74
bool FindOptionCase
Definition: dialog_find.cpp:44
BOARD * GetBoard() const
bool Matches(wxFindReplaceData &aSearchData, void *aAuxData) override
Function Matches compares the item against the search criteria in aSearchData.
void onSearchAgainClick(wxCommandEvent &event) override
void FocusOnLocation(const wxPoint &aPos)
Useful to focus on a particular location, in find functions Move the graphic cursor (crosshair cursor...
wxCheckBox * m_wrap
DRAWINGS & Drawings()
Definition: class_board.h:275
boost::function< void(BOARD_ITEM *)> m_highlightCallback
Definition: dialog_find.h:74
PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
wxCheckBox * m_includeValues
Class DIALOG_FIND_BASE.
wxButton * m_findNext