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-2020 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 
61  // Configure display origin transforms
64 
66 
67  m_menuIDs.push_back( aAnchor );
69 
70  if( aParent->IsType( FRAME_PCB_EDITOR ) )
72 
74 
75  // and set up the entries according to the saved options
76  m_polarCoords->SetValue( m_options.polarCoords );
79 
82  m_anchorOptions->SetSelection( std::min( m_options.entryAnchorSelection, m_menuIDs.size() ) );
83 
84  m_stdButtonsOK->SetDefault();
85 
87 }
88 
89 
91 {
92  wxArrayString menuItems;
93 
94  for( auto anchorID : m_menuIDs )
95  {
96  switch( anchorID )
97  {
99  menuItems.push_back( _( "Rotate around item anchor" ) );
100  break;
102  menuItems.push_back( _( "Rotate around selection center" ) );
103  break;
105  menuItems.push_back( _( "Rotate around local coordinates origin" ) );
106  break;
108  menuItems.push_back( _( "Rotate around drill/place origin" ) );
109  break;
110  }
111  }
112 
113  m_anchorOptions->Set( menuItems );
114 }
115 
116 
117 void DIALOG_MOVE_EXACT::ToPolarDeg( double x, double y, double& r, double& q )
118 {
119  // convert to polar coordinates
120  r = hypot( x, y );
121 
122  q = ( r != 0) ? RAD2DEG( atan2( y, x ) ) : 0;
123 }
124 
125 
126 bool DIALOG_MOVE_EXACT::GetTranslationInIU( wxRealPoint& val, bool polar )
127 {
128  if( polar )
129  {
130  const double r = m_moveX.GetDoubleValue();
131  const double q = m_moveY.GetDoubleValue();
132 
133  val.x = r * cos( DEG2RAD( q / 10.0 ) );
134  val.y = r * sin( DEG2RAD( q / 10.0 ) );
135  }
136  else
137  {
138  // direct read
139  val.x = m_moveX.GetDoubleValue();
140  val.y = m_moveY.GetDoubleValue();
141  }
142 
143  // no validation to do here, but in future, you could return false here
144  return true;
145 }
146 
147 
148 void DIALOG_MOVE_EXACT::OnPolarChanged( wxCommandEvent& event )
149 {
150  bool newPolar = m_polarCoords->IsChecked();
151  double moveX = m_moveX.GetDoubleValue();
152  double moveY = m_moveY.GetDoubleValue();
153  updateDialogControls( newPolar );
154 
155  if( newPolar )
156  {
157  if( moveX != m_stateX || moveY != m_stateY )
158  {
159  m_stateX = moveX;
160  m_stateY = moveY;
162  m_stateTheta *= 10.0;
163 
168  }
169  else
170  {
173  }
174  }
175  else
176  {
177  if( moveX != m_stateRadius || moveY != m_stateTheta )
178  {
179  m_stateRadius = moveX;
180  m_stateTheta = moveY;
181  m_stateX = m_stateRadius * cos( DEG2RAD( m_stateTheta / 10.0 ) );
182  m_stateY = m_stateRadius * sin( DEG2RAD( m_stateTheta / 10.0 ) );
183 
188  }
189  else
190  {
193  }
194  }
195 }
196 
197 
199 {
200  if( aPolar )
201  {
202  m_moveX.SetLabel( _( "Distance:" ) ); // Polar radius
203  m_moveY.SetLabel( _( "Angle:" ) ); // Polar theta or angle
205  }
206  else
207  {
208  m_moveX.SetLabel( _( "Move X:" ) );
209  m_moveY.SetLabel( _( "Move Y:" ) );
211  }
212 
213  Layout();
214 }
215 
216 
217 void DIALOG_MOVE_EXACT::OnClear( wxCommandEvent& event )
218 {
219  wxObject* obj = event.GetEventObject();
220 
221  if( obj == m_clearX )
222  {
223  m_moveX.SetValue( 0 );
224  }
225  else if( obj == m_clearY )
226  {
227  m_moveY.SetValue( 0 );
228  }
229  else if( obj == m_clearRot )
230  {
231  m_rotate.SetValue( 0 );
232  }
233 
234  // Keep m_stdButtonsOK focused to allow enter key actiavte the OK button
235  m_stdButtonsOK->SetFocus();
236 }
237 
238 
240 {
241  // for the output, we only deliver a Cartesian vector
242  wxRealPoint translation;
243  bool ok = GetTranslationInIU( translation, m_polarCoords->IsChecked() );
244  m_translation.x = KiROUND(translation.x);
245  m_translation.y = KiROUND(translation.y);
247  m_rotationAnchor = m_menuIDs[ m_anchorOptions->GetSelection() ];
248 
249  if( ok )
250  {
251  // save the settings
252  m_options.polarCoords = m_polarCoords->GetValue();
256  m_options.entryAnchorSelection = (size_t) std::max( m_anchorOptions->GetSelection(), 0 );
257  return true;
258  }
259 
260  return false;
261 }
262 
263 
264 void DIALOG_MOVE_EXACT::OnTextFocusLost( wxFocusEvent& event )
265 {
266  wxTextCtrl* obj = static_cast<wxTextCtrl*>( event.GetEventObject() );
267 
268  if( obj->GetValue().IsEmpty() )
269  obj->SetValue( "0" );
270 
271  event.Skip();
272 }
273 
274 
275 void DIALOG_MOVE_EXACT::OnTextChanged( wxCommandEvent& event )
276 {
277 
278  double delta_x = m_moveX.GetDoubleValue();
279  double delta_y = m_moveY.GetDoubleValue();
280  double max_border = std::numeric_limits<int>::max() * 0.7071;
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 
306 }
wxString GetOriginalText() const
Function GetOriginalText Returns the pre-evaluated text (or the current text if evaluation is not sup...
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:213
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:220
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:131
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:219
#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:68
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
void SetCoordType(ORIGIN_TRANSFORMS::COORD_TYPES_T aCoordType)
Function SetOriginTransform Sets the current origin transform mode.
Definition: unit_binder.h:171
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:81