KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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) 2019 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <tool/actions.h>
22#include <tool/picker_tool.h>
23#include <view/view_controls.h>
24#include <eda_draw_frame.h>
25#include <wx/debug.h>
26
27
29{
31 m_snap = true;
32
33 m_picked = std::nullopt;
34 m_clickHandler = std::nullopt;
35 m_motionHandler = std::nullopt;
36 m_cancelHandler = std::nullopt;
37 m_finalizeHandler = std::nullopt;
38}
39
40
41PICKER_TOOL::PICKER_TOOL( const std::string& aName ) :
42 TOOL_INTERACTIVE( aName ),
44{
45}
46
47
49 TOOL_INTERACTIVE( "common.InteractivePicker" ),
51{
52}
53
54
56{
58
59 auto& ctxMenu = m_menu->GetMenu();
60
61 // cancel current tool goes in main context menu at the top if present
63 ctxMenu.AddSeparator( 1 );
64
65 // Finally, add the standard zoom/grid items
66 m_frame->AddStandardSubMenus( *m_menu.get() );
67
68 return true;
69}
70
71
72int PICKER_TOOL::Main( const TOOL_EVENT& aEvent )
73{
75 int finalize_state = WAIT_CANCEL;
76
77 wxCHECK_MSG( aEvent.Parameter<const TOOL_EVENT*>(), -1,
78 wxT( "PICKER_TOOL::Main() called without a source event" ) );
79
80 const TOOL_EVENT sourceEvent = *aEvent.Parameter<const TOOL_EVENT*>();
81
82 m_frame->PushTool( sourceEvent );
83 Activate();
84
86
87 auto setCursor =
88 [&]()
89 {
90 m_frame->GetCanvas()->SetCurrentCursor( m_cursor );
91 };
92
93 // Set initial cursor
94 setCursor();
95
96 while( TOOL_EVENT* evt = Wait() )
97 {
98 setCursor();
99 VECTOR2D cursorPos = controls->GetCursorPosition( m_snap && m_frame->IsGridVisible() );
100 m_modifiers = aEvent.Modifier();
101
102 if( evt->IsCancelInteractive() || evt->IsActivate() )
103 {
104 if( m_cancelHandler )
105 {
106 try
107 {
108 (*m_cancelHandler)();
109 }
110 catch( std::exception& )
111 {
112 }
113 }
114
115 // Activating a new tool may have alternate finalization from canceling the current
116 // tool
117 if( evt->IsActivate() )
118 {
119 finalize_state = END_ACTIVATE;
120 }
121 else
122 {
123 evt->SetPassEvent( false );
124 finalize_state = EVT_CANCEL;
125 }
126
127 break;
128 }
129 else if( evt->IsClick( BUT_LEFT ) )
130 {
131 bool getNext = false;
132
133 m_picked = cursorPos;
134
135 if( m_clickHandler )
136 {
137 try
138 {
139 getNext = (*m_clickHandler)( *m_picked );
140 }
141 catch( std::exception& )
142 {
143 finalize_state = EXCEPTION_CANCEL;
144 break;
145 }
146 }
147
148 if( !getNext )
149 {
150 finalize_state = CLICK_CANCEL;
151 break;
152 }
153 else
154 {
155 setControls();
156 }
157 }
158 else if( evt->IsMotion() )
159 {
160 if( m_motionHandler )
161 {
162 try
163 {
164 (*m_motionHandler)( cursorPos );
165 }
166 catch( std::exception& )
167 {
168 }
169 }
170 }
171 else if( evt->IsDblClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) )
172 {
173 // Not currently used, but we don't want to pass them either
174 }
175 else if( evt->IsClick( BUT_RIGHT ) )
176 {
177 m_menu->ShowContextMenu();
178 }
179 else
180 {
181 evt->SetPassEvent();
182 }
183 }
184
186 {
187 try
188 {
189 (*m_finalizeHandler)( finalize_state );
190 }
191 catch( std::exception& )
192 {
193 }
194 }
195
196 reset();
197 controls->ForceCursorPosition( false );
198 m_frame->PopTool( sourceEvent );
199 return 0;
200}
201
202
207
208
210{
212
213 controls->CaptureCursor( false );
214 controls->SetAutoPan( false );
215}
static TOOL_ACTION cancelInteractive
Definition actions.h:68
static TOOL_ACTION pickerTool
Definition actions.h:249
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.
VECTOR2D GetCursorPosition() const
Return the current cursor position in world coordinates.
virtual void SetAutoPan(bool aEnabled)
Turn on/off auto panning (this feature is used when there is a tool active (eg.
std::optional< VECTOR2D > m_picked
std::optional< FINALIZE_HANDLER > m_finalizeHandler
EDA_DRAW_FRAME * m_frame
std::optional< MOTION_HANDLER > m_motionHandler
std::optional< CLICK_HANDLER > m_clickHandler
std::optional< CANCEL_HANDLER > m_cancelHandler
virtual void reset()
Reinitializes tool to its initial state.
void setControls()
Applies the requested VIEW_CONTROLS settings.
bool Init() override
Init() is called once upon a registration of the tool.
int Main(const TOOL_EVENT &aEvent)
Main event loop.
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
T * getEditFrame() const
Return the application window object, casted to requested user type.
Definition tool_base.h:182
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition tool_base.cpp:40
Generic, UI-independent tool event.
Definition tool_event.h:167
int Modifier(int aMask=MD_MODIFIER_MASK) const
Return information about key modifiers state (Ctrl, Alt, etc.).
Definition tool_event.h:362
T Parameter() const
Return a parameter assigned to the event.
Definition tool_event.h:469
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).
std::unique_ptr< TOOL_MENU > m_menu
The functions below are not yet implemented - their interface may change.
TOOL_INTERACTIVE(TOOL_ID aId, const std::string &aName)
Create a tool with given id & name.
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.
@ ARROW
Definition cursors.h:42
@ BUT_LEFT
Definition tool_event.h:128
@ BUT_RIGHT
Definition tool_event.h:129
VECTOR2< double > VECTOR2D
Definition vector2d.h:682