File: pcb_tool.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 (244 lines) | stat: -rw-r--r-- 7,256 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
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2017 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_tool.h"

#include <view/view_controls.h>
#include <view/view.h>
#include <tool/tool_manager.h>
#include <board_commit.h>

#include <class_module.h>
#include <pcb_draw_panel_gal.h>

#include "selection_tool.h"
#include "pcb_actions.h"
#include "tool_event_utils.h"

void PCB_TOOL::doInteractiveItemPlacement( INTERACTIVE_PLACER_BASE* aPlacer,
                                           const wxString& aCommitMessage,
                                           int aOptions )
{
    using namespace std::placeholders;
    std::unique_ptr<BOARD_ITEM> newItem;

    Activate();

    BOARD_COMMIT commit( frame() );

    GetManager()->RunAction( PCB_ACTIONS::selectionClear, true );

    // do not capture or auto-pan until we start placing an item
    controls()->ShowCursor( true );
    controls()->SetSnapping( true );

    // Add a VIEW_GROUP that serves as a preview for the new item
    SELECTION preview;
    view()->Add( &preview );

    aPlacer->m_board = board();
    aPlacer->m_frame = frame();
    aPlacer->m_modifiers = 0;

    if( aOptions & IPO_SINGLE_CLICK )
    {
        VECTOR2I cursorPos = controls()->GetCursorPosition();

        newItem = aPlacer->CreateItem();
        newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );

        preview.Add( newItem.get() );
    }

    // Main loop: keep receiving events
    while( OPT_TOOL_EVENT evt = Wait() )
    {
        VECTOR2I cursorPos = controls()->GetCursorPosition();
        aPlacer->m_modifiers = evt->Modifier();

        if( TOOL_EVT_UTILS::IsCancelInteractive( *evt ) )
        {
            if( newItem )
            {
                // Delete the old item and have another try
                newItem = nullptr;

                preview.Clear();

                if( aOptions & IPO_SINGLE_CLICK )
                    break;

                controls()->SetAutoPan( false );
                controls()->CaptureCursor( false );
                controls()->ShowCursor( true );
            }
            else
            {
                break;
            }

            if( evt->IsActivate() )  // now finish unconditionally
                break;
        }

        else if( evt->IsClick( BUT_LEFT ) )
        {
            if( !newItem )
            {
                // create the item if possible
                newItem = aPlacer->CreateItem();

                // no item created, so wait for another click
                if( !newItem  )
                    continue;

                controls()->CaptureCursor( true );
                controls()->SetAutoPan( true );

                newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );

                preview.Add( newItem.get() );

                if( newItem->Type() == PCB_MODULE_T )
                {
                    auto module = dyn_cast<MODULE*>( newItem.get() );

                    // modules have more drawable parts
                    module->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Add, &preview, _1 ) );
                }
            }
            else
            {
                newItem->ClearFlags();
                preview.Remove( newItem.get() );

                aPlacer->PlaceItem( newItem.get(), commit );

                if( newItem->Type() == PCB_MODULE_T )
                {
                    auto module = dyn_cast<MODULE*>( newItem.get() );
                    module->RunOnChildren( std::bind( &KIGFX::VIEW_GROUP::Remove, &preview, _1 ) );
                }

                newItem.release();
                commit.Push( aCommitMessage );

                controls()->CaptureCursor( false );
                controls()->SetAutoPan( false );
                controls()->ShowCursor( true );

                if( !( aOptions & IPO_REPEAT ) )
                    break;

                if( aOptions & IPO_SINGLE_CLICK )
                {
                    VECTOR2I pos = controls()->GetCursorPosition();

                    newItem = aPlacer->CreateItem();
                    newItem->SetPosition( wxPoint( pos.x, pos.y ) );

                    preview.Add( newItem.get() );
                }
            }
        }

        else if( newItem && evt->Category() == TC_COMMAND )
        {
            /*
             * Handle any events that can affect the item as we move
             * it around, eg rotate and flip
             */

            if( TOOL_EVT_UTILS::IsRotateToolEvt( *evt ) && ( aOptions & IPO_ROTATE ) )
            {
                const auto rotationAngle = TOOL_EVT_UTILS::GetEventRotationAngle(
                        *frame(), *evt );
                newItem->Rotate( newItem->GetPosition(), rotationAngle );
                view()->Update( &preview );
            }
            else if( evt->IsAction( &PCB_ACTIONS::flip ) && ( aOptions & IPO_FLIP ) )
            {
                newItem->Flip( newItem->GetPosition() );
                view()->Update( &preview );
            }
        }

        else if( newItem && evt->IsMotion() )
        {
            // track the cursor
            newItem->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
            aPlacer->SnapItem( newItem.get() );

            // Show a preview of the item
            view()->Update( &preview );
        }
    }

    view()->Remove( &preview );
}

void PCB_TOOL::Reset( RESET_REASON aReason )
{

}

void PCB_TOOL::setTransitions()
{

}

PCB_DISPLAY_OPTIONS* PCB_TOOL::displayOptions() const
{
    return static_cast<PCB_DISPLAY_OPTIONS*>( frame()->GetDisplayOptions() );
}

PCB_DRAW_PANEL_GAL* PCB_TOOL::canvas() const
{
    return static_cast<PCB_DRAW_PANEL_GAL*>( frame()->GetGalCanvas() );
}

const SELECTION& PCB_TOOL::selection() const
{
    auto selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
    const auto& selection = selTool->GetSelection();
    return selection;
}

SELECTION& PCB_TOOL::selection()
{
    auto selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
    auto& selection = selTool->GetSelection();
    return selection;
}


void INTERACTIVE_PLACER_BASE::SnapItem( BOARD_ITEM *aItem )
{
    // Base implementation performs no snapping
}

void INTERACTIVE_PLACER_BASE::PlaceItem( BOARD_ITEM *aItem, BOARD_COMMIT& aCommit )
{
    aCommit.Add( aItem );
}