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, 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
27
28#include <base_units.h>
29#include <eda_base_frame.h>
30#include <widgets/unit_binder.h>
31
32#include <wx/textctrl.h>
34
36 wxWindow* aParent, DRC_RE_ABSOLUTE_LENGTH_TWO_CONSTRAINT_DATA* aData, 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 // Create min length field
57 auto* minLengthField = AddField<wxTextCtrl>( wxS( "min_length" ), positions[0],
58 wxTE_CENTRE | wxTE_PROCESS_ENTER );
60 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, minLengthField->GetControl(),
61 minLengthField->GetLabel(), false, false );
62 minLengthField->SetUnitBinder( m_minLengthBinder.get() );
63
64 // Create opt length field
65 auto* optLengthField = AddField<wxTextCtrl>( wxS( "opt_length" ), positions[1],
66 wxTE_CENTRE | wxTE_PROCESS_ENTER );
68 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, optLengthField->GetControl(),
69 optLengthField->GetLabel(), false, false );
70 optLengthField->SetUnitBinder( m_optLengthBinder.get() );
71
72 // Create max length field
73 auto* maxLengthField = AddField<wxTextCtrl>( wxS( "max_length" ), positions[2],
74 wxTE_CENTRE | wxTE_PROCESS_ENTER );
76 std::make_unique<UNIT_BINDER>( &m_unitsProvider, eventSource, nullptr, maxLengthField->GetControl(),
77 maxLengthField->GetLabel(), false, false );
78 maxLengthField->SetUnitBinder( m_maxLengthBinder.get() );
79
80 auto notifyModified = [this]( wxCommandEvent& )
81 {
83 if( dlg )
84 dlg->SetModified();
85 };
86
87 minLengthField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
88 optLengthField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
89 maxLengthField->GetControl()->Bind( wxEVT_TEXT, notifyModified );
90
91 auto notifySave = [this]( wxCommandEvent& aEvent )
92 {
94 if( dlg )
95 dlg->OnSave( aEvent );
96 };
97
98 minLengthField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
99 optLengthField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
100 maxLengthField->GetControl()->Bind( wxEVT_TEXT_ENTER, notifySave );
101
102 // Position all fields and update the panel layout
105}
106
107
109{
110 if( !m_data )
111 return false;
112
113 m_minLengthBinder->SetDoubleValue( pcbIUScale.mmToIU( m_data->GetMinimumLength() ) );
114 m_optLengthBinder->SetDoubleValue( pcbIUScale.mmToIU( m_data->GetOptimumLength() ) );
115 m_maxLengthBinder->SetDoubleValue( pcbIUScale.mmToIU( m_data->GetMaximumLength() ) );
116
117 return true;
118}
119
120
122{
123 if( !m_data )
124 return false;
125
126 m_data->SetMinimumLength( pcbIUScale.IUTomm( m_minLengthBinder->GetDoubleValue() ) );
127 m_data->SetOptimumLength( pcbIUScale.IUTomm( m_optLengthBinder->GetDoubleValue() ) );
128 m_data->SetMaximumLength( pcbIUScale.IUTomm( m_maxLengthBinder->GetDoubleValue() ) );
129
130 return true;
131}
132
133
135 wxString* aValidationMessage )
136{
138
139 VALIDATION_RESULT result = m_data->Validate();
140
141 if( !result.isValid )
142 {
143 *aErrorCount = result.errors.size();
144
145 for( size_t i = 0; i < result.errors.size(); i++ )
146 *aValidationMessage += DRC_RULE_EDITOR_UTILS::FormatErrorMessage( i + 1, result.errors[i] );
147
148 return false;
149 }
150
151 return true;
152}
153
154
156{
157 if( !m_data )
158 return wxEmptyString;
159
160 return m_data->GenerateRule( aContext );
161}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
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 * 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.
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
Result of a validation operation.
wxString result
Test unit parsing edge cases and error handling.