KiCad PCB EDA Suite
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 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 <footprint.h>
21#include <pcb_edit_frame.h>
22#include <pcb_marker.h>
23#include <pcb_painter.h>
24#include <pcb_textbox.h>
25#include <pcb_text.h>
26#include <string_utils.h>
27#include <tool/tool_manager.h>
28#include <tools/pcb_actions.h>
29#include <zone.h>
30#include "search_handlers.h"
31
32
34 SEARCH_HANDLER( aName ), m_frame( aFrame )
35{
36}
37
38
40{
41 std::vector<long> item = { aItemRow };
42 SelectItems( item );
43
45}
46
47
49 PCB_SEARCH_HANDLER( wxT( "Footprints" ), aFrame )
50{
51 m_columns.emplace_back( wxT( "Reference" ), 1 );
52 m_columns.emplace_back( wxT( "Value" ), 2 );
53 m_columns.emplace_back( wxT( "Layer" ), 1 );
54 m_columns.emplace_back( wxT( "X" ), 1 );
55 m_columns.emplace_back( wxT( "Y" ), 1 );
56}
57
58
59int FOOTPRINT_SEARCH_HANDLER::Search( const wxString& aQuery )
60{
61 m_hitlist.clear();
62 BOARD* board = m_frame->GetBoard();
63
65 frp.findString = aQuery;
67
68 for( FOOTPRINT* fp : board->Footprints() )
69 {
70 if( aQuery.IsEmpty()
71 || fp->Reference().Matches( frp, nullptr )
72 || fp->Value().Matches( frp, nullptr ) )
73 {
74 m_hitlist.push_back( fp );
75 }
76 }
77
78 return (int) m_hitlist.size();
79}
80
81
82wxString FOOTPRINT_SEARCH_HANDLER::GetResultCell( int aRow, int aCol )
83{
84 FOOTPRINT* fp = m_hitlist[aRow];
85
86 if( aCol == 0 )
87 return fp->GetReference();
88 else if( aCol == 1 )
89 return UnescapeString( fp->GetValue() );
90 else if( aCol == 2 )
91 return fp->GetLayerName();
92 else if( aCol == 3 )
93 return m_frame->MessageTextFromValue( fp->GetX() );
94 else if( aCol == 4 )
95 return m_frame->MessageTextFromValue( fp->GetY() );
96
97 return wxEmptyString;
98}
99
100
101void FOOTPRINT_SEARCH_HANDLER::SelectItems( std::vector<long>& aItemRows )
102{
103 std::vector<EDA_ITEM*> selectedItems;
104
105 for( long row : aItemRows )
106 {
107 if( row >= 0 && row < (long) m_hitlist.size() )
108 {
109 FOOTPRINT* fp = m_hitlist[row];
110 selectedItems.push_back( fp );
111 }
112 }
113
115
116 if( selectedItems.size() )
117 m_frame->GetToolManager()->RunAction( PCB_ACTIONS::selectItems, true, &selectedItems );
118
119 m_frame->GetCanvas()->Refresh( false );
120}
121
122
124 PCB_SEARCH_HANDLER( wxT( "Zones" ), aFrame )
125{
126 m_columns.emplace_back( wxT( "Name" ), 2 );
127 m_columns.emplace_back( wxT( "Net" ), 1 );
128 m_columns.emplace_back( wxT( "Layer" ), 1 );
129 m_columns.emplace_back( wxT( "Priority" ), 1 );
130 m_columns.emplace_back( wxT( "X" ), 1 );
131 m_columns.emplace_back( wxT( "Y" ), 1 );
132}
133
134
135int ZONE_SEARCH_HANDLER::Search( const wxString& aQuery )
136{
137 m_hitlist.clear();
138 BOARD* board = m_frame->GetBoard();
139
140 EDA_SEARCH_DATA frp;
141 frp.findString = aQuery;
143
144 for( BOARD_ITEM* item : board->Zones() )
145 {
146 ZONE* zoneItem = dynamic_cast<ZONE*>( item );
147
148 if( zoneItem && ( aQuery.IsEmpty() || zoneItem->Matches( frp, nullptr ) ) )
149 m_hitlist.push_back( zoneItem );
150 }
151
152 return (int) m_hitlist.size();
153}
154
155
156wxString ZONE_SEARCH_HANDLER::GetResultCell( int aRow, int aCol )
157{
158 ZONE* zone = m_hitlist[aRow];
159
160 if( aCol == 0 )
161 return zone->GetZoneName();
162 if( aCol == 1 )
163 return UnescapeString( zone->GetNetname() );
164 else if( aCol == 2 )
165 {
166 wxArrayString layers;
167 BOARD* board = m_frame->GetBoard();
168
169 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
170 layers.Add( board->GetLayerName( layer ) );
171
172 return wxJoin( layers, ',' );
173 }
174 else if( aCol == 3 )
175 return wxString::Format( "%d", zone->GetAssignedPriority() );
176 else if( aCol == 4 )
177 return m_frame->MessageTextFromValue( zone->GetX() );
178 else if( aCol == 5 )
179 return m_frame->MessageTextFromValue( zone->GetY() );
180
181 return wxEmptyString;
182}
183
184
185void ZONE_SEARCH_HANDLER::SelectItems( std::vector<long>& aItemRows )
186{
187 std::vector<EDA_ITEM*> selectedItems;
188
189 for( long row : aItemRows )
190 {
191 if( row >= 0 && row < (long) m_hitlist.size() )
192 {
193 ZONE* zone = m_hitlist[row];
194 selectedItems.push_back( zone );
195 }
196 }
197
199
200 if( selectedItems.size() )
201 m_frame->GetToolManager()->RunAction( PCB_ACTIONS::selectItems, true, &selectedItems );
202
203 m_frame->GetCanvas()->Refresh( false );
204}
205
206
208 PCB_SEARCH_HANDLER( wxT( "Text" ), aFrame )
209{
210 m_columns.emplace_back( wxT( "Type" ), 1 );
211 m_columns.emplace_back( wxT( "Text" ), 3 );
212 m_columns.emplace_back( wxT( "Layer" ), 1 );
213 m_columns.emplace_back( wxT( "X" ), 1 );
214 m_columns.emplace_back( wxT( "Y" ), 1 );
215}
216
217
218int TEXT_SEARCH_HANDLER::Search( const wxString& aQuery )
219{
220 m_hitlist.clear();
221 BOARD* board = m_frame->GetBoard();
222
223 EDA_SEARCH_DATA frp;
224 frp.findString = aQuery;
226
227 for( BOARD_ITEM* item : board->Drawings() )
228 {
229 PCB_TEXT* textItem = dynamic_cast<PCB_TEXT*>( item );
230 PCB_TEXTBOX* textBoxItem = dynamic_cast<PCB_TEXTBOX*>( item );
231
232 if( textItem && ( aQuery.IsEmpty() || textItem->Matches( frp, nullptr ) ) )
233 m_hitlist.push_back( textItem );
234 else if( textBoxItem && ( aQuery.IsEmpty() || textBoxItem->Matches( frp, nullptr ) ) )
235 m_hitlist.push_back( textBoxItem );
236 }
237
238 return (int) m_hitlist.size();
239}
240
241
242wxString TEXT_SEARCH_HANDLER::GetResultCell( int aRow, int aCol )
243{
244 BOARD_ITEM* text = m_hitlist[aRow];
245
246 if( aCol == 0 )
247 {
248 if( PCB_TEXT::ClassOf( text ) )
249 return _( "Text" );
250 else if( PCB_TEXTBOX::ClassOf( text ) )
251 return _( "Textbox" );
252 }
253 else if( aCol == 1 )
254 {
255 if( PCB_TEXT::ClassOf( text ) )
256 return UnescapeString( static_cast<PCB_TEXT*>( text )->GetText() );
257 else if( PCB_TEXTBOX::ClassOf( text ) )
258 return UnescapeString( static_cast<PCB_TEXTBOX*>( text )->GetShownText() );
259 }
260 if( aCol == 2 )
261 return text->GetLayerName();
262 else if( aCol == 3 )
263 return m_frame->MessageTextFromValue( text->GetX() );
264 else if( aCol == 4 )
265 return m_frame->MessageTextFromValue( text->GetY() );
266
267 return wxEmptyString;
268}
269
270
271void TEXT_SEARCH_HANDLER::SelectItems( std::vector<long>& aItemRows )
272{
273 std::vector<EDA_ITEM*> selectedItems;
274
275 for( long row : aItemRows )
276 {
277 if( row >= 0 && row < (long) m_hitlist.size() )
278 {
279 BOARD_ITEM* text = m_hitlist[row];
280 selectedItems.push_back( text );
281 }
282 }
283
285
286 if( selectedItems.size() )
287 m_frame->GetToolManager()->RunAction( PCB_ACTIONS::selectItems, true, &selectedItems );
288
289 m_frame->GetCanvas()->Refresh( false );
290}
291
292
294 PCB_SEARCH_HANDLER( wxT( "Nets" ), aFrame )
295{
296 m_columns.emplace_back( wxT( "Name" ), 2 );
297 m_columns.emplace_back( wxT( "Class" ), 2 );
298}
299
300
301int NETS_SEARCH_HANDLER::Search( const wxString& aQuery )
302{
303 m_hitlist.clear();
304
305 EDA_SEARCH_DATA frp;
306 frp.findString = aQuery;
308
309 BOARD* board = m_frame->GetBoard();
310
311 for( NETINFO_ITEM* net : board->GetNetInfo() )
312 {
313 if( net && ( aQuery.IsEmpty() || net->Matches( frp, nullptr ) ) )
314 m_hitlist.push_back( net );
315 }
316
317 return (int) m_hitlist.size();
318}
319
320
321wxString NETS_SEARCH_HANDLER::GetResultCell( int aRow, int aCol )
322{
323 NETINFO_ITEM* net = m_hitlist[aRow];
324
325 if( net->GetNetCode() == 0 )
326 {
327 if( aCol == 0 )
328 return _( "No Net" );
329 else if( aCol == 1 )
330 return wxT( "" );
331 }
332
333 if( aCol == 0 )
334 return UnescapeString( net->GetNetname() );
335 else if( aCol == 1 )
336 return net->GetNetClass()->GetName();
337
338 return wxEmptyString;
339}
340
341
342void NETS_SEARCH_HANDLER::SelectItems( std::vector<long>& aItemRows )
343{
345 ps->SetHighlight( false );
346
347 std::vector<NETINFO_ITEM*> selectedItems;
348
349 for( long row : aItemRows )
350 {
351 if( row >= 0 && row < (long) m_hitlist.size() )
352 {
353 NETINFO_ITEM* net = m_hitlist[row];
354
355 ps->SetHighlight( true, net->GetNetCode(), true );
356 }
357 }
358
361}
362
363
365{
366 m_frame->ShowBoardSetupDialog( _( "Net Classes" ) );
367}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:70
int GetY() const
Definition: board_item.h:94
int GetX() const
Definition: board_item.h:88
wxString GetLayerName() const
Return the name of the PCB layer on which the item resides.
Definition: board_item.cpp:94
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:269
const NETINFO_LIST & GetNetInfo() const
Definition: board.h:784
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: board.cpp:772
ZONES & Zones()
Definition: board.h:317
FOOTPRINTS & Footprints()
Definition: board.h:311
DRAWINGS & Drawings()
Definition: board.h:314
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:474
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
std::vector< FOOTPRINT * > m_hitlist
FOOTPRINT_SEARCH_HANDLER(PCB_EDIT_FRAME *aFrame)
wxString GetResultCell(int aRow, int aCol) override
void SelectItems(std::vector< long > &aItemRows) override
int Search(const wxString &aQuery) override
const wxString & GetValue() const
Definition: footprint.h:547
const wxString & GetReference() const
Definition: footprint.h:519
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
void SetHighlight(bool aEnabled, int aNetcode=-1, bool aMulti=false)
Turns on/off highlighting.
void UpdateAllLayersColor()
Apply the new coloring scheme to all layers.
Definition: view.cpp:761
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:213
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:411
const wxString GetName() const
Definition: netclass.h:65
Handle the data for a net.
Definition: netinfo.h:67
const wxString & GetNetname() const
Definition: netinfo.h:125
NETCLASS * GetNetClass()
Definition: netinfo.h:112
int GetNetCode() const
Definition: netinfo.h:119
void ActivateItem(long aItemRow) override
int Search(const wxString &aQuery) override
std::vector< NETINFO_ITEM * > m_hitlist
NETS_SEARCH_HANDLER(PCB_EDIT_FRAME *aFrame)
void SelectItems(std::vector< long > &aItemRows) override
wxString GetResultCell(int aRow, int aCol) override
static TOOL_ACTION properties
Activation of the edit tool.
Definition: pcb_actions.h:149
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:59
static TOOL_ACTION selectItems
Select a list of items (specified as the event parameter)
Definition: pcb_actions.h:66
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
BOARD * GetBoard() const
virtual KIGFX::PCB_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
The main frame for Pcbnew.
void ShowBoardSetupDialog(const wxString &aInitialPage=wxEmptyString)
void ActivateItem(long aItemRow) override
PCB_EDIT_FRAME * m_frame
PCB_SEARCH_HANDLER(wxString aName, PCB_EDIT_FRAME *aFrame)
bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition: pcb_textbox.h:85
static bool ClassOf(const EDA_ITEM *aItem)
Definition: pcb_textbox.h:46
bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition: pcb_text.h:71
static bool ClassOf(const EDA_ITEM *aItem)
Definition: pcb_text.h:46
std::vector< std::pair< wxString, int > > m_columns
Definition: search_pane.h:47
virtual void SelectItems(std::vector< long > &aItemRows)
Definition: search_pane.h:42
int Search(const wxString &aQuery) override
void SelectItems(std::vector< long > &aItemRows) override
wxString GetResultCell(int aRow, int aCol) override
std::vector< BOARD_ITEM * > m_hitlist
TEXT_SEARCH_HANDLER(PCB_EDIT_FRAME *aFrame)
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:54
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:142
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A lower-precision version of StringFromValue().
ZONE_SEARCH_HANDLER(PCB_EDIT_FRAME *aFrame)
wxString GetResultCell(int aRow, int aCol) override
int Search(const wxString &aQuery) override
std::vector< ZONE * > m_hitlist
void SelectItems(std::vector< long > &aItemRows) override
Handle a list of polygons defining a copper zone.
Definition: zone.h:57
bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition: zone.h:127
wxString GetZoneName() const
Definition: zone.h:124
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: zone.h:122
unsigned GetAssignedPriority() const
Definition: zone.h:112
#define _(s)
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
wxString GetText(EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Get the units string for a given units type.
Definition: eda_units.cpp:101
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
wxString UnescapeString(const wxString &aSource)
EDA_SEARCH_MATCH_MODE matchMode