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 
29 #include <wxPcbStruct.h>
30 #include <view/view_controls.h>
31 #include <tool/tool_manager.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 
48  assert( !m_picking );
49  m_picking = true;
50  m_picked = NULLOPT;
51 
52  setControls();
53 
54 
55  while( OPT_TOOL_EVENT evt = Wait() )
56  {
57  auto mousePos = controls->GetMousePosition();
58  auto p = grid.BestSnapAnchor( mousePos, nullptr );
59  controls->ForceCursorPosition( true, p );
60 
61  if( evt->IsClick( BUT_LEFT ) )
62  {
63  bool getNext = false;
64 
65  m_picked = VECTOR2D( p );
66 
67  if( m_clickHandler )
68  {
69  try
70  {
71  getNext = (*m_clickHandler)( *m_picked );
72  }
73  catch( std::exception& e )
74  {
75  std::cerr << "PICKER_TOOL click handler error: " << e.what() << std::endl;
76  break;
77  }
78  }
79 
80  if( !getNext )
81  break;
82  else
83  setControls();
84  }
85 
86  else if( evt->IsCancel() || evt->IsActivate() )
87  break;
88 
89  else
91  }
92 
93  reset();
94  controls->ForceCursorPosition( false );
95  getEditFrame<PCB_BASE_FRAME>()->SetNoToolSelected();
96 
97  return 0;
98 }
99 
100 
102 {
104 }
105 
106 
108 {
109  m_cursorSnapping = true;
110  m_cursorVisible = true;
111  m_cursorCapture = false;
112  m_autoPanning = false;
113 
114  m_picking = false;
116 }
117 
118 
120 {
122 
123  controls->ShowCursor( m_cursorVisible );
124  controls->SetSnapping( m_cursorSnapping );
125  controls->CaptureCursor( m_cursorCapture );
126  controls->SetAutoPan( m_autoPanning );
127 }
virtual void ShowCursor(bool aEnabled)
Function ShowCursor() Enables or disables display of cursor.
bool m_picking
Activity status.
Definition: picker_tool.h:117
virtual VECTOR2D GetMousePosition(bool aWorldCoordinates=true) const =0
Function GetMousePosition() Returns the current mouse pointer position.
bool m_autoPanning
Definition: picker_tool.h:108
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:215
bool m_cursorSnapping
Definition: picker_tool.h:105
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:106
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:114
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:133
void setTransitions() override
>
bool m_cursorCapture
Definition: picker_tool.h:107
const auto NULLOPT
Definition: optional.h:9
VECTOR2< double > VECTOR2D
Definition: vector2d.h:588
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:366
PCB_EDIT_FRAME * frame() const
Definition: pcb_tool.h:134
Class TOOL_EVENT.
Definition: tool_event.h:162
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 mouse click event handler.
Definition: picker_tool.h:111
void setControls()
Applies the requested VIEW_CONTROLS settings.
All active tools
Definition: tool_event.h:138
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
Class TOOL_ACTION.
Definition: tool_action.h:46
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:460