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 #include <connectivity.h>
46 
47 #define COL_NETNAME 0
48 #define COL_NETINFO 1
49 
51 {
52 private:
53  wxString m_selection;
56 
57 public:
60 
61  // returns true if a net was selected, and its name in aName
62  bool GetNetName( wxString& aName );
63 
64 private:
65  void onCellClick( wxGridEvent& event ) override;
66  void onFilterChange( wxCommandEvent& event ) override;
67 
68  void buildNetsList();
69 };
70 
71 
72 void PCB_EDIT_FRAME::ListNetsAndSelect( wxCommandEvent& event )
73 {
74  DIALOG_SELECT_NET_FROM_LIST dlg( this );
75  wxString netname;
76 
77  if( dlg.ShowModal() == wxID_CANCEL || !dlg.GetNetName( netname ) )
78  return;
79 
80  // Search for the net selected.
81  NETINFO_ITEM* net = GetBoard()->FindNet( netname );
82 
83  if( net == NULL ) // Should not occur.
84  return;
85 
86  if( IsGalCanvasActive() )
87  {
89  render->SetHighlight( true, net->GetNet() );
90 
92  GetGalCanvas()->Refresh();
93  }
94  else
95  {
97 
98  if( GetBoard()->IsHighLightNetON() )
99  HighLight( &dc );
100 
101  GetBoard()->SetHighLightNet( net->GetNet() );
102  HighLight( &dc );
103  }
104 }
105 
106 
109 {
110  m_brd = aParent->GetBoard();
111  m_wasSelected = false;
112 
113  // Choose selection mode
114  m_netsListGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
115 
116  buildNetsList();
117 
118  m_sdbSizerOK->SetDefault();
119  GetSizer()->SetSizeHints( this );
120  Center();
121 }
122 
124 {
125  wxString netFilter = m_textCtrlFilter->GetValue();
127  filter.SetPattern( netFilter.MakeUpper() );
128  wxString txt;
129 
130  int row_idx = 0;
131 
132  // Populate the nets list with nets names matching the filters:
133  // Note: the filtering is case insensitive.
134  for( unsigned netcode = 0; netcode < m_brd->GetNetCount(); netcode++ )
135  {
136  NETINFO_ITEM* net = m_brd->GetNetInfo().GetNetItem( netcode );
137 
138  if( !netFilter.IsEmpty() )
139  {
140  wxString netname = net->GetNetname();
141  if( filter.Find( netname.MakeUpper() ) == EDA_PATTERN_NOT_FOUND )
142  continue;
143  }
144 
145  unsigned nPads = m_brd->GetConnectivity()->GetPadCount( netcode );
146 
147  if( !m_cbShowZeroPad->IsChecked() && nPads == 0 )
148  continue;
149 
150  if( m_netsListGrid->GetNumberRows() <= row_idx )
151  m_netsListGrid->AppendRows( 1 );
152 
153  txt.Printf( _( "net %.3d" ), net->GetNet() );
154  m_netsListGrid->SetRowLabelValue( row_idx, txt );
155 
156  m_netsListGrid->SetCellValue( row_idx, COL_NETNAME, net->GetNetname() );
157 
158  if( netcode )
159  {
160  txt.Printf( wxT( "%u" ), nPads );
161  m_netsListGrid->SetCellValue( row_idx, COL_NETINFO, txt );
162  }
163  else // For the net 0 (unconnected pads), the pad count is not known
164  m_netsListGrid->SetCellValue( row_idx, COL_NETINFO, "---" );
165 
166  row_idx++;
167  }
168 
169  // Remove extra rows, if any:
170  int extra_row_idx = m_netsListGrid->GetNumberRows() - row_idx;
171 
172  if( extra_row_idx > 0 )
173  m_netsListGrid->DeleteRows( row_idx, extra_row_idx );
174 
175  m_netsListGrid->SetColLabelSize( wxGRID_AUTOSIZE );
176  m_netsListGrid->SetRowLabelSize( wxGRID_AUTOSIZE );
177 
178  m_netsListGrid->ClearSelection();
179  m_wasSelected = false;
180 }
181 
182 
184 {
185 }
186 
187 void DIALOG_SELECT_NET_FROM_LIST::onFilterChange( wxCommandEvent& event )
188 {
189  buildNetsList();
190 }
191 
192 
194 {
195  int selected_row = event.GetRow();
196  m_selection = m_netsListGrid->GetCellValue( selected_row, COL_NETNAME );
197  m_wasSelected = true;
198 
199  // Select the full row when clicking on any cell off the row
200  m_netsListGrid->SelectRow( selected_row, false );
201  m_netsListGrid->SetGridCursor(selected_row, COL_NETNAME );
202 }
203 
204 
206 {
207  aName = m_selection;
208  return m_wasSelected;
209 }
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)
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:809
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:757
#define COL_NETINFO
void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
void SetHighLightNet(int aNetCode)
Function SetHighLightNet.
Definition: class_board.h:364
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.
Definition: class_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...
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:817
const wxString & GetNetname() const
Function GetNetname.
void UpdateAllLayersColor()
Function UpdateAllLayersColor() Applies the new coloring scheme to all layers.
Definition: view.cpp:674
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:786