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 The 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>
29#include <sch_sheet_path.h>
30#include "sch_actions.h"
31
32
34{
36 return UpdateFind( aEvent );
37}
38
39
41{
43 SCH_SEARCH_DATA* schSearchData = dynamic_cast<SCH_SEARCH_DATA*>( &data );
44 SCH_SHEET_PATH* sheetPath = nullptr;
45 bool selectedOnly = schSearchData ? schSearchData->searchSelectedOnly : false;
46
48 sheetPath = &static_cast<SCH_EDIT_FRAME*>( m_frame )->GetCurrentSheet();
49
50 auto visit =
51 [&]( EDA_ITEM* aItem, SCH_SHEET_PATH* aSheet )
52 {
53 // We may get triggered when the dialog is not opened due to binding
54 // SelectedItemsModified we also get triggered when the find dialog is
55 // closed....so we need to double check the dialog is open.
56 if( m_frame->GetFindReplaceDialog() != nullptr
57 && !data.findString.IsEmpty()
58 && aItem->Matches( data, aSheet )
59 && ( !selectedOnly || aItem->IsSelected() ) )
60 {
61 aItem->SetForceVisible( true );
64 }
65 else if( aItem->IsBrightened() || aItem->IsForceVisible() )
66 {
67 aItem->SetForceVisible( false );
69 }
70 };
71
72 auto visitAll =
73 [&]()
74 {
75 if( SYMBOL_EDIT_FRAME* symbolEditor = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame ) )
76 {
77 if( LIB_SYMBOL* symbol = symbolEditor->GetCurSymbol() )
78 {
79 for( SCH_ITEM& item : symbol->GetDrawItems() )
80 visit( &item, nullptr );
81 }
82 }
83 else
84 {
85 for( SCH_ITEM* item : m_frame->GetScreen()->Items() )
86 visit( item, sheetPath );
87 }
88 };
89
90 if( aEvent.IsAction( &ACTIONS::find ) || aEvent.IsAction( &ACTIONS::findAndReplace )
91 || aEvent.IsAction( &ACTIONS::updateFind ) )
92 {
94 visitAll();
95 }
96 else if( aEvent.Matches( EVENTS::SelectedItemsModified ) )
97 {
98 for( EDA_ITEM* item : m_selectionTool->GetSelection() )
99 visit( item, sheetPath );
100 }
101 else if( aEvent.Matches( EVENTS::PointSelectedEvent )
102 || aEvent.Matches( EVENTS::SelectedEvent )
104 || aEvent.Matches( EVENTS::ClearedEvent ) )
105 {
107 {
109 {
111 visitAll();
112 }
113 }
114 else if( selectedOnly )
115 {
116 // Normal find modifies the selection, but selection-based find does not, so we want
117 // to start over in the items we are searching through when the selection changes
118 m_afterItem = nullptr;
119 visitAll();
120 }
121 }
122 else if( m_foundItemHighlighted )
123 {
125 visitAll();
126 }
127
128 getView()->UpdateItems();
130
131 return 0;
132}
133
134
136 EDA_SEARCH_DATA& aData, bool reversed )
137{
138 SCH_SEARCH_DATA* schSearchData = dynamic_cast<SCH_SEARCH_DATA*>( &aData );
139 bool selectedOnly = schSearchData ? schSearchData->searchSelectedOnly : false;
140 bool past_item = !aAfter;
141 std::vector<SCH_ITEM*> sorted_items;
142
143 auto addItem =
144 [&](SCH_ITEM* item)
145 {
146 sorted_items.push_back( item );
147
148 if( item->Type() == SCH_SYMBOL_T )
149 {
150 SCH_SYMBOL* cmp = static_cast<SCH_SYMBOL*>( item );
151
152 for( SCH_FIELD& field : cmp->GetFields() )
153 sorted_items.push_back( &field );
154
155 for( SCH_PIN* pin : cmp->GetPins() )
156 sorted_items.push_back( pin );
157 }
158 else if( item->Type() == SCH_SHEET_T )
159 {
160 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
161
162 for( SCH_FIELD& field : sheet->GetFields() )
163 sorted_items.push_back( &field );
164
165 for( SCH_SHEET_PIN* pin : sheet->GetPins() )
166 sorted_items.push_back( pin );
167 }
168 else if( item->IsType( { SCH_LABEL_LOCATE_ANY_T } ) )
169 {
170 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( item );
171
172 for( SCH_FIELD& field : label->GetFields() )
173 sorted_items.push_back( &field );
174 }
175 };
176
177 if( selectedOnly )
178 {
179 for( EDA_ITEM* item : m_selectionTool->GetSelection() )
180 addItem( static_cast<SCH_ITEM*>( item ) );
181 }
182 else if( SYMBOL_EDIT_FRAME* symbolEditor = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame ) )
183 {
184 if( LIB_SYMBOL* symbol = symbolEditor->GetCurSymbol() )
185 {
186 for( SCH_ITEM& item : symbol->GetDrawItems() )
187 addItem( &item );
188 }
189 }
190 else
191 {
192 for( SCH_ITEM* item : aScreen->Items() )
193 addItem( item );
194 }
195
196 std::sort( sorted_items.begin(), sorted_items.end(),
197 [&]( SCH_ITEM* a, SCH_ITEM* b )
198 {
199 if( a->GetPosition().x == b->GetPosition().x )
200 {
201 // Ensure deterministic sort
202 if( a->GetPosition().y == b->GetPosition().y )
203 return a->m_Uuid < b->m_Uuid;
204
205 return a->GetPosition().y < b->GetPosition().y;
206 }
207 else
208 return a->GetPosition().x < b->GetPosition().x;
209 } );
210
211 if( reversed )
212 std::reverse( sorted_items.begin(), sorted_items.end() );
213
214 for( SCH_ITEM* item : sorted_items )
215 {
216 if( item == aAfter )
217 {
218 past_item = true;
219 }
220 else if( past_item )
221 {
222 if( aData.markersOnly && item->Type() == SCH_MARKER_T )
223 return item;
224
225 if( item->Matches( aData, aSheet ) )
226 return item;
227 }
228 }
229
230 return nullptr;
231}
232
233
235{
237 bool searchAllSheets = false;
238 bool selectedOnly = false;
239 bool isReversed = aEvent.IsAction( &ACTIONS::findPrevious );
240 SCH_ITEM* item = nullptr;
241 SCH_SHEET_PATH* currentSheet = nullptr;
242 SCH_SHEET_PATH* afterSheet = nullptr;
243
244 try
245 {
246 const SCH_SEARCH_DATA& schSearchData = dynamic_cast<const SCH_SEARCH_DATA&>( data );
247
248 if( m_frame->GetFrameType() == FRAME_SCH )
249 {
250 currentSheet = afterSheet = &static_cast<SCH_EDIT_FRAME*>( m_frame )->GetCurrentSheet();
251 searchAllSheets = !schSearchData.searchCurrentSheetOnly;
252 }
253
254 selectedOnly = schSearchData.searchSelectedOnly;
255 }
256 catch( const std::bad_cast& )
257 {
258 }
259
260 if( aEvent.IsAction( &ACTIONS::findNextMarker ) )
261 data.markersOnly = true;
262 else if( data.findString.IsEmpty() )
263 return FindAndReplace( ACTIONS::find.MakeEvent() );
264
265 if( m_wrapAroundTimer.IsRunning() )
266 {
267 afterSheet = nullptr;
268 m_afterItem = nullptr;
269 m_wrapAroundTimer.Stop();
271 }
272
273 if( afterSheet || !searchAllSheets )
274 item = nextMatch( m_frame->GetScreen(), currentSheet, m_afterItem, data, isReversed );
275
276 if( !item && searchAllSheets )
277 {
278 if( SCH_EDIT_FRAME* editFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame ) )
279 {
280 SCH_SCREENS screens( editFrame->Schematic().Root() );
281 SCH_SHEET_LIST paths;
282
283 screens.BuildClientSheetPathList();
284
285 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
286 {
287 for( SCH_SHEET_PATH& sheet : screen->GetClientSheetPaths() )
288 paths.push_back( sheet );
289 }
290
291 paths.SortByPageNumbers( false );
292
293 if( isReversed )
294 std::reverse( paths.begin(), paths.end() );
295
296 for( SCH_SHEET_PATH& sheet : paths )
297 {
298 if( afterSheet )
299 {
300 if( afterSheet->GetCurrentHash() == sheet.GetCurrentHash() )
301 afterSheet = nullptr;
302
303 continue;
304 }
305
306 item = nextMatch( sheet.LastScreen(), &sheet, nullptr, data, isReversed );
307
308 if( item )
309 {
310 if( editFrame->Schematic().CurrentSheet() != sheet )
311 editFrame->GetToolManager()->RunAction<SCH_SHEET_PATH*>( SCH_ACTIONS::changeSheet, &sheet );
312
313 break;
314 }
315 }
316 }
317 }
318
319 if( item )
320 {
321 m_afterItem = item;
322
323 if( !selectedOnly )
324 {
327 }
328
329 if( !item->IsBrightened() )
330 {
331 // Clear any previous brightening
332 UpdateFind( aEvent );
333
334 // Brighten (and show) found object
335 item->SetForceVisible( true );
338 }
339
342 }
343 else
344 {
345 wxString msg;
346
348 msg = _( "Reached end of symbol." );
349 else if( searchAllSheets )
350 msg = _( "Reached end of schematic." );
351 else
352 msg = _( "Reached end of sheet." );
353
354 // Show the popup during the time period the user can wrap the search
355 m_frame->ShowFindReplaceStatus( msg + wxS( " " ) + _( "Find again to wrap around to the start." ), 4000 );
356 m_wrapAroundTimer.StartOnce( 4000 );
357 }
358
359 return 0;
360}
361
363{
365 SCH_SEARCH_DATA* schSearchData = dynamic_cast<SCH_SEARCH_DATA*>( &data );
366 bool selectedOnly = schSearchData ? schSearchData->searchSelectedOnly : false;
367
368 return selectedOnly ? m_afterItem : m_selectionTool->GetSelection().Front();
369}
370
372{
374 EDA_ITEM* match = getCurrentMatch();
375 SCH_SHEET_PATH* sheetPath = nullptr;
376
377 if( m_frame->GetFrameType() == FRAME_SCH )
378 sheetPath = &static_cast<SCH_EDIT_FRAME*>( m_frame )->GetCurrentSheet();
379
380 return match && match->Matches( data, sheetPath );
381}
382
383
385{
387 EDA_ITEM* item = getCurrentMatch();
388 SCH_SHEET_PATH* currentSheet = nullptr;
389
390 if( m_frame->GetFrameType() == FRAME_SCH )
391 currentSheet = &static_cast<SCH_EDIT_FRAME*>( m_frame )->GetCurrentSheet();
392
393 if( data.findString.IsEmpty() )
394 return FindAndReplace( ACTIONS::find.MakeEvent() );
395
396 if( item && HasMatch() )
397 {
398 SCH_COMMIT commit( m_frame );
399 SCH_ITEM* sch_item = static_cast<SCH_ITEM*>( item );
400
401 commit.Modify( sch_item, m_frame->GetScreen(), RECURSE_MODE::NO_RECURSE );
402
403 if( item->Replace( data, currentSheet ) )
404 {
405 if( currentSheet )
406 currentSheet->UpdateAllScreenReferences();
407
408 commit.Push( wxS( "Find and Replace" ) );
409 }
410
411 FindNext( ACTIONS::findNext.MakeEvent() );
412 }
413
414 return 0;
415}
416
417
419{
421 SCH_SHEET_PATH* currentSheet = nullptr;
422 bool currentSheetOnly = true;
423 bool selectedOnly = false;
424
425 try
426 {
427 const SCH_SEARCH_DATA& schSearchData = dynamic_cast<const SCH_SEARCH_DATA&>( data );
428
429 if( m_frame->GetFrameType() == FRAME_SCH )
430 {
431 currentSheet = &static_cast<SCH_EDIT_FRAME*>( m_frame )->GetCurrentSheet();
432 currentSheetOnly = schSearchData.searchCurrentSheetOnly;
433 }
434
435 selectedOnly = schSearchData.searchSelectedOnly;
436 }
437 catch( const std::bad_cast& )
438 {
439 }
440
441 SCH_COMMIT commit( m_frame );
442 bool modified = false; // TODO: move to SCH_COMMIT....
443
444 if( data.findString.IsEmpty() )
445 return FindAndReplace( ACTIONS::find.MakeEvent() );
446
447 auto doReplace =
448 [&]( SCH_ITEM* aItem, SCH_SHEET_PATH* aSheet, EDA_SEARCH_DATA& aData )
449 {
450 commit.Modify( aItem, aSheet ? aSheet->LastScreen() : nullptr, RECURSE_MODE::NO_RECURSE );
451
452 if( aItem->Replace( aData, aSheet ) )
453 {
454 m_frame->UpdateItem( aItem, false, true );
455 modified = true;
456 }
457 };
458
459 if( currentSheetOnly || selectedOnly )
460 {
461 SCH_ITEM* item = nextMatch( m_frame->GetScreen(), currentSheet, nullptr, data, false );
462
463 while( item )
464 {
465 if( !selectedOnly || item->IsSelected() )
466 doReplace( item, currentSheet, data );
467
468 item = nextMatch( m_frame->GetScreen(), currentSheet, item, data, false );
469 }
470 }
471 else if( SCH_EDIT_FRAME* schematicFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame ) )
472 {
473 SCH_SHEET_LIST allSheets = schematicFrame->Schematic().Hierarchy();
474 SCH_SCREENS screens( schematicFrame->Schematic().Root() );
475
476 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
477 {
478 SCH_SHEET_LIST sheets = allSheets.FindAllSheetsForScreen( screen );
479
480 for( unsigned ii = 0; ii < sheets.size(); ++ii )
481 {
482 SCH_ITEM* item = nextMatch( screen, &sheets[ii], nullptr, data, false );
483
484 while( item )
485 {
486 if( ii == 0 )
487 {
488 doReplace( item, &sheets[0], data );
489 }
490 else if( item->Type() == SCH_FIELD_T )
491 {
492 SCH_FIELD* field = static_cast<SCH_FIELD*>( item );
493
494 // References must be handled for each distinct sheet
495 if( field->GetId() == FIELD_T::REFERENCE )
496 doReplace( field, &sheets[ii], data );
497 }
498
499 item = nextMatch( screen, &sheets[ii], item, data, false );
500 }
501 }
502 }
503 }
504
505 if( modified )
506 {
507 commit.Push( wxS( "Find and Replace All" ) );
508
509 if( currentSheet )
510 currentSheet->UpdateAllScreenReferences();
511 }
512
513 return 0;
514}
515
516
518{
532}
static TOOL_ACTION replaceAll
Definition: actions.h:122
static TOOL_ACTION updateFind
Definition: actions.h:123
static TOOL_ACTION findPrevious
Definition: actions.h:119
static TOOL_ACTION findAndReplace
Definition: actions.h:117
static TOOL_ACTION replaceAndFindNext
Definition: actions.h:121
static TOOL_ACTION findNext
Definition: actions.h:118
static TOOL_ACTION findNextMarker
Definition: actions.h:120
static TOOL_ACTION find
Definition: actions.h:116
constexpr const Vec GetCenter() const
Definition: box2.h:230
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition: commit.h:107
FRAME_T GetFrameType() const
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:98
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:272
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition: eda_item.cpp:110
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:110
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:210
virtual bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const
Compare the item against the search criteria in aSearchData.
Definition: eda_item.h:401
bool IsSelected() const
Definition: eda_item.h:127
bool IsBrightened() const
Definition: eda_item.h:129
bool IsForceVisible() const
Definition: eda_item.h:211
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:237
static const TOOL_EVENT ClearedEvent
Definition: actions.h:344
static const TOOL_EVENT SelectedEvent
Definition: actions.h:342
static const TOOL_EVENT SelectedItemsModified
Selected items were moved, this can be very high frequency on the canvas, use with care.
Definition: actions.h:349
static const TOOL_EVENT PointSelectedEvent
Definition: actions.h:341
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:343
void UpdateItems()
Iterate through the list of items that asked for updating and updates them.
Definition: view.cpp:1468
Define a library symbol object.
Definition: lib_symbol.h:85
static TOOL_ACTION changeSheet
Definition: sch_actions.h:217
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
DIALOG_SCH_FIND * GetFindReplaceDialog() const
void ShowFindReplaceDialog(bool aReplace)
Run the Find or Find & Replace dialog.
virtual void UpdateItem(EDA_ITEM *aItem, bool isAddOrDelete=false, bool aUpdateRtree=false)
Mark an item for refresh.
void ShowFindReplaceStatus(const wxString &aMsg, int aStatusTime)
void ClearFindReplaceStatus()
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Execute the changes.
Definition: sch_commit.cpp:489
Schematic editor (Eeschema) main window.
SCH_SHEET_PATH & GetCurrentSheet() const
FIELD_T GetId() const
Definition: sch_field.h:116
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:168
std::vector< SCH_FIELD > & GetFields()
Definition: sch_label.h:205
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:758
SCH_SCREEN * GetNext()
SCH_SCREEN * GetFirst()
void BuildClientSheetPathList()
Build the list of sheet paths sharing a screen for each screen in use.
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition: sch_screen.h:117
int ClearSelection(const TOOL_EVENT &aEvent)
Select all visible items in sheet.
SCH_SELECTION & GetSelection()
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.
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:47
std::vector< SCH_FIELD > & GetFields()
Return a reference to the vector holding the sheet's fields.
Definition: sch_sheet.h:87
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition: sch_sheet.h:187
Schematic symbol object.
Definition: sch_symbol.h:75
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
Definition: sch_symbol.cpp:788
std::vector< SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
SCH_SELECTION_TOOL * m_selectionTool
void BrightenItem(EDA_ITEM *aItem)
int AddItemToSel(const TOOL_EVENT &aEvent)
void UnbrightenItem(EDA_ITEM *aItem)
EDA_ITEM * Front() const
Definition: selection.h:177
The symbol library editor main window.
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:38
Generic, UI-independent tool event.
Definition: tool_event.h:168
bool Matches(const TOOL_EVENT &aEvent) const
Test whether two events match in terms of category & action or command.
Definition: tool_event.h:389
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)
@ FRAME_SCH_SYMBOL_EDITOR
Definition: frame_type.h:35
@ FRAME_SCH
Definition: frame_type.h:34
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
@ SCH_SYMBOL_T
Definition: typeinfo.h:173
@ SCH_FIELD_T
Definition: typeinfo.h:151
@ SCH_SHEET_T
Definition: typeinfo.h:176
@ SCH_MARKER_T
Definition: typeinfo.h:159
@ SCH_LABEL_LOCATE_ANY_T
Definition: typeinfo.h:192