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) 2019 CERN
5  * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
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 <tool/actions.h>
26 #include <tool/picker_tool.h>
27 #include <view/view_controls.h>
28 #include <eda_draw_frame.h>
29 
30 
32  : TOOL_INTERACTIVE( "common.InteractivePicker" )
33 {
34  resetPicker();
35 }
36 
37 
39 {
40  m_frame = getEditFrame<EDA_DRAW_FRAME>();
41 
42  auto& ctxMenu = m_menu.GetMenu();
43 
44  // cancel current tool goes in main context menu at the top if present
46  ctxMenu.AddSeparator( 1 );
47 
48  // Finally, add the standard zoom/grid items
50 
51  return true;
52 }
53 
54 
55 int PICKER_TOOL::Main( const TOOL_EVENT& aEvent )
56 {
58  int finalize_state = WAIT_CANCEL;
59 
60  std::string tool = *aEvent.Parameter<std::string*>();
61  m_frame->PushTool( tool );
62  Activate();
63 
64  // To many things are off-grid in LibEdit; turn snapping off.
65  bool snap = !m_frame->IsType( FRAME_SCH_LIB_EDITOR );
66 
67  setControls();
68 
69  while( TOOL_EVENT* evt = Wait() )
70  {
72  VECTOR2D cursorPos = controls->GetCursorPosition( snap && !evt->Modifier( MD_ALT ) );
73 
74  if( evt->IsCancelInteractive() || evt->IsActivate() )
75  {
76  if( m_cancelHandler )
77  {
78  try
79  {
80  (*m_cancelHandler)();
81  }
82  catch( std::exception& e )
83  {
84  std::cerr << "PICKER_TOOL cancel handler error: " << e.what() << std::endl;
85  }
86  }
87 
88  // Activating a new tool may have alternate finalization from canceling the current
89  // tool
90  if( evt->IsActivate() )
91  finalize_state = END_ACTIVATE;
92  else
93  finalize_state = EVT_CANCEL;
94 
95  break;
96  }
97 
98  else if( evt->IsClick( BUT_LEFT ) )
99  {
100  bool getNext = false;
101 
102  m_picked = cursorPos;
103 
104  if( m_clickHandler )
105  {
106  try
107  {
108  getNext = (*m_clickHandler)( *m_picked );
109  }
110  catch( std::exception& e )
111  {
112  std::cerr << "PICKER_TOOL click handler error: " << e.what() << std::endl;
113  finalize_state = EXCEPTION_CANCEL;
114  break;
115  }
116  }
117 
118  if( !getNext )
119  {
120  finalize_state = CLICK_CANCEL;
121  break;
122  }
123  else
124  setControls();
125  }
126 
127  else if( evt->IsMotion() )
128  {
129  if( m_motionHandler )
130  {
131  try
132  {
133  (*m_motionHandler)( cursorPos );
134  }
135  catch( std::exception& e )
136  {
137  std::cerr << "PICKER_TOOL motion handler error: " << e.what() << std::endl;
138  }
139  }
140  }
141 
142  else if( evt->IsDblClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) )
143  {
144  // Not currently used, but we don't want to pass them either
145  }
146 
147  else if( evt->IsClick( BUT_RIGHT ) )
148  {
150  }
151 
152  else
153  evt->SetPassEvent();
154  }
155 
156  if( m_finalizeHandler )
157  {
158  try
159  {
160  (*m_finalizeHandler)( finalize_state );
161  }
162  catch( std::exception& e )
163  {
164  std::cerr << "PICKER_TOOL finalize handler error: " << e.what() << std::endl;
165  }
166  }
167 
168  resetPicker();
169  controls->ForceCursorPosition( false );
170  m_frame->PopTool( tool );
171  return 0;
172 }
173 
174 
176 {
177  Go( &PICKER_TOOL::Main, ACTIONS::pickerTool.MakeEvent() );
178 }
179 
180 
182 {
183  m_cursor = wxStockCursor( wxCURSOR_ARROW );
184 
185  m_picked = NULLOPT;
190 }
191 
192 
194 {
196 
197  controls->CaptureCursor( false );
198  controls->SetAutoPan( false );
199 }
static bool ShowAlways(const SELECTION &aSelection)
Function ShowAlways The default condition function (always returns true).
void AddStandardSubMenus(TOOL_MENU &aMenu)
Function CreateBasicMenu.
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
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
Definition: picker_tool.cpp:38
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
int Main(const TOOL_EVENT &aEvent)
Main event loop.
Definition: picker_tool.cpp:55
void SetCurrentCursor(wxStockCursor aStockCursorID)
Function SetCurrentCursor Set the current cursor shape for this panel.
VIEW_CONTROLS class definition.
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:46
virtual EDA_DRAW_PANEL_GAL * GetCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
static TOOL_ACTION cancelInteractive
Definition: actions.h:65
wxCursor m_cursor
Definition: picker_tool.h:120
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
static TOOL_ACTION pickerTool
Definition: actions.h:145
OPT< VECTOR2D > m_picked
Definition: picker_tool.h:127
void setTransitions() override
const auto NULLOPT
Definition: optional.h:9
OPT< MOTION_HANDLER > m_motionHandler
Definition: picker_tool.h:123
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.
OPT< CLICK_HANDLER > m_clickHandler
Definition: picker_tool.h:122
void setControls()
Applies the requested VIEW_CONTROLS settings.
OPT< FINALIZE_HANDLER > m_finalizeHandler
Definition: picker_tool.h:125
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg.
bool IsType(FRAME_T aType) const
EDA_DRAW_FRAME * m_frame
Definition: picker_tool.h:119
virtual void PopTool(const std::string &actionName)
void Activate()
Function Activate() Runs the tool.
OPT< CANCEL_HANDLER > m_cancelHandler
Definition: picker_tool.h:124
void resetPicker()
Reinitializes tool to its initial state.
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:42
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Function AddItem()
void ShowContextMenu(SELECTION &aSelection)
Function ShowContextMenu.
Definition: tool_menu.cpp:59
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.