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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "dialog_offset_item.h"
21
22
24 DIALOG_OFFSET_ITEM_BASE( &aParent ),
25 m_originalOffset( aOffset ),
26 m_updatedOffset( aOffset ),
27 m_xOffset( &aParent, m_xLabel, m_xEntry, m_xUnit ),
28 m_yOffset( &aParent, m_yLabel, m_yEntry, m_yUnit ),
29 m_stateX( 0.0 ),
30 m_stateY( 0.0 ),
31 m_stateRadius( 0.0 ),
33{
36
38
40
42}
43
44
45void DIALOG_OFFSET_ITEM::OnTextFocusLost( wxFocusEvent& event )
46{
47 wxTextCtrl* obj = static_cast<wxTextCtrl*>( event.GetEventObject() );
48
49 if( obj->GetValue().IsEmpty() )
50 obj->SetValue( "0" );
51
52 event.Skip();
53}
54
55
56static void ToPolar( double x, double y, double& r, EDA_ANGLE& q )
57{
58 // convert to polar coordinates
59 r = hypot( x, y );
60
61 q = ( r != 0 ) ? EDA_ANGLE( VECTOR2D( x, y ) ) : ANGLE_0;
62}
63
64
65void DIALOG_OFFSET_ITEM::OnClear( wxCommandEvent& event )
66{
67 const wxObject* const obj = event.GetEventObject();
69 double r;
70 EDA_ANGLE q;
71 ToPolar( offset.x, offset.y, r, q );
72
73 if( obj == m_clearX )
74 {
75 m_stateX = offset.x;
76 m_xOffset.SetDoubleValue( r );
77 m_stateRadius = m_xOffset.GetDoubleValue();
78
79 if( m_polarCoords->IsChecked() )
80 m_xOffset.SetDoubleValue( m_stateRadius );
81 else
82 m_xOffset.SetValue( m_stateX );
83 }
84 else if( obj == m_clearY )
85 {
86 m_stateY = offset.y;
87 m_yOffset.SetAngleValue( q );
88 m_stateTheta = m_yOffset.GetAngleValue();
89
90 if( m_polarCoords->IsChecked() )
91 m_yOffset.SetAngleValue( m_stateTheta );
92 else
93 m_yOffset.SetValue( m_stateY );
94 }
95}
96
97void DIALOG_OFFSET_ITEM::OnPolarChanged( wxCommandEvent& event )
98{
99 bool newPolar = m_polarCoords->IsChecked();
100 double xOffset = m_xOffset.GetDoubleValue();
101 double yOffset = m_yOffset.GetDoubleValue();
102 updateDialogControls( newPolar );
103
104 if( newPolar )
105 {
106 if( xOffset != m_stateX || yOffset != m_stateY )
107 {
108 m_stateX = xOffset;
109 m_stateY = yOffset;
111
112 m_xOffset.SetDoubleValue( m_stateRadius );
113 m_stateRadius = m_xOffset.GetDoubleValue();
114 m_yOffset.SetAngleValue( m_stateTheta );
115 m_stateTheta = m_yOffset.GetAngleValue();
116 }
117 else
118 {
119 m_xOffset.SetDoubleValue( m_stateRadius );
120 m_yOffset.SetAngleValue( m_stateTheta );
121 }
122 }
123 else
124 {
125 if( xOffset != m_stateRadius || yOffset != m_stateTheta.AsDegrees() )
126 {
127 m_stateRadius = xOffset;
128 m_stateTheta = EDA_ANGLE( yOffset, DEGREES_T );
131
132 m_xOffset.SetDoubleValue( m_stateX );
133 m_stateX = m_xOffset.GetDoubleValue();
134 m_yOffset.SetDoubleValue( m_stateY );
135 m_stateY = m_yOffset.GetDoubleValue();
136 }
137 else
138 {
139 m_xOffset.SetDoubleValue( m_stateX );
140 m_yOffset.SetDoubleValue( m_stateY );
141 }
142 }
143}
144
146{
147 if( aPolar )
148 {
149 m_xOffset.SetLabel( _( "Distance:" ) ); // Polar radius
150 m_yOffset.SetLabel( _( "Angle:" ) ); // Polar theta or angle
151 m_yOffset.SetUnits( EDA_UNITS::DEGREES );
152 m_clearX->SetToolTip( _( "Reset to the current distance from the reference position." ) );
153 m_clearY->SetToolTip( _( "Reset to the current angle from the reference position." ) );
154 }
155 else
156 {
157 m_xOffset.SetLabel( _( "Offset X:" ) );
158 m_yOffset.SetLabel( _( "Offset Y:" ) );
159 m_yOffset.SetUnits( GetUserUnits() );
160 m_clearX->SetToolTip( _( "Reset to the current X offset from the reference position." ) );
161 m_clearY->SetToolTip( _( "Reset to the current Y offset from the reference position." ) );
162 }
163}
164
166{
167 m_xOffset.ChangeValue( m_originalOffset.x );
168 m_yOffset.ChangeValue( m_originalOffset.y );
169
170 if( m_polarCoords->GetValue() )
171 {
172 wxCommandEvent dummy;
174 }
175
176 return true;
177}
178
180{
181 if( m_polarCoords->GetValue() )
182 {
183 m_stateRadius = m_xOffset.GetDoubleValue();
184 m_stateTheta = m_yOffset.GetAngleValue();
185
188 }
189 else
190 {
191 m_updatedOffset.x = m_xOffset.GetIntValue();
192 m_updatedOffset.y = m_yOffset.GetIntValue();
193 }
194
195 return true;
196}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
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:79
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:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682