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 
71  void buildNetsList();
72 
73  wxString m_selection;
77 };
78 
79 
80 void PCB_EDIT_FRAME::ListNetsAndSelect( wxCommandEvent& event )
81 {
82  DIALOG_SELECT_NET_FROM_LIST dlg( this );
83  wxString netname;
84 
85  if( dlg.ShowModal() == wxID_CANCEL )
86  {
87  // Clear highlight
88  dlg.HighlightNet( false, "" );
89  }
90 }
91 
92 
94  : DIALOG_SELECT_NET_FROM_LIST_BASE( aParent ), m_frame( aParent )
95 {
96  m_brd = aParent->GetBoard();
97  m_wasSelected = false;
98 
99  // Choose selection mode
100  m_netsListGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
101 
102  buildNetsList();
103 
104  m_sdbSizerOK->SetDefault();
105  GetSizer()->SetSizeHints( this );
106  Center();
107 }
108 
109 
111 {
112  wxString netFilter = m_textCtrlFilter->GetValue();
114  filter.SetPattern( netFilter.MakeUpper() );
115  wxString txt;
116 
117  int row_idx = 0;
118 
119  // Populate the nets list with nets names matching the filters:
120  // Note: the filtering is case insensitive.
121  for( unsigned netcode = 0; netcode < m_brd->GetNetCount(); netcode++ )
122  {
123  NETINFO_ITEM* net = m_brd->GetNetInfo().GetNetItem( netcode );
124 
125  if( !netFilter.IsEmpty() )
126  {
127  wxString netname = net->GetNetname();
128  if( filter.Find( netname.MakeUpper() ) == EDA_PATTERN_NOT_FOUND )
129  continue;
130  }
131 
132  unsigned nPads = m_brd->GetConnectivity()->GetPadCount( netcode );
133 
134  if( !m_cbShowZeroPad->IsChecked() && nPads == 0 )
135  continue;
136 
137  if( m_netsListGrid->GetNumberRows() <= row_idx )
138  m_netsListGrid->AppendRows( 1 );
139 
140  txt.Printf( _( "net %.3d" ), net->GetNet() );
141  m_netsListGrid->SetRowLabelValue( row_idx, txt );
142 
143  m_netsListGrid->SetCellValue( row_idx, COL_NETNAME, net->GetNetname() );
144 
145  if( netcode )
146  {
147  txt.Printf( wxT( "%u" ), nPads );
148  m_netsListGrid->SetCellValue( row_idx, COL_NETINFO, txt );
149  }
150  else // For the net 0 (unconnected pads), the pad count is not known
151  m_netsListGrid->SetCellValue( row_idx, COL_NETINFO, "---" );
152 
153  row_idx++;
154  }
155 
156  // Remove extra rows, if any:
157  int extra_row_idx = m_netsListGrid->GetNumberRows() - row_idx;
158 
159  if( extra_row_idx > 0 )
160  m_netsListGrid->DeleteRows( row_idx, extra_row_idx );
161 
162  m_netsListGrid->SetColLabelSize( wxGRID_AUTOSIZE );
163  m_netsListGrid->SetRowLabelSize( wxGRID_AUTOSIZE );
164 
165  m_netsListGrid->ClearSelection();
166  m_wasSelected = false;
167 }
168 
169 
170 void DIALOG_SELECT_NET_FROM_LIST::HighlightNet( bool aEnabled, const wxString& aNetName )
171 {
172  // Search for the net selected.
173  NETINFO_ITEM* net = aEnabled ? m_brd->FindNet( aNetName ) : nullptr;
174  int netCode = net ? net->GetNet() : -1;
175 
176  if( m_frame->IsGalCanvasActive() )
177  {
178  auto galCanvas = m_frame->GetGalCanvas();
179  KIGFX::RENDER_SETTINGS* render = galCanvas->GetView()->GetPainter()->GetSettings();
180  render->SetHighlight( aEnabled, netCode );
181 
182  galCanvas->GetView()->UpdateAllLayersColor();
183  galCanvas->Refresh();
184  }
185  else
186  {
188 
189  if( m_brd->IsHighLightNetON() )
190  m_frame->HighLight( &dc );
191 
192  m_brd->SetHighLightNet( netCode );
193  m_frame->HighLight( &dc );
194  }
195 }
196 
197 
199 {
200 }
201 
202 
203 void DIALOG_SELECT_NET_FROM_LIST::onFilterChange( wxCommandEvent& event )
204 {
205  buildNetsList();
206 }
207 
208 
210 {
211  int selected_row = event.GetRow();
212  m_selection = m_netsListGrid->GetCellValue( selected_row, COL_NETNAME );
213  m_wasSelected = true;
214 
215  // Select the full row when clicking on any cell off the row
216  m_netsListGrid->SelectRow( selected_row, false );
217  m_netsListGrid->SetGridCursor(selected_row, COL_NETNAME );
218 
219  HighlightNet( true, m_selection );
220 }
221 
222 
224 {
225  aName = m_selection;
226  return m_wasSelected;
227 }
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:342
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
Class DIALOG_SELECT_NET_FROM_LIST_BASE.
void onFilterChange(wxCommandEvent &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:874
static const int EDA_PATTERN_NOT_FOUND
int GetNet() const
Function GetNet.
Definition: netinfo.h:227
NETINFO_LIST & GetNetInfo()
Definition: class_board.h:743
#define COL_NETINFO
void SetHighLightNet(int aNetCode)
Function SetHighLightNet.
Definition: class_board.h:372
Class NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:69
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)
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:381
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:882
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
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Function GetConnectivity() returns list of missing connections between components/tracks.
Definition: class_board.h:290
unsigned GetNetCount() const
Function GetNetCount.
Definition: class_board.h:772