KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_routing_width_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>
36
37
39 wxWindow* aParent,
41 EDA_UNITS aUnits ) :
43 m_data( aData ),
45{
47
48 std::vector<DRC_RE_FIELD_POSITION> positions = m_data->GetFieldPositions();
49
50 wxWindow* eventSource = nullptr;
51
52 for( wxWindow* win = aParent; win; win = win->GetParent() )
53 {
54 if( dynamic_cast<EDA_BASE_FRAME*>( win ) )
55 {
56 eventSource = win;
57 break;
58 }
59 }
60
61
62 // Create opt width field
63 auto* optWidthField = AddField<wxTextCtrl>( wxS( "opt_width" ), positions[0], wxTE_CENTRE | wxTE_PROCESS_ENTER );
65 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, optWidthField->GetControl(),
66 optWidthField->GetLabel(), false, false );
67 optWidthField->SetUnitBinder( m_optWidthBinder.get() );
68 optWidthField->GetControl()->SetValidator( VALIDATOR_NUMERIC_CTRL( false, false ) );
69
70 // Create width tolerance field
71 auto* widthTolField =
72 AddField<wxTextCtrl>( wxS( "width_tolerance" ), positions[1], wxTE_CENTRE | wxTE_PROCESS_ENTER );
74 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, widthTolField->GetControl(),
75 widthTolField->GetLabel(), false, false );
76 widthTolField->SetUnitBinder( m_widthToleranceBinder.get() );
77 widthTolField->GetControl()->SetValidator( VALIDATOR_NUMERIC_CTRL( false, false ) );
78
79 // Add ± label between opt width and tolerance
80 {
81 const DRC_RE_FIELD_POSITION& optPos = positions[0];
82 const DRC_RE_FIELD_POSITION& tolPos = positions[1];
83 int fieldHeight = optWidthField->GetControl()->GetBestSize().GetHeight();
84
85 auto* plusMinus = new wxStaticText( this, wxID_ANY, wxS( "\u00B1" ) );
86 wxSize pmSize = plusMinus->GetBestSize();
87 wxStaticText* optMmLabel = optWidthField->GetLabel();
88 int afterOptLabel = optMmLabel->GetPosition().x + optMmLabel->GetBestSize().GetWidth();
89 int gapMid = ( afterOptLabel + tolPos.xStart ) / 2;
90 plusMinus->SetPosition(
91 wxPoint( gapMid - pmSize.GetWidth() / 2, optPos.yTop + ( fieldHeight - pmSize.GetHeight() ) / 2 ) );
92 }
93
94 auto notifyModified = [this]( wxCommandEvent& )
95 {
97 if( dlg )
98 dlg->SetModified();
99 };
100
101 optWidthField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
102 widthTolField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
103
104 auto notifySave = [this]( wxCommandEvent& aEvent )
105 {
107 if( dlg )
108 dlg->OnSave( aEvent );
109 };
110
111 optWidthField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
112 widthTolField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
113
114 // Position all fields and update the panel layout
117}
118
119
121{
122 if( !m_data )
123 return false;
124
125 m_optWidthBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetOptWidth() ) );
126 m_widthToleranceBinder->ChangeDoubleValue( pcbIUScale.mmToIU( m_data->GetWidthTolerance() ) );
127
128 return true;
129}
130
131
133{
134 if( !m_data )
135 return false;
136
137 m_data->SetOptWidth( pcbIUScale.IUTomm( m_optWidthBinder->GetDoubleValue() ) );
138 m_data->SetWidthTolerance( pcbIUScale.IUTomm( m_widthToleranceBinder->GetDoubleValue() ) );
139
140 return true;
141}
142
143
145 wxString* aValidationMessage )
146{
148
149 VALIDATION_RESULT result = m_data->Validate();
150
151 if( !result.isValid )
152 {
153 *aErrorCount = result.errors.size();
154
155 for( size_t i = 0; i < result.errors.size(); i++ )
156 *aValidationMessage += DRC_RULE_EDITOR_UTILS::FormatErrorMessage( i + 1, result.errors[i] );
157
158 return false;
159 }
160
161 return true;
162}
163
164
166{
167 if( !m_data )
168 return wxEmptyString;
169
170 return m_data->GenerateRule( aContext );
171}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
@ constraint_routing_width
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.
wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext) override
DRC_RE_ROUTING_WIDTH_CONSTRAINT_DATA * m_data
bool ValidateInputs(int *aErrorCount, wxString *aValidationMessage) override
DRC_RE_ROUTING_WIDTH_OVERLAY_PANEL(wxWindow *aParent, DRC_RE_ROUTING_WIDTH_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
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.