KiCad PCB EDA Suite
tools_holder.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) 2020-2021 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 <pgm_base.h>
26 #include <tool/action_manager.h>
27 #include <tool/action_menu.h>
28 #include <tool/actions.h>
29 #include <tool/tools_holder.h>
30 #include <tool/tool_manager.h>
31 
32 
34  m_toolManager( nullptr ),
35  m_actions( nullptr ),
36  m_toolDispatcher( nullptr ),
37  m_immediateActions( true ),
38  m_dragAction( MOUSE_DRAG_ACTION::SELECT ),
39  m_moveWarpsCursor( true )
40 { }
41 
42 
43 // TODO: Implement an RAII mechanism for the stack PushTool/PopTool pairs
44 void TOOLS_HOLDER::PushTool( const std::string& actionName )
45 {
46  m_toolStack.push_back( actionName );
47 
48  // Human cognitive stacking is very shallow; deeper tool stacks just get annoying
49  if( m_toolStack.size() > 3 )
50  m_toolStack.erase( m_toolStack.begin() );
51 
52  TOOL_ACTION* action = m_toolManager->GetActionManager()->FindAction( actionName );
53 
54  if( action )
55  DisplayToolMsg( action->GetLabel() );
56  else
57  DisplayToolMsg( actionName );
58 }
59 
60 
61 void TOOLS_HOLDER::PopTool( const std::string& actionName )
62 {
63  // Push/pop events can get out of order (such as when they're generated by the Simulator
64  // frame but not processed until the mouse is back in the Schematic frame), so make sure
65  // we're popping the right stack frame.
66 
67  for( int i = (int) m_toolStack.size() - 1; i >= 0; --i )
68  {
69  if( m_toolStack[ i ] == actionName )
70  {
71  m_toolStack.erase( m_toolStack.begin() + i );
72 
73  // If there's something underneath us, and it's now the top of the stack, then
74  // re-activate it
75  if( ( --i ) >= 0 && i == (int)m_toolStack.size() - 1 )
76  {
77  std::string back = m_toolStack[ i ];
79 
80  if( action )
81  {
82  // Pop the action as running it will push it back onto the stack
83  m_toolStack.pop_back();
84 
85  TOOL_EVENT evt = action->MakeEvent();
86  evt.SetHasPosition( false );
87  evt.SetReactivate( true );
88  GetToolManager()->PostEvent( evt );
89  }
90  }
91  else
93 
94  return;
95  }
96  }
97 }
98 
99 
100 std::string TOOLS_HOLDER::CurrentToolName() const
101 {
102  if( m_toolStack.empty() )
103  return ACTIONS::selectionTool.GetName();
104  else
105  return m_toolStack.back();
106 }
107 
108 
109 bool TOOLS_HOLDER::IsCurrentTool( const TOOL_ACTION& aAction ) const
110 {
111  if( m_toolStack.empty() )
112  return &aAction == &ACTIONS::selectionTool;
113  else
114  return m_toolStack.back() == aAction.GetName();
115 }
116 
117 
118 void TOOLS_HOLDER::CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVarsChanged )
119 {
120  if( GetToolManager() )
122 
123  COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
124 
126  m_dragAction = settings->m_Input.drag_left;
128 }
129 
bool IsCurrentTool(const TOOL_ACTION &aAction) const
MOUSE_DRAG_ACTION drag_left
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
virtual void DisplayToolMsg(const wxString &msg)
Definition: tools_holder.h:121
void UpdateHotKeys(bool aFullUpdate)
Optionally read the hotkey config files and then rebuilds the internal hotkey maps.
bool m_immediateActions
Definition: tools_holder.h:169
void SetReactivate(bool aReactivate=true)
Returns information about difference between current mouse cursor position and the place where draggi...
Definition: tool_event.h:253
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
TOOL_ACTION * FindAction(const std::string &aActionName) const
Find an action with a given name (if there is one available).
MOUSE_DRAG_ACTION
virtual void PopTool(const std::string &actionName)
Generic, UI-independent tool event.
Definition: tool_event.h:152
bool m_moveWarpsCursor
Definition: tools_holder.h:175
std::string CurrentToolName() const
MOUSE_DRAG_ACTION m_dragAction
Definition: tools_holder.h:173
wxString GetLabel() const
Definition: tool_action.cpp:83
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:156
see class PGM_BASE
std::vector< std::string > m_toolStack
Definition: tools_holder.h:162
Represent a single user action.
Definition: tool_action.h:67
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
Definition: tool_action.cpp:72
ACTION_MANAGER * GetActionManager() const
Definition: tool_manager.h:199
virtual void CommonSettingsChanged(bool aEnvVarsChanged, bool aTextVarsChanged)
Notification event that some of the common (suite-wide) settings have changed.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:54
void PostEvent(const TOOL_EVENT &aEvent)
Put an event to the event queue to be processed at the end of event processing cycle.
static TOOL_ACTION selectionTool
Definition: actions.h:153
void SetHasPosition(bool aHasPosition)
Returns if the action associated with this event should be treated as immediate regardless of the cur...
Definition: tool_event.h:241
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:101