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) 2015 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 "picker_tool.h"
26 #include "pcb_actions.h"
27 #include "grid_helper.h"
28 #include <view/view_controls.h>
29 #include <tool/tool_manager.h>
30 #include "tool_event_utils.h"
31 #include "selection_tool.h"
32 
33 TOOL_ACTION PCB_ACTIONS::pickerTool( "pcbnew.Picker", AS_GLOBAL, 0, "", "", NULL, AF_ACTIVATE );
34 
35 
37  : PCB_TOOL_BASE( "pcbnew.Picker" )
38 {
39  reset();
40 }
41 
42 
43 int PICKER_TOOL::Main( const TOOL_EVENT& aEvent )
44 {
46  GRID_HELPER grid( frame() );
47  int finalize_state = WAIT_CANCEL;
48 
49  setControls();
50 
51  while( OPT_TOOL_EVENT evt = Wait() )
52  {
53  grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
54  grid.SetUseGrid( !evt->Modifier( MD_ALT ) );
55  controls->SetSnapping( !evt->Modifier( MD_ALT ) );
56  VECTOR2I cursorPos = grid.BestSnapAnchor( controls->GetMousePosition(), nullptr );
57  controls->ForceCursorPosition(true, cursorPos );
58 
59  if( evt->IsClick( BUT_LEFT ) )
60  {
61  bool getNext = false;
62 
63  m_picked = cursorPos;
64 
65  if( m_clickHandler )
66  {
67  try
68  {
69  getNext = (*m_clickHandler)( *m_picked );
70  }
71  catch( std::exception& e )
72  {
73  std::cerr << "PICKER_TOOL click handler error: " << e.what() << std::endl;
74  finalize_state = EXCEPTION_CANCEL;
75  break;
76  }
77  }
78 
79  if( !getNext )
80  {
81  finalize_state = CLICK_CANCEL;
82  break;
83  }
84  else
85  setControls();
86  }
87 
88  else if( TOOL_EVT_UTILS::IsCancelInteractive( *evt ) )
89  {
90  if( m_cancelHandler )
91  {
92  try
93  {
94  (*m_cancelHandler)();
95  }
96  catch( std::exception& e )
97  {
98  std::cerr << "PICKER_TOOL cancel handler error: " << e.what() << std::endl;
99  }
100  }
101 
102  // Activating a new tool may have alternate finalization from canceling the current tool
103  if( evt->IsActivate() )
104  finalize_state = END_ACTIVATE;
105  else
106  finalize_state = EVT_CANCEL;
107 
108  break;
109  }
110 
111  else if( evt->IsClick( BUT_RIGHT ) )
113 
114  else
115  m_toolMgr->PassEvent();
116  }
117 
118  if( m_finalizeHandler )
119  {
120  try
121  {
122  (*m_finalizeHandler)( finalize_state );
123  }
124  catch( std::exception& e )
125  {
126  std::cerr << "PICKER_TOOL finalize handler error: " << e.what() << std::endl;
127  }
128  }
129 
130  reset();
131  controls->ForceCursorPosition( false );
132  getEditFrame<PCB_BASE_FRAME>()->SetNoToolSelected();
133 
134  return 0;
135 }
136 
137 
139 {
141 }
142 
143 
145 {
146  m_cursorCapture = false;
147  m_autoPanning = false;
149 
150  m_picked = NULLOPT;
154 }
155 
156 
158 {
160 
161  // Ensure that the view controls do not handle our snapping as we use the GRID_HELPER
162  controls->SetSnapping( false );
163 
166 }
TOOL_MENU m_menu
Menu model displayed by the tool.
virtual VECTOR2D GetMousePosition(bool aWorldCoordinates=true) const =0
Function GetMousePosition() Returns the current mouse pointer position.
bool m_autoPanning
Definition: picker_tool.h:114
int Main(const TOOL_EVENT &aEvent)
Main event loop.
Definition: picker_tool.cpp:43
VIEW_CONTROLS class definition.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:216
OPT_TOOL_EVENT Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
virtual void SetSnapping(bool aEnabled)
Function SetSnapping() Enables/disables snapping cursor to grid.
VECTOR2I BestSnapAnchor(const VECTOR2I &aOrigin, BOARD_ITEM *aDraggedItem)
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
OPT< VECTOR2D > m_picked
Definition: picker_tool.h:123
void PassEvent()
Allows a tool to pass the already handled event to the next tool on the stack.
Definition: tool_manager.h:356
LSET m_layerMask
The layer set to use for optional snapping
Definition: picker_tool.h:117
void setTransitions() override
bool m_cursorCapture
Definition: picker_tool.h:113
const auto NULLOPT
Definition: optional.h:9
virtual void CaptureCursor(bool aEnabled)
Function CaptureCursor() Forces the cursor to stay within the drawing panel area.
static TOOL_ACTION pickerTool
Definition: pcb_actions.h:306
Class TOOL_EVENT.
Definition: tool_event.h:167
void reset()
Reinitializes tool to its initial state.
Class VIEW_CONTROLS is an interface for classes handling user events controlling the view behaviour (...
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:119
static LSET AllLayersMask()
Definition: lset.cpp:713
void setControls()
Applies the requested VIEW_CONTROLS settings.
OPT< FINALIZE_HANDLER > m_finalizeHandler
Definition: picker_tool.h:121
bool IsCancelInteractive(const TOOL_EVENT &aEvt)
Function IsCancelInteractive()
Definition: tool_event.cpp:177
All active tools
Definition: tool_event.h:143
PCB_EDIT_FRAME * frame() const
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg.
void SetSnap(bool aSnap)
Definition: grid_helper.h:72
KIGFX::VIEW_CONTROLS * controls() const
Class TOOL_ACTION.
Definition: tool_action.h:46
OPT< CANCEL_HANDLER > m_cancelHandler
Definition: picker_tool.h:120
void SetUseGrid(bool aGrid=true)
Definition: grid_helper.h:77
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:41
void ShowContextMenu(SELECTION &aSelection)
Function ShowContextMenu.
Definition: tool_menu.cpp:59
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:486