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