KiCad PCB EDA Suite
pcb_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 "pcb_picker_tool.h"
27 #include "pcb_actions.h"
28 #include "pcb_grid_helper.h"
29 #include <view/view_controls.h>
30 #include <tool/tool_manager.h>
31 #include "pcb_selection_tool.h"
32 
33 
35  PCB_TOOL_BASE( "pcbnew.InteractivePicker" ),
37 {
38 }
39 
40 
41 int PCB_PICKER_TOOL::Main( const TOOL_EVENT& aEvent )
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  VECTOR2D cursorPos;
65 
66  while( TOOL_EVENT* evt = Wait() )
67  {
68  setCursor();
69  cursorPos = controls->GetMousePosition();
70 
71  if( m_snap )
72  {
73  grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
74  grid.SetUseGrid( getView()->GetGAL()->GetGridSnapping() && !evt->Modifier( MD_ALT ) );
75  cursorPos = grid.BestSnapAnchor( cursorPos, nullptr );
76  controls->ForceCursorPosition( true, cursorPos );
77  }
78 
79  if( evt->IsCancelInteractive() || evt->IsActivate() )
80  {
81  if( m_cancelHandler )
82  {
83  try
84  {
85  (*m_cancelHandler)();
86  }
87  catch( std::exception& )
88  {
89  }
90  }
91 
92  // Activating a new tool may have alternate finalization from canceling the current tool
93  if( evt->IsActivate() )
94  finalize_state = END_ACTIVATE;
95  else
96  finalize_state = EVT_CANCEL;
97 
98  break;
99  }
100 
101  else if( evt->IsClick( BUT_LEFT ) )
102  {
103  bool getNext = false;
104 
105  m_picked = cursorPos;
106 
107  if( m_clickHandler )
108  {
109  try
110  {
111  getNext = (*m_clickHandler)( *m_picked );
112  }
113  catch( std::exception& )
114  {
115  finalize_state = EXCEPTION_CANCEL;
116  break;
117  }
118  }
119 
120  if( !getNext )
121  {
122  finalize_state = CLICK_CANCEL;
123  break;
124  }
125  else
126  setControls();
127  }
128 
129  else if( evt->IsMotion() )
130  {
131  if( m_motionHandler )
132  {
133  try
134  {
135  (*m_motionHandler)( cursorPos );
136  }
137  catch( std::exception& )
138  {
139  }
140  }
141  }
142 
143  else if( evt->IsDblClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) )
144  {
145  // Not currently used, but we don't want to pass them either
146  }
147 
148  else if( evt->IsClick( BUT_RIGHT ) )
149  {
152  }
153 
154  else
155  evt->SetPassEvent();
156  }
157 
158  if( m_finalizeHandler )
159  {
160  try
161  {
162  (*m_finalizeHandler)( finalize_state );
163  }
164  catch( std::exception& )
165  {
166  }
167  }
168 
169  reset();
170  controls->ForceCursorPosition( false );
171 
172  if( !tool.empty() )
173  frame->PopTool( tool );
174 
175  return 0;
176 }
177 
178 
180 {
182 }
183 
184 
186 {
189 }
190 
191 
193 {
195 
196  controls->CaptureCursor( false );
197  controls->SetAutoPan( false );
198 }
TOOL_MENU m_menu
The functions below are not yet implemented - their interface may change.
KICURSOR m_cursor
Definition: picker_tool.h:111
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Suspend execution of the tool until an event specified in aEventList arrives.
virtual VECTOR2D GetMousePosition(bool aWorldCoordinates=true) const =0
Return the current mouse pointer position.
OPT< VECTOR2D > m_picked
Definition: picker_tool.h:119
int Main(const TOOL_EVENT &aEvent)
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:215
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
Definition: sch_symbol.cpp:69
OPT< CANCEL_HANDLER > m_cancelHandler
Definition: picker_tool.h:116
virtual MAGNETIC_SETTINGS * GetMagneticItemsSettings()
void setTransitions() override
<
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
OPT< FINALIZE_HANDLER > m_finalizeHandler
Definition: picker_tool.h:117
void SetCurrentCursor(KICURSOR cursor)
Set the current cursor shape for this panel.
static TOOL_ACTION pickerTool
Definition: actions.h:158
PCB_BASE_EDIT_FRAME * frame() const
virtual void reset()
< Reinitializes tool to its initial state.
Definition: picker_tool.cpp:31
virtual void PopTool(const std::string &actionName)
virtual void CaptureCursor(bool aEnabled)
Force the cursor to stay within the drawing panel area.
T Parameter() const
Return a non-standard parameter assigned to the event.
Definition: tool_event.h:443
Generic, UI-independent tool event.
Definition: tool_event.h:173
void setControls()
Reinitialize tool to its initial state.
An interface for classes handling user events controlling the view behavior such as zooming,...
virtual void ForceCursorPosition(bool aEnabled, const VECTOR2D &aPosition=VECTOR2D(0, 0))
Place the cursor immediately at a given point.
static LSET AllLayersMask()
Definition: lset.cpp:787
void reset() override
< Reinitializes tool to its initial state.
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:36
virtual void SetAutoPan(bool aEnabled)
Turn on/off auto panning (this feature is used when there is a tool active (eg.
OPT< CLICK_HANDLER > m_clickHandler
Definition: picker_tool.h:114
KIGFX::VIEW_CONTROLS * controls() const
OPT< MOTION_HANDLER > m_motionHandler
Definition: picker_tool.h:115
void Activate()
Run the tool.
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition: tool_base.cpp:42
LSET m_layerMask
< The layer set to use for optional snapping.
void ShowContextMenu(SELECTION &aSelection)
Helper function to set and immediately show a CONDITIONAL_MENU in concert with the given SELECTION.
Definition: tool_menu.cpp:59
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.