KiCad PCB EDA Suite
sch_find_replace_tool.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) 2019 CERN
5 * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <sch_sheet_pin.h>
26#include <schematic.h>
27#include <tools/ee_actions.h>
29#include <sch_sheet_path.h>
30
31
33{
35 return UpdateFind( aEvent );
36}
37
38
40{
42 SCH_SEARCH_DATA* schSearchData = dynamic_cast<SCH_SEARCH_DATA*>( &data );
43 bool selectedOnly = schSearchData ? schSearchData->searchSelectedOnly : false;
44
45 auto visit =
46 [&]( EDA_ITEM* aItem, SCH_SHEET_PATH* aSheet )
47 {
48 // We may get triggered when the dialog is not opened due to binding
49 // SelectedItemsModified we also get triggered when the find dialog is
50 // closed....so we need to double check the dialog is open.
51 if( m_frame->m_findReplaceDialog != nullptr
52 && !data.findString.IsEmpty()
53 && aItem->Matches( data, aSheet )
54 && ( !selectedOnly || aItem->IsSelected() ) )
55 {
56 aItem->SetForceVisible( true );
59 }
60 else if( aItem->IsBrightened() )
61 {
62 aItem->SetForceVisible( false );
64 }
65 };
66
67 auto visitAll =
68 [&]()
69 {
70 for( SCH_ITEM* item : m_frame->GetScreen()->Items() )
71 {
72 visit( item, &m_frame->GetCurrentSheet() );
73
74 item->RunOnChildren(
75 [&]( SCH_ITEM* aChild )
76 {
77 visit( aChild, &m_frame->GetCurrentSheet() );
78 } );
79 }
80 };
81
82 if( aEvent.IsAction( &ACTIONS::find ) || aEvent.IsAction( &ACTIONS::findAndReplace )
83 || aEvent.IsAction( &ACTIONS::updateFind ) )
84 {
86 visitAll();
87 }
88 else if( aEvent.Matches( EVENTS::SelectedItemsModified ) )
89 {
90 for( EDA_ITEM* item : m_selectionTool->GetSelection() )
91 visit( item, &m_frame->GetCurrentSheet() );
92 }
93 else if( aEvent.Matches( EVENTS::PointSelectedEvent )
96 || aEvent.Matches( EVENTS::ClearedEvent ) )
97 {
98 // Normal find modifies the selection, but selection-based find does
99 // not so we want to start over in the items we are searching through when
100 // the selection changes
101 if( selectedOnly )
102 {
103 m_afterItem = nullptr;
104 visitAll();
105 }
106 }
107 else if( m_foundItemHighlighted )
108 {
110 visitAll();
111 }
112
113 getView()->UpdateItems();
116
117 return 0;
118}
119
120
122 SCH_ITEM* aAfter, EDA_SEARCH_DATA& aData,
123 bool reversed )
124{
125 SCH_SEARCH_DATA* schSearchData = dynamic_cast<SCH_SEARCH_DATA*>( &aData );
126 bool selectedOnly = schSearchData ? schSearchData->searchSelectedOnly : false;
127 bool past_item = !aAfter;
128 std::vector<SCH_ITEM*> sorted_items;
129
130 auto addItem =
131 [&](SCH_ITEM* item)
132 {
133 sorted_items.push_back( item );
134
135 if( item->Type() == SCH_SYMBOL_T )
136 {
137 SCH_SYMBOL* cmp = static_cast<SCH_SYMBOL*>( item );
138
139 for( SCH_FIELD& field : cmp->GetFields() )
140 sorted_items.push_back( &field );
141
142 for( SCH_PIN* pin : cmp->GetPins() )
143 sorted_items.push_back( pin );
144 }
145
146 if( item->Type() == SCH_SHEET_T )
147 {
148 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
149
150 for( SCH_FIELD& field : sheet->GetFields() )
151 sorted_items.push_back( &field );
152
153 for( SCH_SHEET_PIN* pin : sheet->GetPins() )
154 sorted_items.push_back( pin );
155 }
156 };
157
158 if( selectedOnly )
159 for( EDA_ITEM* item : m_selectionTool->GetSelection() )
160 addItem( static_cast<SCH_ITEM*>( item ) );
161 else
162 for( SCH_ITEM* item : aScreen->Items() )
163 addItem( item );
164
165 std::sort( sorted_items.begin(), sorted_items.end(),
166 [&]( SCH_ITEM* a, SCH_ITEM* b )
167 {
168 if( a->GetPosition().x == b->GetPosition().x )
169 {
170 // Ensure deterministic sort
171 if( a->GetPosition().y == b->GetPosition().y )
172 return a->m_Uuid < b->m_Uuid;
173
174 return a->GetPosition().y < b->GetPosition().y;
175 }
176 else
177 return a->GetPosition().x < b->GetPosition().x;
178 } );
179
180 if( reversed )
181 std::reverse( sorted_items.begin(), sorted_items.end() );
182
183 for( SCH_ITEM* item : sorted_items )
184 {
185 if( item == aAfter )
186 {
187 past_item = true;
188 }
189 else if( past_item )
190 {
191 if( aData.markersOnly && item->Type() == SCH_MARKER_T )
192 return item;
193
194 if( item->Matches( aData, aSheet ) )
195 return item;
196 }
197 }
198
199 return nullptr;
200}
201
202
204{
206 bool searchAllSheets = false;
207 bool selectedOnly = false;
208 bool isReversed = aEvent.IsAction( &ACTIONS::findPrevious );
209 SCH_ITEM* item = nullptr;
210 SCH_SHEET_PATH* afterSheet = &m_frame->GetCurrentSheet();
211
212 try
213 {
214 const SCH_SEARCH_DATA& schSearchData = dynamic_cast<const SCH_SEARCH_DATA&>( data );
215 searchAllSheets = !( schSearchData.searchCurrentSheetOnly );
216 selectedOnly = schSearchData.searchSelectedOnly;
217 }
218 catch( const std::bad_cast& )
219 {
220 }
221
222 if( aEvent.IsAction( &ACTIONS::findNextMarker ) )
223 data.markersOnly = true;
224 else if( data.findString.IsEmpty() )
225 return FindAndReplace( ACTIONS::find.MakeEvent() );
226
227 if( m_wrapAroundTimer.IsRunning() )
228 {
229 afterSheet = nullptr;
230 m_afterItem = nullptr;
231 m_wrapAroundTimer.Stop();
233 }
234
235 if( afterSheet || !searchAllSheets )
237 isReversed );
238
239 if( !item && searchAllSheets )
240 {
241 SCH_SCREENS screens( m_frame->Schematic().Root() );
242 std::vector<SCH_SHEET_PATH*> paths;
243
244 screens.BuildClientSheetPathList();
245
246 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
247 {
248 for( SCH_SHEET_PATH& sheet : screen->GetClientSheetPaths() )
249 paths.push_back( &sheet );
250 }
251
252 std::sort( paths.begin(), paths.end(), [] ( const SCH_SHEET_PATH* lhs,
253 const SCH_SHEET_PATH* rhs ) -> bool
254 {
255 int retval = lhs->ComparePageNum( *rhs );
256
257 if( retval < 0 )
258 return true;
259 else if( retval > 0 )
260 return false;
261 else
262 return lhs->GetCurrentHash() < rhs->GetCurrentHash();
263 } );
264
265 if( isReversed )
266 std::reverse( paths.begin(), paths.end() );
267
268 for( SCH_SHEET_PATH* sheet : paths )
269 {
270 if( afterSheet )
271 {
272 if( afterSheet->GetCurrentHash() == sheet->GetCurrentHash() )
273 afterSheet = nullptr;
274
275 continue;
276 }
277
278 item = nextMatch( sheet->LastScreen(), sheet, nullptr, data, isReversed );
279
280 if( item )
281 {
282 m_frame->Schematic().SetCurrentSheet( *sheet );
284
285 break;
286 }
287 }
288 }
289
290 if( item )
291 {
292 m_afterItem = item;
293
294 if( !item->IsBrightened() )
295 {
296 // Clear any previous brightening
297 UpdateFind( aEvent );
298
299 // Brighten (and show) found object
300 item->SetForceVisible( true );
303 }
304
305 if( !selectedOnly )
306 {
309 }
310
313 }
314 else
315 {
316 wxString msg = searchAllSheets ? _( "Reached end of schematic." )
317 : _( "Reached end of sheet." );
318
319 // Show the popup during the time period the user can wrap the search
320 m_frame->ShowFindReplaceStatus( msg + wxS( " " ) +
321 _( "Find again to wrap around to the start." ), 4000 );
322 m_wrapAroundTimer.StartOnce( 4000 );
323 }
324
325 return 0;
326}
327
329{
331 SCH_SEARCH_DATA* schSearchData = dynamic_cast<SCH_SEARCH_DATA*>( &data );
332 bool selectedOnly = schSearchData ? schSearchData->searchSelectedOnly : false;
333
334 return selectedOnly ? m_afterItem : m_selectionTool->GetSelection().Front();
335}
336
338{
340 EDA_ITEM* match = getCurrentMatch();
341
342 return match && match->Matches( data, &m_frame->GetCurrentSheet() );
343}
344
345
347{
349 EDA_ITEM* item = getCurrentMatch();
351
352 if( data.findString.IsEmpty() )
353 return FindAndReplace( ACTIONS::find.MakeEvent() );
354
355 if( item && HasMatch() )
356 {
357 SCH_ITEM* sch_item = static_cast<SCH_ITEM*>( item );
358
359 m_frame->SaveCopyInUndoList( sheet->LastScreen(), sch_item, UNDO_REDO::CHANGED, false );
360
361 if( item->Replace( data, sheet ) )
362 {
363 m_frame->UpdateItem( item, false, true );
365 m_frame->OnModify();
366 }
367
368 FindNext( ACTIONS::findNext.MakeEvent() );
369 }
370
371 return 0;
372}
373
374
376{
378 bool currentSheetOnly = false;
379 bool selectedOnly = false;
380
381 try
382 {
383 const SCH_SEARCH_DATA& schSearchData = dynamic_cast<const SCH_SEARCH_DATA&>( data );
384 currentSheetOnly = schSearchData.searchCurrentSheetOnly;
385 selectedOnly = schSearchData.searchSelectedOnly;
386 }
387 catch( const std::bad_cast& )
388 {
389 }
390
391 bool modified = false;
392
393 if( data.findString.IsEmpty() )
394 return FindAndReplace( ACTIONS::find.MakeEvent() );
395
396 auto doReplace =
397 [&]( SCH_ITEM* aItem, SCH_SHEET_PATH* aSheet, EDA_SEARCH_DATA& aData )
398 {
399 m_frame->SaveCopyInUndoList( aSheet->LastScreen(), aItem, UNDO_REDO::CHANGED,
400 modified );
401
402 if( aItem->Replace( aData, aSheet ) )
403 {
404 m_frame->UpdateItem( aItem, false, true );
405 modified = true;
406 }
407 };
408
409 if( currentSheetOnly || selectedOnly )
410 {
411 SCH_SHEET_PATH* currentSheet = &m_frame->GetCurrentSheet();
412
413 SCH_ITEM* item = nextMatch( m_frame->GetScreen(), currentSheet, nullptr, data, false );
414
415 while( item )
416 {
417 if( !selectedOnly || item->IsSelected() )
418 doReplace( item, currentSheet, data );
419
420 item = nextMatch( m_frame->GetScreen(), currentSheet, item, data, false );
421 }
422 }
423 else
424 {
425 SCH_SHEET_LIST allSheets = m_frame->Schematic().GetSheets();
426 SCH_SCREENS screens( m_frame->Schematic().Root() );
427
428 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
429 {
430 SCH_SHEET_LIST sheets = allSheets.FindAllSheetsForScreen( screen );
431
432 for( unsigned ii = 0; ii < sheets.size(); ++ii )
433 {
434 SCH_ITEM* item = nextMatch( screen, &sheets[ii], nullptr, data, false );
435
436 while( item )
437 {
438 if( ii == 0 )
439 {
440 doReplace( item, &sheets[0], data );
441 }
442 else if( item->Type() == SCH_FIELD_T )
443 {
444 SCH_FIELD* field = static_cast<SCH_FIELD*>( item );
445
446 if( field->GetParent() && field->GetParent()->Type() == SCH_SYMBOL_T )
447 {
448 switch( field->GetId() )
449 {
450 case REFERENCE_FIELD:
451 case VALUE_FIELD:
452 case FOOTPRINT_FIELD:
453 // must be handled for each distinct sheet
454 doReplace( field, &sheets[ii], data );
455 break;
456
457 default:
458 // handled in first iteration
459 break;
460 }
461 }
462 }
463
464 item = nextMatch( screen, &sheets[ii], item, data, false );
465 }
466 }
467 }
468 }
469
470 if( modified )
471 {
473 m_frame->OnModify();
474 }
475
476 return 0;
477}
478
479
481{
495}
static TOOL_ACTION replaceAll
Definition: actions.h:84
static TOOL_ACTION updateFind
Definition: actions.h:85
static TOOL_ACTION findPrevious
Definition: actions.h:81
static TOOL_ACTION findAndReplace
Definition: actions.h:79
static TOOL_ACTION replaceAndFindNext
Definition: actions.h:83
static TOOL_ACTION findNext
Definition: actions.h:80
static TOOL_ACTION findNextMarker
Definition: actions.h:82
static TOOL_ACTION find
Definition: actions.h:78
const Vec GetCenter() const
Definition: box2.h:195
void FocusOnLocation(const VECTOR2I &aPos)
Useful to focus on a particular location, in find functions.
EDA_SEARCH_DATA & GetFindReplaceData()
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:249
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition: eda_item.cpp:74
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
void SetForceVisible(bool aEnable)
Set and clear force visible flag used to force the item to be drawn even if it's draw attribute is se...
Definition: eda_item.h:199
virtual bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const
Compare the item against the search criteria in aSearchData.
Definition: eda_item.h:382
bool IsSelected() const
Definition: eda_item.h:106
EDA_ITEM * GetParent() const
Definition: eda_item.h:99
bool IsBrightened() const
Definition: eda_item.h:108
static bool Replace(const EDA_SEARCH_DATA &aSearchData, wxString &aText)
Perform a text replace on aText using the find and replace criteria in aSearchData on items that supp...
Definition: eda_item.cpp:168
int ClearSelection(const TOOL_EVENT &aEvent)
Select all visible items in sheet.
EE_SELECTION & GetSelection()
EE_SELECTION_TOOL * m_selectionTool
Definition: ee_tool_base.h:191
static const TOOL_EVENT ClearedEvent
Definition: actions.h:209
static const TOOL_EVENT SelectedEvent
Definition: actions.h:207
static const TOOL_EVENT SelectedItemsModified
Selected items were moved, this can be very high frequency on the canvas, use with care.
Definition: actions.h:214
static const TOOL_EVENT PointSelectedEvent
Definition: actions.h:206
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:208
void UpdateItems()
Iterate through the list of items that asked for updating and updates them.
Definition: view.cpp:1401
void SetCurrentSheet(const SCH_SHEET_PATH &aPath) override
Definition: schematic.h:127
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:86
SCH_SHEET & Root() const
Definition: schematic.h:91
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag and update other data struc...
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
void ShowFindReplaceDialog(bool aReplace)
Run the Find or Find & Replace dialog.
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO aTypeCommand, bool aAppend, bool aDirtyConnectivity=true)
Create a copy of the current schematic item, and put it in the undo list.
void ClearFindReplaceStatus()
SCH_SHEET_PATH & GetCurrentSheet() const
SCHEMATIC & Schematic() const
void updateTitle()
Set the main window title bar text.
void ShowFindReplaceStatus(const wxString &aMsg, int aStatusTime)
void DisplayCurrentSheet()
Draw the current sheet on the display.
void UpdateItem(EDA_ITEM *aItem, bool isAddOrDelete=false, bool aUpdateRtree=false) override
Mark an item for refresh.
DIALOG_SCH_FIND * m_findReplaceDialog
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
int GetId() const
Definition: sch_field.h:125
void setTransitions() override
< Set up handlers for various events.
int UpdateFind(const TOOL_EVENT &aEvent)
int FindNext(const TOOL_EVENT &aEvent)
SCH_ITEM * nextMatch(SCH_SCREEN *aScreen, SCH_SHEET_PATH *aSheet, SCH_ITEM *aAfter, EDA_SEARCH_DATA &aData, bool reverse)
Advance the search and returns the next matching item after aAfter.
int ReplaceAll(const TOOL_EVENT &aEvent)
int FindAndReplace(const TOOL_EVENT &aEvent)
int ReplaceAndFindNext(const TOOL_EVENT &aEvent)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:147
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:662
SCH_SCREEN * GetNext()
SCH_SCREEN * GetFirst()
void BuildClientSheetPathList()
built the list of sheet paths sharing a screen for each screen in use
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:109
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
SCH_SHEET_LIST FindAllSheetsForScreen(const SCH_SCREEN *aScreen) const
Return a SCH_SHEET_LIST with a copy of all the SCH_SHEET_PATH using a particular screen.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
size_t GetCurrentHash() const
void UpdateAllScreenReferences() const
Update all the symbol references for this sheet path.
SCH_SCREEN * LastScreen()
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet_pin.h:66
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:57
std::vector< SCH_FIELD > & GetFields()
Definition: sch_sheet.h:93
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition: sch_sheet.h:175
Schematic symbol object.
Definition: sch_symbol.h:81
std::vector< SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet=nullptr) const
Retrieve a list of the SCH_PINs for the given sheet path.
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly)
Populate a std::vector with SCH_FIELDs.
Definition: sch_symbol.cpp:901
void BrightenItem(EDA_ITEM *aItem)
int AddItemToSel(const TOOL_EVENT &aEvent)
void UnbrightenItem(EDA_ITEM *aItem)
EDA_ITEM * Front() const
Definition: selection.h:208
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:36
Generic, UI-independent tool event.
Definition: tool_event.h:156
bool Matches(const TOOL_EVENT &aEvent) const
Test whether two events match in terms of category & action or command.
Definition: tool_event.h:365
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
Definition: tool_event.cpp:81
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
#define _(s)
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
@ FOOTPRINT_FIELD
Field Name Module PCB, i.e. "16DIP300".
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".
@ SCH_SYMBOL_T
Definition: typeinfo.h:156
@ SCH_FIELD_T
Definition: typeinfo.h:155
@ SCH_SHEET_T
Definition: typeinfo.h:158
@ SCH_MARKER_T
Definition: typeinfo.h:141