KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_abs_length_two_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/checkbox.h>
30#include <wx/textctrl.h>
31#include <wx/stattext.h>
33
35 wxWindow* aParent, DRC_RE_ABSOLUTE_LENGTH_TWO_CONSTRAINT_DATA* aData, EDA_UNITS aUnits ) :
37 m_data( aData ),
39{
41
42 std::vector<DRC_RE_FIELD_POSITION> positions = m_data->GetFieldPositions();
43
44 wxWindow* eventSource = nullptr;
45
46 for( wxWindow* win = aParent; win; win = win->GetParent() )
47 {
48 if( dynamic_cast<EDA_BASE_FRAME*>( win ) )
49 {
50 eventSource = win;
51 break;
52 }
53 }
54
55 // Create opt length field
56 auto* optLengthField = AddField<wxTextCtrl>( wxS( "opt_length" ), positions[0], wxTE_CENTRE | wxTE_PROCESS_ENTER );
58 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, optLengthField->GetControl(),
59 optLengthField->GetLabel(), false, false );
60 optLengthField->SetUnitBinder( m_optLengthBinder.get() );
61 optLengthField->GetControl()->SetValidator( VALIDATOR_NUMERIC_CTRL( false, false ) );
62
63 // Create tolerance field
64 auto* toleranceField = AddField<wxTextCtrl>( wxS( "tolerance" ), positions[1], wxTE_CENTRE | wxTE_PROCESS_ENTER );
66 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, toleranceField->GetControl(),
67 toleranceField->GetLabel(), false, false );
68 toleranceField->SetUnitBinder( m_toleranceBinder.get() );
69 toleranceField->GetControl()->SetValidator( VALIDATOR_NUMERIC_CTRL( false, false ) );
70
71 auto notifyModified = [this]( wxCommandEvent& )
72 {
74 if( dlg )
75 dlg->SetModified();
76 };
77
78 optLengthField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
79 toleranceField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
80
81 auto notifySave = [this]( wxCommandEvent& aEvent )
82 {
84 if( dlg )
85 dlg->OnSave( aEvent );
86 };
87
88 optLengthField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
89 toleranceField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
90
91 DRC_RE_FIELD_POSITION checkboxPos( 80, 300, 170, _( "Time domain (ps)" ), LABEL_POSITION::NONE,
92 _( "The values are picoseconds instead of a length" ) );
93
94 m_timeDomainCheckbox = static_cast<wxCheckBox*>( AddCheckbox( wxS( "time_domain" ), checkboxPos )->GetControl() );
95 m_timeDomainCheckbox->SetValue( m_data->IsTimeDomain() );
96
97 m_timeDomainCheckbox->Bind( wxEVT_CHECKBOX,
98 [this, notifyModified]( wxCommandEvent& aEvent )
99 {
101 m_data->SetTimeDomain( m_timeDomainCheckbox->GetValue() );
104 notifyModified( aEvent );
105 } );
106
107 SetMinSize( wxSize( GetMinSize().x, FromDIP( 190 ) ) );
108
109 // Position all fields and update the panel layout
113}
114
115
117{
118 bool timeDomain = m_data->IsTimeDomain();
119
120 for( UNIT_BINDER* binder : { m_optLengthBinder.get(), m_toleranceBinder.get() } )
121 {
122 binder->SetUnits( timeDomain ? EDA_UNITS::PS : m_unitsProvider.GetUserUnits() );
123 binder->SetDataType( timeDomain ? EDA_DATA_TYPE::TIME : EDA_DATA_TYPE::DISTANCE );
124 }
125}
126
127
129{
130 return m_data->IsTimeDomain() ? aValue * pcbIUScale.IU_PER_PS : pcbIUScale.mmToIU( aValue );
131}
132
133
135{
136 return m_data->IsTimeDomain() ? aValue / pcbIUScale.IU_PER_PS : pcbIUScale.IUTomm( aValue );
137}
138
139
141{
142 if( !m_data )
143 return false;
144
145 m_optLengthBinder->ChangeDoubleValue( displayToIU( m_data->GetOptimumLength() ) );
146 m_toleranceBinder->ChangeDoubleValue( displayToIU( m_data->GetTolerance() ) );
147
148 return true;
149}
150
151
153{
154 if( !m_data )
155 return false;
156
157 m_data->SetOptimumLength( iuToDisplay( m_optLengthBinder->GetDoubleValue() ) );
158 m_data->SetTolerance( iuToDisplay( m_toleranceBinder->GetDoubleValue() ) );
159
160 return true;
161}
162
163
165 wxString* aValidationMessage )
166{
168
169 VALIDATION_RESULT result = m_data->Validate();
170
171 if( !result.isValid )
172 {
173 *aErrorCount = result.errors.size();
174
175 for( size_t i = 0; i < result.errors.size(); i++ )
176 *aValidationMessage += DRC_RULE_EDITOR_UTILS::FormatErrorMessage( i + 1, result.errors[i] );
177
178 return false;
179 }
180
181 return true;
182}
183
184
186{
187 if( !m_data )
188 return wxEmptyString;
189
190 return m_data->GenerateRule( aContext );
191}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
BITMAPS GetOverlayBitmap() const override
Returns the bitmap to use for the overlay panel background.
DRC_RE_ABS_LENGTH_TWO_OVERLAY_PANEL(wxWindow *aParent, DRC_RE_ABSOLUTE_LENGTH_TWO_CONSTRAINT_DATA *aData, EDA_UNITS aUnits)
wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext) override
bool ValidateInputs(int *aErrorCount, wxString *aValidationMessage) override
DRC_RE_ABSOLUTE_LENGTH_TWO_CONSTRAINT_DATA * m_data
DRC_RE_OVERLAY_FIELD * AddCheckbox(const wxString &aId, const DRC_RE_FIELD_POSITION &aPosition)
Create and position a checkbox control on the bitmap overlay.
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.
wxControl * GetControl() const
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:44
Specifies the position and size of a field overlaid on a constraint bitmap.
Result of a validation operation.
wxString result
Test unit parsing edge cases and error handling.