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, 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 ),
42 { }
43
44 wxString GetStringValue() const override
45 {
46 if( m_selectedNetcode == -1 )
48
50
51 if( netInfo && netInfo->GetNetCode() > 0 )
52 return netInfo->GetNetname();
53
54 return NO_NET;
55 }
56
57 void SetNetInfo( const NETINFO_LIST* aNetInfoList )
58 {
59 m_netinfoList = aNetInfoList;
61 }
62
63 void SetIndeterminateLabel( const wxString& aIndeterminateLabel )
64 {
65 m_indeterminateLabel = aIndeterminateLabel;
67 }
68
70 bool IsIndeterminate() { return m_selectedNetcode == -1; }
71
72 void SetSelectedNetcode( int aNetcode ) { m_selectedNetcode = aNetcode; }
74
75 void SetSelectedNet( const wxString& aNetname )
76 {
77 if( m_netinfoList && m_netinfoList->GetNetItem( aNetname ) )
79 }
80
82 {
85 else
86 return wxEmptyString;
87 }
88
89 void Accept() override
90 {
91 wxString escapedNetName;
92 wxString remainingName;
93 wxString selectedNetName = getSelectedValue().value_or( wxEmptyString );
94
95 auto it = m_unescapedNetNameMap.find( selectedNetName );
96
97 if( it != m_unescapedNetNameMap.end() )
98 escapedNetName = it->second;
99 else // shouldn't happen....
100 escapedNetName = selectedNetName;
101
102 Dismiss();
103
104 if( escapedNetName.IsEmpty() || escapedNetName == m_indeterminateLabel )
105 {
107 GetComboCtrl()->SetValue( m_indeterminateLabel );
108 }
109 else if( escapedNetName == NO_NET )
110 {
112 GetComboCtrl()->SetValue( NO_NET );
113 }
114 else if( escapedNetName.StartsWith( CREATE_NET, &remainingName ) && !remainingName.IsEmpty() )
115 {
116 // Remove the first character ':' and all whitespace
117 remainingName = remainingName.Mid( 1 ).Trim().Trim( false );
118
119 if( BOARD* board = m_netinfoList->GetParent() )
120 {
121 NETINFO_ITEM *newnet = new NETINFO_ITEM( board, remainingName, 0 );
122
123 board->Add( newnet );
124 rebuildList();
125
126 if( newnet->GetNetCode() > 0 )
127 {
128 m_selectedNetcode = newnet->GetNetCode();
129 GetComboCtrl()->SetValue( UnescapeString( remainingName ) );
130 }
131 else
132 {
133 // This indicates that the NETINFO_ITEM was not successfully appended to the
134 // list for unknown reasons
135 board->Remove( newnet );
136 delete newnet;
137 }
138 }
139 }
140 else
141 {
142 NETINFO_ITEM* netInfo = m_netinfoList->GetNetItem( escapedNetName );
143
144 if( netInfo == nullptr || netInfo->GetNetCode() == 0 )
145 {
147 GetComboCtrl()->SetValue( NO_NET );
148 }
149 else
150 {
151 m_selectedNetcode = netInfo->GetNetCode();
152 GetComboCtrl()->SetValue( UnescapeString( escapedNetName ) );
153 }
154 }
155
156 wxCommandEvent changeEvent( FILTERED_ITEM_SELECTED );
157 wxPostEvent( GetComboCtrl(), changeEvent );
158 }
159
160protected:
161 void getListContent( wxArrayString& aNetnames ) override
162 {
163 wxString netstring = getFilterValue();
164 wxString filter = netstring.Lower();
165
166 m_unescapedNetNameMap.clear();
167
168 if( !filter.IsEmpty() )
169 filter = wxT( "*" ) + filter + wxT( "*" );
170
171 for( NETINFO_ITEM* netinfo : *m_netinfoList )
172 {
173 if( netinfo->GetNetCode() > 0 && netinfo->IsCurrent() )
174 {
175 wxString netname = UnescapeString( netinfo->GetNetname() );
176
177 if( filter.IsEmpty() || wxString( netname ).MakeLower().Matches( filter ) )
178 {
179 aNetnames.push_back( netname );
180 m_unescapedNetNameMap[ netname ] = netinfo->GetNetname();
181 }
182 }
183 }
184
185 std::sort( aNetnames.begin(), aNetnames.end(),
186 []( const wxString& lhs, const wxString& rhs )
187 {
188 return StrNumCmp( lhs, rhs, true /* ignore case */ ) < 0;
189 } );
190
191 // Special handling for <no net>
192 if( filter.IsEmpty() || wxString( NO_NET ).MakeLower().Matches( filter ) )
193 aNetnames.insert( aNetnames.begin(), NO_NET );
194
195 if( !filter.IsEmpty() && !m_netinfoList->GetNetItem( netstring ) )
196 {
197 wxString newnet = wxString::Format( "%s: %s", CREATE_NET, netstring );
198 aNetnames.insert( aNetnames.end(), newnet );
199 }
200
201 if( !m_indeterminateLabel.IsEmpty() )
202 aNetnames.push_back( m_indeterminateLabel );
203 }
204
205protected:
209
210 std::map<wxString, wxString> m_unescapedNetNameMap;
211};
212
213
214NET_SELECTOR::NET_SELECTOR( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
215 long style ) :
216 FILTER_COMBOBOX( parent, id, pos, size, style|wxCB_READONLY )
217{
220}
221
222
223void NET_SELECTOR::SetNetInfo( const NETINFO_LIST* aNetInfoList )
224{
225 m_netSelectorPopup->SetNetInfo( aNetInfoList );
226}
227
228
229void NET_SELECTOR::SetIndeterminateString( const wxString& aString )
230{
231 m_indeterminateString = aString;
233}
234
235
237{
240}
241
242
243void NET_SELECTOR::SetSelectedNet( const wxString& aNetname )
244{
247}
248
249
251{
253}
254
255
257{
259 SetValue( m_indeterminateString );
260}
261
262
264{
266}
267
268
270{
272}
273
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:317
A combobox that has a filterable popup.
void setFilterPopup(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
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 Accept() override
void SetNetInfo(const NETINFO_LIST *aNetInfoList)
wxString m_indeterminateString
Definition: net_selector.h:62
NET_SELECTOR_COMBOPOPUP * m_netSelectorPopup
Definition: net_selector.h:61
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)