KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_offset_item.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_offset_item.h"
25
26
28 DIALOG_OFFSET_ITEM_BASE( &aParent ),
29 m_originalOffset( aOffset ),
30 m_updatedOffset( aOffset ),
31 m_xOffset( &aParent, m_xLabel, m_xEntry, m_xUnit ),
32 m_yOffset( &aParent, m_yLabel, m_yEntry, m_yUnit ),
33 m_stateX( 0.0 ),
34 m_stateY( 0.0 ),
35 m_stateRadius( 0.0 ),
37{
40
42
44
46}
47
48
49void DIALOG_OFFSET_ITEM::OnTextFocusLost( wxFocusEvent& event )
50{
51 wxTextCtrl* obj = static_cast<wxTextCtrl*>( event.GetEventObject() );
52
53 if( obj->GetValue().IsEmpty() )
54 obj->SetValue( "0" );
55
56 event.Skip();
57}
58
59
60static void ToPolar( double x, double y, double& r, EDA_ANGLE& q )
61{
62 // convert to polar coordinates
63 r = hypot( x, y );
64
65 q = ( r != 0 ) ? EDA_ANGLE( VECTOR2D( x, y ) ) : ANGLE_0;
66}
67
68
69void DIALOG_OFFSET_ITEM::OnClear( wxCommandEvent& event )
70{
71 const wxObject* const obj = event.GetEventObject();
73 double r;
74 EDA_ANGLE q;
75 ToPolar( offset.x, offset.y, r, q );
76
77 if( obj == m_clearX )
78 {
79 m_stateX = offset.x;
80 m_xOffset.SetDoubleValue( r );
81 m_stateRadius = m_xOffset.GetDoubleValue();
82
83 if( m_polarCoords->IsChecked() )
84 m_xOffset.SetDoubleValue( m_stateRadius );
85 else
86 m_xOffset.SetValue( m_stateX );
87 }
88 else if( obj == m_clearY )
89 {
90 m_stateY = offset.y;
91 m_yOffset.SetAngleValue( q );
92 m_stateTheta = m_yOffset.GetAngleValue();
93
94 if( m_polarCoords->IsChecked() )
95 m_yOffset.SetAngleValue( m_stateTheta );
96 else
97 m_yOffset.SetValue( m_stateY );
98 }
99}
100
101void DIALOG_OFFSET_ITEM::OnPolarChanged( wxCommandEvent& event )
102{
103 bool newPolar = m_polarCoords->IsChecked();
104 double xOffset = m_xOffset.GetDoubleValue();
105 double yOffset = m_yOffset.GetDoubleValue();
106 updateDialogControls( newPolar );
107
108 if( newPolar )
109 {
110 if( xOffset != m_stateX || yOffset != m_stateY )
111 {
112 m_stateX = xOffset;
113 m_stateY = yOffset;
115
116 m_xOffset.SetDoubleValue( m_stateRadius );
117 m_stateRadius = m_xOffset.GetDoubleValue();
118 m_yOffset.SetAngleValue( m_stateTheta );
119 m_stateTheta = m_yOffset.GetAngleValue();
120 }
121 else
122 {
123 m_xOffset.SetDoubleValue( m_stateRadius );
124 m_yOffset.SetAngleValue( m_stateTheta );
125 }
126 }
127 else
128 {
129 if( xOffset != m_stateRadius || yOffset != m_stateTheta.AsDegrees() )
130 {
131 m_stateRadius = xOffset;
132 m_stateTheta = EDA_ANGLE( yOffset, DEGREES_T );
135
136 m_xOffset.SetDoubleValue( m_stateX );
137 m_stateX = m_xOffset.GetDoubleValue();
138 m_yOffset.SetDoubleValue( m_stateY );
139 m_stateY = m_yOffset.GetDoubleValue();
140 }
141 else
142 {
143 m_xOffset.SetDoubleValue( m_stateX );
144 m_yOffset.SetDoubleValue( m_stateY );
145 }
146 }
147}
148
150{
151 if( aPolar )
152 {
153 m_xOffset.SetLabel( _( "Distance:" ) ); // Polar radius
154 m_yOffset.SetLabel( _( "Angle:" ) ); // Polar theta or angle
155 m_yOffset.SetUnits( EDA_UNITS::DEGREES );
156 m_clearX->SetToolTip( _( "Reset to the current distance from the reference position." ) );
157 m_clearY->SetToolTip( _( "Reset to the current angle from the reference position." ) );
158 }
159 else
160 {
161 m_xOffset.SetLabel( _( "Offset X:" ) );
162 m_yOffset.SetLabel( _( "Offset Y:" ) );
163 m_yOffset.SetUnits( GetUserUnits() );
164 m_clearX->SetToolTip( _( "Reset to the current X offset from the reference position." ) );
165 m_clearY->SetToolTip( _( "Reset to the current Y offset from the reference position." ) );
166 }
167}
168
170{
171 m_xOffset.ChangeValue( m_originalOffset.x );
172 m_yOffset.ChangeValue( m_originalOffset.y );
173
174 if( m_polarCoords->GetValue() )
175 {
176 wxCommandEvent dummy;
178 }
179
180 return true;
181}
182
184{
185 if( m_polarCoords->GetValue() )
186 {
187 m_stateRadius = m_xOffset.GetDoubleValue();
188 m_stateTheta = m_yOffset.GetAngleValue();
189
192 }
193 else
194 {
195 m_updatedOffset.x = m_xOffset.GetIntValue();
196 m_updatedOffset.y = m_yOffset.GetIntValue();
197 }
198
199 return true;
200}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
DIALOG_OFFSET_ITEM_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Offset Item"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
DIALOG_OFFSET_ITEM(PCB_BASE_FRAME &aFrame, VECTOR2I &aOffset)
void updateDialogControls(bool aPolar)
const VECTOR2I m_originalOffset
virtual bool TransferDataToWindow() override
virtual bool TransferDataFromWindow() override
virtual void OnPolarChanged(wxCommandEvent &event) override
virtual void OnTextFocusLost(wxFocusEvent &event) override
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 ToPolar(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
std::vector< FAB_LAYER_COLOR > dummy
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694