KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_condition_row_panel.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 (C) 2024 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 <board.h>
26#include <bitmaps.h>
27#include <widgets/ui_common.h>
28
29#include <algorithm>
30
31#include <wx/sizer.h>
32#include <wx/choice.h>
33#include <wx/bmpbuttn.h>
34#include <wx/stc/stc.h>
35#include <scintilla_tricks.h>
37#include <wx/log.h>
38
39static constexpr const wxChar* TRACE_COND = wxT( "KI_TRACE_DRC_RULE_EDITOR" );
40
41
43 bool aShowObjectSelector ) :
44 wxPanel( aParent ),
45 m_showObjectSelector( aShowObjectSelector )
46{
47 wxLogTrace( TRACE_COND, wxS( "[DRC_RE_CONDITION_ROW_PANEL] ctor START" ) );
48
49 // Outer horizontal sizer: content on left, delete button on right
50 m_mainSizer = new wxBoxSizer( wxHORIZONTAL );
51
52 // Content sizer holds row of dropdowns and custom query text below
53 m_contentSizer = new wxBoxSizer( wxVERTICAL );
54 m_rowSizer = new wxBoxSizer( wxHORIZONTAL );
55
56 // Object selector (A/B) - only shown for two-object constraints
57 wxArrayString objectChoices;
58 objectChoices.Add( _( "Object A" ) );
59 objectChoices.Add( _( "Object B" ) );
60
61 m_objectChoice = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, objectChoices );
62 m_objectChoice->SetSelection( 0 );
63 m_objectChoice->SetToolTip( _( "Which of the two compared objects this condition applies to" ) );
64
65 // The best size ellipsises these labels, so fit the widest plus room for the drop-down arrow
66 int widest = 0;
67
68 for( const wxString& choice : objectChoices )
69 widest = std::max( widest, KIUI::GetTextSize( choice, m_objectChoice ).x );
70
71 widest += m_objectChoice->FromDIP( 34 );
72
73 m_objectChoice->SetMinSize( wxSize( std::max( widest, m_objectChoice->GetBestSize().x ), -1 ) );
74 m_rowSizer->Add( m_objectChoice, 0, wxALL | wxALIGN_CENTER_VERTICAL, 2 );
75
76 if( !aShowObjectSelector )
77 m_objectChoice->Hide();
78
79 // Condition type dropdown
80 wxArrayString conditionChoices;
81 conditionChoices.Add( _( "Any" ) );
82 conditionChoices.Add( _( "Net" ) );
83 conditionChoices.Add( _( "Netclass" ) );
84 conditionChoices.Add( _( "Within Area" ) );
85 conditionChoices.Add( _( "Custom Query" ) );
86
87 m_conditionChoice = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, conditionChoices );
88 m_conditionChoice->SetSelection( 0 );
89 m_conditionChoice->SetToolTip( _( "How items are matched by this condition" ) );
90 m_rowSizer->Add( m_conditionChoice, 0, wxALL | wxALIGN_CENTER_VERTICAL, 2 );
91
92 // Net selector
93 m_netSelector = new NET_SELECTOR( this, wxID_ANY );
94 m_netSelector->SetNetInfo( &aBoard->GetNetInfo() );
95 m_netSelector->SetToolTip( _( "Net an item must belong to" ) );
96 m_rowSizer->Add( m_netSelector, 1, wxALL | wxEXPAND, 2 );
97 m_netSelector->Hide();
98
99 // Netclass selector
100 m_netclassSelector = new NETCLASS_SELECTOR( this, wxID_ANY );
101 m_netclassSelector->SetBoard( aBoard );
102 m_netclassSelector->SetToolTip( _( "Net class an item must belong to" ) );
103 m_rowSizer->Add( m_netclassSelector, 1, wxALL | wxEXPAND, 2 );
104 m_netclassSelector->Hide();
105
106 // Area selector
107 m_areaSelector = new AREA_SELECTOR( this, wxID_ANY );
108 m_areaSelector->SetBoard( aBoard );
109 m_areaSelector->SetToolTip( _( "Rule area an item must be inside" ) );
110 m_rowSizer->Add( m_areaSelector, 1, wxALL | wxEXPAND, 2 );
111 m_areaSelector->Hide();
112
113 m_contentSizer->Add( m_rowSizer, 0, wxEXPAND, 0 );
114
115 // Custom query text control - shown only when Custom Query is selected
116 m_customQueryCtrl = new wxStyledTextCtrl( this, wxID_ANY, wxDefaultPosition, wxSize( -1, 60 ) );
117 m_customQueryCtrl->SetToolTip( _( "Custom match expression in the DRC rule language" ) );
118 m_customQueryCtrl->SetUseTabs( true );
119 m_customQueryCtrl->SetTabWidth( 4 );
120 m_customQueryCtrl->SetIndent( 4 );
121 m_customQueryCtrl->SetTabIndents( true );
122 m_customQueryCtrl->SetBackSpaceUnIndents( true );
123 m_customQueryCtrl->SetViewEOL( false );
124 m_customQueryCtrl->SetViewWhiteSpace( false );
125 m_customQueryCtrl->SetIndentationGuides( true );
126 m_customQueryCtrl->SetReadOnly( false );
127 m_customQueryCtrl->SetUseHorizontalScrollBar( false );
128 m_customQueryCtrl->SetMaxSize( wxSize( -1, 60 ) );
129
130 m_scintillaTricks = std::make_unique<SCINTILLA_TRICKS>(
131 m_customQueryCtrl, wxT( "()" ), false,
132 []( wxKeyEvent& aEvent )
133 {
134 aEvent.Skip();
135 },
136 []( wxStyledTextEvent& aEvent )
137 {
138 } );
139
140 m_contentSizer->Add( m_customQueryCtrl, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 5 );
141 m_customQueryCtrl->Hide();
142
143 m_mainSizer->Add( m_contentSizer, 1, wxEXPAND, 0 );
144
145 // Delete button on right side, aligned to top
146 m_deleteBtn = new wxBitmapButton( this, wxID_ANY, KiBitmapBundle( BITMAPS::trash ) );
147 m_deleteBtn->SetToolTip( _( "Remove this condition" ) );
148 m_mainSizer->Add( m_deleteBtn, 0, wxALL | wxALIGN_TOP, 2 );
149
150 wxLogTrace( TRACE_COND, wxS( "[DRC_RE_CONDITION_ROW_PANEL] setting sizer" ) );
151 SetSizer( m_mainSizer );
152
153 // Bind events
156 m_deleteBtn->Bind( wxEVT_BUTTON, &DRC_RE_CONDITION_ROW_PANEL::onDeleteClicked, this );
157 m_netSelector->Bind( FILTERED_ITEM_SELECTED, &DRC_RE_CONDITION_ROW_PANEL::onValueChanged, this );
158 m_netclassSelector->Bind( FILTERED_ITEM_SELECTED, &DRC_RE_CONDITION_ROW_PANEL::onValueChanged, this );
159 m_areaSelector->Bind( FILTERED_ITEM_SELECTED, &DRC_RE_CONDITION_ROW_PANEL::onValueChanged, this );
161
162 wxLogTrace( TRACE_COND, wxS( "[DRC_RE_CONDITION_ROW_PANEL] ctor END" ) );
163}
164
165
167{
168 m_objectChoice->SetSelection( static_cast<int>( aTarget ) );
169}
170
171
176
177
179{
180 m_conditionChoice->SetSelection( static_cast<int>( aType ) );
182}
183
184
189
190
191void DRC_RE_CONDITION_ROW_PANEL::SetValue( const wxString& aValue )
192{
193 switch( GetConditionType() )
194 {
196 m_netSelector->SetSelectedNet( aValue );
197 break;
198
200 m_netclassSelector->SetSelectedNetclass( aValue );
201 break;
202
204 m_areaSelector->SetSelectedArea( aValue );
205 break;
206
208 m_customQueryCtrl->SetValue( aValue );
209 break;
210
211 default:
212 break;
213 }
214}
215
216
218{
219 switch( GetConditionType() )
220 {
222 return m_netSelector->GetSelectedNetname();
223
225 return m_netclassSelector->GetSelectedNetclass();
226
228 return m_areaSelector->GetSelectedArea();
229
231 return m_customQueryCtrl->GetText();
232
233 default:
234 return wxString();
235 }
236}
237
238
240{
241 wxLogTrace( TRACE_COND, wxS( "[ParseExpression] expr='%s'" ), aExpr );
242
243 if( aExpr.IsEmpty() )
244 {
245 wxLogTrace( TRACE_COND, wxS( "[ParseExpression] empty -> ANY" ) );
247 return true;
248 }
249
250 wxString expr = aExpr;
251
252 // Check for object prefix (A. or B.)
253 if( expr.StartsWith( "A." ) )
254 {
256 expr = expr.Mid( 2 );
257 }
258 else if( expr.StartsWith( "B." ) )
259 {
261 expr = expr.Mid( 2 );
262 }
263
264 if( std::optional<DRC_RULE_EDITOR_UTILS::SELECTOR_CONDITION> cond =
266 {
267 switch( cond->kind )
268 {
271 m_netSelector->SetSelectedNet( cond->operand );
272 return true;
273
276 m_netclassSelector->SetSelectedNetclass( cond->operand );
277 return true;
278
281 m_areaSelector->SetSelectedArea( cond->operand );
282 return true;
283
284 // The area row writes intersectsArea, so it cannot hold this test
286 break;
287 }
288 }
289
290 // Fallback to custom query
291 wxLogTrace( TRACE_COND, wxS( "[ParseExpression] no match -> CUSTOM" ) );
293 m_customQueryCtrl->SetValue( aExpr );
294
295 return true;
296}
297
298
300{
301 wxString prefix = m_showObjectSelector
302 ? ( GetObjectTarget() == OBJECT_TARGET::OBJECT_A ? "A" : "B" )
303 : "A";
304
305 switch( GetConditionType() )
306 {
308 {
309 wxString net = m_netSelector->GetSelectedNetname();
310
311 if( net.IsEmpty() )
312 return wxEmptyString;
313
314 return prefix + wxString::Format( ".NetName == '%s'", net );
315 }
316
318 {
319 wxString netclass = m_netclassSelector->GetSelectedNetclass();
320
321 if( netclass.IsEmpty() )
322 return wxEmptyString;
323
324 return prefix + wxString::Format( ".hasNetclass('%s')", netclass );
325 }
326
328 {
329 wxString area = m_areaSelector->GetSelectedArea();
330
331 if( area.IsEmpty() )
332 return wxEmptyString;
333
334 return prefix + wxString::Format( ".intersectsArea('%s')", area );
335 }
336
338 return m_customQueryCtrl->GetText();
339
340 default:
341 return wxEmptyString;
342 }
343}
344
345
347{
348 m_deleteBtn->Show( aShow );
349 Layout();
350}
351
352
357
358
359void DRC_RE_CONDITION_ROW_PANEL::onObjectChoice( wxCommandEvent& aEvent )
360{
361 if( m_changeCallback )
363}
364
365
367{
369
370 if( m_changeCallback )
372}
373
374
376{
377 if( m_deleteCallback )
379}
380
381
382void DRC_RE_CONDITION_ROW_PANEL::onValueChanged( wxCommandEvent& aEvent )
383{
384 if( m_changeCallback )
386}
387
388
390{
391 if( m_changeCallback )
393}
394
395
397{
398 m_netSelector->Hide();
399 m_netclassSelector->Hide();
400 m_areaSelector->Hide();
401 m_customQueryCtrl->Hide();
402
403 switch( GetConditionType() )
404 {
406 m_netSelector->Show();
407 break;
408
410 m_netclassSelector->Show();
411 break;
412
414 m_areaSelector->Show();
415 break;
416
418 m_customQueryCtrl->Show();
419 break;
420
421 default:
422 break;
423 }
424
425 Layout();
426
427 // Notify parent to update layout since our size may have changed
428 if( wxWindow* parent = GetParent() )
429 parent->Layout();
430}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
const NETINFO_LIST & GetNetInfo() const
Definition board.h:1241
void SetValue(const wxString &aValue)
DRC_RE_CONDITION_ROW_PANEL(wxWindow *aParent, BOARD *aBoard, bool aShowObjectSelector)
wxString BuildExpression() const
Build a condition expression from the row's current state.
void SetConditionType(CONDITION_TYPE aType)
std::unique_ptr< SCINTILLA_TRICKS > m_scintillaTricks
bool ParseExpression(const wxString &aExpr)
Parse a condition expression and set the row's state.
void onCustomQueryChanged(wxStyledTextEvent &aEvent)
void onConditionChoice(wxCommandEvent &aEvent)
void onObjectChoice(wxCommandEvent &aEvent)
void onValueChanged(wxCommandEvent &aEvent)
void onDeleteClicked(wxCommandEvent &aEvent)
void SetObjectTarget(OBJECT_TARGET aTarget)
static std::optional< SELECTOR_CONDITION > ParseSelectorCondition(const wxString &aExpr)
Match a condition that a net, netclass or area selector can show.
static constexpr const wxChar * TRACE_COND
#define _(s)
KICOMMON_API wxSize GetTextSize(const wxString &aSingleLine, wxWindow *aWindow)
Return the size of aSingleLine of text when it is rendered in aWindow using whatever font is currentl...
Definition ui_common.cpp:80
Functions to provide common constants and other functions to assist in making a consistent UI.