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 The 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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <pcb_edit_frame.h> // Keep this include at top to avoid compil issue on MSYS2
23#include <board.h>
24#include <pcb_marker.h>
25#include <footprint.h>
26#include <pcb_text.h>
27#include <zone.h>
28#include <dialog_find.h>
29#include <string_utils.h>
30#include <hotkeys_basic.h>
31#include <string>
32#include <tool/tool_manager.h>
33#include <tools/pcb_actions.h>
34#include <wx/fdrepdlg.h>
35
36
38 DIALOG_FIND_BASE( aFrame, wxID_ANY, _( "Find" ) ),
39 m_frame( aFrame )
40{
41 GetSizer()->SetSizeHints( this );
42
44
45 m_searchCombo->Append( m_frame->GetFindHistoryList() );
46
47 while( m_searchCombo->GetCount() > 10 )
48 {
49 m_frame->GetFindHistoryList().pop_back();
50 m_searchCombo->Delete( 9 );
51 }
52
53 if( m_searchCombo->GetCount() )
54 {
55 m_searchCombo->SetSelection( 0 );
56 m_searchCombo->SelectAll();
57 }
58
59 m_status->SetLabel( wxEmptyString);
60 m_upToDate = false;
61
62 m_hitList.clear();
63 m_it = m_hitList.begin();
64
65 m_board = m_frame->GetBoard();
66
67 if( m_board )
68 m_board->AddListener( this );
69
70 m_frame->Bind( EDA_EVT_BOARD_CHANGED, &DIALOG_FIND::OnBoardChanged, this );
71
72 if( int hotkey = ACTIONS::showSearch.GetHotKey() )
73 {
74 wxString hotkeyHint = wxString::Format( wxT( " (%s)" ), KeyNameFromKeyCode( hotkey ) );
75 m_searchPanelLink->SetLabel( m_searchPanelLink->GetLabel() + hotkeyHint );
76 m_searchPanelLink->GetParent()->Layout();
77 }
78
79 m_findNext->SetDefault();
81
82 Center();
83}
84
85
87{
88 if( m_board )
89 m_board->RemoveListener( this );
90
91 m_frame->Unbind( EDA_EVT_BOARD_CHANGED, &DIALOG_FIND::OnBoardChanged, this );
92}
93
94
95void DIALOG_FIND::Preload( const wxString& aFindString )
96{
97 if( !aFindString.IsEmpty() )
98 {
99 m_searchCombo->SetValue( aFindString );
100 m_searchCombo->SelectAll();
101 }
102}
103
104
105void DIALOG_FIND::onTextEnter( wxCommandEvent& aEvent )
106{
107 search( true );
108}
109
110
111void DIALOG_FIND::onFindNextClick( wxCommandEvent& aEvent )
112{
113 search( true );
114}
115
116
117void DIALOG_FIND::onFindPreviousClick( wxCommandEvent& aEvent )
118{
119 search( false );
120}
121
122
123void DIALOG_FIND::OnCharHook( wxKeyEvent& aEvent )
124{
125 if( aEvent.GetKeyCode() == WXK_F3 )
126 {
127 FindNext( aEvent.ShiftDown() );
128 return;
129 }
130
131 DIALOG_SHIM::OnCharHook( aEvent );
132}
133
134
135void DIALOG_FIND::onSearchAgainClick( wxCommandEvent& aEvent )
136{
137 m_upToDate = false;
138 search( true );
139}
140
141
142void DIALOG_FIND::onShowSearchPanel( wxHyperlinkEvent& event )
143{
144 m_frame->GetToolManager()->RunAction( ACTIONS::showSearch );
145
146 Show( false );
147
148 CallAfter(
149 []()
150 {
151 if( wxWindow* frame = wxWindow::FindWindowByName( PCB_EDIT_FRAME_NAME ) )
152 static_cast<PCB_EDIT_FRAME*>( frame )->FocusSearch();
153 } );
154}
155
156
157void DIALOG_FIND::search( bool aDirection )
158{
159 PCB_SCREEN* screen = m_frame->GetScreen();
160 int index;
161 wxString msg;
162 wxString searchString;
163 bool endIsReached = false;
164 bool isFirstSearch = false;
165
166 searchString = m_searchCombo->GetValue();
167
168 if( searchString.IsEmpty() )
169 {
170 Show();
171 return;
172 }
173
174 // Add/move the search string to the top of the list if it isn't already there
175 index = m_searchCombo->FindString( searchString, true );
176
177 if( index == wxNOT_FOUND )
178 {
179 m_searchCombo->Insert( searchString, 0 );
180 m_searchCombo->SetSelection( 0 );
181 m_upToDate = false;
182 m_frame->GetFindHistoryList().Insert( searchString, 0 );
183
184 if( m_searchCombo->GetCount() > 10 )
185 {
186 m_frame->GetFindHistoryList().pop_back();
187 m_searchCombo->Delete( 10 );
188 }
189 }
190 else if( index != 0 )
191 {
192 m_searchCombo->Delete( index );
193 m_searchCombo->Insert( searchString, 0 );
194 m_searchCombo->SetSelection( 0 );
195 m_upToDate = false;
196
197 if( m_frame->GetFindHistoryList().Index( searchString ) )
198 m_frame->GetFindHistoryList().Remove( searchString );
199
200 m_frame->GetFindHistoryList().Insert( searchString, 0 );
201 }
202
203 EDA_SEARCH_DATA& frd = m_frame->GetFindReplaceData();
204
205 if( m_matchCase->GetValue() )
206 frd.matchCase = true;
207
208 if( m_matchWords->GetValue() )
210 else if( m_wildcards->GetValue() )
212 else
214
215 frd.searchAllFields = m_checkAllFields->GetValue();
216
217 // Search parameters
218 frd.findString = searchString;
219
220 m_frame->GetCanvas()->GetViewStart( &screen->m_StartVisu.x, &screen->m_StartVisu.y );
221
222 BOARD* board = m_frame->GetBoard();
223
224 // Refresh the list of results
225 if( !m_upToDate )
226 {
227 m_status->SetLabel( _( "Searching..." ) );
228 m_hitList.clear();
229
230 if( m_includeTexts->GetValue() || m_includeValues->GetValue() || m_includeReferences->GetValue() )
231 {
232 for( FOOTPRINT* fp : board->Footprints() )
233 {
234 bool found = false;
235
236 if( m_includeReferences->GetValue() && fp->Reference().Matches( frd, nullptr ) )
237 found = true;
238
239 if( !found && m_includeValues->GetValue() && fp->Value().Matches( frd, nullptr ) )
240 found = true;
241
242 if( !found && m_includeTexts->GetValue() )
243 {
244 for( BOARD_ITEM* item : fp->GraphicalItems() )
245 {
246 if( item->Type() == PCB_TEXT_T )
247 {
248 PCB_TEXT* text = static_cast<PCB_TEXT*>( item );
249
250 if( text->Matches( frd, nullptr ) )
251 {
252 found = true;
253 break;
254 }
255 }
256 }
257 }
258
259 if( !found && m_includeTexts->GetValue() )
260 {
261 for( PCB_FIELD* field : fp->GetFields() )
262 {
263 wxCHECK2( field, continue );
264
265 if( field->Matches( frd, nullptr ) )
266 {
267 found = true;
268 break;
269 }
270 }
271 }
272
273 if( found )
274 m_hitList.push_back( fp );
275 }
276
277 if( m_includeTexts->GetValue() )
278 {
279 for( BOARD_ITEM* item : board->Drawings() )
280 {
281 if( item->Type() == PCB_TEXT_T )
282 {
283 PCB_TEXT* text = static_cast<PCB_TEXT*>( item );
284
285 if( text && text->Matches( frd, nullptr ) )
286 m_hitList.push_back( text );
287 }
288 }
289
290 for( BOARD_ITEM* item : board->Zones() )
291 {
292 ZONE* zone = static_cast<ZONE*>( item );
293
294 if( zone->Matches( frd, nullptr ) )
295 m_hitList.push_back( zone );
296 }
297 }
298 }
299
300 if( m_includeMarkers->GetValue() )
301 {
302 for( PCB_MARKER* marker : board->Markers() )
303 {
304 if( marker->Matches( frd, nullptr ) )
305 m_hitList.push_back( marker );
306 }
307 }
308
309 if( m_includeNets->GetValue() )
310 {
311 for( NETINFO_ITEM* net : board->GetNetInfo() )
312 {
313 if( net && net->Matches( frd, nullptr ) )
314 m_hitList.push_back( net );
315 }
316 }
317
318 m_upToDate = true;
319 isFirstSearch = true;
320
321 if( aDirection )
322 m_it = m_hitList.begin();
323 else
324 m_it = m_hitList.end();
325 }
326
327 // Do we want a sorting algorithm ? If so, implement it here.
328
329 // Get the item to display
330 if( m_hitList.empty() )
331 {
332 m_frame->SetStatusText( wxEmptyString );
333 }
334 else
335 {
336 if( aDirection )
337 {
338 if( m_it != m_hitList.end() && !isFirstSearch )
339 m_it++;
340
341 if( m_it == m_hitList.end() )
342 {
343 if( m_wrap->GetValue() )
344 {
345 m_it = m_hitList.begin();
346 }
347 else
348 {
349 endIsReached = true;
350 m_it--; // point to the last REAL result
351 }
352 }
353 }
354 else
355 {
356 if( m_it == m_hitList.begin() )
357 {
358 if( m_wrap->GetValue() )
359 m_it = m_hitList.end();
360 else
361 endIsReached = true;
362 }
363
364 if( m_it != m_hitList.begin() )
365 m_it--;
366 }
367 }
368
369 // Display the item
370 if( m_hitList.empty() )
371 {
372 m_frame->SetStatusText( wxEmptyString );
373 msg.Printf( _( "'%s' not found" ), searchString );
374 m_frame->ShowInfoBarMsg( msg );
375
376 m_status->SetLabel( msg );
377 }
378 else if( endIsReached || m_it == m_hitList.end() )
379 {
380 m_frame->SetStatusText( wxEmptyString );
381 m_frame->ShowInfoBarMsg( _( "No more items to show" ) );
382
383 m_status->SetLabel( _( "No hits" ) );
384 }
385 else
386 {
387 m_frame->GetToolManager()->RunAction( ACTIONS::selectionClear );
388 m_frame->GetToolManager()->RunAction<EDA_ITEM*>( ACTIONS::selectItem, *m_it );
389
390 msg.Printf( _( "'%s' found" ), searchString );
391 m_frame->SetStatusText( msg );
392
393 msg.Printf( _( "Hit(s): %d / %zu" ),
394 (int)std::distance( m_hitList.begin(), m_it ) + 1,
395 m_hitList.size() );
396 m_status->SetLabel( msg );
397 }
398
401}
402
403
404bool DIALOG_FIND::Show( bool show )
405{
406 bool ret = DIALOG_FIND_BASE::Show( show );
407
408 if( show )
409 m_searchCombo->SetFocus();
410
411 return ret;
412}
413
414
415void DIALOG_FIND::OnBoardChanged( wxCommandEvent& event )
416{
417 m_board = m_frame->GetBoard();
418
419 if( m_board )
420 m_board->AddListener( this );
421
422 m_upToDate = false;
423
424 event.Skip();
425}
int index
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition actions.h:223
static TOOL_ACTION showSearch
Definition actions.h:112
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:220
VECTOR2I m_StartVisu
Coordinates in drawing units of the current view position (upper left corner of device)
Definition base_screen.h:89
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:83
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:373
const NETINFO_LIST & GetNetInfo() const
Definition board.h:1098
const ZONES & Zones() const
Definition board.h:425
const MARKERS & Markers() const
Definition board.h:440
const FOOTPRINTS & Footprints() const
Definition board.h:421
const DRAWINGS & Drawings() const
Definition board.h:423
wxCheckBox * m_checkAllFields
wxCheckBox * m_matchCase
wxCheckBox * m_includeTexts
wxCheckBox * m_includeValues
wxHyperlinkCtrl * m_searchPanelLink
wxComboBox * m_searchCombo
wxCheckBox * m_includeReferences
wxCheckBox * m_matchWords
wxCheckBox * m_includeNets
wxStaticText * m_status
wxCheckBox * m_includeMarkers
wxCheckBox * m_wildcards
wxCheckBox * m_wrap
DIALOG_FIND_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=wxEmptyString, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_DIALOG_STYLE)
PCB_EDIT_FRAME * m_frame
~DIALOG_FIND() override
void onTextEnter(wxCommandEvent &event) override
DIALOG_FIND(PCB_EDIT_FRAME *aParent)
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
void Preload(const wxString &aFindString)
void onFindNextClick(wxCommandEvent &event) override
void search(bool direction)
void onSearchAgainClick(wxCommandEvent &event) override
BOARD * m_board
BOARD_ITEM * GetItem() const
Return the currently found item or nullptr in the case of no items found.
Definition dialog_find.h:48
void onShowSearchPanel(wxHyperlinkEvent &event) override
void onFindPreviousClick(wxCommandEvent &event) override
void OnCharHook(wxKeyEvent &aEvent) override
std::function< void(BOARD_ITEM *)> m_highlightCallback
void FindNext(bool reverse)
Finds the next item.
Definition dialog_find.h:69
std::deque< BOARD_ITEM * > m_hitList
void OnBoardChanged(wxCommandEvent &event)
bool Show(bool show) override
void OptOut(wxWindow *aWindow)
Opt out of control state saving.
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition dialog_shim.h:94
virtual void OnCharHook(wxKeyEvent &aEvt)
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
Handle the data for a net.
Definition netinfo.h:46
The main frame for Pcbnew.
Handle a list of polygons defining a copper zone.
Definition zone.h:70
bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition zone.h:163
#define _(s)
#define PCB_EDIT_FRAME_NAME
wxString KeyNameFromKeyCode(int aKeycode, bool *aIsFound)
Return the key name from the key code.
EDA_SEARCH_MATCH_MODE matchMode
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:85