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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <tool/actions.h>
26#include <tool/picker_tool.h>
27#include <view/view_controls.h>
28#include <eda_draw_frame.h>
29#include <wx/debug.h>
30
31
33{
35 m_snap = true;
36
37 m_picked = std::nullopt;
38 m_clickHandler = std::nullopt;
39 m_motionHandler = std::nullopt;
40 m_cancelHandler = std::nullopt;
41 m_finalizeHandler = std::nullopt;
42}
43
44
45PICKER_TOOL::PICKER_TOOL( const std::string& aName ) :
46 TOOL_INTERACTIVE( aName ),
48{
49}
50
51
53 TOOL_INTERACTIVE( "common.InteractivePicker" ),
55{
56}
57
58
60{
62
63 auto& ctxMenu = m_menu->GetMenu();
64
65 // cancel current tool goes in main context menu at the top if present
67 ctxMenu.AddSeparator( 1 );
68
69 // Finally, add the standard zoom/grid items
70 m_frame->AddStandardSubMenus( *m_menu.get() );
71
72 return true;
73}
74
75
76int PICKER_TOOL::Main( const TOOL_EVENT& aEvent )
77{
79 int finalize_state = WAIT_CANCEL;
80
81 wxCHECK_MSG( aEvent.Parameter<const TOOL_EVENT*>(), -1,
82 wxT( "PICKER_TOOL::Main() called without a source event" ) );
83
84 const TOOL_EVENT sourceEvent = *aEvent.Parameter<const TOOL_EVENT*>();
85
86 m_frame->PushTool( sourceEvent );
87 Activate();
88
90
91 auto setCursor =
92 [&]()
93 {
94 m_frame->GetCanvas()->SetCurrentCursor( m_cursor );
95 };
96
97 // Set initial cursor
98 setCursor();
99
100 while( TOOL_EVENT* evt = Wait() )
101 {
102 setCursor();
103 VECTOR2D cursorPos = controls->GetCursorPosition( m_snap && m_frame->IsGridVisible() );
104 m_modifiers = aEvent.Modifier();
105
106 if( evt->IsCancelInteractive() || evt->IsActivate() )
107 {
108 if( m_cancelHandler )
109 {
110 try
111 {
112 (*m_cancelHandler)();
113 }
114 catch( std::exception& )
115 {
116 }
117 }
118
119 // Activating a new tool may have alternate finalization from canceling the current
120 // tool
121 if( evt->IsActivate() )
122 {
123 finalize_state = END_ACTIVATE;
124 }
125 else
126 {
127 evt->SetPassEvent( false );
128 finalize_state = EVT_CANCEL;
129 }
130
131 break;
132 }
133 else if( evt->IsClick( BUT_LEFT ) )
134 {
135 bool getNext = false;
136
137 m_picked = cursorPos;
138
139 if( m_clickHandler )
140 {
141 try
142 {
143 getNext = (*m_clickHandler)( *m_picked );
144 }
145 catch( std::exception& )
146 {
147 finalize_state = EXCEPTION_CANCEL;
148 break;
149 }
150 }
151
152 if( !getNext )
153 {
154 finalize_state = CLICK_CANCEL;
155 break;
156 }
157 else
158 {
159 setControls();
160 }
161 }
162 else if( evt->IsMotion() )
163 {
164 if( m_motionHandler )
165 {
166 try
167 {
168 (*m_motionHandler)( cursorPos );
169 }
170 catch( std::exception& )
171 {
172 }
173 }
174 }
175 else if( evt->IsDblClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) )
176 {
177 // Not currently used, but we don't want to pass them either
178 }
179 else if( evt->IsClick( BUT_RIGHT ) )
180 {
181 m_menu->ShowContextMenu();
182 }
183 else
184 {
185 evt->SetPassEvent();
186 }
187 }
188
190 {
191 try
192 {
193 (*m_finalizeHandler)( finalize_state );
194 }
195 catch( std::exception& )
196 {
197 }
198 }
199
200 reset();
201 controls->ForceCursorPosition( false );
202 m_frame->PopTool( sourceEvent );
203 return 0;
204}
205
206
211
212
214{
216
217 controls->CaptureCursor( false );
218 controls->SetAutoPan( false );
219}
static TOOL_ACTION cancelInteractive
Definition actions.h:72
static TOOL_ACTION pickerTool
Definition actions.h:252
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:186
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition tool_base.cpp:44
Generic, UI-independent tool event.
Definition tool_event.h:171
int Modifier(int aMask=MD_MODIFIER_MASK) const
Return information about key modifiers state (Ctrl, Alt, etc.).
Definition tool_event.h:366
T Parameter() const
Return a parameter assigned to the event.
Definition tool_event.h:473
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:46
@ BUT_LEFT
Definition tool_event.h:132
@ BUT_RIGHT
Definition tool_event.h:133
VECTOR2< double > VECTOR2D
Definition vector2d.h:694