KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_object_selector_panel.cpp
Go to the documentation of this file.
2
3#include <board.h>
7
8#include <wx/sizer.h>
9#include <wx/stattext.h>
10#include <wx/choice.h>
11#include <wx/stc/stc.h>
12#include <wx/regex.h>
13
14DRC_RE_OBJECT_SELECTOR_PANEL::DRC_RE_OBJECT_SELECTOR_PANEL( wxWindow* parent, BOARD* board, const wxString& label ) :
15 wxPanel( parent ),
16 m_customQueryCtrl( nullptr )
17{
18 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
19
20 m_label = new wxStaticText( this, wxID_ANY, label );
21 mainSizer->Add( m_label, 0, wxALL, 5 );
22
23 m_rowSizer = new wxBoxSizer( wxHORIZONTAL );
24
25 wxArrayString choices;
26 choices.Add( _( "Any" ) );
27 choices.Add( _( "Net" ) );
28 choices.Add( _( "Netclass" ) );
29 choices.Add( _( "Within Area" ) );
30 choices.Add( _( "Custom Query" ) );
31
32 m_choice = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, choices );
33 m_choice->SetSelection( 0 );
34 m_rowSizer->Add( m_choice, 0, wxALL, 5 );
35
36 m_netSelector = new NET_SELECTOR( this, wxID_ANY );
37 m_netSelector->SetNetInfo( &board->GetNetInfo() );
38 m_rowSizer->Add( m_netSelector, 1, wxALL | wxEXPAND, 5 );
39 m_netSelector->Hide();
40
41 m_netclassSelector = new NETCLASS_SELECTOR( this, wxID_ANY );
42 m_netclassSelector->SetBoard( board );
43 m_rowSizer->Add( m_netclassSelector, 1, wxALL | wxEXPAND, 5 );
44 m_netclassSelector->Hide();
45
46 m_areaSelector = new AREA_SELECTOR( this, wxID_ANY );
47 m_areaSelector->SetBoard( board );
48 m_rowSizer->Add( m_areaSelector, 1, wxALL | wxEXPAND, 5 );
49 m_areaSelector->Hide();
50
51 mainSizer->Add( m_rowSizer, 0, wxEXPAND, 0 );
52
53 SetSizer( mainSizer );
54
55 m_choice->Bind( wxEVT_CHOICE, &DRC_RE_OBJECT_SELECTOR_PANEL::onChoice, this );
56}
57
59{
60 m_label->SetLabelText( text );
61}
62
64{
65 // Just store the pointer. The control is managed by the parent panel's sizer, not ours.
66 // Do not reparent or add to sizer since the control may already be in another sizer.
67 m_customQueryCtrl = ctrl;
68}
69
70void DRC_RE_OBJECT_SELECTOR_PANEL::onChoice( const wxCommandEvent& aEvent )
71{
72 m_netSelector->Hide();
73 m_netclassSelector->Hide();
74 m_areaSelector->Hide();
75
77 m_customQueryCtrl->Hide();
78
79 switch( m_choice->GetSelection() )
80 {
81 case 1: // Net
82 m_netSelector->Show();
83 break;
84 case 2: // Netclass
85 m_netclassSelector->Show();
86 break;
87 case 3: // Within Area
88 m_areaSelector->Show();
89 break;
90 case 4: // Custom Query
92 m_customQueryCtrl->Show();
93 break;
94 default: break;
95 }
96
97 Layout();
98
101}
102
103
105{
106 return m_choice->GetSelection() == 4;
107}
108
109
110void DRC_RE_OBJECT_SELECTOR_PANEL::ParseCondition( const wxString& aExpr, const wxString& aPrefix )
111{
112 wxString expr = aExpr;
113
114 if( expr.IsEmpty() )
115 {
116 m_choice->SetSelection( 0 );
117 onChoice( wxCommandEvent() );
118
120 m_customQueryCtrl->SetValue( wxEmptyString );
121
122 return;
123 }
124
125 wxString prefix = aPrefix;
126 if( !prefix.IsEmpty() && expr.StartsWith( prefix + "." ) )
127 expr = expr.Mid( prefix.Length() + 1 );
128
129 wxRegEx netRe( wxT( "^NetName\\s*==\\s*'([^']*)'" ) );
130 wxRegEx netclassRe1( wxT( "^hasNetclass\\('([^']*)'\\)" ) );
131 wxRegEx netclassRe2( wxT( "^NetClass\\s*==\\s*'([^']*)'" ) );
132 wxRegEx areaRe( wxT( "^(?:enclosedByArea|intersectsArea)\\('([^']*)'\\)" ) );
133
134 if( netRe.Matches( expr ) )
135 {
136 m_choice->SetSelection( 1 );
137 onChoice( wxCommandEvent() );
138 m_netSelector->SetSelectedNet( netRe.GetMatch( expr, 1 ) );
139 return;
140 }
141 else if( netclassRe1.Matches( expr ) )
142 {
143 m_choice->SetSelection( 2 );
144 onChoice( wxCommandEvent() );
145 m_netclassSelector->SetSelectedNetclass( netclassRe1.GetMatch( expr, 1 ) );
146 return;
147 }
148 else if( netclassRe2.Matches( expr ) )
149 {
150 m_choice->SetSelection( 2 );
151 onChoice( wxCommandEvent() );
152 m_netclassSelector->SetSelectedNetclass( netclassRe2.GetMatch( expr, 1 ) );
153 return;
154 }
155 else if( areaRe.Matches( expr ) )
156 {
157 m_choice->SetSelection( 3 );
158 onChoice( wxCommandEvent() );
159 m_areaSelector->SetSelectedArea( areaRe.GetMatch( expr, 1 ) );
160 return;
161 }
162
163 m_choice->SetSelection( 4 );
164 onChoice( wxCommandEvent() );
166 m_customQueryCtrl->SetValue( expr );
167}
168
169
170wxString DRC_RE_OBJECT_SELECTOR_PANEL::BuildCondition( const wxString& aPrefix ) const
171{
172 wxString prefix = aPrefix;
173
174 switch( m_choice->GetSelection() )
175 {
176 case 1: // Net
177 if( m_netSelector->GetSelectedNetname().IsEmpty() )
178 return wxEmptyString;
179 return prefix + wxString::Format( ".NetName == '%s'", m_netSelector->GetSelectedNetname() );
180
181 case 2: // Netclass
182 if( m_netclassSelector->GetSelectedNetclass().IsEmpty() )
183 return wxEmptyString;
184 return prefix + wxString::Format( ".hasNetclass('%s')", m_netclassSelector->GetSelectedNetclass() );
185
186 case 3: // Within Area
187 if( m_areaSelector->GetSelectedArea().IsEmpty() )
188 return wxEmptyString;
189 return prefix + wxString::Format( ".enclosedByArea('%s')", m_areaSelector->GetSelectedArea() );
190
191 case 4: // Custom
193 return m_customQueryCtrl->GetText();
194 else
195 return wxEmptyString;
196
197 default: return wxEmptyString;
198 }
199}
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
const NETINFO_LIST & GetNetInfo() const
Definition board.h:996
void ParseCondition(const wxString &aExpr, const wxString &aPrefix=wxEmptyString)
Populate the panel controls from a rule condition expression.
void SetCustomQueryCtrl(wxStyledTextCtrl *ctrl)
void onChoice(const wxCommandEvent &aEvent)
DRC_RE_OBJECT_SELECTOR_PANEL(wxWindow *parent, BOARD *board, const wxString &label)
wxString BuildCondition(const wxString &aPrefix) const
Build a rule condition expression based on the panel state.
#define _(s)