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 <dialog_find.h>
32 #include <fctsys.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 //Defined as global because these values have to survive the destructor
41 
42 bool FindOptionCase = false;
43 bool FindOptionWords = false;
44 bool FindOptionWildcards = false;
45 bool FindOptionWrap = true;
46 
47 bool FindIncludeTexts = true;
48 bool FindIncludeValues = true;
50 bool FindIncludeMarkers = true;
51 
52 
54 {
55  m_frame = aFrame;
56  GetSizer()->SetSizeHints( this );
57 
59 
60  while( m_searchCombo->GetCount() > 10 )
61  {
62  m_frame->GetFindHistoryList().pop_back();
63  m_searchCombo->Delete( 9 );
64  }
65 
66  if( m_searchCombo->GetCount() )
67  {
68  m_searchCombo->SetSelection( 0 );
69  m_searchCombo->SelectAll();
70  }
71 
72  m_matchCase->SetValue( FindOptionCase );
73  m_matchWords->SetValue( FindOptionWords );
74  m_wildcards->SetValue( FindOptionWildcards );
75  m_wrap->SetValue( FindOptionWrap );
76 
77  m_includeTexts->SetValue( FindIncludeTexts );
81 
82  m_status->SetLabel( wxEmptyString);
83  m_upToDate = false;
84 
85  m_hitList.clear();
86  m_it = m_hitList.begin();
87 
88  m_findNext->SetDefault();
90 
91  Center();
92 }
93 
94 void DIALOG_FIND::onTextEnter( wxCommandEvent& aEvent )
95 {
96  search( true );
97 }
98 
99 void DIALOG_FIND::onFindNextClick( wxCommandEvent& aEvent )
100 {
101  search( true );
102 }
103 
104 void DIALOG_FIND::onFindPreviousClick( wxCommandEvent& aEvent )
105 {
106  search( false );
107 }
108 
109 void DIALOG_FIND::onSearchAgainClick( wxCommandEvent& aEvent )
110 {
111  m_upToDate = false;
112  search( true );
113 }
114 
115 void DIALOG_FIND::search( bool aDirection )
116 {
117  PCB_SCREEN* screen = m_frame->GetScreen();
118  int flags;
119  int index;
120  wxString msg;
121  wxString searchString;
122 
123  // Add/move the search string to the top of the list if it isn't already there
124  searchString = m_searchCombo->GetValue();
125  index = m_searchCombo->FindString( searchString, true );
126 
127  if( index == wxNOT_FOUND )
128  {
129  m_searchCombo->Insert( searchString, 0 );
130  m_searchCombo->SetSelection( 0 );
131  m_upToDate = false;
132  m_frame->GetFindHistoryList().Insert( searchString, 0 );
133 
134  if( m_searchCombo->GetCount() > 10 )
135  {
136  m_frame->GetFindHistoryList().pop_back();
137  m_searchCombo->Delete( 10 );
138  }
139  }
140  else if( index != 0 )
141  {
142  m_searchCombo->Delete( index );
143  m_searchCombo->Insert( searchString, 0 );
144  m_searchCombo->SetSelection( 0 );
145  m_upToDate = false;
146 
147  if( m_frame->GetFindHistoryList().Index( searchString ) )
148  m_frame->GetFindHistoryList().Remove( searchString );
149 
150  m_frame->GetFindHistoryList().Insert( searchString, 0 );
151  }
152 
153  // Update search flags
154  flags = 0;
155 
156  if( FindOptionCase != m_matchCase->GetValue() )
157  {
158  FindOptionCase = m_matchCase->GetValue();
159  m_upToDate = false;
160  }
161 
162  if( FindOptionWords != m_matchWords->GetValue() )
163  {
164  FindOptionWords = m_matchWords->GetValue();
165  m_upToDate = false;
166  }
167 
168  if( FindOptionWildcards != m_wildcards->GetValue() )
169  {
170  FindOptionWildcards = m_wildcards->GetValue();
171  m_upToDate = false;
172  }
173 
174  FindOptionWrap = m_wrap->GetValue();
175 
176  if( FindIncludeTexts != m_includeTexts->GetValue() )
177  {
178  FindIncludeTexts = m_includeTexts->GetValue();
179  m_upToDate = false;
180  }
181 
182  if( FindIncludeValues != m_includeValues->GetValue() )
183  {
184  FindIncludeValues = m_includeValues->GetValue();
185  m_upToDate = false;
186  }
187 
188  if( FindIncludeReferences != m_includeReferences->GetValue() )
189  {
191  m_upToDate = false;
192  }
193 
194  if( FindIncludeMarkers != m_includeMarkers->GetValue() )
195  {
196  FindIncludeMarkers = m_includeMarkers->GetValue();
197  m_upToDate = false;
198  }
199 
200  if( FindOptionCase )
201  flags |= wxFR_MATCHCASE;
202 
203  if( FindOptionWords )
204  flags |= wxFR_WHOLEWORD;
205 
206  if( FindOptionWildcards )
207  flags |= FR_MATCH_WILDCARD;
208 
209  // Search parameters
210  m_frame->GetFindReplaceData().SetFindString( searchString );
211  m_frame->GetFindReplaceData().SetFlags( flags );
212 
214  m_frame->GetCanvas()->GetViewStart( &screen->m_StartVisu.x, &screen->m_StartVisu.y );
215 
216  // Refresh the list of results
217  if( !m_upToDate )
218  {
219  m_status->SetLabel( _( "Searching..." ) );
220  m_hitList.clear();
221 
223  {
224  for( MODULE* module : m_frame->GetBoard()->Modules() )
225  {
226  if( ( module->Reference().Matches( m_frame->GetFindReplaceData(), nullptr )
228  || ( module->Value().Matches( m_frame->GetFindReplaceData(), nullptr )
229  && FindIncludeValues ) )
230  {
231  m_hitList.push_back( module );
232  }
233 
234  if( m_includeTexts->GetValue() )
235  {
236  for( BOARD_ITEM* item : module->GraphicalItems() )
237  {
238  TEXTE_MODULE* textItem = dynamic_cast<TEXTE_MODULE*>( item );
239 
240  if( textItem
241  && textItem->Matches( m_frame->GetFindReplaceData(), nullptr ) )
242  {
243  m_hitList.push_back( module );
244  }
245  }
246  }
247  }
248 
249  if( FindIncludeTexts )
250  {
251  for( BOARD_ITEM* item : m_frame->GetBoard()->Drawings() )
252  {
253  TEXTE_PCB* textItem = dynamic_cast<TEXTE_PCB*>( item );
254 
255  if( textItem && textItem->Matches( m_frame->GetFindReplaceData(), nullptr ) )
256  {
257  m_hitList.push_back( textItem );
258  }
259  }
260  }
261  }
262 
263  if( FindIncludeMarkers )
264  {
265  for( MARKER_PCB* marker : m_frame->GetBoard()->Markers() )
266  {
267  if( marker->Matches( m_frame->GetFindReplaceData(), nullptr ) )
268  m_hitList.push_back( marker );
269  }
270  }
271 
272  m_upToDate = true;
273 
274  if( aDirection )
275  m_it = m_hitList.begin();
276  else
277  m_it = m_hitList.end();
278  }
279 
280  // Do we want a sorting algorithm ? If so, implement it here.
281 
282  // Get the item to display
283  if( m_hitList.empty() )
284  {
285  m_frame->SetStatusText( wxEmptyString );
286  }
287  else
288  {
289  if( aDirection )
290  {
291  m_it++;
292 
293  if( m_it == m_hitList.end() )
294  {
295  if( m_wrap->GetValue() )
296  m_it = m_hitList.begin();
297  else
298  {
299  m_frame->SetStatusText( wxEmptyString );
300  m_frame->ShowInfoBarMsg( _( "No more item to show" ) );
301  return;
302  }
303  }
304  }
305  else
306  {
307  if( m_it == m_hitList.begin() )
308  {
309  if( m_wrap->GetValue() )
310  {
311  m_it = m_hitList.end();
312  }
313  else
314  {
315  m_frame->SetStatusText( wxEmptyString );
316  m_frame->ShowInfoBarMsg( _( "No more item to show" ) );
317  return;
318  }
319  }
320 
321  m_it--;
322  }
323  }
324 
325  // Display the item
326  if( m_it != m_hitList.end() )
327  {
329  m_frame->FocusOnLocation( ( *m_it )->GetPosition() );
330 
331  msg.Printf( _( "\"%s\" found" ), searchString );
332  m_frame->SetStatusText( msg );
333 
334  msg.Printf( _( "Hit(s): %ld / %lu" ), std::distance( m_hitList.begin(), m_it ),
335  m_hitList.size() );
336  m_status->SetLabel( msg );
337  }
338  else
339  {
340  m_frame->SetStatusText( wxEmptyString );
341 
342  msg.Printf( _( "\"%s\" not found" ), searchString );
343  m_frame->ShowInfoBarMsg( msg );
344 
345  m_status->SetLabel( _( "No hits" ) );
346  }
347 
348  if( m_highlightCallback )
350 }
351 
352 void DIALOG_FIND::onClose( wxCommandEvent& aEvent )
353 {
354  FindOptionCase = m_matchCase->GetValue();
355  FindOptionWords = m_matchWords->GetValue();
356  FindOptionWildcards = m_wildcards->GetValue();
357  FindOptionWrap = m_wrap->GetValue();
358 
359  FindIncludeTexts = m_includeTexts->GetValue();
360  FindIncludeValues = m_includeValues->GetValue();
361  FindIncludeMarkers = m_includeMarkers->GetValue();
363 
364  EndModal( 1 );
365 }
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:62
bool Matches(wxFindReplaceData &aSearchData, void *aAuxData) override
Function Matches compares the item against the search criteria in aSearchData.
TEXTE_PCB class definition.
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
MARKERS & Markers()
Definition: class_board.h:257
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
wxCheckBox * m_includeReferences
void ShowInfoBarMsg(const wxString &aMsg)
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:43
bool FindIncludeTexts
Definition: dialog_find.cpp:47
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:50
wxStaticText * m_status
wxFindReplaceData & GetFindReplaceData()
void onTextEnter(wxCommandEvent &event) override
Definition: dialog_find.cpp:94
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:65
Markers used to show a drc problem on boards.
bool FindIncludeValues
Definition: dialog_find.cpp:48
Footprint text class description.
void onFindNextClick(wxCommandEvent &event) override
Definition: dialog_find.cpp:99
wxComboBox * m_searchCombo
void onFindPreviousClick(wxCommandEvent &event) override
MODULES & Modules()
Definition: class_board.h:249
wxCheckBox * m_includeTexts
wxCheckBox * m_matchCase
DIALOG_FIND(PCB_BASE_FRAME *aParent)
Definition: dialog_find.cpp:53
wxCheckBox * m_wildcards
bool FindIncludeReferences
Definition: dialog_find.cpp:49
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:45
bool FindOptionWildcards
Definition: dialog_find.cpp:44
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:78
bool FindOptionCase
Definition: dialog_find.cpp:42
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:252
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