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 #include <trigo.h>
30 
31 // initialise statics
33 
34 
36  double& aRotate, ROTATION_ANCHOR& aAnchor,
37  const EDA_RECT& aBbox ) :
38  DIALOG_MOVE_EXACT_BASE( aParent ),
39  m_translation( aTranslate ),
40  m_rotation( aRotate ),
41  m_rotationAnchor( aAnchor ),
42  m_bbox( aBbox ),
43  m_moveX( aParent, m_xLabel, m_xEntry, m_xUnit ),
44  m_moveY( aParent, m_yLabel, m_yEntry, m_yUnit ),
45  m_rotate( aParent, m_rotLabel, m_rotEntry, m_rotUnit ),
46  m_stateX( 0.0 ),
47  m_stateY( 0.0 ),
48  m_stateRadius( 0.0 ),
49  m_stateTheta( 0.0 )
50 {
51  // We can't set the tab order through wxWidgets due to shortcomings in their mnemonics
52  // implementation on MSW
53  m_tabOrder = {
54  m_xEntry,
55  m_yEntry,
56  m_rotEntry,
60  };
61 
62  // Configure display origin transforms
65 
67 
68  m_menuIDs.push_back( aAnchor );
70 
71  if( aParent->IsType( FRAME_PCB_EDITOR ) )
73 
75 
76  // and set up the entries according to the saved options
77  m_polarCoords->SetValue( m_options.polarCoords );
80 
83  m_anchorOptions->SetSelection( std::min( m_options.entryAnchorSelection, m_menuIDs.size() ) );
84 
85  m_stdButtonsOK->SetDefault();
86 
88 }
89 
90 
92 {
93  wxArrayString menuItems;
94 
95  for( auto anchorID : m_menuIDs )
96  {
97  switch( anchorID )
98  {
100  menuItems.push_back( _( "Rotate around item anchor" ) );
101  break;
103  menuItems.push_back( _( "Rotate around selection center" ) );
104  break;
106  menuItems.push_back( _( "Rotate around local coordinates origin" ) );
107  break;
109  menuItems.push_back( _( "Rotate around drill/place origin" ) );
110  break;
111  }
112  }
113 
114  m_anchorOptions->Set( menuItems );
115 }
116 
117 
118 void DIALOG_MOVE_EXACT::ToPolarDeg( double x, double y, double& r, double& q )
119 {
120  // convert to polar coordinates
121  r = hypot( x, y );
122 
123  q = ( r != 0) ? RAD2DEG( atan2( y, x ) ) : 0;
124 }
125 
126 
127 bool DIALOG_MOVE_EXACT::GetTranslationInIU( wxRealPoint& val, bool polar )
128 {
129  if( polar )
130  {
131  const double r = m_moveX.GetDoubleValue();
132  const double q = m_moveY.GetDoubleValue();
133 
134  val.x = r * cos( DEG2RAD( q / 10.0 ) );
135  val.y = r * sin( DEG2RAD( q / 10.0 ) );
136  }
137  else
138  {
139  // direct read
140  val.x = m_moveX.GetDoubleValue();
141  val.y = m_moveY.GetDoubleValue();
142  }
143 
144  // no validation to do here, but in future, you could return false here
145  return true;
146 }
147 
148 
149 void DIALOG_MOVE_EXACT::OnPolarChanged( wxCommandEvent& event )
150 {
151  bool newPolar = m_polarCoords->IsChecked();
152  double moveX = m_moveX.GetDoubleValue();
153  double moveY = m_moveY.GetDoubleValue();
154  updateDialogControls( newPolar );
155 
156  if( newPolar )
157  {
158  if( moveX != m_stateX || moveY != m_stateY )
159  {
160  m_stateX = moveX;
161  m_stateY = moveY;
163  m_stateTheta *= 10.0;
164 
169  }
170  else
171  {
174  }
175  }
176  else
177  {
178  if( moveX != m_stateRadius || moveY != m_stateTheta )
179  {
180  m_stateRadius = moveX;
181  m_stateTheta = moveY;
182  m_stateX = m_stateRadius * cos( DEG2RAD( m_stateTheta / 10.0 ) );
183  m_stateY = m_stateRadius * sin( DEG2RAD( m_stateTheta / 10.0 ) );
184 
189  }
190  else
191  {
194  }
195  }
196 }
197 
198 
200 {
201  if( aPolar )
202  {
203  m_moveX.SetLabel( _( "Distance:" ) ); // Polar radius
204  m_moveY.SetLabel( _( "Angle:" ) ); // Polar theta or angle
206  }
207  else
208  {
209  m_moveX.SetLabel( _( "Move X:" ) );
210  m_moveY.SetLabel( _( "Move Y:" ) );
212  }
213 
214  Layout();
215 }
216 
217 
218 void DIALOG_MOVE_EXACT::OnClear( wxCommandEvent& event )
219 {
220  wxObject* obj = event.GetEventObject();
221 
222  if( obj == m_clearX )
223  {
224  m_moveX.SetValue( 0 );
225  }
226  else if( obj == m_clearY )
227  {
228  m_moveY.SetValue( 0 );
229  }
230  else if( obj == m_clearRot )
231  {
232  m_rotate.SetValue( 0 );
233  }
234 
235  // Keep m_stdButtonsOK focused to allow enter key actiavte the OK button
236  m_stdButtonsOK->SetFocus();
237 }
238 
239 
241 {
242  // for the output, we only deliver a Cartesian vector
243  wxRealPoint translation;
244  bool ok = GetTranslationInIU( translation, m_polarCoords->IsChecked() );
245  m_translation.x = KiROUND(translation.x);
246  m_translation.y = KiROUND(translation.y);
248  m_rotationAnchor = m_menuIDs[ m_anchorOptions->GetSelection() ];
249 
250  if( ok )
251  {
252  // save the settings
253  m_options.polarCoords = m_polarCoords->GetValue();
257  m_options.entryAnchorSelection = (size_t) std::max( m_anchorOptions->GetSelection(), 0 );
258  return true;
259  }
260 
261  return false;
262 }
263 
264 
265 void 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 
276 void 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 
306 }
wxString GetOriginalText() const
Return the pre-evaluated text (or the current text if evaluation is not supported).
Functions for manipulating tab traversal in forms and dialogs.
void OnTextChanged(wxCommandEvent &event) override
int GetTop() const
Definition: eda_rect.h:118
ROTATION_ANCHOR & m_rotationAnchor
std::vector< wxWindow * > m_tabOrder
Definition: dialog_shim.h:219
std::vector< ROTATION_ANCHOR > m_menuIDs
int GetLeft() const
Definition: eda_rect.h:117
Class DIALOG_MOVE_EXACT_BASE.
double RAD2DEG(double rad)
Definition: trigo.h:232
bool GetTranslationInIU(wxRealPoint &val, bool polar)
Get the (Cartesian) translation described by the text entries.
int GetBottom() const
Definition: eda_rect.h:119
EDA_UNITS GetUserUnits() const
Definition: dialog_shim.h:120
void OnPolarChanged(wxCommandEvent &event) override
const EDA_RECT & m_bbox
int GetRight() const
Definition: eda_rect.h:116
ROTATION_ANCHOR
void OnTextFocusLost(wxFocusEvent &event) override
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
virtual void SetUnits(EDA_UNITS aUnits)
Normally not needed (as the UNIT_BINDER inherits from the parent frame), but can be used to set to DE...
Definition: unit_binder.cpp:81
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:231
#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)
Set new value (in Internal Units) for the text field, taking care of units conversion.
bool TransferDataFromWindow() override
Handle the component boundary box.
Definition: eda_rect.h:42
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.
static MOVE_EXACT_OPTIONS m_options
void SetCoordType(ORIGIN_TRANSFORMS::COORD_TYPES_T aCoordType)
Set the current origin transform mode.
Definition: unit_binder.h:173
virtual void SetDoubleValue(double aValue)
Set new value (in Internal Units) for the text field, taking care of units conversion.
wxStdDialogButtonSizer * m_stdButtons
virtual double GetDoubleValue()
Return the current value in Internal Units.
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.