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