File: dialog_move_exact.cpp

package info (click to toggle)
kicad 5.0.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 234,592 kB
  • sloc: cpp: 505,330; ansic: 57,038; python: 4,886; sh: 879; awk: 294; makefile: 253; xml: 103; perl: 5
file content (346 lines) | stat: -rw-r--r-- 10,706 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2014 John Beard, john.j.beard@gmail.com
 * Copyright (C) 2018 KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <pcb_edit_frame.h>
#include <base_units.h>
#include <macros.h>

#include <footprint_edit_frame.h>
#include <widgets/text_ctrl_eval.h>

#include "dialog_move_exact.h"

// initialise statics
DIALOG_MOVE_EXACT::MOVE_EXACT_OPTIONS DIALOG_MOVE_EXACT::m_options;


DIALOG_MOVE_EXACT::DIALOG_MOVE_EXACT(PCB_BASE_FRAME *aParent, MOVE_PARAMETERS &aParams ) :
    DIALOG_MOVE_EXACT_BASE( aParent ),
    m_parent( aParent ),
    m_translation( aParams.translation ),
    m_rotation( aParams.rotation ),
    m_origin( aParams.origin ),
    m_anchor( aParams.anchor ),
    m_allowOverride( aParams.allowOverride ),
    m_editingFootprint( aParams.editingFootprint )
{
    // set the unit labels
    m_xUnit->SetLabelText( GetAbbreviatedUnitsLabel( g_UserUnit ) );
    m_yUnit->SetLabelText( GetAbbreviatedUnitsLabel( g_UserUnit ) );

    // tabbing goes through the entries in sequence
    m_yEntry->MoveAfterInTabOrder( m_xEntry );
    m_rotEntry->MoveAfterInTabOrder( m_yEntry );

    // and set up the entries according to the saved options
    m_polarCoords->SetValue( m_options.polarCoords );
    m_xEntry->SetValue( wxString::FromDouble( m_options.entry1 ) );
    m_yEntry->SetValue( wxString::FromDouble( m_options.entry2 ) );
    m_rotEntry->SetValue( wxString::FromDouble( m_options.entryRotation ) );
    m_originChooser->SetSelection( m_options.origin );

    if( m_allowOverride )
    {
        m_cbOverride->SetValue( m_options.overrideAnchor );
        m_anchorChoice->Enable( m_options.overrideAnchor );

        // ME_ANCHOR_FROM_LIBRARY is not in the wxChoice options so show the first choice instead
        if( m_options.anchor == ANCHOR_FROM_LIBRARY )
        {
            m_anchorChoice->SetSelection( ANCHOR_TOP_LEFT_PAD );
        }
        else
        {
            m_anchorChoice->SetSelection( m_options.anchor );
        }

        if( m_options.origin == RELATIVE_TO_CURRENT_POSITION )
        {
            // no footprint override necessary in this mode
            m_cbOverride->Disable();
            m_anchorChoice->Disable();
        }

        if( m_editingFootprint )
        {
            // there is no point in showing the center footprint option when editing footprints
            m_anchorChoice->Delete( ANCHOR_CENTER_FOOTPRINT );
        }
    }
    else
    {
        // hide the checkbox and choice control if overides are not allowed
        bMainSizer->Hide( bAnchorSizer, true );
    }

    if( wxPoint( 0, 0 ) == aParent->GetScreen()->m_O_Curseur )
    {
        // disble the user origin option when the user oigin is not set
        m_originChooser->Enable( RELATIVE_TO_USER_ORIGIN, false );
        m_originChooser->SetItemToolTip( RELATIVE_TO_USER_ORIGIN,
                                         wxString( "The user origin is currently not set\n"
                                                   "Set it by using the <space> hotkey" ) );
    }

    if( wxPoint( 0, 0 ) == aParent->GetGridOrigin() )
    {
        // disble the grid origin option when the user oigin is not set
        m_originChooser->Enable( RELATIVE_TO_GRID_ORIGIN, false );
        m_originChooser->SetItemToolTip( RELATIVE_TO_GRID_ORIGIN,
                                         wxString( "The grid origin is currently not set\n"
                                                   "Set it by using the tool in the <place> menu" ) );
    }

    if( wxPoint( 0, 0 ) == aParent->GetAuxOrigin() )
    {
        // disble the grid origin option when the drill/place oigin is not set
        m_originChooser->Enable( RELATIVE_TO_DRILL_PLACE_ORIGIN, false );
        m_originChooser->SetItemToolTip( RELATIVE_TO_DRILL_PLACE_ORIGIN,
                                         wxString( "The drill/place origin is currently not set\n"
                                                   "Set it by using the tool in the <place> menu" ) );
    }

    updateDlgTexts( m_polarCoords->IsChecked() );

    m_stdButtonsOK->SetDefault();

    GetSizer()->SetSizeHints( this );
    Layout();
}


