KiCad PCB EDA Suite
dialog_select_net_from_list.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) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2017 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 
30 #include <wx/grid.h>
31 
32 #include <fctsys.h>
33 #include <kicad_string.h>
34 #include <kicad_device_context.h>
35 #include <class_drawpanel.h>
36 #include <pcbnew.h>
37 #include <pcb_edit_frame.h>
38 #include <class_board.h>
40 #include <eda_pattern_match.h>
41 
42 #include <view/view.h>
43 #include <view/view_controls.h>
44 #include <pcb_painter.h>
45 #include <connectivity_data.h>
46 
47 #define COL_NETNAME 0
48 #define COL_NETINFO 1
49 
51 {
52 private:
53 public:
56 
57  // returns true if a net was selected, and its name in aName
58  bool GetNetName( wxString& aName );
59 
65  void HighlightNet( bool aEnabled, const wxString& aNetName );
66 
67 private:
68  void onCellClick( wxGridEvent& event ) override;
69  void onFilterChange( wxCommandEvent& event ) override;
70  void onColumnResize( wxGridSizeEvent& event ) override;
71  void onSelectCell( wxGridEvent& event ) override;
72  void updateSize( wxSizeEvent& event ) override;
73 
74  void setColumnSize();
75  void buildNetsList();
76 
77  wxString m_selection;
82 };
83 
84 
85 void PCB_EDIT_FRAME::ListNetsAndSelect( wxCommandEvent& event )
86 {
87  DIALOG_SELECT_NET_FROM_LIST dlg( this );
88  wxString netname;
89 
90  if( dlg.ShowModal() == wxID_CANCEL )
91  {
92  // Clear highlight
93  dlg.HighlightNet( false, "" );
94  }
95 }
96 
97 
99  : DIALOG_SELECT_NET_FROM_LIST_BASE( aParent ), m_frame( aParent )
100 {
101  m_brd = aParent->GetBoard();
102  m_wasSelected = false;
103 
104  // Choose selection mode
105  m_netsListGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
106  buildNetsList();
107 
108  m_sdbSizerOK->SetDefault();
109  GetSizer()->SetSizeHints( this );
110  Center();
111 
112  m_firstWidth = m_netsListGrid->GetColSize( 0 );
113  setColumnSize();
114 }
115 
116 
118 {
119  wxString netFilter = m_textCtrlFilter->GetValue();
121  filter.SetPattern( netFilter.MakeUpper() );
122  wxString txt;
123 
124  int row_idx = 0;
125 
126  // Populate the nets list with nets names matching the filters:
127  // Note: the filtering is case insensitive.
128  for( unsigned netcode = 0; netcode < m_brd->GetNetCount(); netcode++ )
129  {
130  NETINFO_ITEM* net = m_brd->GetNetInfo().GetNetItem( netcode );
131 
132  if( !netFilter.IsEmpty() )
133  {
134  wxString netname = net->GetNetname();
135  if( filter.Find( netname.MakeUpper() ) == EDA_PATTERN_NOT_FOUND )
136  continue;
137  }
138 
139  unsigned nodes = m_brd->GetNodesCount( netcode );
140 
141  if( !m_cbShowZeroPad->IsChecked() && nodes == 0 )
142  continue;
143 
144  if( m_netsListGrid->GetNumberRows() <= row_idx )
145  m_netsListGrid->AppendRows( 1 );
146 
147  txt.Printf( _( "net %.3d" ), net->GetNet() );
148  m_netsListGrid->SetRowLabelValue( row_idx, txt );
149 
150  m_netsListGrid->SetCellValue( row_idx, COL_NETNAME, net->GetNetname() );
151 
152  if( netcode )
153  {
154  txt.Printf( wxT( "%u" ), nodes );
155  m_netsListGrid->SetCellValue( row_idx, COL_NETINFO, txt );
156  }
157  else // For the net 0 (unconnected pads), the pad count is not known
158  m_netsListGrid->SetCellValue( row_idx, COL_NETINFO, "---" );
159 
160  row_idx++;
161  }
162 
163  // Remove extra rows, if any:
164  int extra_row_idx = m_netsListGrid->GetNumberRows() - row_idx;
165 
166  if( extra_row_idx > 0 )
167  m_netsListGrid->DeleteRows( row_idx, extra_row_idx );
168 
169  m_netsListGrid->SetColLabelSize( wxGRID_AUTOSIZE );
170  m_netsListGrid->SetRowLabelSize( wxGRID_AUTOSIZE );
171 
172  m_netsListGrid->ClearSelection();
173  m_wasSelected = false;
174 }
175 
176 
177 void DIALOG_SELECT_NET_FROM_LIST::HighlightNet( bool aEnabled, const wxString& aNetName )
178 {
179  // Search for the net selected.
180  NETINFO_ITEM* net = aEnabled ? m_brd->FindNet( aNetName ) : nullptr;
181  int netCode = net ? net->GetNet() : -1;
182 
183  if( m_frame->IsGalCanvasActive() )
184  {
185  auto galCanvas = m_frame->GetGalCanvas();
186  KIGFX::RENDER_SETTINGS* render = galCanvas->GetView()->GetPainter()->GetSettings();
187  render->SetHighlight( aEnabled, netCode );
188 
189  galCanvas->GetView()->UpdateAllLayersColor();
190  galCanvas->Refresh();
191  }
192  else
193  {
195 
196  if( m_brd->IsHighLightNetON() )
197  m_frame->HighLight( &dc );
198 
199  m_brd->SetHighLightNet( netCode );
200  m_frame->HighLight( &dc );
201  }
202 }
203 
205 {
206  auto size = m_netsListGrid->GetGridWindow()->GetSize();
207 
208  m_netsListGrid->SetColSize( 0, m_firstWidth );
209  m_netsListGrid->SetColSize( 1, size.x - m_firstWidth );
210 }
211 
213 {
214 }
215 
216 
217 void DIALOG_SELECT_NET_FROM_LIST::onFilterChange( wxCommandEvent& event )
218 {
219  buildNetsList();
220 }
221 
222 
223 void DIALOG_SELECT_NET_FROM_LIST::updateSize( wxSizeEvent& event )
224 {
225  setColumnSize();
226  this->Refresh();
227  event.Skip();
228 }
229 
230 
231 void DIALOG_SELECT_NET_FROM_LIST::onColumnResize( wxGridSizeEvent& event )
232 {
233  m_firstWidth = m_netsListGrid->GetColSize( 0 );
234  setColumnSize();
235 }
236 
237 
239 {
240 
241  int selected_row = event.GetRow();
242  m_selection = m_netsListGrid->GetCellValue( selected_row, COL_NETNAME );
243 
244  // Select the full row when clicking on any cell off the row
245  m_netsListGrid->SelectRow( selected_row, false );
246 
247  HighlightNet( true, m_selection );
248 }
249 
250 
252 {
253  int selected_row = event.GetRow();
254 
255  m_selection = m_netsListGrid->GetCellValue( selected_row, COL_NETNAME );
256  m_wasSelected = true;
257 
258  // Select the full row when clicking on any cell off the row
259  m_netsListGrid->SelectRow( selected_row, false );
260  m_netsListGrid->SetGridCursor(selected_row, COL_NETNAME );
261 
262  HighlightNet( true, m_selection );
263 }
264 
265 
267 {
268  aName = m_selection;
269  return m_wasSelected;
270 }
Class RENDER_SETTINGS Contains all the knowledge about how graphical objects are drawn on any output ...
Definition: painter.h:56
void HighlightNet(bool aEnabled, const wxString &aNetName)
Visually highlights a net.
EDA_DRAW_PANEL * GetCanvas()
Definition: draw_frame.h:359
VIEW_CONTROLS class definition.
Class BOARD to handle a board.
NETINFO_ITEM * GetNetItem(int aNetCode) const
Function GetItem.
BOARD * GetBoard() const
DIALOG_SELECT_NET_FROM_LIST(PCB_EDIT_FRAME *aParent)
virtual bool SetPattern(const wxString &aPattern) override
Set the pattern against which candidates will be matched.
Abstract pattern-matching tool and implementations.
a helper to handle the real device context used in KiCad
unsigned GetNodesCount(int aNet=-1)
Function GetNodesCount.
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
Class DIALOG_SELECT_NET_FROM_LIST_BASE.
void onFilterChange(wxCommandEvent &event) override
void onSelectCell(wxGridEvent &event) override
void HighLight(wxDC *DC)
Function HighLight.
Definition: highlight.cpp:101
bool IsGalCanvasActive() const
Function IsGalCanvasActive is used to check which canvas (GAL-based or standard) is currently in use...
Definition: draw_frame.h:895
static const int EDA_PATTERN_NOT_FOUND
int GetNet() const
Function GetNet.
Definition: netinfo.h:227
NETINFO_LIST & GetNetInfo()
Definition: class_board.h:747
#define COL_NETINFO
void SetHighLightNet(int aNetCode)
Function SetHighLightNet.
Definition: class_board.h:375
Class NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:69
void updateSize(wxSizeEvent &event) override
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:169
#define COL_NETNAME
#define INSTALL_UNBUFFERED_DC(name, parent)
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
NETINFO_ITEM * FindNet(int aNetcode) const
Function FindNet searches for a net with the given netcode.
void ListNetsAndSelect(wxCommandEvent &event)
Function ListNetsAndSelect called by a command event displays the sorted list of nets in a dialog fra...
bool IsHighLightNetON() const
Function IsHighLightNetON.
Definition: class_board.h:384
virtual int Find(const wxString &aCandidate) const override
Return the location of a match iff a given candidate string matches the set pattern.
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Function GetGalCanvas returns a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:903
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:235
void SetHighlight(bool aEnabled, int aNetcode=-1)
Function SetHighlight Turns on/off highlighting - it may be done for the active layer or the specifie...
Definition: painter.h:140
void onCellClick(wxGridEvent &event) override
void onColumnResize(wxGridSizeEvent &event) override
unsigned GetNetCount() const
Function GetNetCount.
Definition: class_board.h:776