KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_matched_length_diff_pair_overlay_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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
28
29#include <base_units.h>
30#include <eda_base_frame.h>
31#include <widgets/unit_binder.h>
32
33#include <wx/textctrl.h>
34#include <wx/stattext.h>
35#include <wx/checkbox.h>
37
39 wxWindow* aParent, DRC_RE_MATCHED_LENGTH_DIFF_PAIR_CONSTRAINT_DATA* aData, EDA_UNITS aUnits ) :
41 m_data( aData ),
43{
45
46 std::vector<DRC_RE_FIELD_POSITION> positions = m_data->GetFieldPositions();
47
48 wxWindow* eventSource = nullptr;
49
50 for( wxWindow* win = aParent; win; win = win->GetParent() )
51 {
52 if( dynamic_cast<EDA_BASE_FRAME*>( win ) )
53 {
54 eventSource = win;
55 break;
56 }
57 }
58
59 // Create opt length field
60 auto* optLengthField = AddField<wxTextCtrl>( wxS( "opt_length" ), positions[0], wxTE_CENTRE | wxTE_PROCESS_ENTER );
62 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, optLengthField->GetControl(),
63 optLengthField->GetLabel(), false, false );
64 optLengthField->SetUnitBinder( m_optLengthBinder.get() );
65 optLengthField->GetControl()->SetValidator( VALIDATOR_NUMERIC_CTRL( false, false ) );
66
67 // Create tolerance field
68 auto* toleranceField = AddField<wxTextCtrl>( wxS( "tolerance" ), positions[1], wxTE_CENTRE | wxTE_PROCESS_ENTER );
70 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, toleranceField->GetControl(),
71 toleranceField->GetLabel(), false, false );
72 toleranceField->SetUnitBinder( m_toleranceBinder.get() );
73 toleranceField->GetControl()->SetValidator( VALIDATOR_NUMERIC_CTRL( false, false ) );
74
75 // Add +/- decoration between opt and tolerance fields
76 {
77 const DRC_RE_FIELD_POSITION& optPos = positions[0];
78 const DRC_RE_FIELD_POSITION& tolPos = positions[1];
79 int fieldHeight = optLengthField->GetControl()->GetBestSize().GetHeight();
80
81 auto* plusMinus = new wxStaticText( this, wxID_ANY, wxS( "\u00B1" ) );
82 wxSize pmSize = plusMinus->GetBestSize();
83 wxStaticText* optMmLabel = optLengthField->GetLabel();
84 int afterOptLabel = optMmLabel->GetPosition().x + optMmLabel->GetBestSize().GetWidth();
85 int gapMid = ( afterOptLabel + tolPos.xStart ) / 2;
86 plusMinus->SetPosition(
87 wxPoint( gapMid - pmSize.GetWidth() / 2, optPos.yTop + ( fieldHeight - pmSize.GetHeight() ) / 2 ) );
88 }
89
90 // Create max skew field
91 auto* maxSkewField = AddField<wxTextCtrl>( wxS( "max_skew" ), positions[2], wxTE_CENTRE | wxTE_PROCESS_ENTER );
92 m_maxSkewBinder = std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, maxSkewField->GetControl(),
93 maxSkewField->GetLabel(), false, false );
94 maxSkewField->SetUnitBinder( m_maxSkewBinder.get() );
95 maxSkewField->GetControl()->SetValidator( VALIDATOR_NUMERIC_CTRL( false, false ) );
96
97 {
98 const DRC_RE_FIELD_POSITION& skewPos = positions[2];
99 wxStaticText* skewMmLabel = maxSkewField->GetLabel();
100 int afterSkewLabel = skewMmLabel->GetPosition().x + skewMmLabel->GetBestSize().GetWidth();
101 int fieldHeight = maxSkewField->GetControl()->GetBestSize().GetHeight();
102
103 m_withinDiffPairsCheckbox = new wxCheckBox( this, wxID_ANY, _( "Within diff pairs" ) );
104 wxSize cbSize = m_withinDiffPairsCheckbox->GetBestSize();
105 m_withinDiffPairsCheckbox->SetPosition(
106 wxPoint( afterSkewLabel + 8, skewPos.yTop + ( fieldHeight - cbSize.GetHeight() ) / 2 ) );
107 }
108
109 auto notifyModified = [this]( wxCommandEvent& )
110 {
112 if( dlg )
113 dlg->SetModified();
114 };
115
116 optLengthField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
117 toleranceField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
118 maxSkewField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
119 m_withinDiffPairsCheckbox->Bind( wxEVT_CHECKBOX, notifyModified );
120
121 auto notifySave = [this]( wxCommandEvent& aEvent )
122 {
124 if( dlg )
125 dlg->OnSave( aEvent );
126 };
127
128 optLengthField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
129 toleranceField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
130 maxSkewField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
131 m_withinDiffPairsCheckbox->Bind( wxEVT_CHECKBOX, notifySave );
132
133 // Position all fields and update the panel layout
136}
137
138
140{
141 if( !m_data )
142 return false;
143
144 m_optLengthBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetOptimumLength() ) );
145 m_toleranceBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetTolerance() ) );
146 m_maxSkewBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetMaxSkew() ) );
147 m_withinDiffPairsCheckbox->SetValue( m_data->GetWithinDiffPairs() );
148
149 return true;
150}
151
152
154{
155 if( !m_data )
156 return false;
157
158 m_data->SetOptimumLength( pcbIUScale.IUTomm( m_optLengthBinder->GetDoubleValue() ) );
159 m_data->SetTolerance( pcbIUScale.IUTomm( m_toleranceBinder->GetDoubleValue() ) );
160 m_data->SetMaxSkew( pcbIUScale.IUTomm( m_maxSkewBinder->GetDoubleValue() ) );
161 m_data->SetWithinDiffPairs( m_withinDiffPairsCheckbox->GetValue() );
162
163 return true;
164}
165
166
167bool DRC_RE_MATCHED_LENGTH_DIFF_PAIR_OVERLAY_PANEL::ValidateInputs( int* aErrorCount, wxString* aValidationMessage )
168{
170
171 VALIDATION_RESULT result = m_data->Validate();
172
173 if( !result.isValid )
174 {
175 *aErrorCount = result.errors.size();
176
177 for( size_t i = 0; i < result.errors.size(); i++ )
178 *aValidationMessage += DRC_RULE_EDITOR_UTILS::FormatErrorMessage( i + 1, result.errors[i] );
179
180 return false;
181 }
182
183 return true;
184}
185
186
188{
189 if( !m_data )
190 return wxEmptyString;
191
192 return m_data->GenerateRule( aContext );
193}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
DRC_RE_OVERLAY_FIELD * AddField(const wxString &aId, const DRC_RE_FIELD_POSITION &aPosition, long aStyle=0)
Create and position a field control on the bitmap overlay.
void SetBackgroundBitmap(BITMAPS aBitmap)
Set the background bitmap for this panel.
DRC_RE_BITMAP_OVERLAY_PANEL(wxWindow *aParent, wxWindowID aId=wxID_ANY)
void PositionFields()
Position all fields based on the current scale factor.
BITMAPS GetOverlayBitmap() const override
Returns the bitmap to use for the overlay panel background.
bool ValidateInputs(int *aErrorCount, wxString *aValidationMessage) override
wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext) override
DRC_RE_MATCHED_LENGTH_DIFF_PAIR_OVERLAY_PANEL(wxWindow *aParent, DRC_RE_MATCHED_LENGTH_DIFF_PAIR_CONSTRAINT_DATA *aData, EDA_UNITS aUnits)
static wxString FormatErrorMessage(int aErrorCount, const wxString &aErrorMessage)
The base frame for deriving all KiCad main window classes.
void SetModified()
Marks the dialog as modified, indicating unsaved changes.
static RULE_EDITOR_DIALOG_BASE * GetDialog(wxWindow *aWindow)
Static method to retrieve the rule editor dialog instance associated with a given window.
virtual void OnSave(wxCommandEvent &aEvent)=0
#define _(s)
Base window classes and related definitions.
EDA_UNITS
Definition eda_units.h:48
Specifies the position and size of a field overlaid on a constraint bitmap.
int xStart
Left edge X coordinate where the field starts.
int yTop
Top edge Y coordinate of the field.
Result of a validation operation.
wxString result
Test unit parsing edge cases and error handling.