DIALOG_MOVE_EXACT::~DIALOG_MOVE_EXACT()
{
}


void DIALOG_MOVE_EXACT::ToPolarDeg( double x, double y, double& r, double& q )
{
    // convert to polar coordinates
    r = hypot ( x, y );

    q = ( r != 0) ? RAD2DEG( atan2( y, x ) ) : 0;
}


bool DIALOG_MOVE_EXACT::GetTranslationInIU ( wxPoint& val, bool polar )
{
    if( polar )
    {
        const int r = ValueFromTextCtrl( *m_xEntry );
        const double q = DoubleValueFromString( DEGREES, m_yEntry->GetValue() );

        val.x = r * cos( DEG2RAD( q / 10.0 ) );
        val.y = r * sin( DEG2RAD( q / 10.0 ) );
    }
    else
    {
        // direct read
        val.x = ValueFromTextCtrl( *m_xEntry );
        val.y = ValueFromTextCtrl( *m_yEntry );
    }

    // no validation to do here, but in future, you could return false here
    return true;
}


void DIALOG_MOVE_EXACT::OnPolarChanged( wxCommandEvent& event )
{
    bool newPolar = m_polarCoords->IsChecked();
    updateDlgTexts( newPolar );
    wxPoint val;

    // get the value as previously stored
    GetTranslationInIU( val, !newPolar );

    if( newPolar )
    {
        // convert to polar coordinates
        double r, q;
        ToPolarDeg( val.x, val.y, r, q );

        PutValueInLocalUnits( *m_xEntry, KiROUND( r / 10.0) * 10 );
        m_yEntry->SetValue( wxString::FromDouble( q ) );
    }
    else
    {
        // vector is already in Cartesian, so just render out
        // note - round off the last decimal place (10nm) to prevent
        // (some) rounding causing errors when round-tripping
        // you can never eliminate entirely, however
        PutValueInLocalUnits( *m_xEntry, KiROUND( val.x / 10.0 ) * 10 );
        PutValueInLocalUnits( *m_yEntry, KiROUND( val.y / 10.0 ) * 10 );
    }
    Layout();
}


void DIALOG_MOVE_EXACT::OnOriginChanged( wxCommandEvent& event )
{
    if( m_originChooser->GetSelection() == RELATIVE_TO_CURRENT_POSITION )
    {
        //no need to override the achor in this mode since the reference in the current position
        m_cbOverride->Disable();
        m_anchorChoice->Disable();
    }
    else if( m_allowOverride )
    {
        m_cbOverride->Enable();

        if( m_cbOverride->IsChecked() )
            m_anchorChoice->Enable();
    }
}


void DIALOG_MOVE_EXACT::OnOverrideChanged( wxCommandEvent& event )
{
    if( m_cbOverride->IsChecked() )
    {
        m_anchorChoice->Enable();
    }
    else
    {
        m_anchorChoice->Disable();
    }
}


