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