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_areaHandler = std::nullopt;
37 m_cancelHandler = std::nullopt;
38 m_finalizeHandler = std::nullopt;
39}
40
41
42PICKER_TOOL::PICKER_TOOL( const std::string& aName ) :
43 TOOL_INTERACTIVE( aName ),
45{
46}
47
48
50 TOOL_INTERACTIVE( "common.InteractivePicker" ),
52{
53}
54
55
57{
59
60 auto& ctxMenu = m_menu->GetMenu();
61
62 // cancel current tool goes in main context menu at the top if present
64 ctxMenu.AddSeparator( 1 );
65
66 // Finally, add the standard zoom/grid items
67 m_frame->AddStandardSubMenus( *m_menu.get() );
68
69 return true;
70}
71
72
73int PICKER_TOOL::Main( const TOOL_EVENT& aEvent )
74{
76 int finalize_state = WAIT_CANCEL;
77
78 wxCHECK_MSG( aEvent.Parameter<const TOOL_EVENT*>(), -1,
79 wxT( "PICKER_TOOL::Main() called without a source event" ) );
80
81 const TOOL_EVENT sourceEvent = *aEvent.Parameter<const TOOL_EVENT*>();
82
83 SCOPED_TOOL_PUSHER raii( m_frame, sourceEvent );
84 Activate();
85
87
88 auto setCursor =
89 [&]()
90 {
91 m_frame->GetCanvas()->SetCurrentCursor( m_cursor );
92 };
93
94 // Set initial cursor
95 setCursor();
96
97 while( TOOL_EVENT* evt = Wait() )
98 {
99 setCursor();
100 VECTOR2D cursorPos = controls->GetCursorPosition( m_snap && m_frame->IsGridVisible() );
101 m_modifiers = aEvent.Modifier();
102
103 if( evt->IsCancelInteractive() || evt->IsActivate() )
104 {
105 if( m_cancelHandler )
106 {
107 try
108 {
109 (*m_cancelHandler)();
110 }
111 catch( std::exception& )
112 {
113 }
114 }
115
116 // Activating a new tool may have alternate finalization from canceling the current
117 // tool
118 if( evt->IsActivate() )
119 {
120 finalize_state = END_ACTIVATE;
121 }
122 else
123 {
124 evt->SetPassEvent( false );
125 finalize_state = EVT_CANCEL;
126 }
127
128 break;
129 }
130 else if( evt->IsClick( BUT_LEFT ) )
131 {
132 bool getNext = false;
133
134 m_picked = cursorPos;
135
136 if( m_clickHandler )
137 {
138 try
139 {
140 getNext = (*m_clickHandler)( *m_picked );
141 }
142 catch( std::exception& )
143 {
144 finalize_state = EXCEPTION_CANCEL;
145 break;
146 }
147 }
148
149 if( !getNext )
150 {
151 finalize_state = CLICK_CANCEL;
152 break;
153 }
154 else
155 {
156 setControls();
157 }
158 }
159 else if( evt->IsMotion() )
160 {
161 if( m_motionHandler )
162 {
163 try
164 {
165 (*m_motionHandler)( cursorPos );
166 }
167 catch( std::exception& )
168 {
169 }
170 }
171 }
172 else if( evt->IsDblClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) )
173 {
174 // Not currently used, but we don't want to pass them either
175 }
176 else if( evt->IsClick( BUT_RIGHT ) )
177 {
178 m_menu->ShowContextMenu();
179 }
180 else
181 {
182 evt->SetPassEvent();
183 }
184 }
185
187 {
188 try
189 {
190 (*m_finalizeHandler)( finalize_state );
191 }
192 catch( std::exception& )
193 {
194 }
195 }
196
197 reset();
198 controls->ForceCursorPosition( false );
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.
std::optional< AREA_HANDLER > m_areaHandler
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