KiCad PCB EDA Suite
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 if( aEvent.IsAction( &ACTIONS::pickerTool ) )
48 frame->PushTool( aEvent );
49
50 Activate();
52
53 auto setCursor =
54 [&]()
55 {
57 controls->ShowCursor( true );
58 };
59
60 // Set initial cursor
61 setCursor();
62 VECTOR2D cursorPos;
63
64 while( TOOL_EVENT* evt = Wait() )
65 {
66 setCursor();
67 cursorPos = controls->GetMousePosition();
68
69 if( m_snap )
70 {
71 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
72 grid.SetUseGrid( getView()->GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
73 cursorPos = grid.BestSnapAnchor( cursorPos, nullptr );
74 controls->ForceCursorPosition( true, cursorPos );
75 }
76
77 if( evt->IsCancelInteractive() || evt->IsActivate() )
78 {
79 if( m_cancelHandler )
80 {
81 try
82 {
83 (*m_cancelHandler)();
84 }
85 catch( std::exception& )
86 {
87 }
88 }
89
90 // Activating a new tool may have alternate finalization from canceling the current tool
91 if( evt->IsActivate() )
92 finalize_state = END_ACTIVATE;
93 else
94 finalize_state = EVT_CANCEL;
95
96 break;
97 }
98 else if( evt->IsClick( BUT_LEFT ) )
99 {
100 bool getNext = false;
101
102 m_picked = cursorPos;
103
104 if( m_clickHandler )
105 {
106 try
107 {
108 getNext = (*m_clickHandler)( *m_picked );
109 }
110 catch( std::exception& )
111 {
112 finalize_state = EXCEPTION_CANCEL;
113 break;
114 }
115 }
116
117 if( !getNext )
118 {
119 finalize_state = CLICK_CANCEL;
120 break;
121 }
122 else
123 {
124 setControls();
125 }
126 }
127 else if( evt->IsMotion() )
128 {
129 if( m_motionHandler )
130 {
131 try
132 {
133 (*m_motionHandler)( cursorPos );
134 }
135 catch( std::exception& )
136 {
137 }
138 }
139 }
140 else if( evt->IsDblClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) )
141 {
142 // Not currently used, but we don't want to pass them either
143 }
144 else if( evt->IsClick( BUT_RIGHT ) )
145 {
148 }
149 // TODO: It'd be nice to be able to say "don't allow any non-trivial editing actions",
150 // but we don't at present have that, so we just knock out some of the egregious ones.
151 else if( ZONE_FILLER_TOOL::IsZoneFillAction( evt ) )
152 {
153 wxBell();
154 }
155 else
156 {
157 evt->SetPassEvent();
158 }
159 }
160
162 {
163 try
164 {
165 (*m_finalizeHandler)( finalize_state );
166 }
167 catch( std::exception& )
168 {
169 }
170 }
171
172 reset();
174 controls->ShowCursor( false );
175
176 if( aEvent.IsAction( &ACTIONS::pickerTool ) )
177 frame->PopTool( aEvent );
178
179 return 0;
180}
181
182
184{
187}
188
189
191{
193
194 controls->CaptureCursor( false );
195 controls->SetAutoPan( false );
196}
197
198
200{
203}
204
205
static TOOL_ACTION pickerSubTool
Definition: actions.h:159
static TOOL_ACTION pickerTool
Definition: actions.h:158
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:31
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:215
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
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:88
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