KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_set_offset.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 The 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
24#include "dialog_set_offset.h"
25
26
27DIALOG_SET_OFFSET::DIALOG_SET_OFFSET( PCB_BASE_FRAME& aParent, VECTOR2I& aOffset, bool aClearToZero ) :
28 DIALOG_SET_OFFSET_BASE( &aParent ), m_clearToZero( aClearToZero ),
29 m_originalOffset( aOffset ), m_updatedOffset( aOffset ),
30 m_xOffset( &aParent, m_xLabel, m_xEntry, m_xUnit ),
31 m_yOffset( &aParent, m_yLabel, m_yEntry, m_yUnit ),
32 m_stateX( 0.0 ),
33 m_stateY( 0.0 ),
34 m_stateRadius( 0.0 ),
36{
39
41
42 updateDialogControls( m_polarCoords->IsChecked() );
43
44 if( m_clearToZero )
45 {
46 wxString text = _( "Clear" );
47 m_clearX->SetLabel( text );
48 m_clearY->SetLabel( text );
49
50 text = _( "Reset this value to zero." );
51 m_clearX->SetToolTip( text );
52 m_clearY->SetToolTip( text );
53 }
54 else
55 {
56 wxString text = _( "Reset" );
57 m_clearX->SetLabel( text );
58 m_clearY->SetLabel( text );
59
60 text = _( "Reset this value to the original value." );
61 m_clearX->SetToolTip( text );
62 m_clearY->SetToolTip( text );
63 }
64
66
68}
69
70
71void DIALOG_SET_OFFSET::OnTextFocusLost( wxFocusEvent& event )
72{
73 wxTextCtrl* obj = static_cast<wxTextCtrl*>( event.GetEventObject() );
74
75 if( obj->GetValue().IsEmpty() )
76 obj->SetValue( "0" );
77
78 event.Skip();
79}
80
81
82static void ToPolarDeg( double x, double y, double& r, EDA_ANGLE& q )
83{
84 // convert to polar coordinates
85 r = hypot( x, y );
86
87 q = ( r != 0 ) ? EDA_ANGLE( VECTOR2D( x, y ) ) : ANGLE_0;
88}
89
90
91void DIALOG_SET_OFFSET::OnClear( wxCommandEvent& event )
92{
93 if( m_clearToZero )
94 {
95 m_xOffset.SetDoubleValue( 0.0 );
96 m_yOffset.SetDoubleValue( 0.0 );
97
98 m_stateX = 0.0;
99 m_stateY = 0.0;
100 m_stateRadius = 0.0;
102 return;
103 }
104
105 const wxObject* const obj = event.GetEventObject();
106 VECTOR2I offset = m_originalOffset;
107 double r;
108 EDA_ANGLE q;
109 ToPolarDeg( offset.x, offset.y, r, q );
110
111 if( obj == m_clearX )
112 {
113 m_stateX = offset.x;
114 m_xOffset.SetDoubleValue( r );
115 m_stateRadius = m_xOffset.GetDoubleValue();
116
117 if( m_polarCoords->IsChecked() )
118 m_xOffset.SetDoubleValue( m_stateRadius );
119 else
120 m_xOffset.SetValue( m_stateX );
121 }
122 else if( obj == m_clearY )
123 {
124 m_stateY = offset.y;
125 m_yOffset.SetAngleValue( q );
126 m_stateTheta = m_yOffset.GetAngleValue();
127
128 if( m_polarCoords->IsChecked() )
129 m_yOffset.SetAngleValue( m_stateTheta );
130 else
131 m_yOffset.SetValue( m_stateY );
132 }
133}
134
135void DIALOG_SET_OFFSET::OnPolarChanged( wxCommandEvent& event )
136{
137 bool newPolar = m_polarCoords->IsChecked();
138 double xOffset = m_xOffset.GetDoubleValue();
139 double yOffset = m_yOffset.GetDoubleValue();
140 updateDialogControls( newPolar );
141
142 if( newPolar )
143 {
144 if( xOffset != m_stateX || yOffset != m_stateY )
145 {
146 m_stateX = xOffset;
147 m_stateY = yOffset;
149
150 m_xOffset.SetDoubleValue( m_stateRadius );
151 m_stateRadius = m_xOffset.GetDoubleValue();
152 m_yOffset.SetAngleValue( m_stateTheta );
153 m_stateTheta = m_yOffset.GetAngleValue();
154 }
155 else
156 {
157 m_xOffset.SetDoubleValue( m_stateRadius );
158 m_yOffset.SetAngleValue( m_stateTheta );
159 }
160 }
161 else
162 {
163 if( xOffset != m_stateRadius || yOffset != m_stateTheta.AsDegrees() )
164 {
165 m_stateRadius = xOffset;
166 m_stateTheta = EDA_ANGLE( yOffset, DEGREES_T );
169
170 m_xOffset.SetDoubleValue( m_stateX );
171 m_stateX = m_xOffset.GetDoubleValue();
172 m_yOffset.SetDoubleValue( m_stateY );
173 m_stateY = m_yOffset.GetDoubleValue();
174 }
175 else
176 {
177 m_xOffset.SetDoubleValue( m_stateX );
178 m_yOffset.SetDoubleValue( m_stateY );
179 }
180 }
181}
182
184{
185 if( aPolar )
186 {
187 m_xOffset.SetLabel( _( "Distance:" ) ); // Polar radius
188 m_yOffset.SetLabel( _( "Angle:" ) ); // Polar theta or angle
189 m_yOffset.SetUnits( EDA_UNITS::DEGREES );
190 m_clearX->SetToolTip( _( "Reset to the current distance from the reference position." ) );
191 m_clearY->SetToolTip( _( "Reset to the current angle from the reference position." ) );
192 }
193 else
194 {
195 m_xOffset.SetLabel( _( "Offset X:" ) );
196 m_yOffset.SetLabel( _( "Offset Y:" ) );
197 m_yOffset.SetUnits( GetUserUnits() );
198 m_clearX->SetToolTip( _( "Reset to the current X offset from the reference position." ) );
199 m_clearY->SetToolTip( _( "Reset to the current Y offset from the reference position." ) );
200 }
201}
202
204{
205 m_xOffset.SetValue( m_originalOffset.x );
206 m_yOffset.SetValue( m_originalOffset.y );
207
208 return true;
209}
210
212{
213 m_updatedOffset.x = m_xOffset.GetValue();
214 m_updatedOffset.y = m_yOffset.GetValue();
215
216 return true;
217}
DIALOG_SET_OFFSET_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Set Offset"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
virtual bool TransferDataToWindow() override
virtual void OnPolarChanged(wxCommandEvent &event) override
void updateDialogControls(bool aPolar)
virtual void OnTextFocusLost(wxFocusEvent &event) override
virtual bool TransferDataFromWindow() override
DIALOG_SET_OFFSET(PCB_BASE_FRAME &aFrame, VECTOR2I &aOffset, bool aClearToZero)
const VECTOR2I m_originalOffset
virtual void OnClear(wxCommandEvent &event) override
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition dialog_shim.h:82
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
EDA_UNITS GetUserUnits() const
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
static void ToPolarDeg(double x, double y, double &r, EDA_ANGLE &q)
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:411
@ DEGREES_T
Definition eda_angle.h:31
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694