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_netinfoList || ( m_selectedNetcode == -1 ) )
48
49 NETINFO_ITEM* netInfo = m_netinfoList->GetNetItem( m_selectedNetcode );
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 ) )
78 m_selectedNetcode = m_netinfoList->GetNetItem( aNetname )->GetNetCode();
79 }
80
82 {
83 if( m_netinfoList && m_netinfoList->GetNetItem( m_selectedNetcode ) )
84 return m_netinfoList->GetNetItem( m_selectedNetcode )->GetNetname();
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( m_netinfoList && 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 if( m_netinfoList )
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 if( !m_netinfoList )
164 return;
165
166 wxString netstring = getFilterValue();
167 wxString filter = netstring.Lower();
168
169 m_unescapedNetNameMap.clear();
170
171 if( !filter.IsEmpty() )
172 filter = wxT( "*" ) + filter + wxT( "*" );
173
174 for( NETINFO_ITEM* netinfo : *m_netinfoList )
175 {
176 if( netinfo->GetNetCode() > 0 && netinfo->IsCurrent() )
177 {
178 wxString netname = UnescapeString( netinfo->GetNetname() );
179
180 if( filter.IsEmpty() || wxString( netname ).MakeLower().Matches( filter ) )
181 {
182 aNetnames.push_back( netname );
183 m_unescapedNetNameMap[ netname ] = netinfo->GetNetname();
184 }
185 }
186 }
187
188 std::sort( aNetnames.begin(), aNetnames.end(),
189 []( const wxString& lhs, const wxString& rhs )
190 {
191 return StrNumCmp( lhs, rhs, true /* ignore case */ ) < 0;
192 } );
193
194 // Special handling for <no net>
195 if( filter.IsEmpty() || wxString( NO_NET ).MakeLower().Matches( filter ) )
196 aNetnames.insert( aNetnames.begin(), NO_NET );
197
198 if( !filter.IsEmpty() && !m_netinfoList->GetNetItem( netstring ) )
199 {
200 wxString newnet = wxString::Format( "%s: %s", CREATE_NET, netstring );
201 aNetnames.insert( aNetnames.end(), newnet );
202 }
203
204 if( !m_indeterminateLabel.IsEmpty() )
205 aNetnames.push_back( m_indeterminateLabel );
206 }
207
208protected:
212
213 std::map<wxString, wxString> m_unescapedNetNameMap;
214};
215
216
217NET_SELECTOR::NET_SELECTOR( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
218 long style ) :
219 FILTER_COMBOBOX( parent, id, pos, size, style|wxCB_READONLY )
220{
223}
224
225
226void NET_SELECTOR::SetNetInfo( const NETINFO_LIST* aNetInfoList )
227{
228 m_netSelectorPopup->SetNetInfo( aNetInfoList );
229}
230
231
232void NET_SELECTOR::SetIndeterminateString( const wxString& aString )
233{
234 m_indeterminateString = aString;
235 m_netSelectorPopup->SetIndeterminateLabel( aString );
236}
237
238
240{
241 m_netSelectorPopup->SetSelectedNetcode( aNetcode );
242 SetValue( UnescapeString( m_netSelectorPopup->GetStringValue() ) );
243}
244
245
246void NET_SELECTOR::SetSelectedNet( const wxString& aNetname )
247{
248 m_netSelectorPopup->SetSelectedNet( aNetname );
249 SetValue( UnescapeString( m_netSelectorPopup->GetStringValue() ) );
250}
251
252
254{
255 return m_netSelectorPopup->GetSelectedNetname();
256}
257
258
260{
261 m_netSelectorPopup->SetIndeterminate();
262 SetValue( m_indeterminateString );
263}
264
265
267{
268 return m_netSelectorPopup->IsIndeterminate();
269}
270
271
273{
274 return m_netSelectorPopup->GetSelectedNetcode();
275}
276
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
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:54
const wxString & GetNetname() const
Definition netinfo.h:112
int GetNetCode() const
Definition netinfo.h:106
Container for NETINFO_ITEM elements, which are the nets.
Definition netinfo.h:212
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)