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 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  auto tool_frame = getEditFrame<PCB_BASE_FRAME>();
45  GRID_HELPER grid( tool_frame );
46  int finalize_state = WAIT_CANCEL;
47 
48  std::string tool = *aEvent.Parameter<std::string*>();
49  tool_frame->PushTool( tool );
50  Activate();
51  setControls();
52 
53  while( TOOL_EVENT* evt = Wait() )
54  {
55  tool_frame->GetCanvas()->SetCursor( m_cursor );
56 
57  grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
58  grid.SetUseGrid( !evt->Modifier( MD_ALT ) );
59  controls->SetSnapping( !evt->Modifier( MD_ALT ) );
60  VECTOR2I cursorPos = grid.BestSnapAnchor( controls->GetMousePosition(), nullptr );
61  controls->ForceCursorPosition(true, cursorPos );
62 
63  if( evt->IsCancelInteractive() || evt->IsActivate() )
64  {
65  if( m_cancelHandler )
66  {
67  try
68  {
69  (*m_cancelHandler)();
70  }
71  catch( std::exception& e )
72  {
73  std::cerr << "PCBNEW_PICKER_TOOL cancelHandler error: " << e.what() << std::endl;
74  }
75  }
76 
77  // Activating a new tool may have alternate finalization from canceling the current tool
78  if( evt->IsActivate() )
79  finalize_state = END_ACTIVATE;
80  else
81  finalize_state = EVT_CANCEL;
82 
83  break;
84  }
85 
86  else if( evt->IsClick( BUT_LEFT ) )
87  {
88  bool getNext = false;
89 
90  m_picked = cursorPos;
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 << "PCBNEW_PICKER_TOOL clickHandler error: " << e.what() << std::endl;
101  finalize_state = EXCEPTION_CANCEL;
102  break;
103  }
104  }
105 
106  if( !getNext )
107  {
108  finalize_state = CLICK_CANCEL;
109  break;
110  }
111  else
112  setControls();
113  }
114 
115  else if( evt->IsMotion() )
116  {
117  if( m_motionHandler )
118  {
119  try
120  {
121  (*m_motionHandler)( cursorPos );
122  }
123  catch( std::exception& e )
124  {
125  std::cerr << "PCBNEW_PICKER_TOOL motion handler error: " << e.what() << std::endl;
126  }
127  }
128  }
129 
130  else if( evt->IsDblClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) )
131  {
132  // Not currently used, but we don't want to pass them either
133  }
134 
135  else if( evt->IsClick( BUT_RIGHT ) )
136  {
139  }
140 
141  else
142  evt->SetPassEvent();
143  }
144 
145  if( m_finalizeHandler )
146  {
147  try
148  {
149  (*m_finalizeHandler)( finalize_state );
150  }
151  catch( std::exception& e )
152  {
153  std::cerr << "PCBNEW_PICKER_TOOL finalizeHandler error: " << e.what() << std::endl;
154  }
155  }
156 
157  reset();
158  controls->ForceCursorPosition( false );
159  tool_frame->PopTool( tool );
160  return 0;
161 }
162 
163 
165 {
167 }
168 
169 
171 {
173  m_cursor = wxStockCursor( wxCURSOR_ARROW );
174 
175  m_picked = NULLOPT;
180 }
181 
182 
184 {
186 
187  // Ensure that the view controls do not handle our snapping as we use the GRID_HELPER
188  controls->SetSnapping( false );
189 
190  controls->CaptureCursor( false );
191  controls->SetAutoPan( false );
192 }
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.
virtual void SetSnapping(bool aEnabled)
Function SetSnapping() Enables/disables snapping cursor to grid.
VECTOR2I BestSnapAnchor(const VECTOR2I &aOrigin, BOARD_ITEM *aDraggedItem)
OPT< VECTOR2D > m_picked
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
static TOOL_ACTION pickerTool
Definition: actions.h:145
OPT< CANCEL_HANDLER > m_cancelHandler
const auto NULLOPT
Definition: optional.h:9
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
Class TOOL_EVENT.
Definition: tool_event.h:171
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.
static LSET AllLayersMask()
Definition: lset.cpp:713
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.
void SetSnap(bool aSnap)
Definition: grid_helper.h:66
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.
void SetUseGrid(bool aGrid=true)
Definition: grid_helper.h:71
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
void reset()
Reinitializes tool to its initial state.