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) 1992-2014 KiCad Developers, see AUTHORS.txt for contributors.
6  * Copyright (C) 2017 KiCad Developers, see CHANGELOG.TXT for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <wxPcbStruct.h>
27 #include <base_units.h>
28 #include <macros.h>
29 
30 #include <module_editor_frame.h>
31 
32 #include "dialog_move_exact.h"
33 
34 // initialise statics
36 
37 
39  DIALOG_MOVE_EXACT_BASE( aParent ),
40  m_parent( aParent ),
41  m_translation( aParams.translation ),
42  m_rotation( aParams.rotation ),
43  m_origin( aParams.origin ),
44  m_anchor( aParams.anchor ),
45  m_allowOverride( aParams.allowOverride ),
46  m_editingFootprint( aParams.editingFootprint )
47 {
48  // set the unit labels
49  m_xUnit->SetLabelText( GetAbbreviatedUnitsLabel( g_UserUnit ) );
50  m_yUnit->SetLabelText( GetAbbreviatedUnitsLabel( g_UserUnit ) );
51 
52  // tabbing goes through the entries in sequence
53  m_yEntry->MoveAfterInTabOrder( m_xEntry );
54  m_rotEntry->MoveAfterInTabOrder( m_yEntry );
55 
56  // and set up the entries according to the saved options
57  m_polarCoords->SetValue( m_options.polarCoords );
58  m_xEntry->SetValue( wxString::FromDouble( m_options.entry1 ) );
59  m_yEntry->SetValue( wxString::FromDouble( m_options.entry2 ) );
60  m_rotEntry->SetValue( wxString::FromDouble( m_options.entryRotation ) );
61  m_originChooser->SetSelection( m_options.origin );
62 
63  if( m_allowOverride )
64  {
67 
68  // ME_ANCHOR_FROM_LIBRARY is not in the wxChoice options so show the first choice instead
70  {
71  m_anchorChoice->SetSelection( ANCHOR_TOP_LEFT_PAD );
72  }
73  else
74  {
75  m_anchorChoice->SetSelection( m_options.anchor );
76  }
77 
79  {
80  // no footprint override necessary in this mode
81  m_cbOverride->Disable();
82  m_anchorChoice->Disable();
83  }
84 
85  if( m_editingFootprint )
86  {
87  // there is no point in showing the center footprint option when editing footprints
89  }
90  }
91  else
92  {
93  // hide the checkbox and choice control if overides are not allowed
94  bMainSizer->Hide( bAnchorSizer, true );
95  }
96 
97  if( wxPoint( 0, 0 ) == aParent->GetScreen()->m_O_Curseur )
98  {
99  // disble the user origin option when the user oigin is not set
100  m_originChooser->Enable( RELATIVE_TO_USER_ORIGIN, false );
101  m_originChooser->SetItemToolTip( RELATIVE_TO_USER_ORIGIN,
102  wxString( "The user origin is currently not set\n"
103  "Set it by using the <space> hotkey" ) );
104  }
105 
106  if( wxPoint( 0, 0 ) == aParent->GetGridOrigin() )
107  {
108  // disble the grid origin option when the user oigin is not set
109  m_originChooser->Enable( RELATIVE_TO_GRID_ORIGIN, false );
110  m_originChooser->SetItemToolTip( RELATIVE_TO_GRID_ORIGIN,
111  wxString( "The grid origin is currently not set\n"
112  "Set it by using the tool in the <place> menu" ) );
113  }
114 
115  if( wxPoint( 0, 0 ) == aParent->GetAuxOrigin() )
116  {
117  // disble the grid origin option when the drill/place oigin is not set
120  wxString( "The drill/place origin is currently not set\n"
121  "Set it by using the tool in the <place> menu" ) );
122  }
123 
124  updateDlgTexts( m_polarCoords->IsChecked() );
125 
126  m_stdButtonsOK->SetDefault();
127 
128  GetSizer()->SetSizeHints( this );
129  Layout();
130 }
131 
132 
134 {
135 }
136 
137 
138 void DIALOG_MOVE_EXACT::ToPolarDeg( double x, double y, double& r, double& q )
139 {
140  // convert to polar coordinates
141  r = hypot ( x, y );
142 
143  q = ( r != 0) ? RAD2DEG( atan2( y, x ) ) : 0;
144 }
145 
146 
148 {
149  if( polar )
150  {
151  const int r = ValueFromTextCtrl( *m_xEntry );
152  const double q = DoubleValueFromString( DEGREES, m_yEntry->GetValue() );
153 
154  val.x = r * cos( DEG2RAD( q / 10.0 ) );
155  val.y = r * sin( DEG2RAD( q / 10.0 ) );
156  }
157  else
158  {
159  // direct read
160  val.x = ValueFromTextCtrl( *m_xEntry );
161  val.y = ValueFromTextCtrl( *m_yEntry );
162  }
163 
164  // no validation to do here, but in future, you could return false here
165  return true;
166 }
167 
168 
169 void DIALOG_MOVE_EXACT::OnPolarChanged( wxCommandEvent& event )
170 {
171  bool newPolar = m_polarCoords->IsChecked();
172  updateDlgTexts( newPolar );
173  wxPoint val;
174 
175  // get the value as previously stored
176  GetTranslationInIU( val, !newPolar );
177 
178  if( newPolar )
179  {
180  // convert to polar coordinates
181  double r, q;
182  ToPolarDeg( val.x, val.y, r, q );
183 
184  PutValueInLocalUnits( *m_xEntry, KiROUND( r / 10.0) * 10 );
185  m_yEntry->SetValue( wxString::FromDouble( q ) );
186  }
187  else
188  {
189  // vector is already in Cartesian, so just render out
190  // note - round off the last decimal place (10nm) to prevent
191  // (some) rounding causing errors when round-tripping
192  // you can never eliminate entirely, however
193  PutValueInLocalUnits( *m_xEntry, KiROUND( val.x / 10.0 ) * 10 );
194  PutValueInLocalUnits( *m_yEntry, KiROUND( val.y / 10.0 ) * 10 );
195  }
196  Layout();
197 }
198 
199 
200 void DIALOG_MOVE_EXACT::OnOriginChanged( wxCommandEvent& event )
201 {
202  if( m_originChooser->GetSelection() == RELATIVE_TO_CURRENT_POSITION )
203  {
204  //no need to override the achor in this mode since the reference in the current position
205  m_cbOverride->Disable();
206  m_anchorChoice->Disable();
207  }
208  else if( m_allowOverride )
209  {
210  m_cbOverride->Enable();
211 
212  if( m_cbOverride->IsChecked() )
213  m_anchorChoice->Enable();
214  }
215 }
216 
217 
218 void DIALOG_MOVE_EXACT::OnOverrideChanged( wxCommandEvent& event )
219 {
220  if( m_cbOverride->IsChecked() )
221  {
222  m_anchorChoice->Enable();
223  }
224  else
225  {
226  m_anchorChoice->Disable();
227  }
228 }
229 
230 
232 {
233  if( aPolar )
234  {
235  m_xLabel->SetLabelText( _( "Distance:" ) ); // Polar radius
236  m_yLabel->SetLabelText( _( "Angle:" ) ); // Polar theta or angle
237 
238  m_yUnit->SetLabelText( GetAbbreviatedUnitsLabel( DEGREES ) );
239  }
240  else
241  {
242  m_xLabel->SetLabelText( _( "Move vector X:" ) );
243  m_yLabel->SetLabelText( _( "Move vector Y:" ) );
244 
245  m_yUnit->SetLabelText( GetAbbreviatedUnitsLabel( g_UserUnit ) );
246  }
247 }
248 
249 
250 void DIALOG_MOVE_EXACT::OnClear( wxCommandEvent& event )
251 {
252  wxObject* obj = event.GetEventObject();
253  wxTextCtrl* entry = NULL;
254 
255  if( obj == m_clearX )
256  {
257  entry = m_xEntry;
258  }
259  else if( obj == m_clearY )
260  {
261  entry = m_yEntry;
262  }
263  else if( obj == m_clearRot )
264  {
265  entry = m_rotEntry;
266  }
267 
268  if( entry )
269  entry->SetValue( "0" );
270 }
271 
272 
273 void DIALOG_MOVE_EXACT::OnOkClick( wxCommandEvent& event )
274 {
276  m_origin = static_cast<MOVE_EXACT_ORIGIN>( m_originChooser->GetSelection() );
277 
278  if( m_cbOverride->IsChecked() && m_allowOverride )
279  {
280  m_anchor = static_cast<MOVE_EXACT_ANCHOR>( m_anchorChoice->GetSelection() );
281  }
282  else
283  {
285  }
286 
287  wxPoint move_vector, origin;
288  // for the output, we only deliver a Cartesian vector
289  bool ok = GetTranslationInIU( move_vector, m_polarCoords->IsChecked() );
290 
291  switch( m_origin )
292  {
294  origin = m_parent->GetScreen()->m_O_Curseur;
295  break;
296 
298  origin = m_parent->GetGridOrigin();
299  break;
300 
302  origin = m_parent->GetAuxOrigin();
303  break;
304 
306  origin = wxPoint( 0, 0 );
307  break;
308 
310  // relative movement means that only the translation values should be used:
311  // -> set origin and anchor to zero
312  origin = wxPoint( 0, 0 );
313  break;
314  }
315 
316  m_translation = move_vector + origin;
317 
318  if( ok )
319  {
320  // save the settings
321  m_options.polarCoords = m_polarCoords->GetValue();
326  m_options.anchor = static_cast<MOVE_EXACT_ANCHOR>( m_anchorChoice->GetSelection() );
327  m_options.overrideAnchor = m_cbOverride->IsChecked();
328  event.Skip();
329  }
330 }
331 
332 
333 void DIALOG_MOVE_EXACT::OnTextFocusLost( wxFocusEvent& event )
334 {
335  wxTextCtrl* obj = static_cast<wxTextCtrl*>( event.GetEventObject() );
336 
337  if( obj->GetValue().IsEmpty() )
338  obj->SetValue( "0" );
339 
340  event.Skip();
341 }
Definition of class FOOTPRINT_EDIT_FRAME.
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
MOVE_EXACT_ORIGIN
Implementation of conversion functions that require both schematic and board internal units...
Class DIALOG_MOVE_EXACT_BASE.
double RAD2DEG(double rad)
Definition: trigo.h:200
const wxPoint & GetGridOrigin() const override
Function GetGridOrigin returns the absolute coordinates of the origin of the snap grid...
MOVE_EXACT_ORIGIN & m_origin
wxPoint m_O_Curseur
Relative Screen cursor coordinate (on grid) in user units.
const wxPoint & GetAuxOrigin() const override
Function GetAuxOrigin returns the origin of the axis used for plotting and various exports...
MOVE_EXACT_ANCHOR
This file contains miscellaneous commonly used macros and functions.
void OnOkClick(wxCommandEvent &event) override
void OnPolarChanged(wxCommandEvent &event) override
DIALOG_MOVE_EXACT(PCB_BASE_FRAME *aParent, MOVE_PARAMETERS &aParams)
void updateDlgTexts(bool aPolar)
bool GetTranslationInIU(wxPoint &val, bool polar)
Get the (Cartesian) translation described by the text entries.
wxString GetAbbreviatedUnitsLabel(EDA_UNITS_T aUnit)
Definition: base_units.cpp:479
int ValueFromTextCtrl(const wxTextCtrl &aTextCtr)
Convert the number Value in a string according to the internal units and the selected unit (g_UserUni...
Definition: base_units.cpp:384
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
void OnTextFocusLost(wxFocusEvent &event) override
void OnClear(wxCommandEvent &event) override
void PutValueInLocalUnits(wxTextCtrl &aTextCtr, int aValue)
Function PutValueInLocalUnits converts aValue from internal units to user units and append the units ...
Definition: base_units.cpp:265
double DEG2RAD(double deg)
Definition: trigo.h:199
void ToPolarDeg(double x, double y, double &r, double &q)
Convert a given Cartesian point into a polar representation.
PCB_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
MOVE_EXACT_ANCHOR & m_anchor
static MOVE_EXACT_OPTIONS m_options
void OnOriginChanged(wxCommandEvent &event) override
double DoubleValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:301
PCB_BASE_FRAME * m_parent
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer...
void OnOverrideChanged(wxCommandEvent &event) override