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 <fctsys.h>
31 #include <kicad_string.h>
32 #include <kicad_device_context.h>
33 #include <class_drawpanel.h>
34 #include <pcbnew.h>
35 #include <pcb_edit_frame.h>
36 #include <class_board.h>
38 #include <eda_pattern_match.h>
39 
40 #include <view/view.h>
41 #include <view/view_controls.h>
42 #include <pcb_painter.h>
44 
46 {
47 private:
48 public:
51 
52  // returns true if a net was selected, and its name in aName
53  bool GetNetName( wxString& aName );
54 
60  void HighlightNet( const wxString& aNetName );
61 
62 private:
63  void onSelChanged( wxDataViewEvent& event ) override;
64  void onFilterChange( wxCommandEvent& event ) override;
65  void onListSize( wxSizeEvent& event ) override;
66 
67  void buildNetsList();
68  wxString getListColumnHeaderNet() { return _( "Net" ); };
69  wxString getListColumnHeaderName() { return _( "Name" ); };
70  wxString getListColumnHeaderCount() { return _( "Pad Count" ); };
71  void adjustListColumns( int aWidth );
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( "" );
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  m_netsList->AppendTextColumn( getListColumnHeaderNet(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_LEFT, 0 );
100  m_netsList->AppendTextColumn( getListColumnHeaderName(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_LEFT, 0 );
101  m_netsList->AppendTextColumn( getListColumnHeaderCount(), wxDATAVIEW_CELL_INERT, 0, wxALIGN_CENTER, 0 );
102 
103  // The fact that we're a list should keep the control from reserving space for the
104  // expander buttons... but it doesn't. Fix by forcing the indent to 0.
105  m_netsList->SetIndent( 0 );
106 
107  buildNetsList();
108 
109  adjustListColumns( wxCOL_WIDTH_AUTOSIZE );
110 
111  m_sdbSizerOK->SetDefault();
112 
114 }
115 
116 
118 {
119  wxString netFilter = m_textCtrlFilter->GetValue();
121 
122  filter.SetPattern( netFilter.MakeUpper() );
123 
124  m_netsList->DeleteAllItems();
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  wxVector<wxVariant> dataLine;
145 
146  dataLine.push_back( wxVariant( wxString::Format( "%.3d", netcode ) ) );
147  dataLine.push_back( wxVariant( net->GetNetname() ) );
148 
149  if( netcode )
150  dataLine.push_back( wxVariant( wxString::Format( "%u", nodes ) ) );
151  else // For the net 0 (unconnected pads), the pad count is not known
152  dataLine.push_back( wxVariant( wxT( "---" ) ) );
153 
154  m_netsList->AppendItem( dataLine );
155  }
156 
157  m_wasSelected = false;
158 }
159 
160 
161 void DIALOG_SELECT_NET_FROM_LIST::HighlightNet( const wxString& aNetName )
162 {
163  NETINFO_ITEM* net = nullptr;
164  int netCode = -1;
165 
166  if( !aNetName.IsEmpty() )
167  {
168  net = m_brd->FindNet( aNetName );
169  netCode = net->GetNet();
170  }
171 
172  if( m_frame->IsGalCanvasActive() )
173  {
174  auto galCanvas = m_frame->GetGalCanvas();
175  KIGFX::RENDER_SETTINGS* render = galCanvas->GetView()->GetPainter()->GetSettings();
176  render->SetHighlight( netCode >= 0, netCode );
177 
178  galCanvas->GetView()->UpdateAllLayersColor();
179  galCanvas->Refresh();
180  }
181  else
182  {
184 
185  if( m_brd->IsHighLightNetON() )
186  m_frame->HighLight( &dc );
187 
188  m_brd->SetHighLightNet( netCode );
189  m_frame->HighLight( &dc );
190  }
191 }
192 
193 
195 {
196 }
197 
198 
199 void DIALOG_SELECT_NET_FROM_LIST::onFilterChange( wxCommandEvent& event )
200 {
201  buildNetsList();
202 }
203 
204 
206 {
207  int selected_row = m_netsList->GetSelectedRow();
208 
209  if( selected_row >= 0 )
210  {
211  m_selection = m_netsList->GetTextValue( selected_row, 1 );
212  m_wasSelected = true;
213 
215  }
216  else
217  {
218  HighlightNet( "" );
219  m_wasSelected = false;
220  }
221 }
222 
223 
225 {
226  int w0, w1, w2;
227 
228  if( aWidth == wxCOL_WIDTH_AUTOSIZE )
229  {
237  wxClientDC dc( GetParent() );
238  int h, minw;
239 
240  aWidth = m_netsList->GetRect().GetWidth();
241 
242  dc.GetTextExtent( getListColumnHeaderNet(), &w0, &h );
243  dc.GetTextExtent( getListColumnHeaderCount(), &w2, &h );
244  dc.GetTextExtent( "0000", &minw, &h );
245 
246  // Considering left and right margins.
247  // For wxRanderGeneric it is 5px.
248  w0 = std::max( w0+10, minw);
249  w2 = std::max( w2+10, minw);
250 
251  m_netsList->GetColumn( 0 )->SetWidth( w0 );
252  m_netsList->GetColumn( 2 )->SetWidth( w2 );
253  }
254  else
255  {
256  w0 = m_netsList->GetColumn( 0 )->GetWidth();
257  w2 = m_netsList->GetColumn( 2 )->GetWidth();
258  }
259 
260  // At resizing of the list the width of middle column (Name) changes only.
261  w1 = aWidth - w0 - w2;
262 
263  m_netsList->GetColumn( 1 )->SetWidth( w1 );
264 }
265 
266 
267 void DIALOG_SELECT_NET_FROM_LIST::onListSize( wxSizeEvent& aEvent )
268 {
269  adjustListColumns( aEvent.GetSize().GetX() );
270 }
271 
272 
274 {
275  aName = m_selection;
276  return m_wasSelected;
277 }
virtual EDA_DRAW_PANEL * GetCanvas() const
Definition: draw_frame.h:388
Class RENDER_SETTINGS Contains all the knowledge about how graphical objects are drawn on any output ...
Definition: painter.h:56
VIEW_CONTROLS class definition.
Class BOARD to handle a board.
NETINFO_ITEM * GetNetItem(int aNetCode) const
Function GetItem.
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
BOARD * GetBoard() const
void onListSize(wxSizeEvent &event) override
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
void onSelChanged(wxDataViewEvent &event) override
unsigned GetNodesCount(int aNet=-1)
Function GetNodesCount.
Class DIALOG_SELECT_NET_FROM_LIST_BASE.
void onFilterChange(wxCommandEvent &event) override
void HighLight(wxDC *DC)
Function HighLight.
Definition: highlight.cpp:105
bool IsGalCanvasActive() const
Function IsGalCanvasActive is used to check which canvas (GAL-based or standard) is currently in use...
Definition: draw_frame.h:911
static const int EDA_PATTERN_NOT_FOUND
int GetNet() const
Function GetNet.
Definition: netinfo.h:231
NETINFO_LIST & GetNetInfo()
Definition: class_board.h:750
void HighlightNet(const wxString &aNetName)
Visually highlights a net.
void SetHighLightNet(int aNetCode)
Function SetHighLightNet.
Definition: class_board.h:378
Class NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:69
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define max(a, b)
Definition: auxiliary.h:86
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
#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:387
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
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:918
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:239
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
unsigned GetNetCount() const
Function GetNetCount.
Definition: class_board.h:779