KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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, 2024 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 {
99 {
101 {
103 visitAll();
104 }
105 }
106 else if( selectedOnly )
107 {
108 // Normal find modifies the selection, but selection-based find does not, so we want
109 // to start over in the items we are searching through when the selection changes
110 m_afterItem = nullptr;
111 visitAll();
112 }
113 }
114 else if( m_foundItemHighlighted )
115 {
117 visitAll();
118 }
119
120 getView()->UpdateItems();
123
124 return 0;
125}
126
127
129 SCH_ITEM* aAfter, EDA_SEARCH_DATA& aData,
130 bool reversed )
131{
132 SCH_SEARCH_DATA* schSearchData = dynamic_cast<SCH_SEARCH_DATA*>( &aData );
133 bool selectedOnly = schSearchData ? schSearchData->searchSelectedOnly : false;
134 bool past_item = !aAfter;
135 std::vector<SCH_ITEM*> sorted_items;
136
137 auto addItem =
138 [&](SCH_ITEM* item)
139 {
140 sorted_items.push_back( item );
141
142 if( item->Type() == SCH_SYMBOL_T )
143 {
144 SCH_SYMBOL* cmp = static_cast<SCH_SYMBOL*>( item );
145
146 for( SCH_FIELD& field : cmp->GetFields() )
147 sorted_items.push_back( &field );
148
149 for( SCH_PIN* pin : cmp->GetPins() )
150 sorted_items.push_back( pin );
151 }
152 else if( item->Type() == SCH_SHEET_T )
153 {
154 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
155
156 for( SCH_FIELD& field : sheet->GetFields() )
157 sorted_items.push_back( &field );
158
159 for( SCH_SHEET_PIN* pin : sheet->GetPins() )
160 sorted_items.push_back( pin );
161 }
162 else if( item->IsType( { SCH_LABEL_LOCATE_ANY_T } ) )
163 {
164 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( item );
165
166 for( SCH_FIELD& field : label->GetFields() )
167 sorted_items.push_back( &field );
168 }
169 };
170
171 if( selectedOnly )
172 for( EDA_ITEM* item : m_selectionTool->GetSelection() )
173 addItem( static_cast<SCH_ITEM*>( item ) );
174 else
175 for( SCH_ITEM* item : aScreen->Items() )
176 addItem( item );
177
178 std::sort( sorted_items.begin(), sorted_items.end(),
179 [&]( SCH_ITEM* a, SCH_ITEM* b )
180 {
181 if( a->GetPosition().x == b->GetPosition().x )
182 {
183 // Ensure deterministic sort
184 if( a->GetPosition().y == b->GetPosition().y )
185 return a->m_Uuid < b->m_Uuid;
186
187 return a->GetPosition().y < b->GetPosition().y;
188 }
189 else
190 return a->GetPosition().x < b->GetPosition().x;
191 } );
192
193 if( reversed )
194 std::reverse( sorted_items.begin(), sorted_items.end() );
195
196 for( SCH_ITEM* item : sorted_items )
197 {
198 if( item == aAfter )
199 {
200 past_item = true;
201 }
202 else if( past_item )
203 {
204 if( aData.markersOnly && item->Type() == SCH_MARKER_T )
205 return item;
206
207 if( item->Matches( aData, aSheet ) )
208 return item;
209 }
210 }
211
212 return nullptr;
213}
214
215
217{
219 bool searchAllSheets = false;
220 bool selectedOnly = false;
221 bool isReversed = aEvent.IsAction( &ACTIONS::findPrevious );
222 SCH_ITEM* item = nullptr;
223 SCH_SHEET_PATH* afterSheet = &m_frame->GetCurrentSheet();
224
225 try
226 {
227 const SCH_SEARCH_DATA& schSearchData = dynamic_cast<const SCH_SEARCH_DATA&>( data );
228 searchAllSheets = !( schSearchData.searchCurrentSheetOnly );
229 selectedOnly = schSearchData.searchSelectedOnly;
230 }
231 catch( const std::bad_cast& )
232 {
233 }
234
235 if( aEvent.IsAction( &ACTIONS::findNextMarker ) )
236 data.markersOnly = true;
237 else if( data.findString.IsEmpty() )
238 return FindAndReplace( ACTIONS::find.MakeEvent() );
239
240 if( m_wrapAroundTimer.IsRunning() )
241 {
242 afterSheet = nullptr;
243 m_afterItem = nullptr;
244 m_wrapAroundTimer.Stop();
246 }
247
248 if( afterSheet || !searchAllSheets )
249 {
251 isReversed );
252 }
253
254 if( !item && searchAllSheets )
255 {
256 SCH_SCREENS screens( m_frame->Schematic().Root() );
257 SCH_SHEET_LIST paths;
258
259 screens.BuildClientSheetPathList();
260
261 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
262 {
263 for( SCH_SHEET_PATH& sheet : screen->GetClientSheetPaths() )
264 paths.push_back( sheet );
265 }
266
267 paths.SortByPageNumbers( false );
268
269 if( isReversed )
270 std::reverse( paths.begin(), paths.end() );
271
272 for( SCH_SHEET_PATH& sheet : paths )
273 {
274 if( afterSheet )
275 {
276 if( afterSheet->GetCurrentHash() == sheet.GetCurrentHash() )
277 afterSheet = nullptr;
278
279 continue;
280 }
281
282 item = nextMatch( sheet.LastScreen(), &sheet, nullptr, data, isReversed );
283
284 if( item )
285 {
286 if( m_frame->Schematic().CurrentSheet() != sheet )
287 {
288 // Store the current zoom level into the current screen before switching
290
293 }
294
295 break;
296 }
297 }
298 }
299
300 if( item )
301 {
302 m_afterItem = item;
303
304 if( !item->IsBrightened() )
305 {
306 // Clear any previous brightening
307 UpdateFind( aEvent );
308
309 // Brighten (and show) found object
310 item->SetForceVisible( true );
313 }
314
315 if( !selectedOnly )
316 {
319 }
320
323 }
324 else
325 {
326 wxString msg = searchAllSheets ? _( "Reached end of schematic." )
327 : _( "Reached end of sheet." );
328
329 // Show the popup during the time period the user can wrap the search
330 m_frame->ShowFindReplaceStatus( msg + wxS( " " ) +
331 _( "Find again to wrap around to the start." ), 4000 );
332 m_wrapAroundTimer.StartOnce( 4000 );
333 }
334
335 return 0;
336}
337
339{
341 SCH_SEARCH_DATA* schSearchData = dynamic_cast<SCH_SEARCH_DATA*>( &data );
342 bool selectedOnly = schSearchData ? schSearchData->searchSelectedOnly : false;
343
344 return selectedOnly ? m_afterItem : m_selectionTool->GetSelection().Front();
345}
346
348{
350 EDA_ITEM* match = getCurrentMatch();
351
352 return match && match->Matches( data, &m_frame->GetCurrentSheet() );
353}
354
355
357{
359 EDA_ITEM* item = getCurrentMatch();
361
362 if( data.findString.IsEmpty() )
363 return FindAndReplace( ACTIONS::find.MakeEvent() );
364
365 // TODO: move to SCH_COMMIT....
366
367 if( item && HasMatch() )
368 {
369 SCH_ITEM* sch_item = static_cast<SCH_ITEM*>( item );
370
371 m_frame->SaveCopyInUndoList( sheet->LastScreen(), sch_item, UNDO_REDO::CHANGED, false );
372
373 if( item->Replace( data, sheet ) )
374 {
375 m_frame->UpdateItem( item, false, true );
377 m_frame->OnModify();
378 }
379
380 FindNext( ACTIONS::findNext.MakeEvent() );
381 }
382
383 return 0;
384}
385
386
388{
390 bool currentSheetOnly = false;
391 bool selectedOnly = false;
392
393 try
394 {
395 const SCH_SEARCH_DATA& schSearchData = dynamic_cast<const SCH_SEARCH_DATA&>( data );
396 currentSheetOnly = schSearchData.searchCurrentSheetOnly;
397 selectedOnly = schSearchData.searchSelectedOnly;
398 }
399 catch( const std::bad_cast& )
400 {
401 }
402
403 bool modified = false; // TODO: move to SCH_COMMIT....
404
405 if( data.findString.IsEmpty() )
406 return FindAndReplace( ACTIONS::find.MakeEvent() );
407
408 auto doReplace =
409 [&]( SCH_ITEM* aItem, SCH_SHEET_PATH* aSheet, EDA_SEARCH_DATA& aData )
410 {
411 m_frame->SaveCopyInUndoList( aSheet->LastScreen(), aItem, UNDO_REDO::CHANGED,
412 modified );
413
414 if( aItem->Replace( aData, aSheet ) )
415 {
416 m_frame->UpdateItem( aItem, false, true );
417 modified = true;
418 }
419 };
420
421 if( currentSheetOnly || selectedOnly )
422 {
423 SCH_SHEET_PATH* currentSheet = &m_frame->GetCurrentSheet();
424
425 SCH_ITEM* item = nextMatch( m_frame->GetScreen(), currentSheet, nullptr, data, false );
426
427 while( item )
428 {
429 if( !selectedOnly || item->IsSelected() )
430 doReplace( item, currentSheet, data );
431
432 item = nextMatch( m_frame->GetScreen(), currentSheet, item, data, false );
433 }
434 }
435 else
436 {
437 SCH_SHEET_LIST allSheets = m_frame->Schematic().Hierarchy();
438 SCH_SCREENS screens( m_frame->Schematic().Root() );
439
440 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
441 {
442 SCH_SHEET_LIST sheets = allSheets.FindAllSheetsForScreen( screen );
443
444 for( unsigned ii = 0; ii < sheets.size(); ++ii )
445 {
446 SCH_ITEM* item = nextMatch( screen, &sheets[ii], nullptr, data, false );
447
448 while( item )
449 {
450 if( ii == 0 )
451 {
452 doReplace( item, &sheets[0], data );
453 }
454 else if( item->Type() == SCH_FIELD_T )
455 {
456 SCH_FIELD* field = static_cast<SCH_FIELD*>( item );
457
458 if( field->GetParent() && field->GetParent()->Type() == SCH_SYMBOL_T )
459 {
460 switch( field->GetId() )
461 {
462 case REFERENCE_FIELD:
463 case VALUE_FIELD:
464 case FOOTPRINT_FIELD:
465 // must be handled for each distinct sheet
466 doReplace( field, &sheets[ii], data );
467 break;
468
469 default:
470 // handled in first iteration
471 break;
472 }
473 }
474 }
475
476 item = nextMatch( screen, &sheets[ii], item, data, false );
477 }
478 }
479 }
480 }
481
482 if( modified )
483 {
485 m_frame->OnModify();
486 }
487
488 return 0;
489}
490
491
493{
507}
static TOOL_ACTION replaceAll
Definition: actions.h:115
static TOOL_ACTION updateFind
Definition: actions.h:116
static TOOL_ACTION findPrevious
Definition: actions.h:112
static TOOL_ACTION findAndReplace
Definition: actions.h:110
static TOOL_ACTION replaceAndFindNext
Definition: actions.h:114
static TOOL_ACTION findNext
Definition: actions.h:111
static TOOL_ACTION findNextMarker
Definition: actions.h:113
static TOOL_ACTION find
Definition: actions.h:109
constexpr const Vec GetCenter() const
Definition: box2.h:230
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
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:243
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition: eda_item.cpp:77
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
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:194
virtual bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const
Compare the item against the search criteria in aSearchData.
Definition: eda_item.h:377
bool IsSelected() const
Definition: eda_item.h:110
EDA_ITEM * GetParent() const
Definition: eda_item.h:103
bool IsBrightened() const
Definition: eda_item.h:112
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:189
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:200
static const TOOL_EVENT ClearedEvent
Definition: actions.h:294
static const TOOL_EVENT SelectedEvent
Definition: actions.h:292
static const TOOL_EVENT SelectedItemsModified
Selected items were moved, this can be very high frequency on the canvas, use with care.
Definition: actions.h:299
static const TOOL_EVENT PointSelectedEvent
Definition: actions.h:291
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:293
double GetScale() const
Definition: view.h:277
void UpdateItems()
Iterate through the list of items that asked for updating and updates them.
Definition: view.cpp:1471
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:152
void SetCurrentSheet(const SCH_SHEET_PATH &aPath) override
Definition: schematic.h:157
SCH_SHEET_LIST Hierarchy() const override
Return the full schematic flattened hierarchical sheet list.
Definition: schematic.cpp:214
SCH_SHEET & Root() const
Definition: schematic.h:121
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
KIGFX::SCH_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
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:133
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:166
std::vector< SCH_FIELD > & GetFields()
Definition: sch_label.h:201
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:712
SCH_SCREEN * GetNext()
SCH_SCREEN * GetFirst()
void BuildClientSheetPathList()
built the list of sheet paths sharing a screen for each screen in use
double m_LastZoomLevel
last value for the zoom level, useful in Eeschema when changing the current displayed sheet to reuse ...
Definition: sch_screen.h:636
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:108
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void SortByPageNumbers(bool aUpdateVirtualPageNums=true)
Sort the list of sheets by page number.
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:181
Schematic symbol object.
Definition: sch_symbol.h:104
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:987
void BrightenItem(EDA_ITEM *aItem)
int AddItemToSel(const TOOL_EVENT &aEvent)
void UnbrightenItem(EDA_ITEM *aItem)
EDA_ITEM * Front() const
Definition: selection.h:172
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:167
bool Matches(const TOOL_EVENT &aEvent) const
Test whether two events match in terms of category & action or command.
Definition: tool_event.h:384
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:82
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:172
@ SCH_FIELD_T
Definition: typeinfo.h:150
@ SCH_SHEET_T
Definition: typeinfo.h:174
@ SCH_MARKER_T
Definition: typeinfo.h:158
@ SCH_LABEL_LOCATE_ANY_T
Definition: typeinfo.h:190