KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_picker_tool.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) 2015 CERN
5 * Copyright (C) 2019-2022 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
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 "pcb_picker_tool.h"
27#include "pcb_actions.h"
28#include "pcb_grid_helper.h"
29#include <view/view_controls.h>
31
32
34 PCB_TOOL_BASE( "pcbnew.InteractivePicker" ),
36{
37}
38
39
41{
43 PCB_BASE_FRAME* frame = getEditFrame<PCB_BASE_FRAME>();
45 int finalize_state = WAIT_CANCEL;
46
47 TOOL_EVENT sourceEvent;
48
49 if( aEvent.IsAction( &ACTIONS::pickerTool ) )
50 {
51 wxCHECK_MSG( aEvent.Parameter<const TOOL_EVENT*>(), -1,
52 wxT( "PCB_PICKER_TOOL::Main() called without a source event" ) );
53
54 sourceEvent = *aEvent.Parameter<const TOOL_EVENT*>();
55 frame->PushTool( sourceEvent );
56 }
57
58 Activate();
60
61 auto setCursor =
62 [&]()
63 {
65 controls->ShowCursor( true );
66 };
67
68 // Set initial cursor
69 setCursor();
70 VECTOR2D cursorPos;
71
72 while( TOOL_EVENT* evt = Wait() )
73 {
74 setCursor();
75 cursorPos = controls->GetMousePosition();
76
77 if( m_snap )
78 {
79 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
80 grid.SetUseGrid( getView()->GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
81 cursorPos = grid.BestSnapAnchor( cursorPos, nullptr );
82 controls->ForceCursorPosition( true, cursorPos );
83 }
84
85 if( evt->IsCancelInteractive() || evt->IsActivate() )
86 {
87 if( m_cancelHandler )
88 {
89 try
90 {
91 (*m_cancelHandler)();
92 }
93 catch( std::exception& )
94 {
95 }
96 }
97
98 // Activating a new tool may have alternate finalization from canceling the current tool
99 if( evt->IsActivate() )
100 finalize_state = END_ACTIVATE;
101 else
102 finalize_state = EVT_CANCEL;
103
104 break;
105 }
106 else if( evt->IsClick( BUT_LEFT ) )
107 {
108 bool getNext = false;
109
110 m_picked = cursorPos;
111
112 if( m_clickHandler )
113 {
114 try
115 {
116 getNext = (*m_clickHandler)( *m_picked );
117 }
118 catch( std::exception& )
119 {
120 finalize_state = EXCEPTION_CANCEL;
121 break;
122 }
123 }
124
125 if( !getNext )
126 {
127 finalize_state = CLICK_CANCEL;
128 break;
129 }
130 else
131 {
132 setControls();
133 }
134 }
135 else if( evt->IsMotion() )
136 {
137 if( m_motionHandler )
138 {
139 try
140 {
141 (*m_motionHandler)( cursorPos );
142 }
143 catch( std::exception& )
144 {
145 }
146 }
147 }
148 else if( evt->IsDblClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) )
149 {
150 // Not currently used, but we don't want to pass them either
151 }
152 else if( evt->IsClick( BUT_RIGHT ) )
153 {
156 }
157 // TODO: It'd be nice to be able to say "don't allow any non-trivial editing actions",
158 // but we don't at present have that, so we just knock out some of the egregious ones.
159 else if( ZONE_FILLER_TOOL::IsZoneFillAction( evt ) )
160 {
161 wxBell();
162 }
163 else
164 {
165 evt->SetPassEvent();
166 }
167 }
168
170 {
171 try
172 {
173 (*m_finalizeHandler)( finalize_state );
174 }
175 catch( std::exception& )
176 {
177 }
178 }
179
180 reset();
182 controls->ShowCursor( false );
183
184 if( aEvent.IsAction( &ACTIONS::pickerTool ) )
185 frame->PopTool( sourceEvent );
186
187 return 0;
188}
189
190
192{
195}
196
197
199{
201
202 controls->CaptureCursor( false );
203 controls->SetAutoPan( false );
204}
205
206
208{
211}
212
213
static TOOL_ACTION pickerSubTool
Definition: actions.h:160
static TOOL_ACTION pickerTool
Definition: actions.h:159
void SetCurrentCursor(KICURSOR aCursor)
Set the current cursor shape for this panel.
An interface for classes handling user events controlling the view behavior such as zooming,...
virtual void CaptureCursor(bool aEnabled)
Force the cursor to stay within the drawing panel area.
virtual void ForceCursorPosition(bool aEnabled, const VECTOR2D &aPosition=VECTOR2D(0, 0))
Place the cursor immediately at a given point.
virtual void ShowCursor(bool aEnabled)
Enable or disables display of cursor.
virtual VECTOR2D GetMousePosition(bool aWorldCoordinates=true) const =0
Return the current mouse pointer position.
virtual void SetAutoPan(bool aEnabled)
Turn on/off auto panning (this feature is used when there is a tool active (eg.
static LSET AllLayersMask()
Definition: lset.cpp:808
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
virtual MAGNETIC_SETTINGS * GetMagneticItemsSettings()
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
LSET m_layerMask
< The layer set to use for optional snapping.
int Main(const TOOL_EVENT &aEvent)
void setControls()
Reinitialize tool to its initial state.
void reset() override
< Reinitializes tool to its initial state.
void setTransitions() override
<
PCB_BASE_EDIT_FRAME * frame() const
KIGFX::VIEW_CONTROLS * controls() const
std::optional< VECTOR2D > m_picked
Definition: picker_tool.h:121
std::optional< FINALIZE_HANDLER > m_finalizeHandler
Definition: picker_tool.h:119
std::optional< MOTION_HANDLER > m_motionHandler
Definition: picker_tool.h:117
std::optional< CLICK_HANDLER > m_clickHandler
Definition: picker_tool.h:116
std::optional< CANCEL_HANDLER > m_cancelHandler
Definition: picker_tool.h:118
KICURSOR m_cursor
Definition: picker_tool.h:113
virtual void reset()
< Reinitializes tool to its initial state.
Definition: picker_tool.cpp:32
virtual void PopTool(const TOOL_EVENT &aEvent)
Pops a tool from the stack.
virtual void PushTool(const TOOL_EVENT &aEvent)
NB: the definition of "tool" is different at the user level.
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition: tool_base.cpp:42
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:216
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:36
Generic, UI-independent tool event.
Definition: tool_event.h:156
T Parameter() const
Return a non-standard parameter assigned to the event.
Definition: tool_event.h:442
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
Definition: tool_event.cpp:81
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
TOOL_MENU m_menu
The functions below are not yet implemented - their interface may change.
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Suspend execution of the tool until an event specified in aEventList arrives.
void Activate()
Run the tool.
void ShowContextMenu(SELECTION &aSelection)
Helper function to set and immediately show a CONDITIONAL_MENU in concert with the given SELECTION.
Definition: tool_menu.cpp:57
static bool IsZoneFillAction(const TOOL_EVENT *aEvent)
std::vector< FAB_LAYER_COLOR > dummy
@ MD_SHIFT
Definition: tool_event.h:138
@ BUT_LEFT
Definition: tool_event.h:127
@ BUT_RIGHT
Definition: tool_event.h:128