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 = board->FindOrAddNet( remainingName, 0 );
114
115 rebuildList();
116
117 if( newnet->GetNetCode() > 0 )
118 {
119 m_selectedNetcode = newnet->GetNetCode();
120 GetComboCtrl()->SetValue( UnescapeString( remainingName ) );
121 }
122 else
123 {
124 // This indicates that the NETINFO_ITEM was not successfully appended to the
125 // list for unknown reasons
126 board->Remove( newnet );
127 delete newnet;
128 }
129 }
130 }
131 else if( m_netinfoList )
132 {
133 NETINFO_ITEM* netInfo = m_netinfoList->GetNetItem( escapedNetName );
134
135 if( netInfo == nullptr || netInfo->GetNetCode() == 0 )
136 {
138 GetComboCtrl()->SetValue( NO_NET );
139 }
140 else
141 {
142 m_selectedNetcode = netInfo->GetNetCode();
143 GetComboCtrl()->SetValue( UnescapeString( escapedNetName ) );
144 }
145 }
146
147 wxCommandEvent changeEvent( FILTERED_ITEM_SELECTED );
148 wxPostEvent( GetComboCtrl(), changeEvent );
149 }
150
151protected:
152 void getListContent( wxArrayString& aNetnames ) override
153 {
154 if( !m_netinfoList )
155 return;
156
157 wxString netstring = getFilterValue();
158 wxString filter = netstring.Lower();
159
160 m_unescapedNetNameMap.clear();
161
162 if( !filter.IsEmpty() )
163 filter = wxT( "*" ) + filter + wxT( "*" );
164
165 for( NETINFO_ITEM* netinfo : *m_netinfoList )
166 {
167 if( netinfo->GetNetCode() > 0 && netinfo->IsCurrent() )
168 {
169 wxString netname = UnescapeString( netinfo->GetNetname() );
170
171 if( filter.IsEmpty() || wxString( netname ).MakeLower().Matches( filter ) )
172 {
173 aNetnames.push_back( netname );
174 m_unescapedNetNameMap[ netname ] = netinfo->GetNetname();
175 }
176 }
177 }
178
179 std::sort( aNetnames.begin(), aNetnames.end(),
180 []( const wxString& lhs, const wxString& rhs )
181 {
182 return StrNumCmp( lhs, rhs, CMP_T::IGNORE_CASE ) < 0;
183 } );
184
185 // Special handling for <no net>
186 if( filter.IsEmpty() || wxString( NO_NET ).MakeLower().Matches( filter ) )
187 aNetnames.insert( aNetnames.begin(), NO_NET );
188
189 if( !filter.IsEmpty() && !m_netinfoList->GetNetItem( netstring ) )
190 {
191 wxString newnet = wxString::Format( "%s: %s", CREATE_NET, netstring );
192 aNetnames.insert( aNetnames.end(), newnet );
193 }
194
195 if( !m_indeterminateLabel.IsEmpty() )
196 aNetnames.push_back( m_indeterminateLabel );
197 }
198
199protected:
203
204 std::map<wxString, wxString> m_unescapedNetNameMap;
205};
206
207
208NET_SELECTOR::NET_SELECTOR( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
209 long style ) :
210 FILTER_COMBOBOX( parent, id, pos, size, style|wxCB_READONLY )
211{
214}
215
216
217void NET_SELECTOR::SetNetInfo( const NETINFO_LIST* aNetInfoList )
218{
219 m_netSelectorPopup->SetNetInfo( aNetInfoList );
220}
221
222
223void NET_SELECTOR::SetIndeterminateString( const wxString& aString )
224{
225 m_indeterminateString = aString;
226 m_netSelectorPopup->SetIndeterminateLabel( aString );
227}
228
229
231{
232 m_netSelectorPopup->SetSelectedNetcode( aNetcode );
233 SetValue( UnescapeString( m_netSelectorPopup->GetStringValue() ) );
234}
235
236
237void NET_SELECTOR::SetSelectedNet( const wxString& aNetname )
238{
239 m_netSelectorPopup->SetSelectedNet( aNetname );
240 SetValue( UnescapeString( m_netSelectorPopup->GetStringValue() ) );
241}
242
243
245{
246 return m_netSelectorPopup->GetSelectedNetname();
247}
248
249
251{
252 m_netSelectorPopup->SetIndeterminate();
253 SetValue( m_indeterminateString );
254}
255
256
258{
259 return m_netSelectorPopup->IsIndeterminate();
260}
261
262
264{
265 return m_netSelectorPopup->GetSelectedNetcode();
266}
267
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
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:232
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)