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 // This can be -1 if uninitialized
106 const int currSelection = m_anchorOptions->GetSelection();
107 if( currSelection < 0 || currSelection >= static_cast<int>( m_menuIDs.size() ) )
108 m_anchorOptions->SetSelection( 0 );
109}
110
111
112void DIALOG_MOVE_EXACT::ToPolarDeg( double x, double y, double& r, EDA_ANGLE& q )
113{
114 // convert to polar coordinates
115 r = hypot( x, y );
116
117 q = ( r != 0) ? EDA_ANGLE( VECTOR2D( x, y ) ) : ANGLE_0;
118}
119
120
121bool DIALOG_MOVE_EXACT::GetTranslationInIU( wxRealPoint& val, bool polar )
122{
123 if( polar )
124 {
125 const double r = m_moveX.GetDoubleValue();
126 const EDA_ANGLE q = m_moveY.GetAngleValue();
127
128 val.x = r * q.Cos();
129 val.y = r * q.Sin();
130 }
131 else
132 {
133 // direct read
134 val.x = m_moveX.GetDoubleValue();
135 val.y = m_moveY.GetDoubleValue();
136 }
137
138 // no validation to do here, but in future, you could return false here
139 return true;
140}
141
142
143void DIALOG_MOVE_EXACT::OnPolarChanged( wxCommandEvent& event )
144{
145 bool newPolar = m_polarCoords->IsChecked();
146 double moveX = m_moveX.GetDoubleValue();
147 double moveY = m_moveY.GetDoubleValue();
148 updateDialogControls( newPolar );
149
150 if( newPolar )
151 {
152 if( moveX != m_stateX || moveY != m_stateY )
153 {
154 m_stateX = moveX;
155 m_stateY = moveY;
157
158 m_moveX.SetDoubleValue( m_stateRadius );
159 m_stateRadius = m_moveX.GetDoubleValue();
160 m_moveY.SetAngleValue( m_stateTheta );
161 m_stateTheta = m_moveY.GetAngleValue();
162 }
163 else
164 {
165 m_moveX.SetDoubleValue( m_stateRadius );
166 m_moveY.SetAngleValue( m_stateTheta );
167 }
168 }
169 else
170 {
171 if( moveX != m_stateRadius || moveY != m_stateTheta.AsDegrees() )
172 {
173 m_stateRadius = moveX;
174 m_stateTheta = EDA_ANGLE( moveY, DEGREES_T );
177
178 m_moveX.SetDoubleValue( m_stateX );
179 m_stateX = m_moveX.GetDoubleValue();
180 m_moveY.SetDoubleValue( m_stateY );
181 m_stateY = m_moveY.GetDoubleValue();
182 }
183 else
184 {
185 m_moveX.SetDoubleValue( m_stateX );
186 m_moveY.SetDoubleValue( m_stateY );
187 }
188 }
189}
190
191
193{
194 if( aPolar )
195 {
196 m_moveX.SetLabel( _( "Distance:" ) ); // Polar radius
197 m_moveY.SetLabel( _( "Angle:" ) ); // Polar theta or angle
198 m_moveY.SetUnits( EDA_UNITS::DEGREES );
199 }
200 else
201 {
202 m_moveX.SetLabel( _( "Move X:" ) );
203 m_moveY.SetLabel( _( "Move Y:" ) );
204 m_moveY.SetUnits( GetUserUnits() );
205 }
206
207 Layout();
208}
209
210
211void DIALOG_MOVE_EXACT::OnClear( wxCommandEvent& event )
212{
213 wxObject* obj = event.GetEventObject();
214
215 if( obj == m_clearX )
216 {
217 m_moveX.SetValue( 0 );
218 }
219 else if( obj == m_clearY )
220 {
221 m_moveY.SetValue( 0 );
222 }
223 else if( obj == m_clearRot )
224 {
225 m_rotate.SetAngleValue( ANGLE_0 );
226 }
227
228 // Keep m_stdButtonsOK focused to allow enter key activate the OK button
229 m_stdButtonsOK->SetFocus();
230}
231
232
234{
235 updateDialogControls( m_polarCoords->GetValue() );
236
237 // Force the evaluation when setting previous values
238 m_moveX.RequireEval();
239 m_moveY.RequireEval();
240 m_rotate.RequireEval();
241
242 return true;
243}
244
245
247{
248 // for the output, we only deliver a Cartesian vector
249 wxRealPoint translation;
250 bool ok = GetTranslationInIU( translation, m_polarCoords->IsChecked() );
251 m_translation.x = KiROUND(translation.x);
252 m_translation.y = KiROUND(translation.y);
253 m_rotation = m_rotate.GetAngleValue();
254
255 const int anchorSelection = m_anchorOptions->GetSelection();
256 wxCHECK_MSG( anchorSelection >= 0 && anchorSelection < static_cast<int>( m_menuIDs.size() ), false,
257 wxString::Format( "Invalid rotation anchor selection: %d", anchorSelection ) );
258
259 m_rotationAnchor = m_menuIDs[anchorSelection];
260
261 return ok;
262}
263
264
265void DIALOG_MOVE_EXACT::OnTextFocusLost( wxFocusEvent& event )
266{
267 wxTextCtrl* obj = static_cast<wxTextCtrl*>( event.GetEventObject() );
268
269 if( obj->GetValue().IsEmpty() )
270 obj->SetValue( "0" );
271
272 event.Skip();
273}
274
275
276void DIALOG_MOVE_EXACT::OnTextChanged( wxCommandEvent& event )
277{
278 double delta_x = m_moveX.GetDoubleValue();
279 double delta_y = m_moveY.GetDoubleValue();
280 double max_border = std::numeric_limits<int>::max() * M_SQRT1_2;
281
282 if( m_bbox.GetLeft() + delta_x < -max_border ||
283 m_bbox.GetRight() + delta_x > max_border ||
284 m_bbox.GetTop() + delta_y < -max_border ||
285 m_bbox.GetBottom() + delta_y > max_border )
286 {
287 const wxString invalid_length = _( "Invalid movement values. Movement would place selection "
288 "outside of the maximum board area." );
289
290 m_xEntry->SetToolTip( invalid_length );
291 m_xEntry->SetForegroundColour( *wxRED );
292 m_yEntry->SetToolTip( invalid_length );
293 m_yEntry->SetForegroundColour( *wxRED );
294 m_stdButtons->GetAffirmativeButton()->Disable();
295 }
296 else
297 {
298 m_xEntry->SetToolTip( "" );
299 m_xEntry->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ) );
300 m_yEntry->SetToolTip( "" );
301 m_yEntry->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ) );
302 m_stdButtons->GetAffirmativeButton()->Enable();
303 event.Skip();
304 }
305}
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