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  GRID_HELPER grid( frame() );
45  int finalize_state = WAIT_CANCEL;
46 
47  std::string tool = *aEvent.Parameter<std::string*>();
48  frame()->PushTool( tool );
49  Activate();
50  setControls();
51 
52  while( TOOL_EVENT* evt = Wait() )
53  {
54  frame()->GetCanvas()->SetCursor( m_cursor );
55 
56  grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
57  grid.SetUseGrid( !evt->Modifier( MD_ALT ) );
58  controls->SetSnapping( !evt->Modifier( MD_ALT ) );
59  VECTOR2I cursorPos = grid.BestSnapAnchor( controls->GetMousePosition(), nullptr );
60  controls->ForceCursorPosition(true, cursorPos );
61 
62  if( evt->IsClick( BUT_LEFT ) )
63  {
64  bool getNext = false;
65 
66  m_picked = cursorPos;
67 
68  if( m_clickHandler )
69  {
70  try
71  {
72  getNext = (*m_clickHandler)( *m_picked );
73  }
74  catch( std::exception& e )
75  {
76  std::cerr << "PCBNEW_PICKER_TOOL clickHandler error: " << e.what() << std::endl;
77  finalize_state = EXCEPTION_CANCEL;
78  break;
79  }
80  }
81 
82  if( !getNext )
83  {
84  finalize_state = CLICK_CANCEL;
85  break;
86  }
87  else
88  setControls();
89  }
90 
91  else if( evt->IsMotion() )
92  {
93  if( m_motionHandler )
94  {
95  try
96  {
97  (*m_motionHandler)( cursorPos );
98  }
99  catch( std::exception& e )
100  {
101  std::cerr << "PCBNEW_PICKER_TOOL motion handler error: " << e.what() << std::endl;
102  }
103  }
104  }
105 
106  else if( evt->IsCancelInteractive() || evt->IsActivate() )
107  {
108  if( m_cancelHandler )
109  {
110  try
111  {
112  (*m_cancelHandler)();
113  }
114  catch( std::exception& e )
115  {
116  std::cerr << "PCBNEW_PICKER_TOOL cancelHandler error: " << e.what() << std::endl;
117  }
118  }
119 
120  // Activating a new tool may have alternate finalization from canceling the current tool
121  if( evt->IsActivate() )
122  finalize_state = END_ACTIVATE;
123  else
124  finalize_state = EVT_CANCEL;
125 
126  break;
127  }
128 
129  else if( evt->IsClick( BUT_RIGHT ) )
130  {
133  }
134 
135  else
136  evt->SetPassEvent();
137  }
138 
139  if( m_finalizeHandler )
140  {
141  try
142  {
143  (*m_finalizeHandler)( finalize_state );
144  }
145  catch( std::exception& e )
146  {
147  std::cerr << "PCBNEW_PICKER_TOOL finalizeHandler error: " << e.what() << std::endl;
148  }
149  }
150 
151  reset();
152  controls->ForceCursorPosition( false );
153  frame()->PopTool( tool );
154  return 0;
155 }
156 
157 
159 {
161 }
162 
163 
165 {
167  m_cursor = wxStockCursor( wxCURSOR_ARROW );
168 
169  m_picked = NULLOPT;
174 }
175 
176 
178 {
180 
181  // Ensure that the view controls do not handle our snapping as we use the GRID_HELPER
182  controls->SetSnapping( false );
183 
184  controls->CaptureCursor( false );
185  controls->SetAutoPan( false );
186 }
OPT< MOTION_HANDLER > m_motionHandler
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
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.
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:144
PCB_BASE_EDIT_FRAME * frame() const
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:427
Class TOOL_EVENT.
Definition: tool_event.h:168
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.
virtual void PopTool(const std::string &actionName)
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.