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->IsClick( BUT_LEFT ) )
75  {
76  bool getNext = false;
77 
78  m_picked = cursorPos;
79 
80  if( m_clickHandler )
81  {
82  try
83  {
84  getNext = (*m_clickHandler)( *m_picked );
85  }
86  catch( std::exception& e )
87  {
88  std::cerr << "PICKER_TOOL click handler error: " << e.what() << std::endl;
89  finalize_state = EXCEPTION_CANCEL;
90  break;
91  }
92  }
93 
94  if( !getNext )
95  {
96  finalize_state = CLICK_CANCEL;
97  break;
98  }
99  else
100  setControls();
101  }
102 
103  else if( evt->IsMotion() )
104  {
105  if( m_motionHandler )
106  {
107  try
108  {
109  (*m_motionHandler)( cursorPos );
110  }
111  catch( std::exception& e )
112  {
113  std::cerr << "PICKER_TOOL motion handler error: " << e.what() << std::endl;
114  }
115  }
116  }
117 
118  else if( evt->IsCancelInteractive() || evt->IsActivate() )
119  {
120  if( m_cancelHandler )
121  {
122  try
123  {
124  (*m_cancelHandler)();
125  }
126  catch( std::exception& e )
127  {
128  std::cerr << "PICKER_TOOL cancel handler error: " << e.what() << std::endl;
129  }
130  }
131 
132  // Activating a new tool may have alternate finalization from canceling the current
133  // tool
134  if( evt->IsActivate() )
135  finalize_state = END_ACTIVATE;
136  else
137  finalize_state = EVT_CANCEL;
138 
139  break;
140  }
141  else if( evt->IsClick( BUT_RIGHT ) )
142  {
144  }
145  else
146  evt->SetPassEvent();
147  }
148 
149  if( m_finalizeHandler )
150  {
151  try
152  {
153  (*m_finalizeHandler)( finalize_state );
154  }
155  catch( std::exception& e )
156  {
157  std::cerr << "PICKER_TOOL finalize handler error: " << e.what() << std::endl;
158  }
159  }
160 
161  resetPicker();
162  controls->ForceCursorPosition( false );
163  m_frame->PopTool( tool );
164  return 0;
165 }
166 
167 
169 {
170  Go( &PICKER_TOOL::Main, ACTIONS::pickerTool.MakeEvent() );
171 }
172 
173 
175 {
176  m_cursor = wxStockCursor( wxCURSOR_ARROW );
177 
178  m_picked = NULLOPT;
183 }
184 
185 
187 {
189 
190  controls->CaptureCursor( false );
191  controls->SetAutoPan( false );
192 }
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:144
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: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.
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.