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  *
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 
25 #include <wxPcbStruct.h>
26 #include <base_units.h>
27 #include <macros.h>
28 
29 #include <module_editor_frame.h>
30 
31 #include "dialog_move_exact.h"
32 
33 // initialise statics
35 
36 
38  wxPoint& translation, double& rotation ):
39  DIALOG_MOVE_EXACT_BASE( aParent ),
40  m_translation( translation ),
41  m_rotation( rotation )
42 {
43  // set the unit labels
44  m_xUnit->SetLabelText( GetAbbreviatedUnitsLabel( g_UserUnit ) );
45  m_yUnit->SetLabelText( GetAbbreviatedUnitsLabel( g_UserUnit ) );
46 
47  // tabbing goes through the entries in sequence
48  m_yEntry->MoveAfterInTabOrder( m_xEntry );
49  m_rotEntry->MoveAfterInTabOrder( m_yEntry );
50 
51  // and set up the entries according to the saved options
52  m_polarCoords->SetValue( m_options.polarCoords );
53  m_xEntry->SetValue( wxString::FromDouble( m_options.entry1 ) );
54  m_yEntry->SetValue( wxString::FromDouble( m_options.entry2 ) );
55  m_rotEntry->SetValue( wxString::FromDouble( m_options.entryRotation ) );
56  updateDlgTexts( m_polarCoords->IsChecked() );
57 
58  m_stdButtonsOK->SetDefault();
59 
60  GetSizer()->SetSizeHints( this );
61  Layout();
62 }
63 
64 
66 {
67 }
68 
69 
70 void DIALOG_MOVE_EXACT::ToPolarDeg( double x, double y, double& r, double& q )
71 {
72  // convert to polar coordinates
73  r = hypot ( x, y );
74 
75  q = ( r != 0) ? RAD2DEG( atan2( y, x ) ) : 0;
76 }
77 
78 
80 {
81  if( polar )
82  {
83  const int r = ValueFromTextCtrl( *m_xEntry );
84  const double q = DoubleValueFromString( DEGREES, m_yEntry->GetValue() );
85 
86  val.x = r * cos( DEG2RAD( q / 10.0 ) );
87  val.y = r * sin( DEG2RAD( q / 10.0 ) );
88  }
89  else
90  {
91  // direct read
92  val.x = ValueFromTextCtrl( *m_xEntry );
93  val.y = ValueFromTextCtrl( *m_yEntry );
94  }
95 
96  // no validation to do here, but in future, you could return false here
97  return true;
98 }
99 
100 
101 void DIALOG_MOVE_EXACT::OnPolarChanged( wxCommandEvent& event )
102 {
103  bool newPolar = m_polarCoords->IsChecked();
104  updateDlgTexts( newPolar );
105  wxPoint val;
106 
107  // get the value as previously stored
108  GetTranslationInIU( val, !newPolar );
109 
110  if( newPolar )
111  {
112  // convert to polar coordinates
113  double r, q;
114  ToPolarDeg( val.x, val.y, r, q );
115 
116  PutValueInLocalUnits( *m_xEntry, KiROUND( r / 10.0) * 10 );
117  m_yEntry->SetValue( wxString::FromDouble( q ) );
118  }
119  else
120  {
121  // vector is already in Cartesian, so just render out
122  // note - round off the last decimal place (10nm) to prevent
123  // (some) rounding causing errors when round-tripping
124  // you can never eliminate entirely, however
125  PutValueInLocalUnits( *m_xEntry, KiROUND( val.x / 10.0 ) * 10 );
126  PutValueInLocalUnits( *m_yEntry, KiROUND( val.y / 10.0 ) * 10 );
127  }
128  Layout();
129 }
130 
131 
133 {
134  if( aPolar )
135  {
136  m_xLabel->SetLabelText( _( "Distance:" ) ); // Polar radius
137  m_yLabel->SetLabelText( _( "Angle:" ) ); // Polar theta or angle
138 
139  m_yUnit->SetLabelText( GetAbbreviatedUnitsLabel( DEGREES ) );
140  }
141  else
142  {
143  m_xLabel->SetLabelText( _( "Move vector X:" ) );
144  m_yLabel->SetLabelText( _( "Move vector Y:" ) );
145 
146  m_yUnit->SetLabelText( GetAbbreviatedUnitsLabel( g_UserUnit ) );
147  }
148 }
149 
150 
151 void DIALOG_MOVE_EXACT::OnClear( wxCommandEvent& event )
152 {
153  wxObject* obj = event.GetEventObject();
154  wxTextCtrl* entry = NULL;
155 
156  if( obj == m_clearX )
157  {
158  entry = m_xEntry;
159  }
160  else if( obj == m_clearY )
161  {
162  entry = m_yEntry;
163  }
164  else if( obj == m_clearRot )
165  {
166  entry = m_rotEntry;
167  }
168 
169  if( entry )
170  entry->SetValue( "0" );
171 }
172 
173 
174 void DIALOG_MOVE_EXACT::OnOkClick( wxCommandEvent& event )
175 {
177 
178  // for the output, we only deliver a Cartesian vector
179  bool ok = GetTranslationInIU( m_translation, m_polarCoords->IsChecked() );
180 
181  if( ok )
182  {
183  // save the settings
184  m_options.polarCoords = m_polarCoords->GetValue();
188 
189  event.Skip();
190  }
191 }
192 
193 
194 void DIALOG_MOVE_EXACT::OnTextFocusLost( wxFocusEvent& event )
195 {
196  wxTextCtrl* obj = static_cast<wxTextCtrl*>( event.GetEventObject() );
197 
198  if( obj->GetValue().IsEmpty() )
199  obj->SetValue( "0" );
200 
201  event.Skip();
202 }
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
Implementation of conversion functions that require both schematic and board internal units...
Class DIALOG_MOVE_EXACT_BASE.
double RAD2DEG(double rad)
Definition: trigo.h:192
DIALOG_MOVE_EXACT(PCB_BASE_FRAME *aParent, wxPoint &translation, double &rotation)
This file contains miscellaneous commonly used macros and functions.
void OnOkClick(wxCommandEvent &event) override
void OnPolarChanged(wxCommandEvent &event) override
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:191
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
double DoubleValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:301
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer...