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 <wxPcbStruct.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 
46 #define COL_NETNAME 0
47 #define COL_NETINFO 1
48 
50 {
51 private:
52  wxString m_selection;
55 
56 public:
59 
60  // returns true if a net was selected, and its name in aName
61  bool GetNetName( wxString& aName );
62 
63 private:
64  void onCellClick( wxGridEvent& event ) override;
65  void onFilterChange( wxCommandEvent& event ) override;
66 
67  void buildNetsList();
68 };
69 
70 
71 void PCB_EDIT_FRAME::ListNetsAndSelect( wxCommandEvent& event )
72 {
73  DIALOG_SELECT_NET_FROM_LIST dlg( this );
74  wxString netname;
75 
76  if( dlg.ShowModal() == wxID_CANCEL || !dlg.GetNetName( netname ) )
77  return;
78 
79  // Search for the net selected.
80  NETINFO_ITEM* net = GetBoard()->FindNet( netname );
81 
82  if( net == NULL ) // Should not occur.
83  return;
84 
85  if( IsGalCanvasActive() )
86  {
88  render->SetHighlight( true, net->GetNet() );
89 
91  GetGalCanvas()->Refresh();
92  }
93  else
94  {
96 
97  if( GetBoard()->IsHighLightNetON() )
98  HighLight( &dc );
99 
100  GetBoard()->SetHighLightNet( net->GetNet() );
101  HighLight( &dc );
102  }
103 }
104 
105 
108 {
109  m_brd = aParent->GetBoard();
110  m_wasSelected = false;
111 
112  // Choose selection mode
113  m_netsListGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
114 
115  buildNetsList();
116 
117  m_sdbSizerOK->SetDefault();
118  GetSizer()->SetSizeHints( this );
119  Center();
120 }
121 
123 {
124  wxString netFilter = m_textCtrlFilter->GetValue();
126  filter.SetPattern( netFilter.MakeUpper() );
127  wxString txt;
128 
129  int row_idx = 0;
130 
131  // Populate the nets list with nets names matching the filters:
132  // Note: the filtering is case insensitive.
133  for( unsigned netcode = 0; netcode < m_brd->GetNetCount(); netcode++ )
134  {
135  NETINFO_ITEM* net = m_brd->GetNetInfo().GetNetItem( netcode );
136 
137  if( !netFilter.IsEmpty() )
138  {
139  wxString netname = net->GetNetname();
140  if( filter.Find( netname.MakeUpper() ) == EDA_PATTERN_NOT_FOUND )
141  continue;
142  }
143 
144  if( !m_cbShowZeroPad->IsChecked() && net->m_PadInNetList.size() == 0 )
145  continue;
146 
147  if( m_netsListGrid->GetNumberRows() <= row_idx )
148  m_netsListGrid->AppendRows( 1 );
149 
150  txt.Printf( _( "net %.3d" ), net->GetNet() );
151  m_netsListGrid->SetRowLabelValue( row_idx, txt );
152 
153  m_netsListGrid->SetCellValue( row_idx, COL_NETNAME, net->GetNetname() );
154 
155  if( netcode )
156  {
157  txt.Printf( wxT( "%u" ), (unsigned) net->m_PadInNetList.size() );
158  m_netsListGrid->SetCellValue( row_idx, COL_NETINFO, txt );
159  }
160  else // For the net 0 (unconnected pads), the pad count is not known
161  m_netsListGrid->SetCellValue( row_idx, COL_NETINFO, "---" );
162 
163  row_idx++;
164  }
165 
166  // Remove extra rows, if any:
167  int extra_row_idx = m_netsListGrid->GetNumberRows() - row_idx;
168 
169  if( extra_row_idx > 0 )
170  m_netsListGrid->DeleteRows( row_idx, extra_row_idx );
171 
172  m_netsListGrid->SetColLabelSize( wxGRID_AUTOSIZE );
173  m_netsListGrid->SetRowLabelSize( wxGRID_AUTOSIZE );
174 
175  m_netsListGrid->ClearSelection();
176  m_wasSelected = false;
177 }
178 
179 
181 {
182 }
183 
184 void DIALOG_SELECT_NET_FROM_LIST::onFilterChange( wxCommandEvent& event )
185 {
186  buildNetsList();
187 }
188 
189 
191 {
192  int selected_row = event.GetRow();
193  m_selection = m_netsListGrid->GetCellValue( selected_row, COL_NETNAME );
194  m_wasSelected = true;
195 
196  // Select the full row when clicking on any cell off the row
197  m_netsListGrid->SelectRow( selected_row, false );
198  m_netsListGrid->SetGridCursor(selected_row, COL_NETNAME );
199 }
200 
201 
203 {
204  aName = m_selection;
205  return m_wasSelected;
206 }
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.
BOARD * GetBoard() const
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
DIALOG_SELECT_NET_FROM_LIST(PCB_EDIT_FRAME *aParent)
D_PADS m_PadInNetList
List of pads connected to this net.
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:795
static const int EDA_PATTERN_NOT_FOUND
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:196
int GetNet() const
Function GetNet.
NETINFO_LIST & GetNetInfo()
Definition: class_board.h:785
#define COL_NETINFO
void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
void SetHighLightNet(int aNetCode)
Function SetHighLightNet.
Definition: class_board.h:361
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
Class NETINFO_ITEM handles the data for a net.
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:166
#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...
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:803
const wxString & GetNetname() const
Function GetNetname.
void UpdateAllLayersColor()
Function UpdateAllLayersColor() Applies the new coloring scheme to all layers.
Definition: view.cpp:673
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:139
void onCellClick(wxGridEvent &event) override
unsigned GetNetCount() const
Function GetNetCount.
Definition: class_board.h:814