KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eeschema/widgets/search_handlers.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) 2022-2023 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <ee_actions.h>
21#include <sch_edit_frame.h>
22#include <sch_painter.h>
23#include <sch_symbol.h>
24#include <sch_label.h>
25#include <sch_text.h>
26#include <sch_textbox.h>
27#include <schematic.h>
28#include <string_utils.h>
29#include <tool/tool_manager.h>
30#include "search_handlers.h"
31
32
34{
35 std::vector<long> item = { aItemRow };
36 SelectItems( item );
37
39}
40
41
42void SCH_SEARCH_HANDLER::FindAll( const std::function<bool( SCH_ITEM*, SCH_SHEET_PATH* )>& aCollector )
43{
44 SCH_SCREENS screens( m_frame->Schematic().Root() );
45 std::vector<SCH_SHEET_PATH*> paths;
46
47 m_hitlist.clear();
48
50
51 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
52 {
53 for( SCH_SHEET_PATH& sheet : screen->GetClientSheetPaths() )
54 paths.push_back( &sheet );
55 }
56
57 for( SCH_SHEET_PATH* sheet : paths )
58 {
59 for( SCH_ITEM* item : sheet->LastScreen()->Items() )
60 {
61 if( aCollector( item, sheet ) )
62 m_hitlist.push_back( { item, sheet } );
63 }
64 }
65}
66
67
68void SCH_SEARCH_HANDLER::Sort( int aCol, bool aAscending, std::vector<long>* aSelection )
69{
70 std::vector<SCH_ITEM*> selection;
71
72 for( long i = 0; i < (long) m_hitlist.size(); ++i )
73 {
74 if( alg::contains( *aSelection, i ) )
75 selection.push_back( m_hitlist[i].item );
76 }
77
78 int col = std::max( 0, aCol ); // Provide a stable order by sorting on first column if no
79 // sort column provided.
80
81 std::sort( m_hitlist.begin(), m_hitlist.end(),
82 [&]( const SCH_SEARCH_HIT& a, const SCH_SEARCH_HIT& b ) -> bool
83 {
84 // N.B. To meet the iterator sort conditions, we cannot simply invert the truth
85 // to get the opposite sort. i.e. ~(a<b) != (a>b)
86 if( aAscending )
87 return StrNumCmp( getResultCell( a, col ), getResultCell( b, col ), true ) < 0;
88 else
89 return StrNumCmp( getResultCell( b, col ), getResultCell( a, col ), true ) < 0;
90 } );
91
92 aSelection->clear();
93
94 for( long i = 0; i < (long) m_hitlist.size(); ++i )
95 {
96 if( alg::contains( selection, m_hitlist[i].item ) )
97 aSelection->push_back( i );
98 }
99}
100
101
102void SCH_SEARCH_HANDLER::SelectItems( std::vector<long>& aItemRows )
103{
104 EDA_ITEMS selectedItems;
105 std::vector<SCH_SEARCH_HIT> selectedHits;
106
108
109 for( long row : aItemRows )
110 {
111 if( row >= 0 && row < (long) m_hitlist.size() )
112 {
113 selectedHits.emplace_back( m_hitlist[row] );
114 selectedItems.emplace_back( m_hitlist[row].item );
115 }
116 }
117
118 if( selectedHits.empty() )
119 return;
120
121 bool allHitsOnSamePage = std::all_of( selectedHits.begin() + 1, selectedHits.end(),
122 [&]( const SCH_SEARCH_HIT& r )
123 {
124 return r.sheetPath == selectedHits.front().sheetPath;
125 } );
126
128
129 if( allHitsOnSamePage && !selectedHits.empty() )
130 {
131 if( m_frame->GetCurrentSheet() != *selectedHits.front().sheetPath )
132 {
133 m_frame->SetCurrentSheet( *selectedHits.front().sheetPath );
135 }
136
137 if( selectedItems.size() )
139
140 switch( settings.selection_zoom )
141 {
144 break;
147 break;
149 break;
150 }
151
152 m_frame->GetCanvas()->Refresh( false );
153 }
154}
155
156
158 SCH_SEARCH_HANDLER( _HKI( "Symbols" ), aFrame )
159{
160 m_columns.emplace_back( _HKI( "Reference" ), 2, wxLIST_FORMAT_LEFT );
161 m_columns.emplace_back( _HKI( "Value" ), 6, wxLIST_FORMAT_LEFT );
162 m_columns.emplace_back( _HKI( "Footprint" ), 10, wxLIST_FORMAT_LEFT );
163 m_columns.emplace_back( _HKI( "Page" ), 1, wxLIST_FORMAT_CENTER );
164 m_columns.emplace_back( wxT( "X" ), 3, wxLIST_FORMAT_CENTER );
165 m_columns.emplace_back( wxT( "Y" ), 3, wxLIST_FORMAT_CENTER );
166 m_columns.emplace_back( _HKI( "Excl. sim" ), 2, wxLIST_FORMAT_CENTER );
167 m_columns.emplace_back( _HKI( "Excl. BOM" ), 2, wxLIST_FORMAT_CENTER );
168 m_columns.emplace_back( _HKI( "Excl. board" ), 2, wxLIST_FORMAT_CENTER );
169 m_columns.emplace_back( _HKI( "DNP" ), 2, wxLIST_FORMAT_CENTER );
170}
171
172
173int SYMBOL_SEARCH_HANDLER::Search( const wxString& aQuery )
174{
175 m_hitlist.clear();
176
177 SCH_SEARCH_DATA frp;
178 frp.findString = aQuery;
179
180 // Try to handle whatever the user throws at us (substring, wildcards, regex, etc.)
181 frp.matchMode = EDA_SEARCH_MATCH_MODE::PERMISSIVE;
182 frp.searchCurrentSheetOnly = false;
183
184 auto search =
185 [frp]( SCH_ITEM* item, SCH_SHEET_PATH* sheet )
186 {
187 if( item && item->Type() == SCH_SYMBOL_T )
188 {
189 SCH_SYMBOL* sym = static_cast<SCH_SYMBOL*>( item );
190
191 // IsPower depends on non-missing lib symbol association
192 if( !sym->IsMissingLibSymbol() && sym->IsPower() )
193 return false;
194
195 for( SCH_FIELD& field : sym->GetFields() )
196 {
197 if( frp.findString.IsEmpty() || field.Matches( frp, sheet ) )
198 return true;
199 }
200 }
201
202 return false;
203 };
204
205 FindAll( search );
206
207 return (int) m_hitlist.size();
208}
209
210
212{
213 SCH_SYMBOL*sym = dynamic_cast<SCH_SYMBOL*>( aHit.item );
214
215 if( !sym )
216 return wxEmptyString;
217
218 if( aCol == 0 )
219 return sym->GetRef( aHit.sheetPath, true );
220 else if( aCol == 1 )
221 return sym->GetField( VALUE_FIELD )->GetShownText( aHit.sheetPath, false );
222 else if( aCol == 2 )
223 return sym->GetField( FOOTPRINT_FIELD )->GetShownText( aHit.sheetPath, false );
224 else if( aCol == 3 )
225 return aHit.sheetPath->GetPageNumber();
226 else if( aCol == 4 )
227 return m_frame->MessageTextFromValue( sym->GetPosition().x );
228 else if( aCol == 5 )
229 return m_frame->MessageTextFromValue( sym->GetPosition().y );
230 else if( aCol == 6 )
231 return sym->GetExcludedFromSim() ? wxS( "X" ) : wxS( " " );
232 else if( aCol == 7 )
233 return sym->GetExcludedFromBOM() ? wxS( "X" ) : wxS( " " );
234 else if( aCol == 8 )
235 return sym->GetExcludedFromBoard() ? wxS( "X" ) : wxS( " " );
236 else if( aCol == 9 )
237 return sym->GetDNP() ? wxS( "X" ) : wxS( " " );
238
239 return wxEmptyString;
240}
241
242
244 SCH_SEARCH_HANDLER( _HKI( "Power" ), aFrame )
245{
246 m_columns.emplace_back( _HKI( "Reference" ), 2, wxLIST_FORMAT_LEFT );
247 m_columns.emplace_back( _HKI( "Value" ), 6, wxLIST_FORMAT_LEFT );
248 m_columns.emplace_back( _HKI( "Page" ), 1, wxLIST_FORMAT_CENTER );
249 m_columns.emplace_back( wxT( "X" ), 3, wxLIST_FORMAT_CENTER );
250 m_columns.emplace_back( wxT( "Y" ), 3, wxLIST_FORMAT_CENTER );
251}
252
253
254int POWER_SEARCH_HANDLER::Search( const wxString& aQuery )
255{
256 m_hitlist.clear();
257
258 SCH_SEARCH_DATA frp;
259 frp.findString = aQuery;
260
261 // Try to handle whatever the user throws at us (substring, wildcards, regex, etc.)
262 frp.matchMode = EDA_SEARCH_MATCH_MODE::PERMISSIVE;
263 frp.searchCurrentSheetOnly = false;
264
265 auto search =
266 [frp]( SCH_ITEM* item, SCH_SHEET_PATH* sheet )
267 {
268 if( item && item->Type() == SCH_SYMBOL_T )
269 {
270 SCH_SYMBOL* sym = static_cast<SCH_SYMBOL*>( item );
271
272 // IsPower depends on non-missing lib symbol association
273 if( sym->IsMissingLibSymbol() || !sym->IsPower() )
274 return false;
275
276 for( SCH_FIELD& field : sym->GetFields() )
277 {
278 if( frp.findString.IsEmpty() || field.Matches( frp, sheet ) )
279 return true;
280 }
281 }
282
283 return false;
284 };
285
286 FindAll( search );
287
288 return (int) m_hitlist.size();
289}
290
291
293{
294 SCH_SYMBOL* sym = dynamic_cast<SCH_SYMBOL*>( aHit.item );
295
296 if( !sym )
297 return wxEmptyString;
298
299 if( aCol == 0 )
300 return sym->GetRef( aHit.sheetPath, true );
301 else if( aCol == 1 )
302 return sym->GetField( VALUE_FIELD )->GetShownText( aHit.sheetPath, false );
303 else if( aCol == 2 )
304 return aHit.sheetPath->GetPageNumber();
305 else if( aCol == 3 )
306 return m_frame->MessageTextFromValue( sym->GetPosition().x );
307 else if( aCol == 4 )
308 return m_frame->MessageTextFromValue( sym->GetPosition().y );
309
310 return wxEmptyString;
311}
312
313
315 SCH_SEARCH_HANDLER( _HKI( "Text" ), aFrame )
316{
317 m_columns.emplace_back( _HKI( "Type" ), 2, wxLIST_FORMAT_LEFT );
318 m_columns.emplace_back( _HKI( "Text" ), 12, wxLIST_FORMAT_LEFT );
319 m_columns.emplace_back( _HKI( "Page" ), 1, wxLIST_FORMAT_CENTER );
320 m_columns.emplace_back( wxT( "X" ), 3, wxLIST_FORMAT_CENTER );
321 m_columns.emplace_back( wxT( "Y" ), 3, wxLIST_FORMAT_CENTER );
322}
323
324
325int TEXT_SEARCH_HANDLER::Search( const wxString& aQuery )
326{
327 m_hitlist.clear();
328
329 SCH_SEARCH_DATA frp;
330 frp.findString = aQuery;
331
332 // Try to handle whatever the user throws at us (substring, wildcards, regex, etc.)
333 frp.matchMode = EDA_SEARCH_MATCH_MODE::PERMISSIVE;
334 frp.searchCurrentSheetOnly = false;
335
336 auto search =
337 [frp]( SCH_ITEM* item, SCH_SHEET_PATH* sheet )
338 {
339 if( item->Type() == SCH_TEXT_T || item->Type() == SCH_TEXTBOX_T )
340 {
341 if( frp.findString.IsEmpty() || item->Matches( frp, sheet ) )
342 return true;
343 }
344
345 return false;
346 };
347
348 FindAll( search );
349
350 return (int) m_hitlist.size();
351}
352
353
354wxString TEXT_SEARCH_HANDLER::getResultCell( const SCH_SEARCH_HIT& aHit, int aCol )
355{
356 if( aHit.item->Type() == SCH_TEXT_T )
357 {
358 SCH_TEXT* txt = dynamic_cast<SCH_TEXT*>( aHit.item );
359
360 if( !txt )
361 return wxEmptyString;
362
363 if( aCol == 0 )
364 return _( "Text" );
365 else if( aCol == 1 )
366 return txt->GetShownText( false );
367 else if( aCol == 2 )
368 return aHit.sheetPath->GetPageNumber();
369 else if( aCol == 3 )
370 return m_frame->MessageTextFromValue( txt->GetPosition().x );
371 else if( aCol == 4 )
372 return m_frame->MessageTextFromValue( txt->GetPosition().y );
373 }
374 else if( aHit.item->Type() == SCH_TEXTBOX_T )
375 {
376 SCH_TEXTBOX* txt = dynamic_cast<SCH_TEXTBOX*>( aHit.item );
377
378 if( !txt )
379 return wxEmptyString;
380
381 if( aCol == 0 )
382 return _( "Text Box" );
383 else if( aCol == 1 )
384 return txt->GetShownText( false );
385 else if( aCol == 2 )
386 return aHit.sheetPath->GetPageNumber();
387 else if( aCol == 3 )
388 return m_frame->MessageTextFromValue( txt->GetPosition().x );
389 else if( aCol == 4 )
390 return m_frame->MessageTextFromValue( txt->GetPosition().y );
391 }
392
393
394 return wxEmptyString;
395}
396
397
399 SCH_SEARCH_HANDLER( _HKI( "Labels" ), aFrame )
400{
401 m_columns.emplace_back( _HKI( "Type" ), 2, wxLIST_FORMAT_LEFT );
402 m_columns.emplace_back( _HKI( "Name" ), 6, wxLIST_FORMAT_LEFT );
403 m_columns.emplace_back( _HKI( "Page" ), 2, wxLIST_FORMAT_CENTER );
404 m_columns.emplace_back( wxT( "X" ), 3, wxLIST_FORMAT_CENTER );
405 m_columns.emplace_back( wxT( "Y" ), 3 , wxLIST_FORMAT_CENTER);
406}
407
408
409int LABEL_SEARCH_HANDLER::Search( const wxString& aQuery )
410{
411 m_hitlist.clear();
412
413 SCH_SEARCH_DATA frp;
414 frp.findString = aQuery;
415
416 // Try to handle whatever the user throws at us (substring, wildcards, regex, etc.)
417 frp.matchMode = EDA_SEARCH_MATCH_MODE::PERMISSIVE;
418 frp.searchCurrentSheetOnly = false;
419
420 auto search =
421 [frp]( SCH_ITEM* item, SCH_SHEET_PATH* sheet )
422 {
423 if( item->IsType( { SCH_LABEL_LOCATE_ANY_T } ) )
424 {
425 SCH_LABEL_BASE* lbl = dynamic_cast<SCH_LABEL_BASE*>( item );
426
427 wxCHECK( lbl, false );
428
429 if( frp.findString.IsEmpty() || lbl->Matches( frp, sheet ) )
430 return true;
431 }
432
433 return false;
434 };
435
436 FindAll( search );
437
438 return (int) m_hitlist.size();
439}
440
441
443{
444 SCH_LABEL_BASE* lbl = dynamic_cast<SCH_LABEL_BASE*>( aHit.item );
445
446 if( !lbl )
447 return wxEmptyString;
448
449 if (aCol == 0)
450 {
451 if(lbl->Type() == SCH_LABEL_T)
452 return _HKI( "Local" );
453 else if( lbl->Type() == SCH_GLOBAL_LABEL_T )
454 return _HKI( "Global" );
455 else if( lbl->Type() == SCH_HIER_LABEL_T )
456 return _HKI( "Hierarchical" );
457 else if( lbl->Type() == SCH_DIRECTIVE_LABEL_T )
458 return _HKI( "Directive" );
459 }
460 else if( aCol == 1 )
461 return lbl->GetShownText( false );
462 else if( aCol == 2 )
463 return aHit.sheetPath->GetPageNumber();
464 else if( aCol == 3 )
465 return m_frame->MessageTextFromValue( lbl->GetPosition().x );
466 else if( aCol == 4 )
467 return m_frame->MessageTextFromValue( lbl->GetPosition().y );
468
469 return wxEmptyString;
470}
static TOOL_ACTION zoomFitSelection
Definition: actions.h:136
static TOOL_ACTION centerSelection
Definition: actions.h:142
SEARCH_PANE m_SearchPane
Definition: app_settings.h:182
virtual APP_SETTINGS_BASE * config() const
Returns the settings object used in SaveSettings(), and is overloaded in KICAD_MANAGER_FRAME.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
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
static TOOL_ACTION properties
Definition: ee_actions.h:135
static TOOL_ACTION addItemsToSel
Selects a list of items (specified as the event parameter)
Definition: ee_actions.h:63
static TOOL_ACTION clearSelection
Clears the current selection.
Definition: ee_actions.h:56
LABEL_SEARCH_HANDLER(SCH_EDIT_FRAME *aFrame)
int Search(const wxString &aQuery) override
wxString getResultCell(const SCH_SEARCH_HIT &hit, int aCol) override
int Search(const wxString &aQuery) override
POWER_SEARCH_HANDLER(SCH_EDIT_FRAME *aFrame)
wxString getResultCell(const SCH_SEARCH_HIT &aHit, int aCol) override
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.
Schematic editor (Eeschema) main window.
SCH_SHEET_PATH & GetCurrentSheet() const
SCHEMATIC & Schematic() const
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
void DisplayCurrentSheet()
Draw the current sheet on the display.
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Definition: sch_field.cpp:211
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:166
bool IsType(const std::vector< KICAD_T > &aScanTypes) const override
Check whether the item is one of the listed types.
Definition: sch_item.h:181
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const override
Definition: sch_label.cpp:816
bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition: sch_label.cpp:848
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
std::vector< SCH_SEARCH_HIT > m_hitlist
void SelectItems(std::vector< long > &aItemRows) override
void FindAll(const std::function< bool(SCH_ITEM *, SCH_SHEET_PATH *)> &aCollector)
void Sort(int aCol, bool aAscending, std::vector< long > *aSelection) override
void ActivateItem(long aItemRow) override
VECTOR2I GetPosition() const override
Definition: sch_shape.h:70
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
wxString GetPageNumber() const
Schematic symbol object.
Definition: sch_symbol.h:104
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:939
bool IsMissingLibSymbol() const
Check to see if the library symbol is set to the dummy library symbol.
Definition: sch_symbol.cpp:237
VECTOR2I GetPosition() const override
Definition: sch_symbol.h:807
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly)
Populate a std::vector with SCH_FIELDs.
Definition: sch_symbol.cpp:987
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
Definition: sch_symbol.cpp:737
bool IsPower() const override
virtual wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
VECTOR2I GetPosition() const override
Definition: sch_text.h:141
virtual wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Definition: sch_text.cpp:406
std::vector< std::tuple< wxString, int, wxListColumnFormat > > m_columns
Definition: search_pane.h:57
wxString getResultCell(const SCH_SEARCH_HIT &aHit, int aCol) override
SYMBOL_SEARCH_HANDLER(SCH_EDIT_FRAME *aFrame)
int Search(const wxString &aQuery) override
bool GetExcludedFromBoard() const
Definition: symbol.h:148
bool GetExcludedFromBOM() const
Definition: symbol.h:142
bool GetDNP() const
Set or clear the 'Do Not Populate' flaga.
Definition: symbol.h:153
bool GetExcludedFromSim() const override
Definition: symbol.h:136
TEXT_SEARCH_HANDLER(SCH_EDIT_FRAME *aFrame)
int Search(const wxString &aQuery) override
wxString getResultCell(const SCH_SEARCH_HIT &hit, int aCol) override
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:150
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
A lower-precision version of StringFromValue().
#define _HKI(x)
#define _(s)
std::vector< EDA_ITEM * > EDA_ITEMS
Define list of drawing items for screens.
Definition: eda_item.h:536
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100
EDA_SEARCH_MATCH_MODE matchMode
@ FOOTPRINT_FIELD
Field Name Module PCB, i.e. "16DIP300".
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ SCH_SYMBOL_T
Definition: typeinfo.h:172
@ SCH_DIRECTIVE_LABEL_T
Definition: typeinfo.h:171
@ SCH_LABEL_T
Definition: typeinfo.h:167
@ SCH_HIER_LABEL_T
Definition: typeinfo.h:169
@ SCH_TEXT_T
Definition: typeinfo.h:151
@ SCH_TEXTBOX_T
Definition: typeinfo.h:152
@ SCH_GLOBAL_LABEL_T
Definition: typeinfo.h:168