KiCad PCB EDA Suite
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, john.j.beard@gmail.com
5  * Copyright (C) 2018 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/util.h> // for KiROUND
27 #include <widgets/tab_traversal.h>
28 #include <pcb_edit_frame.h>
29 
30 // initialise statics
32 
33 
35  double& aRotate, ROTATION_ANCHOR& aAnchor,
36  const EDA_RECT& aBbox ) :
37  DIALOG_MOVE_EXACT_BASE( aParent ),
38  m_translation( aTranslate ),
39  m_rotation( aRotate ),
40  m_rotationAnchor( aAnchor ),
41  m_bbox( aBbox ),
42  m_moveX( aParent, m_xLabel, m_xEntry, m_xUnit ),
43  m_moveY( aParent, m_yLabel, m_yEntry, m_yUnit ),
44  m_rotate( aParent, m_rotLabel, m_rotEntry, m_rotUnit ),
45  m_stateX( 0.0 ),
46  m_stateY( 0.0 ),
47  m_stateRadius( 0.0 ),
48  m_stateTheta( 0.0 )
49 {
50  // We can't set the tab order through wxWidgets due to shortcomings in their mnemonics
51  // implementation on MSW
52  m_tabOrder = {
53  m_xEntry,
54  m_yEntry,
55  m_rotEntry,
59  };
60 
62 
63  m_menuIDs.push_back( aAnchor );
65 
66  if( aParent->IsType( FRAME_PCB_EDITOR ) )
68 
70 
71  // and set up the entries according to the saved options
72  m_polarCoords->SetValue( m_options.polarCoords );
75 
78  m_anchorOptions->SetSelection( std::min( m_options.entryAnchorSelection, m_menuIDs.size() ) );
79 
80  m_stdButtonsOK->SetDefault();
81 
83 }
84 
85 
87 {
88  wxArrayString menuItems;
89 
90  for( auto anchorID : m_menuIDs )
91  {
92  switch( anchorID )
93  {
95  menuItems.push_back( _( "Rotate around item anchor" ) );
96  break;
98  menuItems.push_back( _( "Rotate around selection center" ) );
99  break;
101  menuItems.push_back( _( "Rotate around local coordinates origin" ) );
102  break;
104  menuItems.push_back( _( "Rotate around drill/place origin" ) );
105  break;
106  }
107  }
108 
109  m_anchorOptions->Set( menuItems );
110 }
111 
112 
113 void DIALOG_MOVE_EXACT::ToPolarDeg( double x, double y, double& r, double& q )
114 {
115  // convert to polar coordinates
116  r = hypot( x, y );
117 
118  q = ( r != 0) ? RAD2DEG( atan2( y, x ) ) : 0;
119 }
120 
121 
122 bool DIALOG_MOVE_EXACT::GetTranslationInIU( wxRealPoint& val, bool polar )
123 {
124  if( polar )
125  {
126  const double r = m_moveX.GetDoubleValue();
127  const double q = m_moveY.GetDoubleValue();
128 
129  val.x = r * cos( DEG2RAD( q / 10.0 ) );
130  val.y = r * sin( DEG2RAD( q / 10.0 ) );
131  }
132  else
133  {
134  // direct read
135  val.x = m_moveX.GetDoubleValue();
136  val.y = m_moveY.GetDoubleValue();
137  }
138 
139  // no validation to do here, but in future, you could return false here
140  return true;
141 }
142 
143 
144 void DIALOG_MOVE_EXACT::OnPolarChanged( wxCommandEvent& event )
145 {
146  bool newPolar = m_polarCoords->IsChecked();
147  double moveX = m_moveX.GetDoubleValue();
148  double moveY = m_moveY.GetDoubleValue();
149  updateDialogControls( newPolar );
150 
151  if( newPolar )
152  {
153  if( moveX != m_stateX || moveY != m_stateY )
154  {
155  m_stateX = moveX;
156  m_stateY = moveY;
158  m_stateTheta *= 10.0;
159 
164  }
165  else
166  {
169  }
170  }
171  else
172  {
173  if( moveX != m_stateRadius || moveY != m_stateTheta )
174  {
175  m_stateRadius = moveX;
176  m_stateTheta = moveY;
177  m_stateX = m_stateRadius * cos( DEG2RAD( m_stateTheta / 10.0 ) );
178  m_stateY = m_stateRadius * sin( DEG2RAD( m_stateTheta / 10.0 ) );
179 
184  }
185  else
186  {
189  }
190  }
191 }
192 
193 
195 {
196  if( aPolar )
197  {
198  m_moveX.SetLabel( _( "Distance:" ) ); // Polar radius
199  m_moveY.SetLabel( _( "Angle:" ) ); // Polar theta or angle
201  }
202  else
203  {
204  m_moveX.SetLabel( _( "Move X:" ) );
205  m_moveY.SetLabel( _( "Move Y:" ) );
207  }
208 
209  Layout();
210 }
211 
212 
213 void DIALOG_MOVE_EXACT::OnClear( wxCommandEvent& event )
214 {
215  wxObject* obj = event.GetEventObject();
216 
217  if( obj == m_clearX )
218  {
219  m_moveX.SetValue( 0 );
220  }
221  else if( obj == m_clearY )
222  {
223  m_moveY.SetValue( 0 );
224  }
225  else if( obj == m_clearRot )
226  {
227  m_rotate.SetValue( 0 );
228  }
229 
230  // Keep m_stdButtonsOK focused to allow enter key actiavte the OK button
231  m_stdButtonsOK->SetFocus();
232 }
233 
234 
236 {
237  // for the output, we only deliver a Cartesian vector
238  wxRealPoint translation;
239  bool ok = GetTranslationInIU( translation, m_polarCoords->IsChecked() );
240  m_translation.x = KiROUND(translation.x);
241  m_translation.y = KiROUND(translation.y);
243  m_rotationAnchor = m_menuIDs[ m_anchorOptions->GetSelection() ];
244 
245  if( ok )
246  {
247  // save the settings
248  m_options.polarCoords = m_polarCoords->GetValue();
252  m_options.entryAnchorSelection = (size_t) std::max( m_anchorOptions->GetSelection(), 0 );
253  return true;
254  }
255 
256  return false;
257 }
258 
259 
260 void DIALOG_MOVE_EXACT::OnTextFocusLost( wxFocusEvent& event )
261 {
262  wxTextCtrl* obj = static_cast<wxTextCtrl*>( event.GetEventObject() );
263 
264  if( obj->GetValue().IsEmpty() )
265  obj->SetValue( "0" );
266 
267  event.Skip();
268 }
269 
270 
271 void DIALOG_MOVE_EXACT::OnTextChanged( wxCommandEvent& event )
272 {
273 
274  double delta_x = m_moveX.GetDoubleValue();
275  double delta_y = m_moveY.GetDoubleValue();
276  double max_border = std::numeric_limits<int>::max() * 0.7071;
277 
278  if( m_bbox.GetLeft() + delta_x < -max_border ||
279  m_bbox.GetRight() + delta_x > max_border ||
280  m_bbox.GetTop() + delta_y < -max_border ||
281  m_bbox.GetBottom() + delta_y > max_border )
282  {
283  const wxString invalid_length = _( "Invalid movement values. Movement would place selection "
284  "outside of the maximum board area." );
285 
286  m_xEntry->SetToolTip( invalid_length );
287  m_xEntry->SetForegroundColour( *wxRED );
288  m_yEntry->SetToolTip( invalid_length );
289  m_yEntry->SetForegroundColour( *wxRED );
290  m_stdButtons->GetAffirmativeButton()->Disable();
291  }
292  else
293  {
294  m_xEntry->SetToolTip( "" );
295  m_xEntry->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ) );
296  m_yEntry->SetToolTip( "" );
297  m_yEntry->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ) );
298  m_stdButtons->GetAffirmativeButton()->Enable();
299  event.Skip();
300  }
301 
302 }
Functions for manipulating tab traversal in forms and dialogs.
void OnTextChanged(wxCommandEvent &event) override
int GetTop() const
Definition: eda_rect.h:123
ROTATION_ANCHOR & m_rotationAnchor
std::vector< wxWindow * > m_tabOrder
Definition: dialog_shim.h:210
std::vector< ROTATION_ANCHOR > m_menuIDs
int GetLeft() const
Definition: eda_rect.h:122
Class DIALOG_MOVE_EXACT_BASE.
double RAD2DEG(double rad)
Definition: trigo.h:215
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
bool GetTranslationInIU(wxRealPoint &val, bool polar)
Get the (Cartesian) translation described by the text entries.
int GetBottom() const
Definition: eda_rect.h:124
EDA_UNITS GetUserUnits() const
Definition: dialog_shim.h:132
void OnPolarChanged(wxCommandEvent &event) override
const EDA_RECT & m_bbox
int GetRight() const
Definition: eda_rect.h:121
ROTATION_ANCHOR
void OnTextFocusLost(wxFocusEvent &event) override
void OnClear(wxCommandEvent &event) override
void updateDialogControls(bool aPolar)
void SetLabel(const wxString &aLabel)
bool IsType(FRAME_T aType) const
double DEG2RAD(double deg)
Definition: trigo.h:214
#define _(s)
Definition: 3d_actions.cpp:33
DIALOG_MOVE_EXACT(PCB_BASE_FRAME *aParent, wxPoint &aTranslate, double &aRotate, ROTATION_ANCHOR &aAnchor, const EDA_RECT &aBbox)
virtual void SetValue(int aValue)
Function SetValue Sets new value (in Internal Units) for the text field, taking care of units convers...
bool TransferDataFromWindow() override
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:61
void ToPolarDeg(double x, double y, double &r, double &q)
Convert a given Cartesian point into a polar representation.
virtual long long int GetValue()
Function GetValue Returns the current value in Internal Units.
static MOVE_EXACT_OPTIONS m_options
virtual void SetDoubleValue(double aValue)
Function SetDoubleValue Sets new value (in Internal Units) for the text field, taking care of units c...
wxStdDialogButtonSizer * m_stdButtons
virtual double GetDoubleValue()
Function GetValue Returns the current value in Internal Units.
PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
virtual void SetUnits(EDA_UNITS aUnits, bool aUseMils=false)
Function SetUnits Normally not needed (as the UNIT_BINDER inherits from the parent frame),...
Definition: unit_binder.cpp:68