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( "pcbnew.Picker" )
38 {
39  reset();
40 }
41 
42 
43 int PICKER_TOOL::Main( const TOOL_EVENT& aEvent )
44 {
46  GRID_HELPER grid( frame() );
47  setControls();
48 
49  while( OPT_TOOL_EVENT evt = Wait() )
50  {
51  grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
52  grid.SetUseGrid( !evt->Modifier( MD_ALT ) );
53  controls->SetSnapping( !evt->Modifier( MD_ALT ) );
54  VECTOR2I cursorPos = grid.BestSnapAnchor( controls->GetMousePosition(), nullptr );
55  controls->ForceCursorPosition(true, cursorPos );
56 
57  if( evt->IsClick( BUT_LEFT ) )
58  {
59  bool getNext = false;
60 
61  m_picked = cursorPos;
62 
63  if( m_clickHandler )
64  {
65  try
66  {
67  getNext = (*m_clickHandler)( *m_picked );
68  }
69  catch( std::exception& e )
70  {
71  std::cerr << "PICKER_TOOL click handler error: " << e.what() << std::endl;
72  break;
73  }
74  }
75 
76  if( !getNext )
77  break;
78  else
79  setControls();
80  }
81 
82  else if( evt->IsCancel() || TOOL_EVT_UTILS::IsCancelInteractive( *evt ) || evt->IsActivate() )
83  {
84  if( m_cancelHandler )
85  {
86  try
87  {
88  (*m_cancelHandler)();
89  }
90  catch( std::exception& e )
91  {
92  std::cerr << "PICKER_TOOL cancel handler error: " << e.what() << std::endl;
93  }
94  }
95 
96  break;
97  }
98 
99  else if( evt->IsClick( BUT_RIGHT ) )
101 
102  else
103  m_toolMgr->PassEvent();
104  }
105 
106  reset();
107  controls->ForceCursorPosition( false );
108  getEditFrame<PCB_BASE_FRAME>()->SetNoToolSelected();
109 
110  return 0;
111 }
112 
113 
115 {
117 }
118 
119 
121 {
122  m_cursorSnapping = true;
123  m_cursorVisible = true;
124  m_cursorCapture = false;
125  m_autoPanning = false;
127 
128  m_picked = NULLOPT;
131 }
132 
133 
135 {
137 
138  // Ensure that the view controls do not handle our snapping as we use the GRID_HELPER
139  controls->SetSnapping( false );
140 
141  controls->ShowCursor( m_cursorVisible );
142  controls->CaptureCursor( m_cursorCapture );
143  controls->SetAutoPan( m_autoPanning );
144 }
virtual void ShowCursor(bool aEnabled)
Function ShowCursor() Enables or disables display of cursor.
virtual VECTOR2D GetMousePosition(bool aWorldCoordinates=true) const =0
Function GetMousePosition() Returns the current mouse pointer position.
bool m_autoPanning
Definition: picker_tool.h:109
int Main(const TOOL_EVENT &aEvent)
Main event loop.
Definition: picker_tool.cpp:43
TOOL_MENU m_menu
Menu model displayed by the tool.
Definition: pcb_tool.h:148
VIEW_CONTROLS class definition.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:215
bool m_cursorSnapping
Definition: picker_tool.h:106
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.
bool m_cursorVisible
Definition: picker_tool.h:107
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
Picked point (if any).
Definition: picker_tool.h:119
void PassEvent()
Allows a tool to pass the already handled event to the next tool on the stack.
Definition: tool_manager.h:351
KIGFX::VIEW_CONTROLS * controls() const
Definition: pcb_tool.h:138
LSET m_layerMask
The layer set to use for optional snapping
Definition: picker_tool.h:112
void setTransitions() override
>
bool m_cursorCapture
Definition: picker_tool.h:108
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:363
PCB_EDIT_FRAME * frame() const
Definition: pcb_tool.h:139
Class TOOL_EVENT.
Definition: tool_event.h:168
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
Optional event handlers.
Definition: picker_tool.h:115
static LSET AllLayersMask()
Definition: lset.cpp:710
void setControls()
Applies the requested VIEW_CONTROLS settings.
bool IsCancelInteractive(const TOOL_EVENT &aEvt)
Function IsCancelInteractive()
All active tools
Definition: tool_event.h:144
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg...
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:41
void SetSnap(bool aSnap)
Definition: grid_helper.h:63
Class TOOL_ACTION.
Definition: tool_action.h:46
OPT< CANCEL_HANDLER > m_cancelHandler
Definition: picker_tool.h:116
void SetUseGrid(bool aGrid=true)
Definition: grid_helper.h:68
void ShowContextMenu(SELECTION &aSelection)
Function ShowContextMenu.
Definition: tool_menu.cpp:62
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:495