KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2022 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
43 const ACTION_CONDITIONS& aConditions )
44{
45 RegisterUIUpdateHandler( aAction.GetUIId(), aConditions );
46}
47
48
50{
52}
53
54
55// TODO: Implement an RAII mechanism for the stack PushTool/PopTool pairs
57{
58 const std::string& actionName = aEvent.getCommandStr();
59
60 wxASSERT_MSG( !actionName.empty(), wxS( "Pushed Empty Tool Name!" ) );
61
62 m_toolStack.push_back( actionName );
63
64 // Human cognitive stacking is very shallow; deeper tool stacks just get annoying
65 if( m_toolStack.size() > 3 )
66 m_toolStack.erase( m_toolStack.begin() );
67
68 TOOL_ACTION* action = m_toolManager->GetActionManager()->FindAction( actionName );
69
70 if( action )
72 else
73 DisplayToolMsg( actionName );
74}
75
76
77void TOOLS_HOLDER::PopTool( const TOOL_EVENT& aEvent )
78{
79 const std::string& actionName = aEvent.getCommandStr();
80
81 wxASSERT_MSG( !aEvent.getCommandStr().empty(), wxS( "Popped Empty Tool Name!" ) );
82
83 // Push/pop events can get out of order (such as when they're generated by the Simulator
84 // frame but not processed until the mouse is back in the Schematic frame), so make sure
85 // we're popping the right stack frame.
86
87 for( int i = (int) m_toolStack.size() - 1; i >= 0; --i )
88 {
89 if( m_toolStack[ i ] == actionName )
90 {
91 m_toolStack.erase( m_toolStack.begin() + i );
92
93 // If there's something underneath us, and it's now the top of the stack, then
94 // re-activate it
95 if( ( --i ) >= 0 && i == (int)m_toolStack.size() - 1 )
96 {
97 std::string back = m_toolStack[ i ];
99
100 if( action )
101 {
102 // Pop the action as running it will push it back onto the stack
103 m_toolStack.pop_back();
104
105 TOOL_EVENT evt = action->MakeEvent();
106 evt.SetHasPosition( false );
107 evt.SetReactivate( true );
108 GetToolManager()->PostEvent( evt );
109 }
110 }
111 else
112 DisplayToolMsg( ACTIONS::selectionTool.GetFriendlyName() );
113
114 return;
115 }
116 }
117
118 wxASSERT_MSG( false, wxS( "Popped a Tool Not on the Tool Stack!" ) );
119}
120
121
123{
124 if( m_toolStack.empty() )
126 else
127 return m_toolStack.back();
128}
129
130
131bool TOOLS_HOLDER::IsCurrentTool( const TOOL_ACTION& aAction ) const
132{
133 if( m_toolStack.empty() )
134 return &aAction == &ACTIONS::selectionTool;
135 else
136 return m_toolStack.back() == aAction.GetName();
137}
138
139
141{
142 if( !GetToolManager() )
143 return;
144
145 std::string actionName = CurrentToolName();
146 TOOL_ACTION* action = GetToolManager()->GetActionManager()->FindAction( actionName );
147
148 if( action )
149 DisplayToolMsg( action->GetFriendlyName() );
150}
151
152
153void TOOLS_HOLDER::CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVarsChanged )
154{
155 if( GetToolManager() )
157
158 COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
159
161 m_dragAction = settings->m_Input.drag_left;
163}
164
static TOOL_ACTION selectionTool
Definition: actions.h:164
TOOL_ACTION * FindAction(const std::string &aActionName) const
Find an action with a given name (if there is one available).
void UpdateHotKeys(bool aFullUpdate)
Optionally read the hotkey config files and then rebuilds the internal hotkey maps.
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:167
bool m_immediateActions
Definition: tools_holder.h:180
std::string CurrentToolName() const
bool m_moveWarpsCursor
Definition: tools_holder.h:186
virtual void ShowChangedLanguage()
virtual void PopTool(const TOOL_EVENT &aEvent)
Pops a tool from the stack.
MOUSE_DRAG_ACTION m_dragAction
Definition: tools_holder.h:184
virtual void PushTool(const TOOL_EVENT &aEvent)
NB: the definition of "tool" is different at the user level.
virtual void RegisterUIUpdateHandler(const TOOL_ACTION &aAction, const ACTION_CONDITIONS &aConditions)
Register an action's update conditions with the UI layer to allow the UI to appropriately display the...
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
std::vector< std::string > m_toolStack
Definition: tools_holder.h:173
virtual void UnregisterUIUpdateHandler(const TOOL_ACTION &aAction)
Unregister a UI handler for an action that was registered using RegisterUIUpdateHandler.
virtual void CommonSettingsChanged(bool aEnvVarsChanged, bool aTextVarsChanged)
Notification event that some of the common (suite-wide) settings have changed.
bool IsCurrentTool(const TOOL_ACTION &aAction) const
virtual void DisplayToolMsg(const wxString &msg)
Definition: tools_holder.h:130
Represent a single user action.
Definition: tool_action.h:269
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:302
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
wxString GetFriendlyName() const
Return the translated user-friendly name of the action.
int GetUIId() const
Definition: tool_action.h:339
Generic, UI-independent tool event.
Definition: tool_event.h:167
void SetReactivate(bool aReactivate=true)
Definition: tool_event.h:269
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:257
const std::string & getCommandStr() const
Definition: tool_event.h:545
void PostEvent(const TOOL_EVENT &aEvent)
Put an event to the event queue to be processed at the end of event processing cycle.
ACTION_MANAGER * GetActionManager() const
Definition: tool_manager.h:289
MOUSE_DRAG_ACTION
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:119
Functors that can be used to figure out how the action controls should be displayed in the UI and if ...
MOUSE_DRAG_ACTION drag_left