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 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 {
290 }
291
292 break;
293 }
294 }
295 }
296
297 if( item )
298 {
299 m_afterItem = item;
300
301 if( !item->IsBrightened() )
302 {
303 // Clear any previous brightening
304 UpdateFind( aEvent );
305
306 // Brighten (and show) found object
307 item->SetForceVisible( true );
310 }
311
312 if( !selectedOnly )
313 {
316 }
317
320 }
321 else
322 {
323 wxString msg = searchAllSheets ? _( "Reached end of schematic." )
324 : _( "Reached end of sheet." );
325
326 // Show the popup during the time period the user can wrap the search
327 m_frame->ShowFindReplaceStatus( msg + wxS( " " ) +
328 _( "Find again to wrap around to the start." ), 4000 );
329 m_wrapAroundTimer.StartOnce( 4000 );
330 }
331
332 return 0;
333}
334
336{
338 SCH_SEARCH_DATA* schSearchData = dynamic_cast<SCH_SEARCH_DATA*>( &data );
339 bool selectedOnly = schSearchData ? schSearchData->searchSelectedOnly : false;
340
341 return selectedOnly ? m_afterItem : m_selectionTool->GetSelection().Front();
342}
343
345{
347 EDA_ITEM* match = getCurrentMatch();
348
349 return match && match->Matches( data, &m_frame->GetCurrentSheet() );
350}
351
352
354{
356 EDA_ITEM* item = getCurrentMatch();
358
359 if( data.findString.IsEmpty() )
360 return FindAndReplace( ACTIONS::find.MakeEvent() );
361
362 // TODO: move to SCH_COMMIT....
363
364 if( item && HasMatch() )
365 {
366 SCH_ITEM* sch_item = static_cast<SCH_ITEM*>( item );
367
368 m_frame->SaveCopyInUndoList( sheet->LastScreen(), sch_item, UNDO_REDO::CHANGED, false );
369
370 if( item->Replace( data, sheet ) )
371 {
372 m_frame->UpdateItem( item, false, true );
374 m_frame->OnModify();
375 }
376
377 FindNext( ACTIONS::findNext.MakeEvent() );
378 }
379
380 return 0;
381}
382
383
385{
387 bool currentSheetOnly = false;
388 bool selectedOnly = false;
389
390 try
391 {
392 const SCH_SEARCH_DATA& schSearchData = dynamic_cast<const SCH_SEARCH_DATA&>( data );
393 currentSheetOnly = schSearchData.searchCurrentSheetOnly;
394 selectedOnly = schSearchData.searchSelectedOnly;
395 }
396 catch( const std::bad_cast& )
397 {
398 }
399
400 bool modified = false; // TODO: move to SCH_COMMIT....
401
402 if( data.findString.IsEmpty() )
403 return FindAndReplace( ACTIONS::find.MakeEvent() );
404
405 auto doReplace =
406 [&]( SCH_ITEM* aItem, SCH_SHEET_PATH* aSheet, EDA_SEARCH_DATA& aData )
407 {
408 m_frame->SaveCopyInUndoList( aSheet->LastScreen(), aItem, UNDO_REDO::CHANGED,
409 modified );
410
411 if( aItem->Replace( aData, aSheet ) )
412 {
413 m_frame->UpdateItem( aItem, false, true );
414 modified = true;
415 }
416 };
417
418 if( currentSheetOnly || selectedOnly )
419 {
420 SCH_SHEET_PATH* currentSheet = &m_frame->GetCurrentSheet();
421
422 SCH_ITEM* item = nextMatch( m_frame->GetScreen(), currentSheet, nullptr, data, false );
423
424 while( item )
425 {
426 if( !selectedOnly || item->IsSelected() )
427 doReplace( item, currentSheet, data );
428
429 item = nextMatch( m_frame->GetScreen(), currentSheet, item, data, false );
430 }
431 }
432 else
433 {
435 SCH_SCREENS screens( m_frame->Schematic().Root() );
436
437 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
438 {
439 SCH_SHEET_LIST sheets = allSheets.FindAllSheetsForScreen( screen );
440
441 for( unsigned ii = 0; ii < sheets.size(); ++ii )
442 {
443 SCH_ITEM* item = nextMatch( screen, &sheets[ii], nullptr, data, false );
444
445 while( item )
446 {
447 if( ii == 0 )
448 {
449 doReplace( item, &sheets[0], data );
450 }
451 else if( item->Type() == SCH_FIELD_T )
452 {
453 SCH_FIELD* field = static_cast<SCH_FIELD*>( item );
454
455 if( field->GetParent() && field->GetParent()->Type() == SCH_SYMBOL_T )
456 {
457 switch( field->GetId() )
458 {
459 case REFERENCE_FIELD:
460 case VALUE_FIELD:
461 case FOOTPRINT_FIELD:
462 // must be handled for each distinct sheet
463 doReplace( field, &sheets[ii], data );
464 break;
465
466 default:
467 // handled in first iteration
468 break;
469 }
470 }
471 }
472
473 item = nextMatch( screen, &sheets[ii], item, data, false );
474 }
475 }
476 }
477 }
478
479 if( modified )
480 {
482 m_frame->OnModify();
483 }
484
485 return 0;
486}
487
488
490{
504}
static TOOL_ACTION replaceAll
Definition: actions.h:107
static TOOL_ACTION updateFind
Definition: actions.h:108
static TOOL_ACTION findPrevious
Definition: actions.h:104
static TOOL_ACTION findAndReplace
Definition: actions.h:102
static TOOL_ACTION replaceAndFindNext
Definition: actions.h:106
static TOOL_ACTION findNext
Definition: actions.h:103
static TOOL_ACTION findNextMarker
Definition: actions.h:105
static TOOL_ACTION find
Definition: actions.h:101
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: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:74
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: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:272
static const TOOL_EVENT SelectedEvent
Definition: actions.h:270
static const TOOL_EVENT SelectedItemsModified
Selected items were moved, this can be very high frequency on the canvas, use with care.
Definition: actions.h:277
static const TOOL_EVENT PointSelectedEvent
Definition: actions.h:269
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:271
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:144
SCH_SHEET_LIST BuildUnorderedSheetList() const
Definition: schematic.h:101
void SetCurrentSheet(const SCH_SHEET_PATH &aPath) override
Definition: schematic.h:149
SCH_SHEET & Root() const
Definition: schematic.h:113
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: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:710
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.
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: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:967
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