48#include <wx/stattext.h>
50#include <wx/dcclient.h>
52#include <wx/settings.h>
53#include <wx/listctrl.h>
54#include <wx/scrolwin.h>
56#include <wx/toplevel.h>
70 m_list->AppendColumn( wxS(
"#" ), wxLIST_FORMAT_RIGHT, 36 );
71 m_list->AppendColumn(
_(
"Name" ), wxLIST_FORMAT_LEFT, 160 );
72 m_list->AppendColumn(
_(
"Condition" ), wxLIST_FORMAT_LEFT, 280 );
73 m_list->AppendColumn(
_(
"Layers" ), wxLIST_FORMAT_LEFT, 120 );
74 m_list->AppendColumn(
_(
"Constraint" ), wxLIST_FORMAT_LEFT, 240 );
98 [
this]( wxSizeEvent& aEvent )
101 if( m_list->GetClientSize().x != m_fittedWidth )
106 m_list->Bind( wxEVT_LIST_ITEM_SELECTED,
107 [
this]( wxListEvent& )
109 if( !m_refreshing && selectedIndex() >= 0 && selectedIndex() != editingIndex()
110 && !EditRule( selectedIndex() ) )
112 if( editingIndex() >= 0 )
114 selectIndex( editingIndex() );
118 for( long i = 0; i < m_list->GetItemCount(); ++i )
119 m_list->SetItemState( i, 0, wxLIST_STATE_SELECTED );
125 m_list->Bind( wxEVT_LIST_ITEM_ACTIVATED,
126 [
this]( wxListEvent& aEvent )
128 long row = aEvent.GetIndex();
130 if( row >= 0 && row < (
long) m_rowToIndex.size() && m_rowToIndex[row] == NETCLASS_ROW )
133 dialog->ShowPage(
_(
"Net Classes" ) );
140 m_list->Bind( wxEVT_LIST_ITEM_DESELECTED,
141 [
this]( wxListEvent& )
145 m_list->Bind( wxEVT_LIST_KEY_DOWN,
146 [
this]( wxListEvent& aEvent )
148 wxCommandEvent
dummy;
150 switch( aEvent.GetKeyCode() )
153 case WXK_BACK: onRemove(
dummy );
break;
154 default: aEvent.Skip();
167 if(
m_frame->GetBoard()->GetDesignRulesPath().IsEmpty() )
168 blocker =
_(
"Design rules cannot be added without a project" );
170 blocker =
_(
"The custom rules text does not parse. Fix it on the Custom Rules page first." );
180 if( blocker.IsEmpty() )
186 long item =
m_list->InsertItem( row, wxString::Format( wxS(
"%d" ), ++rules ) );
200 bool anyRules = rules > 0;
206 m_emptyLabel->SetLabel( blocker.IsEmpty() ?
_(
"No rules yet." ) : blocker );
212 m_newBtn->Show( blocker.IsEmpty() );
235 for(
long i = 0; i <
m_list->GetItemCount(); ++i )
236 m_list->SetItemState( i, 0, wxLIST_STATE_SELECTED );
254 for(
int col : { 1, 3, 4 } )
257 header.SetMask( wxLIST_MASK_TEXT );
258 m_list->GetColumn( col, header );
260 int width =
dc.GetTextExtent( header.GetText() ).x;
262 for(
long row = 0; row <
m_list->GetItemCount(); ++row )
263 width = std::max( width,
dc.GetTextExtent(
m_list->GetItemText( row, col ) ).x );
265 m_list->SetColumnWidth( col, std::max( width + 40, 90 ) );
270 int client =
m_list->GetClientSize().x - 4;
272 m_list->SetColumnWidth( 1, std::min(
m_list->GetColumnWidth( 1 ), client / 4 ) );
273 m_list->SetColumnWidth( 4, std::min(
m_list->GetColumnWidth( 4 ), client * 2 / 5 ) );
275 int others =
m_list->GetColumnWidth( 0 ) +
m_list->GetColumnWidth( 1 ) +
m_list->GetColumnWidth( 3 )
276 +
m_list->GetColumnWidth( 4 );
278 m_list->SetColumnWidth( 2, std::max( client - others, 160 ) );
299 std::vector<std::shared_ptr<NETCLASS>> classes = { netSettings.
GetDefaultNetclass() };
302 classes.push_back( netclass );
304 for(
const std::shared_ptr<NETCLASS>& netclass : classes )
315 wxString condition = wxString::Format( wxS(
"A.hasExactNetclass('%s')" ), netclass->GetName() );
322 m_frame->StringFromValue( *value,
true ) );
324 long item =
m_list->InsertItem( row, wxEmptyString );
325 m_list->SetItem( item, 1, wxString::Format(
_(
"Net class '%s'" ), netclass->GetName() ) );
326 m_list->SetItem( item, 2, condition );
327 m_list->SetItem( item, 3,
_(
"Any" ) );
328 m_list->SetItem( item, 4, constraint );
331 m_list->SetItemTextColour( item, wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
351 static wxRegEx units( wxS(
"([0-9]+(\\.[0-9]+)?)(mm|um|mil|in)\\b" ), wxRE_ADVANCED );
354 wxString rest = aClause;
356 while( units.Matches( rest ) )
358 size_t start, length;
359 units.GetMatch( &start, &length );
365 rest = rest.Mid( start + length );
377 auto custom = std::dynamic_pointer_cast<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>( aEntry.
constraintData );
382 if( start == wxString::npos )
383 return _(
"Custom rule" );
385 size_t body = start + wxString( wxS(
"(constraint" ) ).length();
387 while( body <
text.length() && wxIsspace(
text[body] ) )
390 body += code.length();
394 for(
size_t i = start; i <
text.length(); ++i )
398 else if(
text[i] ==
')' && --depth == 0 )
400 wxString clause =
text.Mid( body, i - body );
401 clause.Replace( wxS(
"\n" ), wxS(
" " ) );
402 clause.Replace( wxS(
"\t" ), wxS(
" " ) );
403 return inUserUnits( clause.Trim(
true ).Trim(
false ), aUnits ) +
_(
" (text rule)" );
407 return _(
"Custom rule" );
415 const wxString opener = wxS(
"(constraint " );
419 for( wxString clause : clauses )
421 if( clause.StartsWith( opener ) && clause.EndsWith( wxS(
")" ) ) )
423 wxString body = clause.Mid( opener.length(), clause.length() - opener.length() - 1 );
424 wxString code = body.BeforeFirst(
' ' );
425 wxString rest = body.AfterFirst(
' ' );
427 clause = clauses.size() > 1 ? code + wxS(
" " ) + rest : rest;
430 if( !
text.IsEmpty() )
442 long row =
m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
464 m_list->SetItemState( (
long) row, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
465 m_list->EnsureVisible( (
long) row );
479 wxString label = editingExisting ?
_(
"Apply" ) :
_(
"Add Rule" );
481 m_primaryBtn->SetToolTip( editingExisting ?
_(
"Save the form back to the selected rule." )
482 :
_(
"Check the form and add it to the list of rules above." ) );
490 m_newBtn->Enable( editingExisting );
492 m_upBtn->Enable( hasSelection );
550 existing ?
_(
"Changes to this rule are still in the form. Only the rules in "
552 :
_(
"This rule is still in the form. Only the rules in the "
554 existing ?
_(
"Rule Not Applied" ) :
_(
"Rule Not Added" ),
555 wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_WARNING | wxCENTRE );
557 dlg.SetYesNoCancelLabels( existing ?
_(
"Apply" ) :
_(
"Add Rule" ),
_(
"Discard" ),
_(
"Cancel" ) );
559 int choice = dlg.ShowModal();
561 if( choice == wxID_YES )
564 if( choice == wxID_CANCEL )
593 std::shared_ptr<DRC_RE_BASE_CONSTRAINT_DATA> data =
m_document->Entries()[
index].constraintData;
650 question =
_(
"Delete rule '%s'? It is a text rule and every constraint it sets goes with it." );
654 question =
_(
"Remove this constraint from rule '%s'? The rule keeps the constraints it shares "
655 "with other pages." );
659 question =
_(
"Delete rule '%s'?" );
662 if( !
IsOK(
this, wxString::Format( question, entry.
ruleName ) ) )
676 wxString boardDefault;
682 wxString shown = read ? read() : wxString();
684 if( shown.IsEmpty() )
687 wxString value = wxString::Format( wxS(
"%s %s" ),
name, shown );
689 boardDefault = boardDefault.IsEmpty() ? value : wxString::Format(
_(
"%s and %s" ), boardDefault, value );
694 m_editor->SetOverrideNote( wxEmptyString );
704 m_editor->SetOverrideNote( wxEmptyString );
710 wxString condition =
m_editor->GetCondition();
711 wxString layers =
m_editor->GetLayerSource();
715 if( condition.IsEmpty() && layers.IsEmpty() )
717 note = wxString::Format(
_(
"Overrides the board default, %s, everywhere" ), boardDefault );
719 else if( condition.IsEmpty() )
721 note = wxString::Format(
_(
"Overrides the board default, %s, on %s" ), boardDefault, layers );
723 else if( layers.IsEmpty() )
725 note = wxString::Format(
_(
"Overrides the board default, %s, where %s" ), boardDefault, condition );
729 note = wxString::Format(
_(
"Overrides the board default, %s, on %s where %s" ), boardDefault, layers,
759 std::shared_ptr<DRC_RE_BASE_CONSTRAINT_DATA> draft =
787 m_editor->SetRuleNameValidationCallback(
788 [
this](
int,
const wxString& aName )
808 data->SetIsNew(
true );
812 if(
long selected =
m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); selected != wxNOT_FOUND )
813 m_list->SetItemState( selected, 0, wxLIST_STATE_SELECTED );
821 if( aIndex < 0 || aIndex >= (
int)
m_document->Entries().size() )
824 std::shared_ptr<DRC_RE_BASE_CONSTRAINT_DATA> target =
m_document->Entries()[aIndex].constraintData;
838 if( aIndex < 0 || aIndex >= (
int)
m_document->Entries().size() )
850 if( !
m_editor->GetIsValidationSucceeded() )
858 std::shared_ptr<DRC_RE_BASE_CONSTRAINT_DATA> data =
m_editingData;
861 data->SetIsNew(
false );
864 LSET( data->GetLayers() ) );
881 if( !
m_editor->GetIsValidationSucceeded() )
890 std::shared_ptr<DRC_RE_BASE_CONSTRAINT_DATA> data = entry.
constraintData;
900 edited.
ruleName = data->GetRuleName();
901 edited.
condition = data->GetRuleCondition();
904 edited.
severity = data->GetSeverity();
920 wxString ruleText =
m_editor->GetConstraintData()->GetGeneratedRule();
922 if( ruleText.IsEmpty() )
928 std::shared_ptr<DRC_RULE> rule;
932 std::vector<std::shared_ptr<DRC_RULE>> rules;
933 DRC_RULES_PARSER parser( wxS(
"(version 2)\n" ) + ruleText, wxS(
"ShowMatches" ) );
934 parser.
Parse( rules,
nullptr );
944 if( rule->m_Condition && !rule->m_Condition->GetExpression().IsEmpty()
945 && !rule->m_Condition->Compile(
nullptr ) )
956 std::vector<BOARD_ITEM*> matches;
960 switch( item->Type() )
966 default: matches.push_back( item );
972 if( !matches.empty() )
974 std::vector<EDA_ITEM*> selectItems( matches.begin(), matches.end() );
980 m_frame->FocusOnItems( matches );
982 return (
int) matches.size();
996 dialog->SetError( aMessage,
this,
nullptr );
1007 const std::vector<DRC_RE_LOADED_PANEL_ENTRY>& entries =
m_document->Entries();
1009 for(
size_t i = 0; i < entries.size(); ++i )
1011 if( entries[i].constraintData == aData )
1041 default:
return wxEmptyString;
1048 const std::vector<DRC_RE_LOADED_PANEL_ENTRY>& entries =
m_document->Entries();
1050 wxArrayString codes;
1051 wxRegEx code( wxS(
"\\(constraint ([a-z_]+)" ) );
1053 for(
size_t i = 0; i < entries.size(); ++i )
1071 text << wxS(
"\n" ) << clause;
1073 while( code.Matches(
text ) )
1075 size_t start, length;
1076 code.GetMatch( &start, &length );
1078 if( codes.Index( code.GetMatch(
text, 1 ) ) == wxNOT_FOUND )
1079 codes.Add( code.GetMatch(
text, 1 ) );
1086 return wxEmptyString;
1090 for(
const wxString& item : codes )
1091 joined << ( joined.IsEmpty() ? wxS(
"" ) : wxS(
", " ) ) << item;
1093 return wxString::Format(
_(
", shared with %s" ), joined );
constexpr EDA_IU_SCALE pcbIUScale
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
static TOOL_ACTION zoomFitSelection
static TOOL_ACTION selectionClear
Clear the current selection.
static TOOL_ACTION selectItems
Select a list of items (specified as the event parameter)
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
CONSTRAINT_RULES_LIST_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
wxStaticText * m_emptyLabel
wxButton * m_duplicateBtn
wxBoxSizer * m_editorSizer
bool ConfirmPendingRule()
Asks what to do with a rule left in the form.
std::function< void()> m_onChanged
wxString sharedWithText(int aIndex) const
bool EditRule(int aIndex)
Selects a rule and opens it in the editor. False if a rule left in the form kept it closed.
static wxString inUserUnits(const wxString &aClause, UNITS_PROVIDER *aUnits)
void onDuplicate(wxCommandEvent &aEvent)
static wxString ConstraintText(const DRC_RE_LOADED_PANEL_ENTRY &aEntry, DRC_RULE_EDITOR_CONSTRAINT_NAME aType, UNITS_PROVIDER *aUnits)
The constraint a rule sets, written the way the rule table shows it.
int addNetclassRows()
Fills the leading rows with the net class values, and returns how many it added.
std::vector< int > m_rowToIndex
The document index each row shows, or NETCLASS_ROW where the row is a net class value.
void onNewRule(wxCommandEvent &aEvent)
bool ApplyPendingRule()
Adds or updates the rule the form holds.
NETCLASS_CONSTRAINT_PART m_netclassPart
void buildEditor(const std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > &aData, DRC_RULE_EDITOR_CONSTRAINT_NAME aPanelType)
static constexpr int NETCLASS_ROW
What m_rowToIndex holds for a row that is not a rule of this document.
bool HasPendingRule() const
True while the form holds typing that has not been added to the list.
void onRemove(wxCommandEvent &aEvent)
int indexOf(const std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > &aData) const
static void copyRuleData(DRC_RE_BASE_CONSTRAINT_DATA &aTarget, const DRC_RE_BASE_CONSTRAINT_DATA &aSource)
Copies everything the form edits, the rule name included.
int selectedIndex() const
void showRule(int aIndex)
void onMoveUp(wxCommandEvent &aEvent)
void selectIndex(int aIndex)
DRC_RULE_DOCUMENT * m_document
std::function< std::optional< int >(const NETCLASS &)> m_netclassValue
void SetNetclasses(std::function< std::optional< int >(const NETCLASS &)> aValue, NETCLASS_CONSTRAINT_PART aPart)
Lists the net class values for this constraint above the rules, where a class can set one.
std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > m_editingData
DRC_RULE_EDITOR_CONSTRAINT_NAME m_editingType
static wxString layersText(const DRC_RE_LOADED_PANEL_ENTRY &aEntry)
DRC_RULE_EDITOR_CONSTRAINT_NAME m_type
CONSTRAINT_RULES_LIST(wxWindow *aParent, PCB_EDIT_FRAME *aFrame, DRC_RULE_DOCUMENT *aDocument, DRC_RULE_EDITOR_CONSTRAINT_NAME aType, const wxString &aTitle)
void moveSelected(int aDelta)
PANEL_DRC_RULE_EDITOR * m_editor
std::vector< std::pair< wxString, std::function< wxString()> > > m_defaults
void onPrimary(wxCommandEvent &aEvent)
void reportError(const wxString &aMessage)
void DiscardPendingRule()
Reloads the form from the document, dropping whatever was typed.
void onMoveDown(wxCommandEvent &aEvent)
static wxString severityText(const DRC_RE_LOADED_PANEL_ENTRY &aEntry)
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
std::vector< BOARD_ITEM * > GetItemsMatchingRule(const std::shared_ptr< DRC_RULE > &aRule, REPORTER *aReporter=nullptr)
wxString GetConstraintCode() const
virtual std::vector< wxString > GetConstraintClauses(const RULE_GENERATION_CONTEXT &aContext) const
Returns just the constraint clauses without the rule wrapper.
void CopyFrom(const ICopyable &aSource) override
virtual wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext)
void Parse(std::vector< std::shared_ptr< DRC_RULE > > &aRules, REPORTER *aReporter)
The custom rules of one board, held in memory while Board Setup is open.
static size_t FindConstraintClause(const wxString &aText, const wxString &aCode)
Position of the constraint clause for aCode in rule text, or npos.
static wxString ConstraintToKicadDrc(DRC_RULE_EDITOR_CONSTRAINT_NAME aType)
Convert a constraint type into the keyword used in a .kicad_drc file.
static std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > CreateConstraintData(DRC_RULE_EDITOR_CONSTRAINT_NAME aType, const DRC_RE_BASE_CONSTRAINT_DATA &aBase)
Create the data object that holds a rule of the given constraint type, taking the identity fields fro...
LSET is a set of PCB_LAYER_IDs.
A collection of nets and the parameters used to route or test these nets.
NET_SETTINGS stores various net-related settings in a project context.
const std::map< wxString, std::shared_ptr< NETCLASS > > & GetNetclasses() const
Gets all netclasses.
std::shared_ptr< NETCLASS > GetDefaultNetclass() const
Gets the default netclass for the project.
static PAGED_DIALOG * GetDialog(wxWindow *aWindow)
static void LayoutAncestors(wxWindow *aWindow)
Lays out aWindow's parents, out to the page it scrolls in.
The main frame for Pcbnew.
void SetRuleName(wxString aRuleName)
Set the name of the rule.
wxString GetRuleName() const
Get the name of the rule.
wxString StringFromValue(double aValue, bool aAddUnitLabel=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aValue in internal units into a united string.
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
This file is part of the common library.
#define KICAD_MESSAGE_DIALOG_BASE
DRC_RULE_EDITOR_CONSTRAINT_NAME
KICOMMON_API double DoubleValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Convert aTextValue to a double.
KICOMMON_API wxFont GetInfoFont(wxWindow *aWindow)
NETCLASS_CONSTRAINT_PART
What a net class value does to a constraint, which is not always raise its minimum.
@ MINIMUM
Raises the minimum, as a net class clearance does.
std::vector< FAB_LAYER_COLOR > dummy
Represents a rule loaded from a .kicad_dru file and mapped to a panel.
wxString originalRuleText
wxString layerSource
Original layer text: "inner", "outer", or layer name.
int ruleGroupId
Shared by entries split out of one rule. Zero when the entry stands alone.
std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > constraintData
DRC_RULE_EDITOR_CONSTRAINT_NAME panelType
A filename or source description, a problem input line, a line number, a byte offset,...
wxString conditionExpression
DATABASE_CONNECTION dc(odbcBackend(cs))
wxString result
Test unit parsing edge cases and error handling.
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
@ PCB_NETINFO_T
class NETINFO_ITEM, a description of a net
Functions to provide common constants and other functions to assist in making a consistent UI.