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 <base_units.h>
21#include <footprint.h>
22#include <pcb_edit_frame.h>
23#include <pcb_marker.h>
24#include <pcb_painter.h>
25#include <pcb_textbox.h>
26#include <pcb_text.h>
27#include <string_utils.h>
28#include <tool/tool_manager.h>
29#include <tools/pcb_actions.h>
30#include <zone.h>
31#include "search_handlers.h"
32
33
35 SEARCH_HANDLER( wxT( "Footprint" ) ), m_frame( aFrame )
36{
37 m_columnNames.emplace_back( wxT( "Reference" ) );
38 m_columnNames.emplace_back( wxT( "Value" ) );
39 m_columnNames.emplace_back( wxT( "Layer" ) );
40 m_columnNames.emplace_back( wxT( "X" ) );
41 m_columnNames.emplace_back( wxT( "Y" ) );
42}
43
44
45int FOOTPRINT_SEARCH_HANDLER::Search( const wxString& aQuery )
46{
47 m_hitlist.clear();
48 BOARD* board = m_frame->GetBoard();
49
51 frp.findString = aQuery;
53
54 for( FOOTPRINT* fp : board->Footprints() )
55 {
56 if( aQuery.IsEmpty() ||
57 ( fp->Reference().Matches( frp, nullptr )
58 || fp->Value().Matches( frp, nullptr ) ) )
59 {
60 m_hitlist.push_back( fp );
61 }
62 }
63
64 return m_hitlist.size();
65}
66
67
68wxString FOOTPRINT_SEARCH_HANDLER::GetResultCell( int aRow, int aCol )
69{
70 FOOTPRINT* fp = m_hitlist[aRow];
71
72 if( aCol == 0 )
73 return fp->GetReference();
74 else if( aCol == 1 )
75 return UnescapeString( fp->GetValue() );
76 else if( aCol == 2 )
77 return fp->GetLayerName();
78 else if( aCol == 3 )
79 return m_frame->MessageTextFromValue( fp->GetX() );
80 else if( aCol == 4 )
81 return m_frame->MessageTextFromValue( fp->GetY() );
82
83 return wxEmptyString;
84}
85
86
87void FOOTPRINT_SEARCH_HANDLER::SelectItems( std::vector<long>& aItemRows )
88{
89 std::vector<EDA_ITEM*> selectedItems;
90
91 for( long row : aItemRows )
92 {
93 if( row >= 0 && row < (long)m_hitlist.size() )
94 {
95 FOOTPRINT* fp = m_hitlist[row];
96 selectedItems.push_back( fp );
97 }
98 }
99
101
102 if( selectedItems.size() )
103 m_frame->GetToolManager()->RunAction( PCB_ACTIONS::selectItems, true, &selectedItems );
104
106}
107
108
110 SEARCH_HANDLER( wxT( "Zones" ) ), m_frame( aFrame )
111{
112 m_columnNames.emplace_back( wxT( "Name" ) );
113 m_columnNames.emplace_back( wxT( "Net" ) );
114 m_columnNames.emplace_back( wxT( "Layer" ) );
115 m_columnNames.emplace_back( wxT( "Priority" ) );
116 m_columnNames.emplace_back( wxT( "X" ) );
117 m_columnNames.emplace_back( wxT( "Y" ) );
118}
119
120
121int ZONE_SEARCH_HANDLER::Search( const wxString& aQuery )
122{
123 m_hitlist.clear();
124 BOARD* board = m_frame->GetBoard();
125
126 EDA_SEARCH_DATA frp;
127 frp.findString = aQuery;
129
130 for( BOARD_ITEM* item : board->Zones() )
131 {
132 ZONE* zoneItem = dynamic_cast<ZONE*>( item );
133
134 if( zoneItem && ( aQuery.IsEmpty() || zoneItem->Matches( frp, nullptr ) ) )
135 {
136 m_hitlist.push_back( zoneItem );
137 }
138 }
139
140 return m_hitlist.size();
141}
142
143
144wxString ZONE_SEARCH_HANDLER::GetResultCell( int aRow, int aCol )
145{
146 ZONE* zone = m_hitlist[aRow];
147
148 if( aCol == 0 )
149 return zone->GetZoneName();
150 if( aCol == 1 )
151 return UnescapeString( zone->GetNetname() );
152 else if( aCol == 2 )
153 {
154 wxArrayString layers;
155 BOARD* board = m_frame->GetBoard();
156
157 for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
158 {
159 layers.Add( board->GetLayerName( layer ) );
160 }
161
162 return wxJoin( layers, ',' );
163 }
164 else if( aCol == 3 )
165 return wxString::Format( "%d", zone->GetAssignedPriority() );
166 else if( aCol == 4 )
167 return m_frame->MessageTextFromValue( zone->GetX() );
168 else if( aCol == 5 )
169 return m_frame->MessageTextFromValue( zone->GetY() );
170
171 return wxEmptyString;
172}
173
174
175void ZONE_SEARCH_HANDLER::SelectItems( std::vector<long>& aItemRows )
176{
177 std::vector<EDA_ITEM*> selectedItems;
178 for( long row : aItemRows )
179 {
180 if( row >= 0 && row < (long)m_hitlist.size() )
181 {
182 ZONE* zone = m_hitlist[row];
183 selectedItems.push_back( zone );
184 }
185 }
186
188
189 if( selectedItems.size() )
190 m_frame->GetToolManager()->RunAction( PCB_ACTIONS::selectItems, true, &selectedItems );
191
193}
194
195
197 SEARCH_HANDLER( wxT( "Text" ) ), m_frame( aFrame )
198{
199 m_columnNames.emplace_back( wxT( "Type" ) );
200 m_columnNames.emplace_back( wxT( "Text" ) );
201 m_columnNames.emplace_back( wxT( "Layer" ) );
202 m_columnNames.emplace_back( wxT( "X" ) );
203 m_columnNames.emplace_back( wxT( "Y" ) );
204}
205
206
207int TEXT_SEARCH_HANDLER::Search( const wxString& aQuery )
208{
209 m_hitlist.clear();
210 BOARD* board = m_frame->GetBoard();
211
212 EDA_SEARCH_DATA frp;
213 frp.findString = aQuery;
215
216 for( BOARD_ITEM* item : board->Drawings() )
217 {
218 PCB_TEXT* textItem = dynamic_cast<PCB_TEXT*>( item );
219 PCB_TEXTBOX* textBoxItem = dynamic_cast<PCB_TEXTBOX*>( item );
220
221 if( textItem && ( aQuery.IsEmpty() || textItem->Matches( frp, nullptr ) ) )
222 {
223 m_hitlist.push_back( textItem );
224 }
225 else if( textBoxItem && ( aQuery.IsEmpty() || textBoxItem->Matches( frp, nullptr ) ) )
226 {
227 m_hitlist.push_back( textBoxItem );
228 }
229 }
230
231 return m_hitlist.size();
232}
233
234
235wxString TEXT_SEARCH_HANDLER::GetResultCell( int aRow, int aCol )
236{
237 BOARD_ITEM* text = m_hitlist[aRow];
238
239 if( aCol == 0 )
240 {
241 if( PCB_TEXT::ClassOf( text ) )
242 {
243 return _( "Text" );
244 }
245 else if( PCB_TEXTBOX::ClassOf( text ) )
246 {
247 return _( "Textbox" );
248 }
249 }
250 else if( aCol == 1 )
251 {
252 if( PCB_TEXT::ClassOf( text ) )
253 {
254 return UnescapeString( static_cast<PCB_TEXT*>( text )->GetText() );
255 }
256 else if( PCB_TEXTBOX::ClassOf( text ) )
257 {
258 return UnescapeString( static_cast<PCB_TEXTBOX*>( text )->GetShownText() );
259 }
260 }
261 if( aCol == 2 )
262 return text->GetLayerName();
263 else if( aCol == 3 )
264 return m_frame->MessageTextFromValue( text->GetX() );
265 else if( aCol == 4 )
266 return m_frame->MessageTextFromValue( text->GetY() );
267
268 return wxEmptyString;
269}
270
271
272void TEXT_SEARCH_HANDLER::SelectItems( std::vector<long>& aItemRows )
273{
274 std::vector<EDA_ITEM*> selectedItems;
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
290}
291
292
294 SEARCH_HANDLER( wxT( "Nets" ) ), m_frame( aFrame )
295{
296 m_columnNames.emplace_back( wxT( "Name" ) );
297 m_columnNames.emplace_back( wxT( "Class" ) );
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 for( NETINFO_ITEM* net : board->GetNetInfo() )
311 {
312 if( net && ( aQuery.IsEmpty() || net->Matches( frp, nullptr ) ) )
313 {
314 m_hitlist.push_back( net );
315 }
316 }
317
318 return m_hitlist.size();
319}
320
321
322wxString NETS_SEARCH_HANDLER::GetResultCell( int aRow, int aCol )
323{
324 NETINFO_ITEM* net = m_hitlist[aRow];
325
326 if( aCol == 0 )
327 return UnescapeString( net->GetNetname() );
328 else if( aCol == 1 )
329 return net->GetNetClass()->GetName();
330
331 return wxEmptyString;
332}
333
334
335void NETS_SEARCH_HANDLER::SelectItems( std::vector<long>& aItemRows )
336{
338 ps->SetHighlight( false );
339
340 std::vector<NETINFO_ITEM*> selectedItems;
341 for( long row : aItemRows )
342 {
343 if( row >= 0 && row < (long) m_hitlist.size() )
344 {
345 NETINFO_ITEM* net = m_hitlist[row];
346
347 ps->SetHighlight( true, net->GetNetCode(), true );
348 }
349 }
350
353}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:58
int GetY() const
Definition: board_item.h:82
int GetX() const
Definition: board_item.h:76
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:265
const NETINFO_LIST & GetNetInfo() const
Definition: board.h:765
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: board.cpp:711
ZONES & Zones()
Definition: board.h:313
FOOTPRINTS & Footprints()
Definition: board.h:307
DRAWINGS & Drawings()
Definition: board.h:310
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:413
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)
PCB_EDIT_FRAME * m_frame
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:758
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:66
const wxString & GetNetname() const
Definition: netinfo.h:119
NETCLASS * GetNetClass()
Definition: netinfo.h:111
int GetNetCode() const
Definition: netinfo.h:113
int Search(const wxString &aQuery) override
std::vector< NETINFO_ITEM * > m_hitlist
PCB_EDIT_FRAME * m_frame
NETS_SEARCH_HANDLER(PCB_EDIT_FRAME *aFrame)
void SelectItems(std::vector< long > &aItemRows) override
wxString GetResultCell(int aRow, int aCol) override
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.
bool Matches(const EDA_SEARCH_DATA &aSearchData, void *aAuxData) const override
Compare the item against the search criteria in aSearchData.
Definition: pcb_textbox.h:83
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< wxString > m_columnNames
Definition: search_pane.h:46
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)
PCB_EDIT_FRAME * m_frame
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
PCB_EDIT_FRAME * m_frame
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:98
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