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  TOOL_INTERACTIVE( "common.InteractivePicker" ),
33  m_frame( nullptr )
34 {
35  resetPicker();
36 }
37 
38 
40 {
41  m_frame = getEditFrame<EDA_DRAW_FRAME>();
42 
43  auto& ctxMenu = m_menu.GetMenu();
44 
45  // cancel current tool goes in main context menu at the top if present
47  ctxMenu.AddSeparator( 1 );
48 
49  // Finally, add the standard zoom/grid items
51 
52  return true;
53 }
54 
55 
56 int PICKER_TOOL::Main( const TOOL_EVENT& aEvent )
57 {
59  int finalize_state = WAIT_CANCEL;
60 
61  std::string tool = *aEvent.Parameter<std::string*>();
62  m_frame->PushTool( tool );
63  Activate();
64 
65  setControls();
66 
67  auto setCursor =
68  [&]()
69  {
71  };
72 
73  // Set initial cursor
74  setCursor();
75 
76  while( TOOL_EVENT* evt = Wait() )
77  {
78  setCursor();
79  VECTOR2D cursorPos = controls->GetCursorPosition( m_frame->IsGridVisible() );
80 
81  if( evt->IsCancelInteractive() || evt->IsActivate() )
82  {
83  if( m_cancelHandler )
84  {
85  try
86  {
87  (*m_cancelHandler)();
88  }
89  catch( std::exception& )
90  {
91  }
92  }
93 
94  // Activating a new tool may have alternate finalization from canceling the current
95  // tool
96  if( evt->IsActivate() )
97  {
98  finalize_state = END_ACTIVATE;
99  }
100  else
101  {
102  evt->SetPassEvent( false );
103  finalize_state = EVT_CANCEL;
104  }
105 
106  break;
107  }
108 
109  else if( evt->IsClick( BUT_LEFT ) )
110  {
111  bool getNext = false;
112 
113  m_picked = cursorPos;
114 
115  if( m_clickHandler )
116  {
117  try
118  {
119  getNext = (*m_clickHandler)( *m_picked );
120  }
121  catch( std::exception& )
122  {
123  finalize_state = EXCEPTION_CANCEL;
124  break;
125  }
126  }
127 
128  if( !getNext )
129  {
130  finalize_state = CLICK_CANCEL;
131  break;
132  }
133  else
134  setControls();
135  }
136 
137  else if( evt->IsMotion() )
138  {
139  if( m_motionHandler )
140  {
141  try
142  {
143  (*m_motionHandler)( cursorPos );
144  }
145  catch( std::exception& )
146  {
147  }
148  }
149  }
150 
151  else if( evt->IsDblClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) )
152  {
153  // Not currently used, but we don't want to pass them either
154  }
155 
156  else if( evt->IsClick( BUT_RIGHT ) )
157  {
159  }
160 
161  else
162  evt->SetPassEvent();
163  }
164 
165  if( m_finalizeHandler )
166  {
167  try
168  {
169  (*m_finalizeHandler)( finalize_state );
170  }
171  catch( std::exception& )
172  {
173  }
174  }
175 
176  resetPicker();
177  controls->ForceCursorPosition( false );
178  m_frame->PopTool( tool );
179  return 0;
180 }
181 
182 
184 {
185  Go( &PICKER_TOOL::Main, ACTIONS::pickerTool.MakeEvent() );
186 }
187 
188 
190 {
192 
193  m_picked = NULLOPT;
198 }
199 
200 
202 {
204 
205  controls->CaptureCursor( false );
206  controls->SetAutoPan( false );
207 }
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
void AddStandardSubMenus(TOOL_MENU &aMenu)
Function CreateBasicMenu.
TOOL_MENU m_menu
functions below are not yet implemented - their interface may change
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
Definition: picker_tool.cpp:39
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
int Main(const TOOL_EVENT &aEvent)
Main event loop.
Definition: picker_tool.cpp:56
VIEW_CONTROLS class definition.
CONDITIONAL_MENU & GetMenu()
Function 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.
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
void SetCurrentCursor(KICURSOR cursor)
Function SetCurrentCursor Set the current cursor shape for this panel.
static TOOL_ACTION pickerTool
Definition: actions.h:153
OPT< VECTOR2D > m_picked
Definition: picker_tool.h:128
void setTransitions() override
const auto NULLOPT
Definition: optional.h:9
bool IsGridVisible() const
OPT< MOTION_HANDLER > m_motionHandler
Definition: picker_tool.h:124
virtual void PopTool(const std::string &actionName)
virtual void CaptureCursor(bool aEnabled)
Function CaptureCursor() Forces the cursor to stay within the drawing panel area.
T Parameter() const
Function Parameter() Returns a non-standard parameter assigned to the event.
Definition: tool_event.h:435
TOOL_EVENT.
Definition: tool_event.h:171
VIEW_CONTROLS is an interface for classes handling user events controlling the view behaviour (such a...
virtual void ForceCursorPosition(bool aEnabled, const VECTOR2D &aPosition=VECTOR2D(0, 0))
Function ForceCursorPosition() Places the cursor immediately at a given point.
OPT< CLICK_HANDLER > m_clickHandler
Definition: picker_tool.h:123
void setControls()
Applies the requested VIEW_CONTROLS settings.
OPT< FINALIZE_HANDLER > m_finalizeHandler
Definition: picker_tool.h:126
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg.
EDA_DRAW_FRAME * m_frame
Definition: picker_tool.h:120
void Activate()
Function Activate() Runs the tool.
OPT< CANCEL_HANDLER > m_cancelHandler
Definition: picker_tool.h:125
KICURSOR m_cursor
Definition: picker_tool.h:121
void resetPicker()
Reinitializes tool to its initial state.
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:42
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Adds a menu entry to run a TOOL_ACTION on selected items.
void ShowContextMenu(SELECTION &aSelection)
Function ShowContextMenu.
Definition: tool_menu.cpp:59
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.