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>
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 if( std::optional<DRC_RULE_EDITOR_UTILS::SELECTOR_CONDITION> cond =
131 {
132 switch( cond->kind )
133 {
135 m_choice->SetSelection( 1 );
136 onChoice( wxCommandEvent() );
137 m_netSelector->SetSelectedNet( cond->operand );
138 return;
139
141 m_choice->SetSelection( 2 );
142 onChoice( wxCommandEvent() );
143 m_netclassSelector->SetSelectedNetclass( cond->operand );
144 return;
145
147 m_choice->SetSelection( 3 );
148 onChoice( wxCommandEvent() );
149 m_areaSelector->SetSelectedArea( cond->operand );
150 return;
151
152 // The area choice writes enclosedByArea, so it cannot hold this test
154 break;
155 }
156 }
157
158 m_choice->SetSelection( 4 );
159 onChoice( wxCommandEvent() );
160
161 // The custom query is saved as written, so it keeps its object prefix
163 m_customQueryCtrl->SetValue( aExpr );
164}
165
166
167wxString DRC_RE_OBJECT_SELECTOR_PANEL::BuildCondition( const wxString& aPrefix ) const
168{
169 wxString prefix = aPrefix;
170
171 switch( m_choice->GetSelection() )
172 {
173 case 1: // Net
174 if( m_netSelector->GetSelectedNetname().IsEmpty() )
175 return wxEmptyString;
176 return prefix + wxString::Format( ".NetName == '%s'", m_netSelector->GetSelectedNetname() );
177
178 case 2: // Netclass
179 if( m_netclassSelector->GetSelectedNetclass().IsEmpty() )
180 return wxEmptyString;
181 return prefix + wxString::Format( ".hasNetclass('%s')", m_netclassSelector->GetSelectedNetclass() );
182
183 case 3: // Within Area
184 if( m_areaSelector->GetSelectedArea().IsEmpty() )
185 return wxEmptyString;
186 return prefix + wxString::Format( ".enclosedByArea('%s')", m_areaSelector->GetSelectedArea() );
187
188 case 4: // Custom
190 return m_customQueryCtrl->GetText();
191 else
192 return wxEmptyString;
193
194 default: return wxEmptyString;
195 }
196}
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
const NETINFO_LIST & GetNetInfo() const
Definition board.h:1217
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.
static std::optional< SELECTOR_CONDITION > ParseSelectorCondition(const wxString &aExpr)
Match a condition that a net, netclass or area selector can show.
#define _(s)