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 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 std::vector<SCH_SHEET_PATH*> 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 std::sort( paths.begin(), paths.end(), [] ( const SCH_SHEET_PATH* lhs,
268 const SCH_SHEET_PATH* rhs ) -> bool
269 {
270 int retval = lhs->ComparePageNum( *rhs );
271
272 if( retval < 0 )
273 return true;
274 else if( retval > 0 )
275 return false;
276 else
277 return lhs->GetCurrentHash() < rhs->GetCurrentHash();
278 } );
279
280 if( isReversed )
281 std::reverse( paths.begin(), paths.end() );
282
283 for( SCH_SHEET_PATH* sheet : paths )
284 {
285 if( afterSheet )
286 {
287 if( afterSheet->GetCurrentHash() == sheet->GetCurrentHash() )
288 afterSheet = nullptr;
289
290 continue;
291 }
292
293 item = nextMatch( sheet->LastScreen(), sheet, nullptr, data, isReversed );
294
295 if( item )
296 {
297 if( m_frame->Schematic().CurrentSheet() != *sheet )
298 {
299 m_frame->Schematic().SetCurrentSheet( *sheet );
301 }
302
303 break;
304 }
305 }
306 }
307
308 if( item )
309 {
310 m_afterItem = item;
311
312 if( !item->IsBrightened() )
313 {
314 // Clear any previous brightening
315 UpdateFind( aEvent );
316
317 // Brighten (and show) found object
318 item->SetForceVisible( true );
321 }
322
323 if( !selectedOnly )
324 {
327 }
328
331 }
332 else
333 {
334 wxString msg = searchAllSheets ? _( "Reached end of schematic." )
335 : _( "Reached end of sheet." );
336
337 // Show the popup during the time period the user can wrap the search
338 m_frame->ShowFindReplaceStatus( msg + wxS( " " ) +
339 _( "Find again to wrap around to the start." ), 4000 );
340 m_wrapAroundTimer.StartOnce( 4000 );
341 }
342
343 return 0;
344}
345
347{
349 SCH_SEARCH_DATA* schSearchData = dynamic_cast<SCH_SEARCH_DATA*>( &data );
350 bool selectedOnly = schSearchData ? schSearchData->searchSelectedOnly : false;
351
352 return selectedOnly ? m_afterItem : m_selectionTool->GetSelection().Front();
353}
354
356{
358 EDA_ITEM* match = getCurrentMatch();
359
360 return match && match->Matches( data, &m_frame->GetCurrentSheet() );
361}
362
363
365{
367 EDA_ITEM* item = getCurrentMatch();
369
370 if( data.findString.IsEmpty() )
371 return FindAndReplace( ACTIONS::find.MakeEvent() );
372
373 // TODO: move to SCH_COMMIT....
374
375 if( item && HasMatch() )
376 {
377 SCH_ITEM* sch_item = static_cast<SCH_ITEM*>( item );
378
379 m_frame->SaveCopyInUndoList( sheet->LastScreen(), sch_item, UNDO_REDO::CHANGED, false );
380
381 if( item->Replace( data, sheet ) )
382 {
383 m_frame->UpdateItem( item, false, true );
385 m_frame->OnModify();
386 }
387
388 FindNext( ACTIONS::findNext.MakeEvent() );
389 }
390
391 return 0;
392}
393
394
396{
398 bool currentSheetOnly = false;
399 bool selectedOnly = false;
400
401 try
402 {
403 const SCH_SEARCH_DATA& schSearchData = dynamic_cast<const SCH_SEARCH_DATA&>( data );
404 currentSheetOnly = schSearchData.searchCurrentSheetOnly;
405 selectedOnly = schSearchData.searchSelectedOnly;
406 }
407 catch( const std::bad_cast& )
408 {
409 }
410
411 bool modified = false; // TODO: move to SCH_COMMIT....
412
413 if( data.findString.IsEmpty() )
414 return FindAndReplace( ACTIONS::find.MakeEvent() );
415
416 auto doReplace =
417 [&]( SCH_ITEM* aItem, SCH_SHEET_PATH* aSheet, EDA_SEARCH_DATA& aData )
418 {
419 m_frame->SaveCopyInUndoList( aSheet->LastScreen(), aItem, UNDO_REDO::CHANGED,
420 modified );
421
422 if( aItem->Replace( aData, aSheet ) )
423 {
424 m_frame->UpdateItem( aItem, false, true );
425 modified = true;
426 }
427 };
428
429 if( currentSheetOnly || selectedOnly )
430 {
431 SCH_SHEET_PATH* currentSheet = &m_frame->GetCurrentSheet();
432
433 SCH_ITEM* item = nextMatch( m_frame->GetScreen(), currentSheet, nullptr, data, false );
434
435 while( item )
436 {
437 if( !selectedOnly || item->IsSelected() )
438 doReplace( item, currentSheet, data );
439
440 item = nextMatch( m_frame->GetScreen(), currentSheet, item, data, false );
441 }
442 }
443 else
444 {
445 SCH_SHEET_LIST allSheets = m_frame->Schematic().GetSheets();
446 SCH_SCREENS screens( m_frame->Schematic().Root() );
447
448 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
449 {
450 SCH_SHEET_LIST sheets = allSheets.FindAllSheetsForScreen( screen );
451
452 for( unsigned ii = 0; ii < sheets.size(); ++ii )
453 {
454 SCH_ITEM* item = nextMatch( screen, &sheets[ii], nullptr, data, false );
455
456 while( item )
457 {
458 if( ii == 0 )
459 {
460 doReplace( item, &sheets[0], data );
461 }
462 else if( item->Type() == SCH_FIELD_T )
463 {
464 SCH_FIELD* field = static_cast<SCH_FIELD*>( item );
465
466 if( field->GetParent() && field->GetParent()->Type() == SCH_SYMBOL_T )
467 {
468 switch( field->GetId() )
469 {
470 case REFERENCE_FIELD:
471 case VALUE_FIELD:
472 case FOOTPRINT_FIELD:
473 // must be handled for each distinct sheet
474 doReplace( field, &sheets[ii], data );
475 break;
476
477 default:
478 // handled in first iteration
479 break;
480 }
481 }
482 }
483
484 item = nextMatch( screen, &sheets[ii], item, data, false );
485 }
486 }
487 }
488 }
489
490 if( modified )
491 {
493 m_frame->OnModify();
494 }
495
496 return 0;
497}
498
499
501{
515}
static TOOL_ACTION replaceAll
Definition: actions.h:105
static TOOL_ACTION updateFind
Definition: actions.h:106
static TOOL_ACTION findPrevious
Definition: actions.h:102
static TOOL_ACTION findAndReplace
Definition: actions.h:100
static TOOL_ACTION replaceAndFindNext
Definition: actions.h:104
static TOOL_ACTION findNext
Definition: actions.h:101
static TOOL_ACTION findNextMarker
Definition: actions.h:103
static TOOL_ACTION find
Definition: actions.h:99
const Vec GetCenter() const
Definition: box2.h:220
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:88
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:242
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:100
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:193
virtual bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const
Compare the item against the search criteria in aSearchData.
Definition: eda_item.h:375
bool IsSelected() const
Definition: eda_item.h:109
EDA_ITEM * GetParent() const
Definition: eda_item.h:102
bool IsBrightened() const
Definition: eda_item.h:111
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:186
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:262
static const TOOL_EVENT SelectedEvent
Definition: actions.h:260
static const TOOL_EVENT SelectedItemsModified
Selected items were moved, this can be very high frequency on the canvas, use with care.
Definition: actions.h:267
static const TOOL_EVENT PointSelectedEvent
Definition: actions.h:259
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:261
void UpdateItems()
Iterate through the list of items that asked for updating and updates them.
Definition: view.cpp:1427
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:136
void SetCurrentSheet(const SCH_SHEET_PATH &aPath) override
Definition: schematic.h:141
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:100
SCH_SHEET & Root() const
Definition: schematic.h:105
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: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:174
std::vector< SCH_FIELD > & GetFields()
Definition: sch_label.h:196
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:703
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:108
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:181
Schematic symbol object.
Definition: sch_symbol.h:105
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:960
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