KiCad PCB EDA Suite
pcbnew_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  * Copyright (C) 2019-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include "pcbnew_picker_tool.h"
27 #include "pcb_actions.h"
28 #include "grid_helper.h"
29 #include <view/view_controls.h>
30 #include <tool/tool_manager.h>
31 #include "selection_tool.h"
32 
33 
35  : PCB_TOOL_BASE( "pcbnew.InteractivePicker" )
36 {
37  reset();
38 }
39 
40 
42 {
44  PCB_BASE_FRAME* frame = getEditFrame<PCB_BASE_FRAME>();
46  int finalize_state = WAIT_CANCEL;
47 
48  std::string tool = *aEvent.Parameter<std::string*>();
49 
50  if( !tool.empty() )
51  frame->PushTool( tool );
52 
53  Activate();
54  setControls();
55 
56  auto setCursor =
57  [&]()
58  {
60  };
61 
62  // Set initial cursor
63  setCursor();
64 
65  while( TOOL_EVENT* evt = Wait() )
66  {
67  setCursor();
68 
69  grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
70  grid.SetUseGrid( !evt->Modifier( MD_ALT ) );
71  VECTOR2I cursorPos = grid.BestSnapAnchor( controls->GetMousePosition(), nullptr );
72  controls->ForceCursorPosition(true, cursorPos );
73 
74  if( evt->IsCancelInteractive() || evt->IsActivate() )
75  {
76  if( m_cancelHandler )
77  {
78  try
79  {
80  (*m_cancelHandler)();
81  }
82  catch( std::exception& )
83  {
84  }
85  }
86 
87  // Activating a new tool may have alternate finalization from canceling the current tool
88  if( evt->IsActivate() )
89  finalize_state = END_ACTIVATE;
90  else
91  finalize_state = EVT_CANCEL;
92 
93  break;
94  }
95 
96  else if( evt->IsClick( BUT_LEFT ) )
97  {
98  bool getNext = false;
99 
100  m_picked = cursorPos;
101 
102  if( m_clickHandler )
103  {
104  try
105  {
106  getNext = (*m_clickHandler)( *m_picked );
107  }
108  catch( std::exception& )
109  {
110  finalize_state = EXCEPTION_CANCEL;
111  break;
112  }
113  }
114 
115  if( !getNext )
116  {
117  finalize_state = CLICK_CANCEL;
118  break;
119  }
120  else
121  setControls();
122  }
123 
124  else if( evt->IsMotion() )
125  {
126  if( m_motionHandler )
127  {
128  try
129  {
130  (*m_motionHandler)( cursorPos );
131  }
132  catch( std::exception& )
133  {
134  }
135  }
136  }
137 
138  else if( evt->IsDblClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) )
139  {
140  // Not currently used, but we don't want to pass them either
141  }
142 
143  else if( evt->IsClick( BUT_RIGHT ) )
144  {
147  }
148 
149  else
150  evt->SetPassEvent();
151  }
152 
153  if( m_finalizeHandler )
154  {
155  try
156  {
157  (*m_finalizeHandler)( finalize_state );
158  }
159  catch( std::exception& )
160  {
161  }
162  }
163 
164  reset();
165  controls->ForceCursorPosition( false );
166 
167  if( !tool.empty() )
168  frame->PopTool( tool );
169 
170  return 0;
171 }
172 
173 
175 {
177 }
178 
179 
181 {
184 
185  m_picked = NULLOPT;
190 }
191 
192 
194 {
196 
197  controls->CaptureCursor( false );
198  controls->SetAutoPan( false );
199 }
OPT< MOTION_HANDLER > m_motionHandler
TOOL_MENU m_menu
functions below are not yet implemented - their interface may change
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
virtual VECTOR2D GetMousePosition(bool aWorldCoordinates=true) const =0
Function GetMousePosition() Returns the current mouse pointer position.
VIEW_CONTROLS class definition.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
OPT< VECTOR2D > m_picked
virtual MAGNETIC_SETTINGS * GetMagneticItemsSettings()
LSET m_layerMask
The layer set to use for optional snapping
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
void setTransitions() override
void SetCurrentCursor(KICURSOR cursor)
Function SetCurrentCursor Set the current cursor shape for this panel.
static TOOL_ACTION pickerTool
Definition: actions.h:153
PCB_BASE_EDIT_FRAME * frame() const
OPT< CANCEL_HANDLER > m_cancelHandler
const auto NULLOPT
Definition: optional.h:9
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.
static LSET AllLayersMask()
Definition: lset.cpp:787
int Main(const TOOL_EVENT &aEvent)
Main event loop.
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg.
OPT< CLICK_HANDLER > m_clickHandler
KIGFX::VIEW_CONTROLS * controls() const
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
void Activate()
Function Activate() Runs the tool.
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:42
void setControls()
Applies the requested VIEW_CONTROLS settings.
OPT< FINALIZE_HANDLER > m_finalizeHandler
void ShowContextMenu(SELECTION &aSelection)
Function ShowContextMenu.
Definition: tool_menu.cpp:59
PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
void reset()
Reinitializes tool to its initial state.