KiCad PCB EDA Suite
Loading...
Searching...
No Matches
net_selector.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) 2018-2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <string_utils.h>
27
28#include <board.h>
29#include <netinfo.h>
30
31
32#define NO_NET _( "<no net>" )
33#define CREATE_NET _( "<create net>" )
34
35
37{
38public:
40 m_netinfoList( nullptr ),
41 m_board( nullptr ),
43 { }
44
45 wxString GetStringValue() const override
46 {
47 if( m_selectedNetcode == -1 )
49
51
52 if( netInfo && netInfo->GetNetCode() > 0 )
53 return netInfo->GetNetname();
54
55 return NO_NET;
56 }
57
58 void SetNetInfo( const NETINFO_LIST* aNetInfoList )
59 {
60 m_netinfoList = aNetInfoList;
62 }
63
64 void SetIndeterminateLabel( const wxString& aIndeterminateLabel )
65 {
66 m_indeterminateLabel = aIndeterminateLabel;
68 }
69
70 void SetBoard( BOARD* aBoard )
71 {
72 m_board = aBoard;
73 }
74
76 bool IsIndeterminate() { return m_selectedNetcode == -1; }
77
78 void SetSelectedNetcode( int aNetcode ) { m_selectedNetcode = aNetcode; }
80
81 void SetSelectedNet( const wxString& aNetname )
82 {
83 if( m_netinfoList && m_netinfoList->GetNetItem( aNetname ) )
85 }
86
88 {
91 else
92 return wxEmptyString;
93 }
94
95 void Accept() override
96 {
97 wxString escapedNetName;
98 wxString remainingName;
99 wxString selectedNetName = getSelectedValue().value_or( wxEmptyString );
100
101 auto it = m_unescapedNetNameMap.find( selectedNetName );
102
103 if( it != m_unescapedNetNameMap.end() )
104 escapedNetName = it->second;
105 else // shouldn't happen....
106 escapedNetName = selectedNetName;
107
108 Dismiss();
109
110 if( escapedNetName.IsEmpty() || escapedNetName == m_indeterminateLabel )
111 {
113 GetComboCtrl()->SetValue( m_indeterminateLabel );
114 }
115 else if( escapedNetName == NO_NET )
116 {
118 GetComboCtrl()->SetValue( NO_NET );
119 }
120 else if( escapedNetName.StartsWith( CREATE_NET, &remainingName ) &&
121 !remainingName.IsEmpty() )
122 {
123 // Remove the first character ':' and all whitespace
124 remainingName = remainingName.Mid( 1 ).Trim().Trim( false );
125
126 BOARD* board = m_netinfoList->GetParent();
127 NETINFO_ITEM *newnet = new NETINFO_ITEM( m_board, remainingName, 0 );
128
129 wxASSERT( board );
130
131 if( board )
132 board->Add( newnet );
133
134 rebuildList();
135
136 if( newnet->GetNetCode() > 0 )
137 {
138 m_selectedNetcode = newnet->GetNetCode();
139 GetComboCtrl()->SetValue( UnescapeString( remainingName ) );
140 }
141 else
142 {
143 // This indicates that the NETINFO_ITEM was not successfully appended to the
144 // list for unknown reasons
145 if( board )
146 board->Remove( newnet );
147
148 delete newnet;
149 }
150 }
151 else
152 {
153 NETINFO_ITEM* netInfo = m_netinfoList->GetNetItem( escapedNetName );
154
155 if( netInfo == nullptr || netInfo->GetNetCode() == 0 )
156 {
158 GetComboCtrl()->SetValue( NO_NET );
159 }
160 else
161 {
162 m_selectedNetcode = netInfo->GetNetCode();
163 GetComboCtrl()->SetValue( UnescapeString( escapedNetName ) );
164 }
165 }
166
167 wxCommandEvent changeEvent( FILTERED_ITEM_SELECTED );
168 wxPostEvent( GetComboCtrl(), changeEvent );
169 }
170
171protected:
172 void getListContent( wxArrayString& aNetnames ) override
173 {
174 wxString netstring = getFilterValue();
175 wxString filter = netstring.Lower();
176
177 m_unescapedNetNameMap.clear();
178
179 if( !filter.IsEmpty() )
180 filter = wxT( "*" ) + filter + wxT( "*" );
181
182 for( NETINFO_ITEM* netinfo : *m_netinfoList )
183 {
184 if( netinfo->GetNetCode() > 0 && netinfo->IsCurrent() )
185 {
186 wxString netname = UnescapeString( netinfo->GetNetname() );
187
188 if( filter.IsEmpty() || wxString( netname ).MakeLower().Matches( filter ) )
189 {
190 aNetnames.push_back( netname );
191 m_unescapedNetNameMap[ netname ] = netinfo->GetNetname();
192 }
193 }
194 }
195
196 std::sort( aNetnames.begin(), aNetnames.end(),
197 []( const wxString& lhs, const wxString& rhs )
198 {
199 return StrNumCmp( lhs, rhs, true /* ignore case */ ) < 0;
200 } );
201
202 // Special handling for <no net>
203 if( filter.IsEmpty() || wxString( NO_NET ).MakeLower().Matches( filter ) )
204 aNetnames.insert( aNetnames.begin(), NO_NET );
205
206 if( !filter.IsEmpty() && !m_netinfoList->GetNetItem( netstring ) )
207 {
208 wxString newnet = wxString::Format( "%s: %s", CREATE_NET, netstring );
209 aNetnames.insert( aNetnames.end(), newnet );
210 }
211
212 if( !m_indeterminateLabel.IsEmpty() )
213 aNetnames.push_back( m_indeterminateLabel );
214 }
215
219
221
222 std::map<wxString, wxString> m_unescapedNetNameMap;
223};
224
225
226NET_SELECTOR::NET_SELECTOR( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
227 long style ) :
228 FILTER_COMBOBOX( parent, id, pos, size, style )
229{
230 std::unique_ptr<NET_SELECTOR_COMBOPOPUP> popup = std::make_unique<NET_SELECTOR_COMBOPOPUP>();
231 m_netSelectorPopup = popup.get();
232 setFilterPopup( std::move( popup ) );
233}
234
235
236void NET_SELECTOR::SetNetInfo( const NETINFO_LIST* aNetInfoList )
237{
238 m_netSelectorPopup->SetNetInfo( aNetInfoList );
239}
240
241
242void NET_SELECTOR::SetIndeterminateString( const wxString& aString )
243{
244 m_indeterminateString = aString;
246}
247
248
250{
251 m_netSelectorPopup->SetBoard( aBoard );
252}
253
254
256{
259}
260
261
262void NET_SELECTOR::SetSelectedNet( const wxString& aNetname )
263{
266}
267
268
270{
272}
273
274
276{
278 SetValue( m_indeterminateString );
279}
280
281
283{
285}
286
287
289{
291}
292
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: board.cpp:1003
void Remove(BOARD_ITEM *aBoardItem, REMOVE_MODE aMode=REMOVE_MODE::NORMAL) override
Removes an item from the container.
Definition: board.cpp:1137
A combobox that has a filterable popup.
void setFilterPopup(std::unique_ptr< FILTER_COMBOPOPUP > aPopup)
void rebuildList()
Call this to rebuild the list from the getListContent() method.
wxString getFilterValue() const
Get the current value of the filter control.
std::optional< wxString > getSelectedValue() const
Get the currently selected value in the list, or std::nullopt.
Handle the data for a net.
Definition: netinfo.h:56
const wxString & GetNetname() const
Definition: netinfo.h:114
int GetNetCode() const
Definition: netinfo.h:108
Container for NETINFO_ITEM elements, which are the nets.
Definition: netinfo.h:346
BOARD * GetParent() const
Definition: netinfo.h:463
NETINFO_ITEM * GetNetItem(int aNetCode) const
void getListContent(wxArrayString &aNetnames) override
Implement this to fill in the given list.
void SetSelectedNet(const wxString &aNetname)
const NETINFO_LIST * m_netinfoList
std::map< wxString, wxString > m_unescapedNetNameMap
void SetIndeterminateLabel(const wxString &aIndeterminateLabel)
wxString GetStringValue() const override
void SetSelectedNetcode(int aNetcode)
void SetNetInfo(const NETINFO_LIST *aNetInfoList)
void SetBoard(BOARD *aBoard)
void Accept() override
void SetNetInfo(const NETINFO_LIST *aNetInfoList)
wxString m_indeterminateString
Definition: net_selector.h:61
NET_SELECTOR_COMBOPOPUP * m_netSelectorPopup
Definition: net_selector.h:60
NET_SELECTOR(wxWindow *parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0)
bool IsIndeterminate()
void SetBoard(BOARD *aBoard)
int GetSelectedNetcode()
void SetIndeterminateString(const wxString &aString)
void SetSelectedNetcode(int aNetcode)
void SetIndeterminate()
void SetSelectedNet(const wxString &aNetname)
wxString GetSelectedNetname()
#define CREATE_NET
#define NO_NET
wxString UnescapeString(const wxString &aSource)