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_commit.h>
26#include <sch_sheet_pin.h>
27#include <schematic.h>
28#include <tools/ee_actions.h>
30#include <sch_sheet_path.h>
31
32
34{
36 return UpdateFind( aEvent );
37}
38
39
41{
43 SCH_SEARCH_DATA* schSearchData = dynamic_cast<SCH_SEARCH_DATA*>( &data );
44 bool selectedOnly = schSearchData ? schSearchData->searchSelectedOnly : false;
45
46 auto visit =
47 [&]( EDA_ITEM* aItem, SCH_SHEET_PATH* aSheet )
48 {
49 // We may get triggered when the dialog is not opened due to binding
50 // SelectedItemsModified we also get triggered when the find dialog is
51 // closed....so we need to double check the dialog is open.
52 if( m_frame->m_findReplaceDialog != nullptr
53 && !data.findString.IsEmpty()
54 && aItem->Matches( data, aSheet )
55 && ( !selectedOnly || aItem->IsSelected() ) )
56 {
57 aItem->SetForceVisible( true );
60 }
61 else if( aItem->IsBrightened() )
62 {
63 aItem->SetForceVisible( false );
65 }
66 };
67
68 auto visitAll =
69 [&]()
70 {
71 for( SCH_ITEM* item : m_frame->GetScreen()->Items() )
72 {
73 visit( item, &m_frame->GetCurrentSheet() );
74
75 item->RunOnChildren(
76 [&]( SCH_ITEM* aChild )
77 {
78 visit( aChild, &m_frame->GetCurrentSheet() );
79 } );
80 }
81 };
82
83 if( aEvent.IsAction( &ACTIONS::find ) || aEvent.IsAction( &ACTIONS::findAndReplace )
84 || aEvent.IsAction( &ACTIONS::updateFind ) )
85 {
87 visitAll();
88 }
89 else if( aEvent.Matches( EVENTS::SelectedItemsModified ) )
90 {
91 for( EDA_ITEM* item : m_selectionTool->GetSelection() )
92 visit( item, &m_frame->GetCurrentSheet() );
93 }
94 else if( aEvent.Matches( EVENTS::PointSelectedEvent )
97 || aEvent.Matches( EVENTS::ClearedEvent ) )
98 {
100 {
102 {
104 visitAll();
105 }
106 }
107 else if( selectedOnly )
108 {
109 // Normal find modifies the selection, but selection-based find does not, so we want
110 // to start over in the items we are searching through when the selection changes
111 m_afterItem = nullptr;
112 visitAll();
113 }
114 }
115 else if( m_foundItemHighlighted )
116 {
118 visitAll();
119 }
120
121 getView()->UpdateItems();
124
125 return 0;
126}
127
128
130 SCH_ITEM* aAfter, EDA_SEARCH_DATA& aData,
131 bool reversed )
132{
133 SCH_SEARCH_DATA* schSearchData = dynamic_cast<SCH_SEARCH_DATA*>( &aData );
134 bool selectedOnly = schSearchData ? schSearchData->searchSelectedOnly : false;
135 bool past_item = !aAfter;
136 std::vector<SCH_ITEM*> sorted_items;
137
138 auto addItem =
139 [&](SCH_ITEM* item)
140 {
141 sorted_items.push_back( item );
142
143 if( item->Type() == SCH_SYMBOL_T )
144 {
145 SCH_SYMBOL* cmp = static_cast<SCH_SYMBOL*>( item );
146
147 for( SCH_FIELD& field : cmp->GetFields() )
148 sorted_items.push_back( &field );
149
150 for( SCH_PIN* pin : cmp->GetPins() )
151 sorted_items.push_back( pin );
152 }
153 else if( item->Type() == SCH_SHEET_T )
154 {
155 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
156
157 for( SCH_FIELD& field : sheet->GetFields() )
158 sorted_items.push_back( &field );
159
160 for( SCH_SHEET_PIN* pin : sheet->GetPins() )
161 sorted_items.push_back( pin );
162 }
163 else if( item->IsType( { SCH_LABEL_LOCATE_ANY_T } ) )
164 {
165 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( item );
166
167 for( SCH_FIELD& field : label->GetFields() )
168 sorted_items.push_back( &field );
169 }
170 };
171
172 if( selectedOnly )
173 {
174 for( EDA_ITEM* item : m_selectionTool->GetSelection() )
175 addItem( static_cast<SCH_ITEM*>( item ) );
176 }
177 else
178 {
179 for( SCH_ITEM* item : aScreen->Items() )
180 addItem( item );
181 }
182
183 std::sort( sorted_items.begin(), sorted_items.end(),
184 [&]( SCH_ITEM* a, SCH_ITEM* b )
185 {
186 if( a->GetPosition().x == b->GetPosition().x )
187 {
188 // Ensure deterministic sort
189 if( a->GetPosition().y == b->GetPosition().y )
190 return a->m_Uuid < b->m_Uuid;
191
192 return a->GetPosition().y < b->GetPosition().y;
193 }
194 else
195 return a->GetPosition().x < b->GetPosition().x;
196 } );
197
198 if( reversed )
199 std::reverse( sorted_items.begin(), sorted_items.end() );
200
201 for( SCH_ITEM* item : sorted_items )
202 {
203 if( item == aAfter )
204 {
205 past_item = true;
206 }
207 else if( past_item )
208 {
209 if( aData.markersOnly && item->Type() == SCH_MARKER_T )
210 return item;
211
212 if( item->Matches( aData, aSheet ) )
213 return item;
214 }
215 }
216
217 return nullptr;
218}
219
220
222{
224 bool searchAllSheets = false;
225 bool selectedOnly = false;
226 bool isReversed = aEvent.IsAction( &ACTIONS::findPrevious );
227 SCH_ITEM* item = nullptr;
228 SCH_SHEET_PATH* afterSheet = &m_frame->GetCurrentSheet();
229
230 try
231 {
232 const SCH_SEARCH_DATA& schSearchData = dynamic_cast<const SCH_SEARCH_DATA&>( data );
233 searchAllSheets = !( schSearchData.searchCurrentSheetOnly );
234 selectedOnly = schSearchData.searchSelectedOnly;
235 }
236 catch( const std::bad_cast& )
237 {
238 }
239
240 if( aEvent.IsAction( &ACTIONS::findNextMarker ) )
241 data.markersOnly = true;
242 else if( data.findString.IsEmpty() )
243 return FindAndReplace( ACTIONS::find.MakeEvent() );
244
245 if( m_wrapAroundTimer.IsRunning() )
246 {
247 afterSheet = nullptr;
248 m_afterItem = nullptr;
249 m_wrapAroundTimer.Stop();
251 }
252
253 if( afterSheet || !searchAllSheets )
254 {
256 isReversed );
257 }
258
259 if( !item && searchAllSheets )
260 {
261 SCH_SCREENS screens( m_frame->Schematic().Root() );
262 SCH_SHEET_LIST paths;
263
264 screens.BuildClientSheetPathList();
265
266 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
267 {
268 for( SCH_SHEET_PATH& sheet : screen->GetClientSheetPaths() )
269 paths.push_back( sheet );
270 }
271
272 paths.SortByPageNumbers( false );
273
274 if( isReversed )
275 std::reverse( paths.begin(), paths.end() );
276
277 for( SCH_SHEET_PATH& sheet : paths )
278 {
279 if( afterSheet )
280 {
281 if( afterSheet->GetCurrentHash() == sheet.GetCurrentHash() )
282 afterSheet = nullptr;
283
284 continue;
285 }
286
287 item = nextMatch( sheet.LastScreen(), &sheet, nullptr, data, isReversed );
288
289 if( item )
290 {
291 if( m_frame->Schematic().CurrentSheet() != sheet )
292 {
293 // Store the current zoom level into the current screen before switching
295
298 }
299
300 break;
301 }
302 }
303 }
304
305 if( item )
306 {
307 m_afterItem = item;
308
309 if( !selectedOnly )
310 {
313 }
314
315 if( !item->IsBrightened() )
316 {
317 // Clear any previous brightening
318 UpdateFind( aEvent );
319
320 // Brighten (and show) found object
321 item->SetForceVisible( true );
324 }
325
328 }
329 else
330 {
331 wxString msg = searchAllSheets ? _( "Reached end of schematic." )
332 : _( "Reached end of sheet." );
333
334 // Show the popup during the time period the user can wrap the search
335 m_frame->ShowFindReplaceStatus( msg + wxS( " " ) +
336 _( "Find again to wrap around to the start." ), 4000 );
337 m_wrapAroundTimer.StartOnce( 4000 );
338 }
339
340 return 0;
341}
342
344{
346 SCH_SEARCH_DATA* schSearchData = dynamic_cast<SCH_SEARCH_DATA*>( &data );
347 bool selectedOnly = schSearchData ? schSearchData->searchSelectedOnly : false;
348
349 return selectedOnly ? m_afterItem : m_selectionTool->GetSelection().Front();
350}
351
353{
355 EDA_ITEM* match = getCurrentMatch();
356
357 return match && match->Matches( data, &m_frame->GetCurrentSheet() );
358}
359
360
362{
364 EDA_ITEM* item = getCurrentMatch();
366
367 if( data.findString.IsEmpty() )
368 return FindAndReplace( ACTIONS::find.MakeEvent() );
369
370 if( item && HasMatch() )
371 {
372 SCH_COMMIT commit( m_frame );
373 SCH_ITEM* sch_item = static_cast<SCH_ITEM*>( item );
374
375 commit.Modify( sch_item, sheet->LastScreen() );
376
377 if( item->Replace( data, sheet ) )
378 {
380 commit.Push( wxS( "Find and Replace" ) );
381 }
382
383 FindNext( ACTIONS::findNext.MakeEvent() );
384 }
385
386 return 0;
387}
388
389
391{
393 bool currentSheetOnly = false;
394 bool selectedOnly = false;
395
396 try
397 {
398 const SCH_SEARCH_DATA& schSearchData = dynamic_cast<const SCH_SEARCH_DATA&>( data );
399 currentSheetOnly = schSearchData.searchCurrentSheetOnly;
400 selectedOnly = schSearchData.searchSelectedOnly;
401 }
402 catch( const std::bad_cast& )
403 {
404 }
405
406 SCH_COMMIT commit( m_frame );
407 bool modified = false; // TODO: move to SCH_COMMIT....
408
409 if( data.findString.IsEmpty() )
410 return FindAndReplace( ACTIONS::find.MakeEvent() );
411
412 auto doReplace =
413 [&]( SCH_ITEM* aItem, SCH_SHEET_PATH* aSheet, EDA_SEARCH_DATA& aData )
414 {
415 commit.Modify( aItem, aSheet->LastScreen() );
416
417 if( aItem->Replace( aData, aSheet ) )
418 {
419 m_frame->UpdateItem( aItem, false, true );
420 modified = true;
421 }
422 };
423
424 if( currentSheetOnly || selectedOnly )
425 {
426 SCH_SHEET_PATH* currentSheet = &m_frame->GetCurrentSheet();
427
428 SCH_ITEM* item = nextMatch( m_frame->GetScreen(), currentSheet, nullptr, data, false );
429
430 while( item )
431 {
432 if( !selectedOnly || item->IsSelected() )
433 doReplace( item, currentSheet, data );
434
435 item = nextMatch( m_frame->GetScreen(), currentSheet, item, data, false );
436 }
437 }
438 else
439 {
440 SCH_SHEET_LIST allSheets = m_frame->Schematic().Hierarchy();
441 SCH_SCREENS screens( m_frame->Schematic().Root() );
442
443 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
444 {
445 SCH_SHEET_LIST sheets = allSheets.FindAllSheetsForScreen( screen );
446
447 for( unsigned ii = 0; ii < sheets.size(); ++ii )
448 {
449 SCH_ITEM* item = nextMatch( screen, &sheets[ii], nullptr, data, false );
450
451 while( item )
452 {
453 if( ii == 0 )
454 {
455 doReplace( item, &sheets[0], data );
456 }
457 else if( item->Type() == SCH_FIELD_T )
458 {
459 SCH_FIELD* field = static_cast<SCH_FIELD*>( item );
460
461 if( field->GetParent() && field->GetParent()->Type() == SCH_SYMBOL_T )
462 {
463 switch( field->GetId() )
464 {
465 case REFERENCE_FIELD:
466 case VALUE_FIELD:
467 case FOOTPRINT_FIELD:
468 // must be handled for each distinct sheet
469 doReplace( field, &sheets[ii], data );
470 break;
471
472 default:
473 // handled in first iteration
474 break;
475 }
476 }
477 }
478
479 item = nextMatch( screen, &sheets[ii], item, data, false );
480 }
481 }
482 }
483 }
484
485 if( modified )
486 {
487 commit.Push( wxS( "Find and Replace All" ) );
489 }
490
491 return 0;
492}
493
494
496{
510}
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
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition: commit.h:105
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:273
void UpdateItems()
Iterate through the list of items that asked for updating and updates them.
Definition: view.cpp:1452
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:156
void SetCurrentSheet(const SCH_SHEET_PATH &aPath) override
Definition: schematic.h:161
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:125
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Revert the commit by restoring the modified items state.
Definition: sch_commit.cpp:432
KIGFX::SCH_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
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 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