KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_move_exact.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) 2014 John Beard, [email protected]
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
26#include <math/box2.h>
27#include <math/util.h> // for KiROUND
29#include <pcb_edit_frame.h>
30#include <trigo.h>
31
32
33
35 ROTATION_ANCHOR& aAnchor, const BOX2I& aBbox ) :
36 DIALOG_MOVE_EXACT_BASE( aParent ),
37 m_translation( aTranslate ),
38 m_rotation( aRotate ),
39 m_rotationAnchor( aAnchor ),
40 m_bbox( aBbox ),
41 m_moveX( aParent, m_xLabel, m_xEntry, m_xUnit ),
42 m_moveY( aParent, m_yLabel, m_yEntry, m_yUnit ),
44 m_stateX( 0.0 ),
45 m_stateY( 0.0 ),
46 m_stateRadius( 0.0 )
47{
48 // We can't set the tab order through wxWidgets due to shortcomings in their mnemonics
49 // implementation on MSW
50 m_tabOrder = {
57 };
58
59 // Configure display origin transforms
62
63 m_menuIDs.push_back( aAnchor );
65
66 if( aParent->IsType( FRAME_PCB_EDITOR ) )
68
69 // DIALOG_SHIM needs a title- and anchor-options-specific hash_key so we don't save/restore state
70 // between usage cases.
71 m_hash_key = GetTitle().ToStdString();
72
73 if( aAnchor == ROTATE_AROUND_SEL_CENTER )
74 m_hash_key += "|with_selection";
75
76 if( aParent->IsType( FRAME_PCB_EDITOR ) )
77 m_hash_key += "|pcb_editor";
78
80 m_rotate.SetUnits( EDA_UNITS::DEGREES );
81
83
85}
86
87
89{
90 wxArrayString menuItems;
91
92 for( const ROTATION_ANCHOR& anchorID : m_menuIDs )
93 {
94 switch( anchorID )
95 {
96 case ROTATE_AROUND_ITEM_ANCHOR: menuItems.push_back( _( "Rotate around item anchor" ) ); break;
97 case ROTATE_AROUND_SEL_CENTER: menuItems.push_back( _( "Rotate around selection center" ) ); break;
98 case ROTATE_AROUND_USER_ORIGIN: menuItems.push_back( _( "Rotate around local coordinates origin" ) ); break;
99 case ROTATE_AROUND_AUX_ORIGIN: menuItems.push_back( _( "Rotate around drill/place origin" ) ); break;
100 }
101 }
102
103 m_anchorOptions->Set( menuItems );
104}
105
106
107void DIALOG_MOVE_EXACT::ToPolarDeg( double x, double y, double& r, EDA_ANGLE& q )
108{
109 // convert to polar coordinates
110 r = hypot( x, y );
111
112 q = ( r != 0) ? EDA_ANGLE( VECTOR2D( x, y ) ) : ANGLE_0;
113}
114
115
116bool DIALOG_MOVE_EXACT::GetTranslationInIU( wxRealPoint& val, bool polar )
117{
118 if( polar )
119 {
120 const double r = m_moveX.GetDoubleValue();
121 const EDA_ANGLE q = m_moveY.GetAngleValue();
122
123 val.x = r * q.Cos();
124 val.y = r * q.Sin();
125 }
126 else
127 {
128 // direct read
129 val.x = m_moveX.GetDoubleValue();
130 val.y = m_moveY.GetDoubleValue();
131 }
132
133 // no validation to do here, but in future, you could return false here
134 return true;
135}
136
137
138void DIALOG_MOVE_EXACT::OnPolarChanged( wxCommandEvent& event )
139{
140 bool newPolar = m_polarCoords->IsChecked();
141 double moveX = m_moveX.GetDoubleValue();
142 double moveY = m_moveY.GetDoubleValue();
143 updateDialogControls( newPolar );
144
145 if( newPolar )
146 {
147 if( moveX != m_stateX || moveY != m_stateY )
148 {
149 m_stateX = moveX;
150 m_stateY = moveY;
152
153 m_moveX.SetDoubleValue( m_stateRadius );
154 m_stateRadius = m_moveX.GetDoubleValue();
155 m_moveY.SetAngleValue( m_stateTheta );
156 m_stateTheta = m_moveY.GetAngleValue();
157 }
158 else
159 {
160 m_moveX.SetDoubleValue( m_stateRadius );
161 m_moveY.SetAngleValue( m_stateTheta );
162 }
163 }
164 else
165 {
166 if( moveX != m_stateRadius || moveY != m_stateTheta.AsDegrees() )
167 {
168 m_stateRadius = moveX;
169 m_stateTheta = EDA_ANGLE( moveY, DEGREES_T );
172
173 m_moveX.SetDoubleValue( m_stateX );
174 m_stateX = m_moveX.GetDoubleValue();
175 m_moveY.SetDoubleValue( m_stateY );
176 m_stateY = m_moveY.GetDoubleValue();
177 }
178 else
179 {
180 m_moveX.SetDoubleValue( m_stateX );
181 m_moveY.SetDoubleValue( m_stateY );
182 }
183 }
184}
185
186
188{
189 if( aPolar )
190 {
191 m_moveX.SetLabel( _( "Distance:" ) ); // Polar radius
192 m_moveY.SetLabel( _( "Angle:" ) ); // Polar theta or angle
193 m_moveY.SetUnits( EDA_UNITS::DEGREES );
194 }
195 else
196 {
197 m_moveX.SetLabel( _( "Move X:" ) );
198 m_moveY.SetLabel( _( "Move Y:" ) );
199 m_moveY.SetUnits( GetUserUnits() );
200 }
201
202 Layout();
203}
204
205
206void DIALOG_MOVE_EXACT::OnClear( wxCommandEvent& event )
207{
208 wxObject* obj = event.GetEventObject();
209
210 if( obj == m_clearX )
211 {
212 m_moveX.SetValue( 0 );
213 }
214 else if( obj == m_clearY )
215 {
216 m_moveY.SetValue( 0 );
217 }
218 else if( obj == m_clearRot )
219 {
220 m_rotate.SetAngleValue( ANGLE_0 );
221 }
222
223 // Keep m_stdButtonsOK focused to allow enter key activate the OK button
224 m_stdButtonsOK->SetFocus();
225}
226
227
229{
230 updateDialogControls( m_polarCoords->GetValue() );
231
232 // Force the evaluation when setting previous values
233 m_moveX.RequireEval();
234 m_moveY.RequireEval();
235 m_rotate.RequireEval();
236
237 return true;
238}
239
240
242{
243 // for the output, we only deliver a Cartesian vector
244 wxRealPoint translation;
245 bool ok = GetTranslationInIU( translation, m_polarCoords->IsChecked() );
246 m_translation.x = KiROUND(translation.x);
247 m_translation.y = KiROUND(translation.y);
248 m_rotation = m_rotate.GetAngleValue();
249 m_rotationAnchor = m_menuIDs[ m_anchorOptions->GetSelection() ];
250
251 return ok;
252}
253
254
255void DIALOG_MOVE_EXACT::OnTextFocusLost( wxFocusEvent& event )
256{
257 wxTextCtrl* obj = static_cast<wxTextCtrl*>( event.GetEventObject() );
258
259 if( obj->GetValue().IsEmpty() )
260 obj->SetValue( "0" );
261
262 event.Skip();
263}
264
265
266void DIALOG_MOVE_EXACT::OnTextChanged( wxCommandEvent& event )
267{
268 double delta_x = m_moveX.GetDoubleValue();
269 double delta_y = m_moveY.GetDoubleValue();
270 double max_border = std::numeric_limits<int>::max() * M_SQRT1_2;
271
272 if( m_bbox.GetLeft() + delta_x < -max_border ||
273 m_bbox.GetRight() + delta_x > max_border ||
274 m_bbox.GetTop() + delta_y < -max_border ||
275 m_bbox.GetBottom() + delta_y > max_border )
276 {
277 const wxString invalid_length = _( "Invalid movement values. Movement would place selection "
278 "outside of the maximum board area." );
279
280 m_xEntry->SetToolTip( invalid_length );
281 m_xEntry->SetForegroundColour( *wxRED );
282 m_yEntry->SetToolTip( invalid_length );
283 m_yEntry->SetForegroundColour( *wxRED );
284 m_stdButtons->GetAffirmativeButton()->Disable();
285 }
286 else
287 {
288 m_xEntry->SetToolTip( "" );
289 m_xEntry->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ) );
290 m_yEntry->SetToolTip( "" );
291 m_yEntry->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ) );
292 m_stdButtons->GetAffirmativeButton()->Enable();
293 event.Skip();
294 }
295}
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
DIALOG_MOVE_EXACT_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Move Item"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
wxStdDialogButtonSizer * m_stdButtons
bool TransferDataToWindow() override
bool TransferDataFromWindow() override
ROTATION_ANCHOR & m_rotationAnchor
void ToPolarDeg(double x, double y, double &r, EDA_ANGLE &q)
Convert a given Cartesian point into a polar representation.
void updateDialogControls(bool aPolar)
void OnClear(wxCommandEvent &event) override
DIALOG_MOVE_EXACT(PCB_BASE_FRAME *aParent, VECTOR2I &aTranslate, EDA_ANGLE &aRotate, ROTATION_ANCHOR &aAnchor, const BOX2I &aBbox)
void OnTextFocusLost(wxFocusEvent &event) override
Reset a text field to be 0 if it was exited while blank.
void OnPolarChanged(wxCommandEvent &event) override
void OnTextChanged(wxCommandEvent &event) override
bool GetTranslationInIU(wxRealPoint &val, bool polar)
Get the (Cartesian) translation described by the text entries.
std::vector< ROTATION_ANCHOR > m_menuIDs
std::vector< wxWindow * > m_tabOrder
void SetupStandardButtons(std::map< int, wxString > aLabels={})
std::string m_hash_key
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
double Sin() const
Definition eda_angle.h:178
double Cos() const
Definition eda_angle.h:197
bool IsType(FRAME_T aType) const
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
ROTATION_ANCHOR
@ ROTATE_AROUND_USER_ORIGIN
@ ROTATE_AROUND_SEL_CENTER
@ ROTATE_AROUND_AUX_ORIGIN
@ ROTATE_AROUND_ITEM_ANCHOR
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:411
@ DEGREES_T
Definition eda_angle.h:31
@ FRAME_PCB_EDITOR
Definition frame_type.h:42
Functions for manipulating tab traversal in forms and dialogs.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694