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