KiCad PCB EDA Suite
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 (C) 2019 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 
30 
32 {
34  m_snap = true;
35 
36  m_picked = NULLOPT;
41 }
42 
43 
44 PICKER_TOOL::PICKER_TOOL( const std::string& aName ) :
45  TOOL_INTERACTIVE( aName ),
47 {
48 }
49 
50 
52  TOOL_INTERACTIVE( "common.InteractivePicker" ),
54 {
55 }
56 
57 
59 {
60  m_frame = getEditFrame<EDA_DRAW_FRAME>();
61 
62  auto& ctxMenu = m_menu.GetMenu();
63 
64  // cancel current tool goes in main context menu at the top if present
66  ctxMenu.AddSeparator( 1 );
67 
68  // Finally, add the standard zoom/grid items
70 
71  return true;
72 }
73 
74 
75 int PICKER_TOOL::Main( const TOOL_EVENT& aEvent )
76 {
78  int finalize_state = WAIT_CANCEL;
79 
80  std::string tool = *aEvent.Parameter<std::string*>();
81  m_frame->PushTool( tool );
82  Activate();
83 
84  setControls();
85 
86  auto setCursor =
87  [&]()
88  {
90  };
91 
92  // Set initial cursor
93  setCursor();
94 
95  while( TOOL_EVENT* evt = Wait() )
96  {
97  setCursor();
98  VECTOR2D cursorPos = controls->GetCursorPosition( m_snap && m_frame->IsGridVisible() );
99 
100  if( evt->IsCancelInteractive() || evt->IsActivate() )
101  {
102  if( m_cancelHandler )
103  {
104  try
105  {
106  (*m_cancelHandler)();
107  }
108  catch( std::exception& )
109  {
110  }
111  }
112 
113  // Activating a new tool may have alternate finalization from canceling the current
114  // tool
115  if( evt->IsActivate() )
116  {
117  finalize_state = END_ACTIVATE;
118  }
119  else
120  {
121  evt->SetPassEvent( false );
122  finalize_state = EVT_CANCEL;
123  }
124 
125  break;
126  }
127 
128  else if( evt->IsClick( BUT_LEFT ) )
129  {
130  bool getNext = false;
131 
132  m_picked = cursorPos;
133 
134  if( m_clickHandler )
135  {
136  try
137  {
138  getNext = (*m_clickHandler)( *m_picked );
139  }
140  catch( std::exception& )
141  {
142  finalize_state = EXCEPTION_CANCEL;
143  break;
144  }
145  }
146 
147  if( !getNext )
148  {
149  finalize_state = CLICK_CANCEL;
150  break;
151  }
152  else
153  setControls();
154  }
155 
156  else if( evt->IsMotion() )
157  {
158  if( m_motionHandler )
159  {
160  try
161  {
162  (*m_motionHandler)( cursorPos );
163  }
164  catch( std::exception& )
165  {
166  }
167  }
168  }
169 
170  else if( evt->IsDblClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) )
171  {
172  // Not currently used, but we don't want to pass them either
173  }
174 
175  else if( evt->IsClick( BUT_RIGHT ) )
176  {
178  }
179 
180  else
181  evt->SetPassEvent();
182  }
183 
184  if( m_finalizeHandler )
185  {
186  try
187  {
188  (*m_finalizeHandler)( finalize_state );
189  }
190  catch( std::exception& )
191  {
192  }
193  }
194 
195  reset();
196  controls->ForceCursorPosition( false );
197  m_frame->PopTool( tool );
198  return 0;
199 }
200 
201 
203 {
204  Go( &PICKER_TOOL::Main, ACTIONS::pickerTool.MakeEvent() );
205 }
206 
207 
209 {
211 
212  controls->CaptureCursor( false );
213  controls->SetAutoPan( false );
214 }
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
void AddStandardSubMenus(TOOL_MENU &aMenu)
Construct a "basic" menu for a tool, containing only items that apply to all tools (e....
TOOL_MENU m_menu
The functions below are not yet implemented - their interface may change.
bool Init() override
Init() is called once upon a registration of the tool.
Definition: picker_tool.cpp:58
KICURSOR m_cursor
Definition: picker_tool.h:111
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.
int Main(const TOOL_EVENT &aEvent)
Definition: picker_tool.cpp:75
OPT< VECTOR2D > m_picked
Definition: picker_tool.h:119
CONDITIONAL_MENU & GetMenu()
Definition: tool_menu.cpp:46
virtual EDA_DRAW_PANEL_GAL * GetCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
static TOOL_ACTION cancelInteractive
Definition: actions.h:65
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
OPT< CANCEL_HANDLER > m_cancelHandler
Definition: picker_tool.h:116
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).
OPT< FINALIZE_HANDLER > m_finalizeHandler
Definition: picker_tool.h:117
void SetCurrentCursor(KICURSOR cursor)
Set the current cursor shape for this panel.
static TOOL_ACTION pickerTool
Definition: actions.h:158
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
const auto NULLOPT
Definition: optional.h:9
virtual void reset()
< Reinitializes tool to its initial state.
Definition: picker_tool.cpp:31
bool IsGridVisible() const
virtual void PopTool(const std::string &actionName)
virtual void CaptureCursor(bool aEnabled)
Force the cursor to stay within the drawing panel area.
T Parameter() const
Return a non-standard parameter assigned to the event.
Definition: tool_event.h:443
Generic, UI-independent tool event.
Definition: tool_event.h:173
An interface for classes handling user events controlling the view behavior such as zooming,...
virtual void ForceCursorPosition(bool aEnabled, const VECTOR2D &aPosition=VECTOR2D(0, 0))
Place the cursor immediately at a given point.
void setControls()
< Applies the requested VIEW_CONTROLS settings.
virtual void SetAutoPan(bool aEnabled)
Turn on/off auto panning (this feature is used when there is a tool active (eg.
OPT< CLICK_HANDLER > m_clickHandler
Definition: picker_tool.h:114
EDA_DRAW_FRAME * m_frame
Definition: picker_tool.h:110
OPT< MOTION_HANDLER > m_motionHandler
Definition: picker_tool.h:115
void Activate()
Run the tool.
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition: tool_base.cpp:42
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Add a menu entry to run a TOOL_ACTION on selected items.
void ShowContextMenu(SELECTION &aSelection)
Helper function to set and immediately show a CONDITIONAL_MENU in concert with the given SELECTION.
Definition: tool_menu.cpp:59
VECTOR2D GetCursorPosition() const
Return the current cursor position in world coordinates.