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>
33#include <tool/tool_manager.h>
34#include <tools/pcb_actions.h>
35#include <wx/fdrepdlg.h>
36
37
39 DIALOG_FIND_BASE( aFrame, wxID_ANY, _( "Find" ) ),
40 m_frame( aFrame )
41{
42 GetSizer()->SetSizeHints( this );
43
45
46 m_searchCombo->Append( m_frame->GetFindHistoryList() );
47
48 while( m_searchCombo->GetCount() > 10 )
49 {
50 m_frame->GetFindHistoryList().pop_back();
51 m_searchCombo->Delete( 9 );
52 }
53
54 if( m_searchCombo->GetCount() )
55 {
56 m_searchCombo->SetSelection( 0 );
57 m_searchCombo->SelectAll();
58 }
59
60 m_status->SetLabel( wxEmptyString);
61 m_upToDate = false;
62
63 m_hitList.clear();
64 m_it = m_hitList.begin();
65
66 m_board = m_frame->GetBoard();
67
68 if( m_board )
69 m_board->AddListener( this );
70
71 m_frame->Bind( EDA_EVT_BOARD_CHANGED, &DIALOG_FIND::OnBoardChanged, this );
72
73 if( int hotkey = ACTIONS::showSearch.GetHotKey() )
74 {
75 wxString hotkeyHint = wxString::Format( wxT( " (%s)" ), KeyNameFromKeyCode( hotkey ) );
76 m_searchPanelLink->SetLabel( m_searchPanelLink->GetLabel() + hotkeyHint );
77 m_searchPanelLink->GetParent()->Layout();
78 }
79
80 m_findNext->SetDefault();
82
83 Center();
84}
85
86
88{
89 if( m_board )
90 m_board->RemoveListener( this );
91
92 m_frame->Unbind( EDA_EVT_BOARD_CHANGED, &DIALOG_FIND::OnBoardChanged, this );
93}
94
95
96void DIALOG_FIND::Preload( const wxString& aFindString )
97{
98 if( !aFindString.IsEmpty() )
99 {
100 m_searchCombo->SetValue( aFindString );
101 m_searchCombo->SelectAll();
102 }
103}
104
105
106void DIALOG_FIND::onTextEnter( wxCommandEvent& aEvent )
107{
108 search( true );
109}
110
111
112void DIALOG_FIND::onFindNextClick( wxCommandEvent& aEvent )
113{
114 search( true );
115}
116
117
118void DIALOG_FIND::onFindPreviousClick( wxCommandEvent& aEvent )
119{
120 search( false );
121}
122
123
124void DIALOG_FIND::OnCharHook( wxKeyEvent& aEvent )
125{
126 bool special = false;
127 std::optional<TOOL_EVENT> evt = m_frame->GetToolDispatcher()->GetToolEvent( &aEvent, &special );
128
129 if( evt && evt->IsKeyPressed() && evt->KeyCode() )
130 {
131 // Match the user's hotkeys for the find actions, so they work while the dialog has focus
132 int hotkey = evt->Modifier() | evt->KeyCode();
133
135 {
136 if( hotkey == action->GetHotKey() || hotkey == action->GetHotKeyAlt() )
137 {
138 FindNext( action == &ACTIONS::findPrevious );
139 return;
140 }
141 }
142 }
143
144 DIALOG_SHIM::OnCharHook( aEvent );
145}
146
147
148void DIALOG_FIND::onSearchAgainClick( wxCommandEvent& aEvent )
149{
150 m_upToDate = false;
151 search( true );
152}
153
154
155void DIALOG_FIND::onShowSearchPanel( wxHyperlinkEvent& event )
156{
157 m_frame->GetToolManager()->RunAction( ACTIONS::showSearch );
158
159 Show( false );
160
161 CallAfter(
162 []()
163 {
164 if( wxWindow* frame = wxWindow::FindWindowByName( PCB_EDIT_FRAME_NAME ) )
165 static_cast<PCB_EDIT_FRAME*>( frame )->FocusSearch();
166 } );
167}
168
169
170void DIALOG_FIND::search( bool aDirection )
171{
172 PCB_SCREEN* screen = m_frame->GetScreen();
173 int index;
174 wxString msg;
175 wxString searchString;
176 bool endIsReached = false;
177 bool isFirstSearch = false;
178
179 searchString = m_searchCombo->GetValue();
180
181 if( searchString.IsEmpty() )
182 {
183 Show();
184 return;
185 }
186
187 // Add/move the search string to the top of the list if it isn't already there
188 index = m_searchCombo->FindString( searchString, true );
189
190 if( index == wxNOT_FOUND )
191 {
192 m_searchCombo->Insert( searchString, 0 );
193 m_searchCombo->SetSelection( 0 );
194 m_upToDate = false;
195 m_frame->GetFindHistoryList().Insert( searchString, 0 );
196
197 if( m_searchCombo->GetCount() > 10 )
198 {
199 m_frame->GetFindHistoryList().pop_back();
200 m_searchCombo->Delete( 10 );
201 }
202 }
203 else if( index != 0 )
204 {
205 m_searchCombo->Delete( index );
206 m_searchCombo->Insert( searchString, 0 );
207 m_searchCombo->SetSelection( 0 );
208 m_upToDate = false;
209
210 if( m_frame->GetFindHistoryList().Index( searchString ) )
211 m_frame->GetFindHistoryList().Remove( searchString );
212
213 m_frame->GetFindHistoryList().Insert( searchString, 0 );
214 }
215
216 EDA_SEARCH_DATA& frd = m_frame->GetFindReplaceData();
217
218 frd.matchCase = m_matchCase->GetValue();
219
220 if( m_matchWords->GetValue() )
222 else if( m_wildcards->GetValue() )
224 else
226
227 frd.searchAllFields = m_checkAllFields->GetValue();
228
229 // Search parameters
230 frd.findString = searchString;
231
232 m_frame->GetCanvas()->GetViewStart( &screen->m_StartVisu.x, &screen->m_StartVisu.y );
233
234 BOARD* board = m_frame->GetBoard();
235
236 // Refresh the list of results
237 if( !m_upToDate )
238 {
239 m_status->SetLabel( _( "Searching..." ) );
240 m_hitList.clear();
241
242 if( m_includeTexts->GetValue() || m_includeValues->GetValue() || m_includeReferences->GetValue() )
243 {
244 for( FOOTPRINT* fp : board->Footprints() )
245 {
246 bool found = false;
247
248 if( m_includeReferences->GetValue() && fp->Reference().Matches( frd, nullptr ) )
249 found = true;
250
251 if( !found && m_includeValues->GetValue() && fp->Value().Matches( frd, nullptr ) )
252 found = true;
253
254 if( !found && m_includeTexts->GetValue() )
255 {
256 for( BOARD_ITEM* item : fp->GraphicalItems() )
257 {
258 if( item->Type() == PCB_TEXT_T )
259 {
260 PCB_TEXT* text = static_cast<PCB_TEXT*>( item );
261
262 if( text->Matches( frd, nullptr ) )
263 {
264 found = true;
265 break;
266 }
267 }
268 }
269 }
270
271 if( !found && m_includeTexts->GetValue() )
272 {
273 for( PCB_FIELD* field : fp->GetFields() )
274 {
275 wxCHECK2( field, continue );
276
277 if( field->Matches( frd, nullptr ) )
278 {
279 found = true;
280 break;
281 }
282 }
283 }
284
285 if( found )
286 m_hitList.push_back( fp );
287 }
288
289 if( m_includeTexts->GetValue() )
290 {
291 for( BOARD_ITEM* item : board->Drawings() )
292 {
293 if( item->Type() == PCB_TEXT_T )
294 {
295 PCB_TEXT* text = static_cast<PCB_TEXT*>( item );
296
297 if( text && text->Matches( frd, nullptr ) )
298 m_hitList.push_back( text );
299 }
300 }
301
302 for( BOARD_ITEM* item : board->Zones() )
303 {
304 ZONE* zone = static_cast<ZONE*>( item );
305
306 if( zone->Matches( frd, nullptr ) )
307 m_hitList.push_back( zone );
308 }
309 }
310 }
311
312 if( m_includeMarkers->GetValue() )
313 {
314 for( PCB_MARKER* marker : board->Markers() )
315 {
316 if( marker->Matches( frd, nullptr ) )
317 m_hitList.push_back( marker );
318 }
319 }
320
321 if( m_includeNets->GetValue() )
322 {
323 for( NETINFO_ITEM* net : board->GetNetInfo() )
324 {
325 if( net && net->Matches( frd, nullptr ) )
326 m_hitList.push_back( net );
327 }
328 }
329
330 m_upToDate = true;
331 isFirstSearch = true;
332
333 if( aDirection )
334 m_it = m_hitList.begin();
335 else
336 m_it = m_hitList.end();
337 }
338
339 // Do we want a sorting algorithm ? If so, implement it here.
340
341 // Get the item to display
342 if( m_hitList.empty() )
343 {
344 m_frame->SetStatusText( wxEmptyString );
345 }
346 else
347 {
348 if( aDirection )
349 {
350 if( m_it != m_hitList.end() && !isFirstSearch )
351 m_it++;
352
353 if( m_it == m_hitList.end() )
354 {
355 if( m_wrap->GetValue() )
356 {
357 m_it = m_hitList.begin();
358 }
359 else
360 {
361 endIsReached = true;
362 m_it--; // point to the last REAL result
363 }
364 }
365 }
366 else
367 {
368 if( m_it == m_hitList.begin() )
369 {
370 if( m_wrap->GetValue() )
371 m_it = m_hitList.end();
372 else
373 endIsReached = true;
374 }
375
376 if( m_it != m_hitList.begin() )
377 m_it--;
378 }
379 }
380
381 // Display the item
382 if( m_hitList.empty() )
383 {
384 m_frame->SetStatusText( wxEmptyString );
385 msg.Printf( _( "'%s' not found" ), searchString );
386 m_frame->ShowInfoBarMsg( msg );
387
388 m_status->SetLabel( msg );
389 }
390 else if( endIsReached || m_it == m_hitList.end() )
391 {
392 m_frame->SetStatusText( wxEmptyString );
393 m_frame->ShowInfoBarMsg( _( "No more items to show" ) );
394
395 m_status->SetLabel( _( "No hits" ) );
396 }
397 else
398 {
399 m_frame->GetToolManager()->RunAction( ACTIONS::selectionClear );
400 m_frame->GetToolManager()->RunAction<EDA_ITEM*>( ACTIONS::selectItem, *m_it );
401
402 msg.Printf( _( "'%s' found" ), searchString );
403 m_frame->SetStatusText( msg );
404
405 msg.Printf( _( "Hit(s): %d / %zu" ),
406 (int)std::distance( m_hitList.begin(), m_it ) + 1,
407 m_hitList.size() );
408 m_status->SetLabel( msg );
409 }
410
413}
414
415
416bool DIALOG_FIND::Show( bool show )
417{
418 bool ret = DIALOG_FIND_BASE::Show( show );
419
420 if( show )
421 m_searchCombo->SetFocus();
422
423 return ret;
424}
425
426
427void DIALOG_FIND::OnBoardChanged( wxCommandEvent& event )
428{
429 m_board = m_frame->GetBoard();
430
431 if( m_board )
432 m_board->AddListener( this );
433
434 m_upToDate = false;
435
436 event.Skip();
437}
int index
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition actions.h:223
static TOOL_ACTION findPrevious
Definition actions.h:116
static TOOL_ACTION findNext
Definition actions.h:115
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:85
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
const NETINFO_LIST & GetNetInfo() const
Definition board.h:1217
const ZONES & Zones() const
Definition board.h:469
const MARKERS & Markers() const
Definition board.h:497
const FOOTPRINTS & Footprints() const
Definition board.h:465
const DRAWINGS & Drawings() const
Definition board.h:467
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:98
Handle the data for a net.
Definition netinfo.h:50
The main frame for Pcbnew.
Represent a single user action.
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:84