KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2022 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 <pcb_edit_frame.h> // Keep this include at top to avoid compil issue on MSYS2
27#include <board.h>
28#include <pcb_marker.h>
29#include <footprint.h>
30#include <pcb_text.h>
31#include <zone.h>
32#include <dialog_find.h>
33#include <string_utils.h>
34#include <string>
35#include <tool/tool_manager.h>
36#include <tools/pcb_actions.h>
37#include <wx/fdrepdlg.h>
38
39
40//Defined as global because these values have to survive the destructor
41
42bool FindOptionCase = false;
43bool FindOptionWords = false;
45bool FindOptionWrap = true;
46
47bool FindIncludeTexts = true;
51bool FindIncludeNets = 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 );
77 m_wrap->SetValue( FindOptionWrap );
78
83 m_includeNets->SetValue( FindIncludeNets );
84
85 m_status->SetLabel( wxEmptyString);
86 m_upToDate = false;
87
88 m_hitList.clear();
89 m_it = m_hitList.begin();
90
91 m_findNext->SetDefault();
93
94 Center();
95}
96
97
98void DIALOG_FIND::Preload( const wxString& aFindString )
99{
100 if( !aFindString.IsEmpty() )
101 {
102 m_searchCombo->SetValue( aFindString );
103 m_searchCombo->SelectAll();
104 }
105}
106
107
108void DIALOG_FIND::onTextEnter( wxCommandEvent& aEvent )
109{
110 search( true );
111}
112
113
114void DIALOG_FIND::onFindNextClick( wxCommandEvent& aEvent )
115{
116 search( true );
117}
118
119
120void DIALOG_FIND::onFindPreviousClick( wxCommandEvent& aEvent )
121{
122 search( false );
123}
124
125
126void DIALOG_FIND::onSearchAgainClick( wxCommandEvent& aEvent )
127{
128 m_upToDate = false;
129 search( true );
130}
131
132
133void DIALOG_FIND::search( bool aDirection )
134{
135 PCB_SCREEN* screen = m_frame->GetScreen();
136 int index;
137 wxString msg;
138 wxString searchString;
139 bool endIsReached = false;
140 bool isFirstSearch = false;
141
142 searchString = m_searchCombo->GetValue();
143
144 if( searchString.IsEmpty() )
145 {
146 Show();
147 return;
148 }
149
150 // Add/move the search string to the top of the list if it isn't already there
151 index = m_searchCombo->FindString( searchString, true );
152
153 if( index == wxNOT_FOUND )
154 {
155 m_searchCombo->Insert( searchString, 0 );
156 m_searchCombo->SetSelection( 0 );
157 m_upToDate = false;
158 m_frame->GetFindHistoryList().Insert( searchString, 0 );
159
160 if( m_searchCombo->GetCount() > 10 )
161 {
162 m_frame->GetFindHistoryList().pop_back();
163 m_searchCombo->Delete( 10 );
164 }
165 }
166 else if( index != 0 )
167 {
168 m_searchCombo->Delete( index );
169 m_searchCombo->Insert( searchString, 0 );
170 m_searchCombo->SetSelection( 0 );
171 m_upToDate = false;
172
173 if( m_frame->GetFindHistoryList().Index( searchString ) )
174 m_frame->GetFindHistoryList().Remove( searchString );
175
176 m_frame->GetFindHistoryList().Insert( searchString, 0 );
177 }
178
179 if( FindOptionCase != m_matchCase->GetValue() )
180 {
181 FindOptionCase = m_matchCase->GetValue();
182 m_upToDate = false;
183 }
184
185 if( FindOptionWords != m_matchWords->GetValue() )
186 {
187 FindOptionWords = m_matchWords->GetValue();
188 m_upToDate = false;
189 }
190
191 if( FindOptionWildcards != m_wildcards->GetValue() )
192 {
193 FindOptionWildcards = m_wildcards->GetValue();
194 m_upToDate = false;
195 }
196
197 FindOptionWrap = m_wrap->GetValue();
198
199 if( FindIncludeTexts != m_includeTexts->GetValue() )
200 {
201 FindIncludeTexts = m_includeTexts->GetValue();
202 m_upToDate = false;
203 }
204
205 if( FindIncludeValues != m_includeValues->GetValue() )
206 {
208 m_upToDate = false;
209 }
210
211 if( FindIncludeReferences != m_includeReferences->GetValue() )
212 {
214 m_upToDate = false;
215 }
216
217 if( FindIncludeMarkers != m_includeMarkers->GetValue() )
218 {
220 m_upToDate = false;
221 }
222
223 if( FindIncludeNets != m_includeNets->GetValue() )
224 {
225 FindIncludeNets = m_includeNets->GetValue();
226 m_upToDate = false;
227 }
228
229 if( FindOptionCase )
231
232 if( FindOptionWords )
233 m_frame->GetFindReplaceData().matchMode = EDA_SEARCH_MATCH_MODE::WHOLEWORD;
234 else if( FindOptionWildcards )
235 m_frame->GetFindReplaceData().matchMode = EDA_SEARCH_MATCH_MODE::WILDCARD;
236 else
237 m_frame->GetFindReplaceData().matchMode = EDA_SEARCH_MATCH_MODE::PLAIN;
238
239 // Search parameters
240 m_frame->GetFindReplaceData().findString = searchString;
241
243 m_frame->GetCanvas()->GetViewStart( &screen->m_StartVisu.x, &screen->m_StartVisu.y );
244
245 BOARD* board = m_frame->GetBoard();
246
247 // Refresh the list of results
248 if( !m_upToDate )
249 {
250 m_status->SetLabel( _( "Searching..." ) );
251 m_hitList.clear();
252
254 {
255 for( FOOTPRINT* fp : board->Footprints() )
256 {
257 if( ( fp->Reference().Matches( m_frame->GetFindReplaceData(), nullptr )
259 || ( fp->Value().Matches( m_frame->GetFindReplaceData(), nullptr )
260 && FindIncludeValues ) )
261 {
262 m_hitList.push_back( fp );
263 }
264
265 if( m_includeTexts->GetValue() )
266 {
267 for( BOARD_ITEM* item : fp->GraphicalItems() )
268 {
269 if( item->Type() == PCB_TEXT_T )
270 {
271 PCB_TEXT* text = static_cast<PCB_TEXT*>( item );
272
273 if( text && text->Matches( m_frame->GetFindReplaceData(), nullptr ) )
274 m_hitList.push_back( fp );
275 }
276 }
277
278 for( PCB_FIELD* field : fp->Fields() )
279 {
280 if( field->Matches( m_frame->GetFindReplaceData(), nullptr ) )
281 m_hitList.push_back( fp );
282 }
283 }
284 }
285
286 if( FindIncludeTexts )
287 {
288 for( BOARD_ITEM* item : board->Drawings() )
289 {
290 if( item->Type() == PCB_TEXT_T )
291 {
292 PCB_TEXT* text = static_cast<PCB_TEXT*>( item );
293
294 if( text && text->Matches( m_frame->GetFindReplaceData(), nullptr ) )
295 m_hitList.push_back( text );
296 }
297 }
298
299 for( BOARD_ITEM* item : board->Zones() )
300 {
301 ZONE* zone = static_cast<ZONE*>( item );
302
303 if( zone->Matches( m_frame->GetFindReplaceData(), nullptr ) )
304 m_hitList.push_back( zone );
305 }
306 }
307 }
308
310 {
311 for( PCB_MARKER* marker : board->Markers() )
312 {
313 if( marker->Matches( m_frame->GetFindReplaceData(), nullptr ) )
314 m_hitList.push_back( marker );
315 }
316 }
317
318 if( FindIncludeNets )
319 {
320 for( NETINFO_ITEM* net : board->GetNetInfo() )
321 {
322 if( net && net->Matches( m_frame->GetFindReplaceData(), nullptr ) )
323 m_hitList.push_back( net );
324 }
325 }
326
327 m_upToDate = true;
328 isFirstSearch = true;
329
330 if( aDirection )
331 m_it = m_hitList.begin();
332 else
333 m_it = m_hitList.end();
334 }
335
336 // Do we want a sorting algorithm ? If so, implement it here.
337
338 // Get the item to display
339 if( m_hitList.empty() )
340 {
341 m_frame->SetStatusText( wxEmptyString );
342 }
343 else
344 {
345 if( aDirection )
346 {
347 if( m_it != m_hitList.end() && !isFirstSearch )
348 m_it++;
349
350 if( m_it == m_hitList.end() )
351 {
352 if( m_wrap->GetValue() )
353 {
354 m_it = m_hitList.begin();
355 }
356 else
357 {
358 endIsReached = true;
359 m_it--; // point to the last REAL result
360 }
361 }
362 }
363 else
364 {
365 if( m_it == m_hitList.begin() )
366 {
367 if( m_wrap->GetValue() )
368 m_it = m_hitList.end();
369 else
370 endIsReached = true;
371 }
372
373 if( m_it != m_hitList.begin() )
374 m_it--;
375 }
376 }
377
378 // Display the item
379 if( m_hitList.empty() )
380 {
381 m_frame->SetStatusText( wxEmptyString );
382 msg.Printf( _( "'%s' not found" ), searchString );
383 m_frame->ShowInfoBarMsg( msg );
384
385 m_status->SetLabel( msg );
386 }
387 else if( endIsReached )
388 {
389 m_frame->SetStatusText( wxEmptyString );
390 m_frame->ShowInfoBarMsg( _( "No more items to show" ) );
391
392 m_status->SetLabel( _( "No hits" ) );
393 }
394 else
395 {
397
398 msg.Printf( _( "'%s' found" ), searchString );
399 m_frame->SetStatusText( msg );
400
401 msg.Printf( _( "Hit(s): %d / %zu" ),
402 (int)std::distance( m_hitList.begin(), m_it ) + 1,
403 m_hitList.size() );
404 m_status->SetLabel( msg );
405 }
406
409}
410
411
412void DIALOG_FIND::OnCloseButtonClick( wxCommandEvent& aEvent )
413{
414 wxCloseEvent tmp;
415
416 OnClose( tmp );
417
418 aEvent.Skip();
419}
420
421bool DIALOG_FIND::Show( bool show )
422{
423 bool ret = DIALOG_FIND_BASE::Show( show );
424
425 if( show )
426 m_searchCombo->SetFocus();
427
428 return ret;
429}
430
431
432void DIALOG_FIND::OnClose( wxCloseEvent& aEvent )
433{
434 FindOptionCase = m_matchCase->GetValue();
435 FindOptionWords = m_matchWords->GetValue();
436 FindOptionWildcards = m_wildcards->GetValue();
437 FindOptionWrap = m_wrap->GetValue();
438
439 FindIncludeTexts = m_includeTexts->GetValue();
443 FindIncludeNets = m_includeNets->GetValue();
444
445 aEvent.Skip();
446}
VECTOR2I m_StartVisu
Coordinates in drawing units of the current view position (upper left corner of device)
Definition: base_screen.h:93
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
const NETINFO_LIST & GetNetInfo() const
Definition: board.h:871
const ZONES & Zones() const
Definition: board.h:335
const MARKERS & Markers() const
Definition: board.h:339
const FOOTPRINTS & Footprints() const
Definition: board.h:331
const DRAWINGS & Drawings() const
Definition: board.h:333
Class DIALOG_FIND_BASE.
wxCheckBox * m_matchCase
wxCheckBox * m_includeTexts
wxCheckBox * m_includeValues
wxButton * m_findNext
wxComboBox * m_searchCombo
wxCheckBox * m_includeReferences
wxCheckBox * m_matchWords
wxCheckBox * m_includeNets
wxStaticText * m_status
wxCheckBox * m_includeMarkers
wxCheckBox * m_wildcards
wxCheckBox * m_wrap
void onTextEnter(wxCommandEvent &event) override
bool Show(bool show=true) override
The Show method is overridden to make the search combobox focused by default.
std::deque< BOARD_ITEM * >::iterator m_it
Definition: dialog_find.h:94
void Preload(const wxString &aFindString)
Definition: dialog_find.cpp:98
void onFindNextClick(wxCommandEvent &event) override
void search(bool direction)
bool m_upToDate
Definition: dialog_find.h:95
void onSearchAgainClick(wxCommandEvent &event) override
DIALOG_FIND(PCB_BASE_FRAME *aParent)
Definition: dialog_find.cpp:54
BOARD_ITEM * GetItem() const
Return the currently found item or nullptr in the case of no items found.
Definition: dialog_find.h:50
void onFindPreviousClick(wxCommandEvent &event) override
void OnClose(wxCloseEvent &event) override
std::function< void(BOARD_ITEM *)> m_highlightCallback
Definition: dialog_find.h:97
void OnCloseButtonClick(wxCommandEvent &aEvent) override
PCB_BASE_FRAME * m_frame
Definition: dialog_find.h:92
std::deque< BOARD_ITEM * > m_hitList
Definition: dialog_find.h:93
bool Show(bool show) override
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:102
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...
wxArrayString & GetFindHistoryList()
EDA_SEARCH_DATA & GetFindReplaceData()
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
Handle the data for a net.
Definition: netinfo.h:56
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:68
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition: pcb_actions.h:71
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
PCB_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
BOARD * GetBoard() const
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:150
Handle a list of polygons defining a copper zone.
Definition: zone.h:73
bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition: zone.h:135
bool FindIncludeMarkers
Definition: dialog_find.cpp:50
bool FindOptionCase
Definition: dialog_find.cpp:42
bool FindIncludeValues
Definition: dialog_find.cpp:48
bool FindOptionWords
Definition: dialog_find.cpp:43
bool FindOptionWrap
Definition: dialog_find.cpp:45
bool FindIncludeReferences
Definition: dialog_find.cpp:49
bool FindOptionWildcards
Definition: dialog_find.cpp:44
bool FindIncludeTexts
Definition: dialog_find.cpp:47
bool FindIncludeNets
Definition: dialog_find.cpp:51
#define _(s)
EDA_SEARCH_MATCH_MODE matchMode
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92