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"
30#include <view/view_controls.h>
32
33
35 PCB_TOOL_BASE( "pcbnew.InteractivePicker" ),
37{
38}
39
40
42{
44 PCB_BASE_FRAME* frame = getEditFrame<PCB_BASE_FRAME>();
46 int finalize_state = WAIT_CANCEL;
47
48 TOOL_EVENT sourceEvent;
49
50 if( aEvent.IsAction( &ACTIONS::pickerTool ) )
51 {
52 wxCHECK_MSG( aEvent.Parameter<const TOOL_EVENT*>(), -1,
53 wxT( "PCB_PICKER_TOOL::Main() called without a source event" ) );
54
55 sourceEvent = *aEvent.Parameter<const TOOL_EVENT*>();
56 frame->PushTool( sourceEvent );
57 }
58
59 Activate();
61
62 auto setCursor =
63 [&]()
64 {
66 controls->ShowCursor( true );
67 };
68
69 // Set initial cursor
70 setCursor();
71 VECTOR2D cursorPos;
72
73 while( TOOL_EVENT* evt = Wait() )
74 {
75 setCursor();
76 cursorPos = controls->GetMousePosition();
77
78 if( m_snap )
79 {
80 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
81 grid.SetUseGrid( getView()->GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
82 cursorPos = grid.BestSnapAnchor( cursorPos, nullptr );
83 controls->ForceCursorPosition( true, cursorPos );
84 }
85
86 if( evt->IsCancelInteractive() || evt->IsActivate() )
87 {
88 if( m_cancelHandler )
89 {
90 try
91 {
92 (*m_cancelHandler)();
93 }
94 catch( std::exception& )
95 {
96 }
97 }
98
99 // Activating a new tool may have alternate finalization from canceling the current tool
100 if( evt->IsActivate() )
101 finalize_state = END_ACTIVATE;
102 else
103 finalize_state = EVT_CANCEL;
104
105 break;
106 }
107 else if( evt->IsClick( BUT_LEFT ) )
108 {
109 bool getNext = false;
110
111 m_picked = cursorPos;
112
113 if( m_clickHandler )
114 {
115 try
116 {
117 getNext = (*m_clickHandler)( *m_picked );
118 }
119 catch( std::exception& )
120 {
121 finalize_state = EXCEPTION_CANCEL;
122 break;
123 }
124 }
125
126 if( !getNext )
127 {
128 finalize_state = CLICK_CANCEL;
129 break;
130 }
131 else
132 {
133 setControls();
134 }
135 }
136 else if( evt->IsMotion() )
137 {
138 if( m_motionHandler )
139 {
140 try
141 {
142 (*m_motionHandler)( cursorPos );
143 }
144 catch( std::exception& )
145 {
146 }
147 }
148 }
149 else if( evt->IsDblClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) )
150 {
151 // Not currently used, but we don't want to pass them either
152 }
153 else if( evt->IsClick( BUT_RIGHT ) )
154 {
157 }
158 // TODO: It'd be nice to be able to say "don't allow any non-trivial editing actions",
159 // but we don't at present have that, so we just knock out some of the egregious ones.
160 else if( ZONE_FILLER_TOOL::IsZoneFillAction( evt ) )
161 {
162 wxBell();
163 }
164 else
165 {
166 evt->SetPassEvent();
167 }
168 }
169
171 {
172 try
173 {
174 (*m_finalizeHandler)( finalize_state );
175 }
176 catch( std::exception& )
177 {
178 }
179 }
180
181 reset();
183 controls->ShowCursor( false );
184
185 if( aEvent.IsAction( &ACTIONS::pickerTool ) )
186 frame->PopTool( sourceEvent );
187
188 return 0;
189}
190
191
193{
196}
197
198
200{
202
203 controls->CaptureCursor( false );
204 controls->SetAutoPan( false );
205}
206
207
209{
212}
213
214
static TOOL_ACTION pickerSubTool
Definition: actions.h:190
static TOOL_ACTION pickerTool
Definition: actions.h:189
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:898
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:122
std::optional< FINALIZE_HANDLER > m_finalizeHandler
Definition: picker_tool.h:120
std::optional< MOTION_HANDLER > m_motionHandler
Definition: picker_tool.h:118
std::optional< CLICK_HANDLER > m_clickHandler
Definition: picker_tool.h:117
std::optional< CANCEL_HANDLER > m_cancelHandler
Definition: picker_tool.h:119
KICURSOR m_cursor
Definition: picker_tool.h:114
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:167
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:82
T Parameter() const
Return a parameter assigned to the event.
Definition: tool_event.h:460
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:142
@ BUT_LEFT
Definition: tool_event.h:131
@ BUT_RIGHT
Definition: tool_event.h:132