File: dialog_move_exact.cpp

package info (click to toggle)
kicad 9.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 769,124 kB
  • sloc: cpp: 960,330; ansic: 121,001; xml: 66,428; python: 18,382; sh: 1,010; awk: 301; asm: 292; makefile: 227; javascript: 167; perl: 10
file content (306 lines) | stat: -rw-r--r-- 9,436 bytes parent folder | download | duplicates (4)
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
/*
 * 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 The 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 <dialogs/dialog_move_exact.h>
#include <math/box2.h>
#include <math/util.h>      // for KiROUND
#include <widgets/tab_traversal.h>
#include <pcb_edit_frame.h>
#include <trigo.h>

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


DIALOG_MOVE_EXACT::DIALOG_MOVE_EXACT( PCB_BASE_FRAME *aParent, VECTOR2I& aTranslate,
                                      EDA_ANGLE& aRotate, ROTATION_ANCHOR& aAnchor,
                                      const BOX2I& aBbox ) :
    DIALOG_MOVE_EXACT_BASE( aParent ),
    m_translation( aTranslate ),
    m_rotation( aRotate ),
    m_rotationAnchor( aAnchor ),
    m_bbox( aBbox ),
    m_moveX( aParent, m_xLabel, m_xEntry, m_xUnit ),
    m_moveY( aParent, m_yLabel, m_yEntry, m_yUnit ),
    m_rotate( aParent, m_rotLabel, m_rotEntry, m_rotUnit ),
    m_stateX( 0.0 ),
    m_stateY( 0.0 ),
    m_stateRadius( 0.0 )
{
    // We can't set the tab order through wxWidgets due to shortcomings in their mnemonics
    // implementation on MSW
    m_tabOrder = {
        m_xEntry,
        m_yEntry,
        m_rotEntry,
        m_anchorOptions,
        m_stdButtonsOK,
        m_stdButtonsCancel
    };

    // Configure display origin transforms
    m_moveX.SetCoordType( ORIGIN_TRANSFORMS::REL_X_COORD );
    m_moveY.SetCoordType( ORIGIN_TRANSFORMS::REL_Y_COORD );

    updateDialogControls( m_options.polarCoords );

    m_menuIDs.push_back( aAnchor );
    m_menuIDs.push_back( ROTATE_AROUND_USER_ORIGIN );

    if( aParent->IsType( FRAME_PCB_EDITOR ) )
        m_menuIDs.push_back( ROTATE_AROUND_AUX_ORIGIN );

    buildRotationAnchorMenu();

    // and set up the entries according to the saved options
    m_polarCoords->SetValue( m_options.polarCoords );
    m_xEntry->ChangeValue( m_options.entry1 );
    m_yEntry->ChangeValue( m_options.entry2 );

    // Force the evaluation when setting previous values
    m_moveX.RequireEval();
    m_moveY.RequireEval();
    m_rotate.RequireEval();

    m_rotate.SetUnits( EDA_UNITS::DEGREES );
    m_rotate.SetValue( m_options.entryRotation );
    m_anchorOptions->SetSelection( std::min( m_options.entryAnchorSelection, m_menuIDs.size() ) );

    SetupStandardButtons();

    finishDialogSettings();
}


void DIALOG_MOVE_EXACT::buildRotationAnchorMenu()
{
    wxArrayString menuItems;

    for( auto anchorID : m_menuIDs )
    {
        switch( anchorID )
        {
        case ROTATE_AROUND_ITEM_ANCHOR:
            menuItems.push_back( _( "Rotate around item anchor" ) );
            break;
        case ROTATE_AROUND_SEL_CENTER:
            menuItems.push_back( _( "Rotate around selection center" ) );
            break;
        case ROTATE_AROUND_USER_ORIGIN:
            menuItems.push_back( _( "Rotate around local coordinates origin" ) );
            break;
        case ROTATE_AROUND_AUX_ORIGIN:
            menuItems.push_back( _( "Rotate around drill/place origin" ) );
            break;
        }
    }

    m_anchorOptions->Set( menuItems );
}


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

    q = ( r != 0) ? EDA_ANGLE( VECTOR2D( x, y ) ) : ANGLE_0;
}


bool DIALOG_MOVE_EXACT::GetTranslationInIU( wxRealPoint& val, bool polar )
{
    if( polar )
    {
        const double    r = m_moveX.GetDoubleValue();
        const EDA_ANGLE q = m_moveY.GetAngleValue();

        val.x = r * q.Cos();
        val.y = r * q.Sin();
    }
    else
    {
        // direct read
        val.x = m_moveX.GetDoubleValue();
        val.y = m_moveY.GetDoubleValue();
    }

    // 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();
    double moveX = m_moveX.GetDoubleValue();
    double moveY = m_moveY.GetDoubleValue();
    updateDialogControls( newPolar );

    if( newPolar )
    {
        if( moveX != m_stateX || moveY != m_stateY )
        {
            m_stateX = moveX;
            m_stateY = moveY;
            ToPolarDeg( m_stateX, m_stateY, m_stateRadius, m_stateTheta );

            m_moveX.SetDoubleValue( m_stateRadius );
            m_stateRadius = m_moveX.GetDoubleValue();
            m_moveY.SetAngleValue( m_stateTheta );
            m_stateTheta = m_moveY.GetAngleValue();
        }
        else
        {
            m_moveX.SetDoubleValue( m_stateRadius );
            m_moveY.SetAngleValue( m_stateTheta );
        }
    }
    else
    {
        if( moveX != m_stateRadius || moveY != m_stateTheta.AsDegrees() )
        {
            m_stateRadius = moveX;
            m_stateTheta = EDA_ANGLE( moveY, DEGREES_T );
            m_stateX = m_stateRadius * m_stateTheta.Cos();
            m_stateY = m_stateRadius * m_stateTheta.Sin();

            m_moveX.SetDoubleValue( m_stateX );
            m_stateX = m_moveX.GetDoubleValue();
            m_moveY.SetDoubleValue( m_stateY );
            m_stateY = m_moveY.GetDoubleValue();
        }
        else
        {
            m_moveX.SetDoubleValue( m_stateX );
            m_moveY.SetDoubleValue( m_stateY );
        }
    }
}


void DIALOG_MOVE_EXACT::updateDialogControls( bool aPolar )
{
    if( aPolar )
    {
        m_moveX.SetLabel( _( "Distance:" ) );     // Polar radius
        m_moveY.SetLabel( _( "Angle:" ) );        // Polar theta or angle
        m_moveY.SetUnits( EDA_UNITS::DEGREES );
    }
    else
    {
        m_moveX.SetLabel( _( "Move X:" ) );
        m_moveY.SetLabel( _( "Move Y:" ) );
        m_moveY.SetUnits( GetUserUnits() );
    }

    Layout();
}


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

    if( obj == m_clearX )
    {
        m_moveX.SetValue( 0 );
    }
    else if( obj == m_clearY )
    {
        m_moveY.SetValue( 0 );
    }
    else if( obj == m_clearRot )
    {
        m_rotate.SetAngleValue( ANGLE_0 );
    }

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


bool DIALOG_MOVE_EXACT::TransferDataFromWindow()
{
    // for the output, we only deliver a Cartesian vector
    wxRealPoint translation;
    bool ok = GetTranslationInIU( translation, m_polarCoords->IsChecked() );
    m_translation.x = KiROUND(translation.x);
    m_translation.y = KiROUND(translation.y);
    m_rotation = m_rotate.GetAngleValue();
    m_rotationAnchor = m_menuIDs[ m_anchorOptions->GetSelection() ];

    // save the settings
    m_options.polarCoords = m_polarCoords->GetValue();
    m_options.entry1 = m_xEntry->GetValue();
    m_options.entry2 = m_yEntry->GetValue();
    m_options.entryRotation = m_rotEntry->GetValue();
    m_options.entryAnchorSelection = (size_t) std::max( m_anchorOptions->GetSelection(), 0 );

    return ok;
}


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

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

    event.Skip();
}


void DIALOG_MOVE_EXACT::OnTextChanged( wxCommandEvent& event )
{
    double delta_x = m_moveX.GetDoubleValue();
    double delta_y = m_moveY.GetDoubleValue();
    double max_border = std::numeric_limits<int>::max() * M_SQRT1_2;

    if( m_bbox.GetLeft() + delta_x < -max_border ||
            m_bbox.GetRight() + delta_x > max_border ||
            m_bbox.GetTop() + delta_y < -max_border ||
            m_bbox.GetBottom() + delta_y > max_border )
    {
        const wxString invalid_length = _( "Invalid movement values.  Movement would place selection "
                                           "outside of the maximum board area." );

        m_xEntry->SetToolTip( invalid_length );
        m_xEntry->SetForegroundColour( *wxRED );
        m_yEntry->SetToolTip( invalid_length );
        m_yEntry->SetForegroundColour( *wxRED );
        m_stdButtons->GetAffirmativeButton()->Disable();
    }
    else
    {
        m_xEntry->SetToolTip( "" );
        m_xEntry->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ) );
        m_yEntry->SetToolTip( "" );
        m_yEntry->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ) );
        m_stdButtons->GetAffirmativeButton()->Enable();
        event.Skip();
    }

}