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  m_frame( nullptr )
34 {
35  resetPicker();
36 }
37 
38 
40 {
41  m_frame = getEditFrame<EDA_DRAW_FRAME>();
42 
43  auto& ctxMenu = m_menu.GetMenu();
44 
45  // cancel current tool goes in main context menu at the top if present
47  ctxMenu.AddSeparator( 1 );
48 
49  // Finally, add the standard zoom/grid items
51 
52  return true;
53 }
54 
55 
56 int PICKER_TOOL::Main( const TOOL_EVENT& aEvent )
57 {
59  int finalize_state = WAIT_CANCEL;
60 
61  std::string tool = *aEvent.Parameter<std::string*>();
62  m_frame->PushTool( tool );
63  Activate();
64 
65  // To many things are off-grid in LibEdit; turn snapping off.
66  bool snap = !m_frame->IsType( FRAME_SCH_LIB_EDITOR );
67 
68  setControls();
69 
70  while( TOOL_EVENT* evt = Wait() )
71  {
73  VECTOR2D cursorPos = controls->GetCursorPosition( snap && !evt->Modifier( MD_ALT ) );
74 
75  if( evt->IsCancelInteractive() || evt->IsActivate() )
76  {
77  if( m_cancelHandler )
78  {
79  try
80  {
81  (*m_cancelHandler)();
82  }
83  catch( std::exception& e )
84  {
85  std::cerr << "PICKER_TOOL cancel handler error: " << e.what() << std::endl;
86  }
87  }
88 
89  // Activating a new tool may have alternate finalization from canceling the current
90  // tool
91  if( evt->IsActivate() )
92  finalize_state = END_ACTIVATE;
93  else
94  finalize_state = EVT_CANCEL;
95 
96  break;
97  }
98 
99  else if( evt->IsClick( BUT_LEFT ) )
100  {
101  bool getNext = false;
102 
103  m_picked = cursorPos;
104 
105  if( m_clickHandler )
106  {
107  try
108  {
109  getNext = (*m_clickHandler)( *m_picked );
110  }
111  catch( std::exception& e )
112  {
113  std::cerr << "PICKER_TOOL click handler error: " << e.what() << std::endl;
114  finalize_state = EXCEPTION_CANCEL;
115  break;
116  }
117  }
118 
119  if( !getNext )
120  {
121  finalize_state = CLICK_CANCEL;
122  break;
123  }
124  else
125  setControls();
126  }
127 
128  else if( evt->IsMotion() )
129  {
130  if( m_motionHandler )
131  {
132  try
133  {
134  (*m_motionHandler)( cursorPos );
135  }
136  catch( std::exception& e )
137  {
138  std::cerr << "PICKER_TOOL motion handler error: " << e.what() << std::endl;
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  {
151  }
152 
153  else
154  evt->SetPassEvent();
155  }
156 
157  if( m_finalizeHandler )
158  {
159  try
160  {
161  (*m_finalizeHandler)( finalize_state );
162  }
163  catch( std::exception& e )
164  {
165  std::cerr << "PICKER_TOOL finalize handler error: " << e.what() << std::endl;
166  }
167  }
168 
169  resetPicker();
170  controls->ForceCursorPosition( false );
171  m_frame->PopTool( tool );
172  return 0;
173 }
174 
175 
177 {
178  Go( &PICKER_TOOL::Main, ACTIONS::pickerTool.MakeEvent() );
179 }
180 
181 
183 {
184  m_cursor = wxStockCursor( wxCURSOR_ARROW );
185 
186  m_picked = NULLOPT;
191 }
192 
193 
195 {
197 
198  controls->CaptureCursor( false );
199  controls->SetAutoPan( false );
200 }
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:39
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:56
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
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.
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.