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 The 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, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <string_utils.h>
23
24#include <board.h>
25#include <netinfo.h>
26
27
28#define NO_NET _( "<no net>" )
29#define CREATE_NET _( "<create net>" )
30
31
33{
34public:
36 m_netinfoList( nullptr ),
38 { }
39
40 wxString GetStringValue() const override
41 {
42 if( !m_netinfoList || ( m_selectedNetcode == -1 ) )
44
45 NETINFO_ITEM* netInfo = m_netinfoList->GetNetItem( m_selectedNetcode );
46
47 if( netInfo && netInfo->GetNetCode() > 0 )
48 return netInfo->GetNetname();
49
50 return NO_NET;
51 }
52
53 void SetNetInfo( const NETINFO_LIST* aNetInfoList )
54 {
55 m_netinfoList = aNetInfoList;
57 }
58
59 void SetIndeterminateLabel( const wxString& aIndeterminateLabel )
60 {
61 m_indeterminateLabel = aIndeterminateLabel;
63 }
64
66 bool IsIndeterminate() { return m_selectedNetcode == -1; }
67
68 void SetSelectedNetcode( int aNetcode ) { m_selectedNetcode = aNetcode; }
70
71 void SetSelectedNet( const wxString& aNetname )
72 {
73 if( m_netinfoList && m_netinfoList->GetNetItem( aNetname ) )
74 m_selectedNetcode = m_netinfoList->GetNetItem( aNetname )->GetNetCode();
75 }
76
78 {
79 if( m_netinfoList && m_netinfoList->GetNetItem( m_selectedNetcode ) )
80 return m_netinfoList->GetNetItem( m_selectedNetcode )->GetNetname();
81 else
82 return wxEmptyString;
83 }
84
85 void Accept() override
86 {
87 wxString escapedNetName;
88 wxString remainingName;
89 wxString selectedNetName = getSelectedValue().value_or( wxEmptyString );
90
91 auto it = m_unescapedNetNameMap.find( selectedNetName );
92
93 if( it != m_unescapedNetNameMap.end() )
94 escapedNetName = it->second;
95 else // shouldn't happen....
96 escapedNetName = selectedNetName;
97
98 Dismiss();
99
100 if( escapedNetName.IsEmpty() || escapedNetName == m_indeterminateLabel )
101 {
103 GetComboCtrl()->SetValue( m_indeterminateLabel );
104 }
105 else if( escapedNetName == NO_NET )
106 {
108 GetComboCtrl()->SetValue( NO_NET );
109 }
110 else if( m_netinfoList && escapedNetName.StartsWith( CREATE_NET, &remainingName ) && !remainingName.IsEmpty() )
111 {
112 // Remove the first character ':' and all whitespace
113 remainingName = remainingName.Mid( 1 ).Trim().Trim( false );
114
115 if( BOARD* board = m_netinfoList->GetParent() )
116 {
117 NETINFO_ITEM *newnet = new NETINFO_ITEM( board, remainingName, 0 );
118
119 board->Add( newnet );
120 rebuildList();
121
122 if( newnet->GetNetCode() > 0 )
123 {
124 m_selectedNetcode = newnet->GetNetCode();
125 GetComboCtrl()->SetValue( UnescapeString( remainingName ) );
126 }
127 else
128 {
129 // This indicates that the NETINFO_ITEM was not successfully appended to the
130 // list for unknown reasons
131 board->Remove( newnet );
132 delete newnet;
133 }
134 }
135 }
136 else if( m_netinfoList )
137 {
138 NETINFO_ITEM* netInfo = m_netinfoList->GetNetItem( escapedNetName );
139
140 if( netInfo == nullptr || netInfo->GetNetCode() == 0 )
141 {
143 GetComboCtrl()->SetValue( NO_NET );
144 }
145 else
146 {
147 m_selectedNetcode = netInfo->GetNetCode();
148 GetComboCtrl()->SetValue( UnescapeString( escapedNetName ) );
149 }
150 }
151
152 wxCommandEvent changeEvent( FILTERED_ITEM_SELECTED );
153 wxPostEvent( GetComboCtrl(), changeEvent );
154 }
155
156protected:
157 void getListContent( wxArrayString& aNetnames ) override
158 {
159 if( !m_netinfoList )
160 return;
161
162 wxString netstring = getFilterValue();
163 wxString filter = netstring.Lower();
164
165 m_unescapedNetNameMap.clear();
166
167 if( !filter.IsEmpty() )
168 filter = wxT( "*" ) + filter + wxT( "*" );
169
170 for( NETINFO_ITEM* netinfo : *m_netinfoList )
171 {
172 if( netinfo->GetNetCode() > 0 && netinfo->IsCurrent() )
173 {
174 wxString netname = UnescapeString( netinfo->GetNetname() );
175
176 if( filter.IsEmpty() || wxString( netname ).MakeLower().Matches( filter ) )
177 {
178 aNetnames.push_back( netname );
179 m_unescapedNetNameMap[ netname ] = netinfo->GetNetname();
180 }
181 }
182 }
183
184 std::sort( aNetnames.begin(), aNetnames.end(),
185 []( const wxString& lhs, const wxString& rhs )
186 {
187 return StrNumCmp( lhs, rhs, true /* ignore case */ ) < 0;
188 } );
189
190 // Special handling for <no net>
191 if( filter.IsEmpty() || wxString( NO_NET ).MakeLower().Matches( filter ) )
192 aNetnames.insert( aNetnames.begin(), NO_NET );
193
194 if( !filter.IsEmpty() && !m_netinfoList->GetNetItem( netstring ) )
195 {
196 wxString newnet = wxString::Format( "%s: %s", CREATE_NET, netstring );
197 aNetnames.insert( aNetnames.end(), newnet );
198 }
199
200 if( !m_indeterminateLabel.IsEmpty() )
201 aNetnames.push_back( m_indeterminateLabel );
202 }
203
204protected:
208
209 std::map<wxString, wxString> m_unescapedNetNameMap;
210};
211
212
213NET_SELECTOR::NET_SELECTOR( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
214 long style ) :
215 FILTER_COMBOBOX( parent, id, pos, size, style|wxCB_READONLY )
216{
219}
220
221
222void NET_SELECTOR::SetNetInfo( const NETINFO_LIST* aNetInfoList )
223{
224 m_netSelectorPopup->SetNetInfo( aNetInfoList );
225}
226
227
228void NET_SELECTOR::SetIndeterminateString( const wxString& aString )
229{
230 m_indeterminateString = aString;
231 m_netSelectorPopup->SetIndeterminateLabel( aString );
232}
233
234
236{
237 m_netSelectorPopup->SetSelectedNetcode( aNetcode );
238 SetValue( UnescapeString( m_netSelectorPopup->GetStringValue() ) );
239}
240
241
242void NET_SELECTOR::SetSelectedNet( const wxString& aNetname )
243{
244 m_netSelectorPopup->SetSelectedNet( aNetname );
245 SetValue( UnescapeString( m_netSelectorPopup->GetStringValue() ) );
246}
247
248
250{
251 return m_netSelectorPopup->GetSelectedNetname();
252}
253
254
256{
257 m_netSelectorPopup->SetIndeterminate();
258 SetValue( m_indeterminateString );
259}
260
261
263{
264 return m_netSelectorPopup->IsIndeterminate();
265}
266
267
269{
270 return m_netSelectorPopup->GetSelectedNetcode();
271}
272
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
void setFilterPopup(FILTER_COMBOPOPUP *aPopup)
FILTER_COMBOBOX(wxWindow *parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0)
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:46
const wxString & GetNetname() const
Definition netinfo.h:100
int GetNetCode() const
Definition netinfo.h:94
Container for NETINFO_ITEM elements, which are the nets.
Definition netinfo.h:221
void getListContent(wxArrayString &aNetnames) override
Fill the combobox 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 SetNetInfo(const NETINFO_LIST *aNetInfoList)
wxString m_indeterminateString
NET_SELECTOR_COMBOPOPUP * m_netSelectorPopup
NET_SELECTOR(wxWindow *parent, wxWindowID id, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0)
bool IsIndeterminate()
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)