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_menu.h>
27 #include <tool/actions.h>
28 #include <tool/tools_holder.h>
29 #include <tool/tool_manager.h>
30 
31 
33  m_toolManager( nullptr ),
34  m_actions( nullptr ),
35  m_toolDispatcher( nullptr ),
36  m_immediateActions( true ),
37  m_dragAction( MOUSE_DRAG_ACTION::SELECT ),
38  m_moveWarpsCursor( true )
39 { }
40 
41 
42 // TODO: Implement an RAII mechanism for the stack PushTool/PopTool pairs
43 void TOOLS_HOLDER::PushTool( const std::string& actionName )
44 {
45  m_toolStack.push_back( actionName );
46 
47  // Human cognitive stacking is very shallow; deeper tool stacks just get annoying
48  if( m_toolStack.size() > 3 )
49  m_toolStack.erase( m_toolStack.begin() );
50 
51  TOOL_ACTION* action = m_toolManager->GetActionManager()->FindAction( actionName );
52 
53  if( action )
54  DisplayToolMsg( action->GetLabel() );
55  else
56  DisplayToolMsg( actionName );
57 }
58 
59 
60 void TOOLS_HOLDER::PopTool( const std::string& actionName )
61 {
62  // Push/pop events can get out of order (such as when they're generated by the Simulator
63  // frame but not processed until the mouse is back in the Schematic frame), so make sure
64  // we're popping the right stack frame.
65 
66  for( int i = (int) m_toolStack.size() - 1; i >= 0; --i )
67  {
68  if( m_toolStack[ i ] == actionName )
69  {
70  m_toolStack.erase( m_toolStack.begin() + i );
71 
72  // If there's something underneath us, and it's now the top of the stack, then
73  // re-activate it
74  if( ( --i ) >= 0 && i == (int)m_toolStack.size() - 1 )
75  {
76  std::string back = m_toolStack[ i ];
78 
79  if( action )
80  {
81  // Pop the action as running it will push it back onto the stack
82  m_toolStack.pop_back();
83 
84  TOOL_EVENT evt = action->MakeEvent();
85  evt.SetHasPosition( false );
86  evt.SetReactivate( true );
87  GetToolManager()->PostEvent( evt );
88  }
89  }
90  else
92 
93  return;
94  }
95  }
96 }
97 
98 
99 std::string TOOLS_HOLDER::CurrentToolName() const
100 {
101  if( m_toolStack.empty() )
102  return ACTIONS::selectionTool.GetName();
103  else
104  return m_toolStack.back();
105 }
106 
107 
108 bool TOOLS_HOLDER::IsCurrentTool( const TOOL_ACTION& aAction ) const
109 {
110  if( m_toolStack.empty() )
111  return &aAction == &ACTIONS::selectionTool;
112  else
113  return m_toolStack.back() == aAction.GetName();
114 }
115 
116 
117 void TOOLS_HOLDER::CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVarsChanged )
118 {
119  if( GetToolManager() )
121 
122  COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
123 
125  m_dragAction = settings->m_Input.drag_left;
127 }
128 
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:122
void UpdateHotKeys(bool aFullUpdate)
Optionally read the hotkey config files and then rebuilds the internal hotkey maps.
bool m_immediateActions
Definition: tools_holder.h:170
void SetReactivate(bool aReactivate=true)
Returns information about difference between current mouse cursor position and the place where draggi...
Definition: tool_event.h:274
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:173
bool m_moveWarpsCursor
Definition: tools_holder.h:176
std::string CurrentToolName() const
MOUSE_DRAG_ACTION m_dragAction
Definition: tools_holder.h:174
wxString GetLabel() const
Definition: tool_action.cpp:69
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:157
see class PGM_BASE
std::vector< std::string > m_toolStack
Definition: tools_holder.h:163
ACTION_MANAGER * GetActionManager()
Definition: tool_manager.h:197
Represent a single user action.
Definition: tool_action.h:49
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
Definition: tool_action.h:123
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:55
void PostEvent(const TOOL_EVENT &aEvent)
Put an event to the event queue to be processed at the end of event processing cycle.
Definition: tool_manager.h:267
static TOOL_ACTION selectionTool
Definition: actions.h:156
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:262
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:83