KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_rtg_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, see <https://www.gnu.org/licenses/>.
18 */
19
24
25#include <base_units.h>
26#include <eda_base_frame.h>
27#include <widgets/unit_binder.h>
28
29#include <wx/textctrl.h>
30#include <wx/stattext.h>
31
32
34 wxWindow* aParent,
36 EDA_UNITS aUnits ) :
38 m_data( aData ),
40{
42
43 std::vector<DRC_RE_FIELD_POSITION> positions = m_data->GetFieldPositions();
44
45 wxWindow* eventSource = nullptr;
46
47 for( wxWindow* win = aParent; win; win = win->GetParent() )
48 {
49 if( dynamic_cast<EDA_BASE_FRAME*>( win ) )
50 {
51 eventSource = win;
52 break;
53 }
54 }
55
56 auto* optWidthField = AddField<wxTextCtrl>( wxS( "opt_width" ), positions[0], wxTE_CENTRE | wxTE_PROCESS_ENTER );
58 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, optWidthField->GetControl(),
59 optWidthField->GetLabel(), false, false );
60 optWidthField->SetUnitBinder( m_optWidthBinder.get() );
61
62 auto* widthTolField =
63 AddField<wxTextCtrl>( wxS( "width_tolerance" ), positions[1], wxTE_CENTRE | wxTE_PROCESS_ENTER );
65 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, widthTolField->GetControl(),
66 widthTolField->GetLabel(), false, false );
67 widthTolField->SetUnitBinder( m_widthToleranceBinder.get() );
68
69 auto* optGapField = AddField<wxTextCtrl>( wxS( "opt_gap" ), positions[2], wxTE_CENTRE | wxTE_PROCESS_ENTER );
70 m_optGapBinder = std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, optGapField->GetControl(),
71 optGapField->GetLabel(), false, false );
72 optGapField->SetUnitBinder( m_optGapBinder.get() );
73
74 auto* gapTolField = AddField<wxTextCtrl>( wxS( "gap_tolerance" ), positions[3], wxTE_CENTRE | wxTE_PROCESS_ENTER );
75 m_gapToleranceBinder = std::make_unique<UNIT_BINDER>(
76 &m_unitsProvider, eventSource, nullptr, gapTolField->GetControl(), gapTolField->GetLabel(), false, false );
77 gapTolField->SetUnitBinder( m_gapToleranceBinder.get() );
78
79 auto* maxUncoupledField =
80 AddField<wxTextCtrl>( wxS( "max_uncoupled" ), positions[4], wxTE_CENTRE | wxTE_PROCESS_ENTER );
82 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, maxUncoupledField->GetControl(),
83 maxUncoupledField->GetLabel(), false, false );
84 maxUncoupledField->SetUnitBinder( m_maxUncoupledLengthBinder.get() );
85
86 {
87 const DRC_RE_FIELD_POSITION& optPos = positions[0];
88 const DRC_RE_FIELD_POSITION& tolPos = positions[1];
89 int fieldHeight = optWidthField->GetControl()->GetBestSize().GetHeight();
90
91 auto* plusMinus = new wxStaticText( this, wxID_ANY, wxS( "\u00B1" ) );
92 wxSize pmSize = plusMinus->GetBestSize();
93 wxStaticText* optMmLabel = optWidthField->GetLabel();
94 int afterOptLabel = optMmLabel->GetPosition().x + optMmLabel->GetBestSize().GetWidth();
95 int gapMid = ( afterOptLabel + tolPos.xStart ) / 2;
96 plusMinus->SetPosition(
97 wxPoint( gapMid - pmSize.GetWidth() / 2, optPos.yTop + ( fieldHeight - pmSize.GetHeight() ) / 2 ) );
98 }
99
100 {
101 const DRC_RE_FIELD_POSITION& optPos = positions[2];
102 const DRC_RE_FIELD_POSITION& tolPos = positions[3];
103 int fieldHeight = optGapField->GetControl()->GetBestSize().GetHeight();
104
105 auto* plusMinus = new wxStaticText( this, wxID_ANY, wxS( "\u00B1" ) );
106 wxSize pmSize = plusMinus->GetBestSize();
107 wxStaticText* optMmLabel = optGapField->GetLabel();
108 int afterOptLabel = optMmLabel->GetPosition().x + optMmLabel->GetBestSize().GetWidth();
109 int gapMid = ( afterOptLabel + tolPos.xStart ) / 2;
110 plusMinus->SetPosition(
111 wxPoint( gapMid - pmSize.GetWidth() / 2, optPos.yTop + ( fieldHeight - pmSize.GetHeight() ) / 2 ) );
112 }
113
114 auto notifyModified = [this]( wxCommandEvent& )
115 {
117 if( dlg )
118 dlg->SetModified();
119 };
120
121 optWidthField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
122 widthTolField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
123 optGapField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
124 gapTolField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
125 maxUncoupledField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
126
127 auto notifySave = [this]( wxCommandEvent& aEvent )
128 {
130 if( dlg )
131 dlg->OnSave( aEvent );
132 };
133
134 optWidthField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
135 widthTolField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
136 optGapField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
137 gapTolField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
138 maxUncoupledField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
139
140 // Position all fields and update the panel layout
143}
144
145
147{
148 if( !m_data )
149 return false;
150
151 // Convert mm values to internal units and set them in the binders
152 // Use ChangeDoubleValue to avoid triggering modification events during loading
153 m_optWidthBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetOptWidth() ) );
154 m_widthToleranceBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetWidthTolerance() ) );
155
156 m_optGapBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetOptGap() ) );
157 m_gapToleranceBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetGapTolerance() ) );
158
159 m_maxUncoupledLengthBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetMaxUncoupledLength() ) );
160
161 return true;
162}
163
164
166{
167 if( !m_data )
168 return false;
169
170 // Read values from binders (in internal units) and convert to mm for storage
171 m_data->SetOptWidth( pcbIUScale.IUTomm( m_optWidthBinder->GetDoubleValue() ) );
172 m_data->SetWidthTolerance( pcbIUScale.IUTomm( m_widthToleranceBinder->GetDoubleValue() ) );
173
174 m_data->SetOptGap( pcbIUScale.IUTomm( m_optGapBinder->GetDoubleValue() ) );
175 m_data->SetGapTolerance( pcbIUScale.IUTomm( m_gapToleranceBinder->GetDoubleValue() ) );
176
177 m_data->SetMaxUncoupledLength( pcbIUScale.IUTomm( m_maxUncoupledLengthBinder->GetDoubleValue() ) );
178
179 return true;
180}
181
182
184 wxString* aValidationMessage )
185{
187
188 VALIDATION_RESULT result = m_data->Validate();
189
190 if( !result.isValid )
191 {
192 *aErrorCount = result.errors.size();
193
194 for( size_t i = 0; i < result.errors.size(); i++ )
195 *aValidationMessage += DRC_RULE_EDITOR_UTILS::FormatErrorMessage( i + 1, result.errors[i] );
196
197 return false;
198 }
199
200 return true;
201}
202
203
205{
206 if( !m_data )
207 return wxEmptyString;
208
209 return m_data->GenerateRule( aContext );
210}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
@ constraint_routing_diff_pair
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.
DRC_RE_ROUTING_DIFF_PAIR_CONSTRAINT_DATA * m_data
DRC_RE_ROUTING_DIFF_PAIR_OVERLAY_PANEL(wxWindow *aParent, DRC_RE_ROUTING_DIFF_PAIR_CONSTRAINT_DATA *aData, EDA_UNITS aUnits)
bool ValidateInputs(int *aErrorCount, wxString *aValidationMessage) override
wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext) override
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
Base window classes and related definitions.
EDA_UNITS
Definition eda_units.h:44
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.