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 <pcb_edit_frame.h>
30 #include <view/view_controls.h>
31 #include <tool/tool_manager.h>
32 #include "tool_event_utils.h"
33 #include "selection_tool.h"
34 
35 
36 TOOL_ACTION PCB_ACTIONS::pickerTool( "pcbnew.Picker", AS_GLOBAL, 0, "", "", NULL, AF_ACTIVATE );
37 
38 
40  : PCB_TOOL( "pcbnew.Picker" )
41 {
42  reset();
43 }
44 
45 
47 {
48  auto activeToolCondition = [ this ] ( const SELECTION& aSel ) {
49  return ( frame()->GetToolId() != ID_NO_TOOL_SELECTED );
50  };
51 
53 
54  // We delegate our context menu to the Selection tool, so make sure it has a
55  // "Cancel" item at the top.
56  if( selTool )
57  {
58  auto& toolMenu = selTool->GetToolMenu();
59  auto& menu = toolMenu.GetMenu();
60 
61  menu.AddItem( ACTIONS::cancelInteractive, activeToolCondition, 1000 );
62  menu.AddSeparator( activeToolCondition, 1000 );
63  }
64 
65  return true;
66 }
67 
68 
69 int PICKER_TOOL::Main( const TOOL_EVENT& aEvent )
70 {
72  GRID_HELPER grid( frame() );
73 
74  assert( !m_picking );
75  m_picking = true;
76  m_picked = NULLOPT;
77 
78  setControls();
79 
80  while( OPT_TOOL_EVENT evt = Wait() )
81  {
82  auto mousePos = controls->GetMousePosition();
83  auto p = grid.BestSnapAnchor( mousePos, nullptr );
84  controls->ForceCursorPosition( true, p );
85 
86  if( evt->IsClick( BUT_LEFT ) )
87  {
88  bool getNext = false;
89 
90  m_picked = VECTOR2D( p );
91 
92  if( m_clickHandler )
93  {
94  try
95  {
96  getNext = (*m_clickHandler)( *m_picked );
97  }
98  catch( std::exception& e )
99  {
100  std::cerr << "PICKER_TOOL click handler error: " << e.what() << std::endl;
101  break;
102  }
103  }
104 
105  if( !getNext )
106  break;
107  else
108  setControls();
109  }
110 
111  else if( evt->IsCancel() || TOOL_EVT_UTILS::IsCancelInteractive( *evt ) || evt->IsActivate() )
112  break;
113 
114  else
115  m_toolMgr->PassEvent();
116  }
117 
118  reset();
119  controls->ForceCursorPosition( false );
120  getEditFrame<PCB_BASE_FRAME>()->SetNoToolSelected();
121 
122  return 0;
123 }
124 
125 
127 {
129 }
130 
131 
133 {
134  m_cursorSnapping = true;
135  m_cursorVisible = true;
136  m_cursorCapture = false;
137  m_autoPanning = false;
138 
139  m_picking = false;
141 }
142 
143 
145 {
147 
148  controls->ShowCursor( m_cursorVisible );
149  controls->SetSnapping( m_cursorSnapping );
150  controls->CaptureCursor( m_cursorCapture );
151  controls->SetAutoPan( m_autoPanning );
152 }
virtual void ShowCursor(bool aEnabled)
Function ShowCursor() Enables or disables display of cursor.
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
Definition: picker_tool.cpp:46
bool m_picking
Activity status.
Definition: picker_tool.h:120
virtual VECTOR2D GetMousePosition(bool aWorldCoordinates=true) const =0
Function GetMousePosition() Returns the current mouse pointer position.
bool m_autoPanning
Definition: picker_tool.h:111
int Main(const TOOL_EVENT &aEvent)
Main event loop.
Definition: picker_tool.cpp:69
VIEW_CONTROLS class definition.
Class SELECTION_TOOL.
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:49
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:215
bool m_cursorSnapping
Definition: picker_tool.h:108
static TOOL_ACTION cancelInteractive
Definition: actions.h:45
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:109
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:117
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:134
void setTransitions() override
>
bool m_cursorCapture
Definition: picker_tool.h:110
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:379
PCB_EDIT_FRAME * frame() const
Definition: pcb_tool.h:135
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:114
void setControls()
Applies the requested VIEW_CONTROLS settings.
bool IsCancelInteractive(const TOOL_EVENT &aEvt)
Function IsCancelInteractive()
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
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
Function AddItem()
int GetToolId() const
Definition: draw_frame.h:488
Class TOOL_ACTION.
Definition: tool_action.h:46
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:465