void DIALOG_MOVE_EXACT::updateDlgTexts( bool aPolar )
{
    if( aPolar )
    {
        m_xLabel->SetLabelText( _( "Distance:" ) );     // Polar radius
        m_yLabel->SetLabelText( _( "Angle:" ) );        // Polar theta or angle

        m_yUnit->SetLabelText( GetAbbreviatedUnitsLabel( DEGREES ) );
    }
    else
    {
        m_xLabel->SetLabelText( _( "Move vector X:" ) );
        m_yLabel->SetLabelText( _( "Move vector Y:" ) );

        m_yUnit->SetLabelText( GetAbbreviatedUnitsLabel( g_UserUnit ) );
    }
}


void DIALOG_MOVE_EXACT::OnClear( wxCommandEvent& event )
{
    wxObject* obj = event.GetEventObject();
    wxTextCtrl* entry = NULL;

    if( obj == m_clearX )
    {
        entry = m_xEntry;
    }
    else if( obj == m_clearY )
    {
        entry = m_yEntry;
    }
    else if( obj == m_clearRot )
    {
        entry = m_rotEntry;
    }

    if( entry )
        entry->SetValue( "0" );

    // Keep m_stdButtonsOK focused to allow enter key actiavte the OK button
    m_stdButtonsOK->SetFocus();
}


bool DIALOG_MOVE_EXACT::TransferDataFromWindow()
{
    m_rotation = DoubleValueFromString( DEGREES, m_rotEntry->GetValue() );
    m_origin = static_cast<MOVE_EXACT_ORIGIN>( m_originChooser->GetSelection() );

    if( m_cbOverride->IsChecked() && m_allowOverride )
    {
        m_anchor = static_cast<MOVE_EXACT_ANCHOR>( m_anchorChoice->GetSelection() );
    }
    else
    {
        m_anchor = ANCHOR_FROM_LIBRARY;
    }

    wxPoint move_vector, origin;
    // for the output, we only deliver a Cartesian vector
    bool ok = GetTranslationInIU( move_vector, m_polarCoords->IsChecked() );

    switch( m_origin )
    {
    case RELATIVE_TO_USER_ORIGIN:
        origin = m_parent->GetScreen()->m_O_Curseur;
        break;

    case RELATIVE_TO_GRID_ORIGIN:
        origin = m_parent->GetGridOrigin();
        break;

    case RELATIVE_TO_DRILL_PLACE_ORIGIN:
        origin = m_parent->GetAuxOrigin();
        break;

    case RELATIVE_TO_SHEET_ORIGIN:
        origin = wxPoint( 0, 0 );
        break;

    case RELATIVE_TO_CURRENT_POSITION:
        // relative movement means that only the translation values should be used:
        // -> set origin and anchor to zero
        origin = wxPoint( 0, 0 );
        break;
    }

    m_translation = move_vector + origin;

    if( ok )
    {
        // save the settings
        m_options.polarCoords = m_polarCoords->GetValue();
        m_options.entry1 = DoubleValueFromString( UNSCALED_UNITS, m_xEntry->GetValue() );
        m_options.entry2 = DoubleValueFromString( UNSCALED_UNITS, m_yEntry->GetValue() );
        m_options.entryRotation = DoubleValueFromString( UNSCALED_UNITS, m_rotEntry->GetValue() );
        m_options.origin = m_origin;
        m_options.anchor = static_cast<MOVE_EXACT_ANCHOR>( m_anchorChoice->GetSelection() );
        m_options.overrideAnchor = m_cbOverride->IsChecked();
        return true;
    }

    return false;
}


void DIALOG_MOVE_EXACT::OnTextFocusLost( wxFocusEvent& event )
{
    wxTextCtrl* obj = static_cast<wxTextCtrl*>( event.GetEventObject() );

    if( obj->GetValue().IsEmpty() )
        obj->SetValue( "0" );

    event.Skip();
}