KiCad PCB EDA Suite
ee_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 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <ee_picker_tool.h>
25 #include <ee_actions.h>
26 #include <view/view_controls.h>
27 #include <tool/tool_manager.h>
28 #include <sch_base_frame.h>
29 
30 TOOL_ACTION EE_ACTIONS::pickerTool( "eeschema.Picker", AS_GLOBAL, 0, "", "", NULL, AF_ACTIVATE );
31 
32 
34  : EE_TOOL_BASE<SCH_BASE_FRAME>( "eeschema.Picker" )
35 {
36  resetPicker();
37 }
38 
39 
40 int EE_PICKER_TOOL::Main( const TOOL_EVENT& aEvent )
41 {
43  int finalize_state = WAIT_CANCEL;
44 
45  setControls();
46 
47  while( OPT_TOOL_EVENT evt = Wait() )
48  {
49  VECTOR2I cursorPos = controls->GetCursorPosition( !evt->Modifier( MD_ALT ) );
50 
51  if( evt->IsClick( BUT_LEFT ) )
52  {
53  bool getNext = false;
54 
55  m_picked = cursorPos;
56 
57  if( m_clickHandler )
58  {
59  try
60  {
61  getNext = (*m_clickHandler)( *m_picked );
62  }
63  catch( std::exception& e )
64  {
65  std::cerr << "EE_PICKER_TOOL click handler error: " << e.what() << std::endl;
66  finalize_state = EXCEPTION_CANCEL;
67  break;
68  }
69  }
70 
71  if( !getNext )
72  {
73  finalize_state = CLICK_CANCEL;
74  break;
75  }
76  else
77  setControls();
78  }
79 
80  else if( TOOL_EVT_UTILS::IsCancelInteractive( evt.get() ) )
81  {
82  if( m_cancelHandler )
83  {
84  try
85  {
86  (*m_cancelHandler)();
87  }
88  catch( std::exception& e )
89  {
90  std::cerr << "EE_PICKER_TOOL cancel handler error: " << e.what() << std::endl;
91  }
92  }
93 
94  // Activating a new tool may have alternate finalization from canceling the current tool
95  if( evt->IsActivate() )
96  finalize_state = END_ACTIVATE;
97  else
98  finalize_state = EVT_CANCEL;
99 
100  break;
101  }
102  else if( evt->IsClick( BUT_RIGHT ) )
103  {
104  // TODO...
105  // m_menu.ShowContextMenu();
106  }
107  else
108  {
109  m_toolMgr->PassEvent();
110  }
111  }
112 
113  if( m_finalizeHandler )
114  {
115  try
116  {
117  (*m_finalizeHandler)( finalize_state );
118  }
119  catch( std::exception& e )
120  {
121  std::cerr << "EE_PICKER_TOOL finalize handler error: " << e.what() << std::endl;
122  }
123  }
124 
125  resetPicker();
126  controls->ForceCursorPosition( false );
127  getEditFrame<SCH_BASE_FRAME>()->SetNoToolSelected();
128 
129  return 0;
130 }
131 
132 
134 {
136 }
137 
138 
140 {
141  m_cursorCapture = false;
142  m_autoPanning = false;
143 
144  m_picked = NULLOPT;
148 }
149 
150 
152 {
154 
155  controls->CaptureCursor( m_cursorCapture );
156  controls->SetAutoPan( m_autoPanning );
157 }
OPT< CANCEL_HANDLER > m_cancelHandler
VIEW_CONTROLS class definition.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:216
OPT_TOOL_EVENT Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
OPT< VECTOR2D > m_picked
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
int Main(const TOOL_EVENT &aEvent)
Main event loop.
void setControls()
Applies the requested VIEW_CONTROLS settings.
void PassEvent()
Allows a tool to pass the already handled event to the next tool on the stack.
Definition: tool_manager.h:356
void setTransitions() override
const auto NULLOPT
Definition: optional.h:9
virtual void CaptureCursor(bool aEnabled)
Function CaptureCursor() Forces the cursor to stay within the drawing panel area.
Class TOOL_EVENT.
Definition: tool_event.h:167
void resetPicker()
Reinitializes tool to its initial state.
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
bool IsCancelInteractive(const TOOL_EVENT &aEvt)
Function IsCancelInteractive()
Definition: tool_event.cpp:177
All active tools
Definition: tool_event.h:143
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg.
OPT< FINALIZE_HANDLER > m_finalizeHandler
Class TOOL_ACTION.
Definition: tool_action.h:46
static TOOL_ACTION pickerTool
Definition: ee_actions.h:77
Class EE_TOOL_BASE.
Definition: ee_tool_base.h:49
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:41
A shim class between EDA_DRAW_FRAME and several derived classes: LIB_EDIT_FRAME, LIB_VIEW_FRAME,...
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:486