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 The 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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <pgm_base.h>
22#include <tool/action_manager.h>
23#include <tool/action_menu.h>
24#include <tool/actions.h>
25#include <tool/tools_holder.h>
26#include <tool/tool_manager.h>
27
28
30 m_toolManager( nullptr ),
31 m_actions( nullptr ),
32 m_toolDispatcher( nullptr ),
33 m_immediateActions( true ),
35 m_moveWarpsCursor( true )
36{ }
37
38
40 const ACTION_CONDITIONS& aConditions )
41{
42 RegisterUIUpdateHandler( aAction.GetUIId(), aConditions );
43}
44
45
50
51
52// TODO: Implement an RAII mechanism for the stack PushTool/PopTool pairs
54{
55 const std::string& actionName = aEvent.getCommandStr();
56
57 wxASSERT_MSG( !actionName.empty(), wxS( "Pushed Empty Tool Name!" ) );
58
59 m_toolStack.push_back( actionName );
60
61 // Human cognitive stacking is very shallow; deeper tool stacks just get annoying
62 if( m_toolStack.size() > 3 )
63 m_toolStack.erase( m_toolStack.begin() );
64
65 TOOL_ACTION* action = m_toolManager->GetActionManager()->FindAction( actionName );
66
67 if( action )
69 else
70 DisplayToolMsg( actionName );
71}
72
73
74void TOOLS_HOLDER::PopTool( const TOOL_EVENT& aEvent )
75{
76 const std::string& actionName = aEvent.getCommandStr();
77
78 wxASSERT_MSG( !aEvent.getCommandStr().empty(), wxS( "Popped Empty Tool Name!" ) );
79
80 // Push/pop events can get out of order (such as when they're generated by the Simulator
81 // frame but not processed until the mouse is back in the Schematic frame), so make sure
82 // we're popping the right stack frame.
83 for( int i = (int) m_toolStack.size() - 1; i >= 0; --i )
84 {
85 if( m_toolStack[ i ] == actionName )
86 {
87 m_toolStack.erase( m_toolStack.begin() + i );
88
89 // If there's something underneath us, and it's now the top of the stack, then
90 // re-activate it
91 if( ( --i ) >= 0 && i == (int)m_toolStack.size() - 1 )
92 {
93 std::string back = m_toolStack[ i ];
94 TOOL_ACTION* action = m_toolManager->GetActionManager()->FindAction( back );
95
96 if( action )
97 {
98 // Pop the action as running it will push it back onto the stack
99 m_toolStack.pop_back();
100
101 TOOL_EVENT evt = action->MakeEvent();
102 evt.SetHasPosition( false );
103 evt.SetReactivate( true );
104 GetToolManager()->PostEvent( evt );
105 }
106 }
107 else
108 DisplayToolMsg( ACTIONS::selectionTool.GetFriendlyName() );
109
110 return;
111 }
112 }
113}
114
115
117{
118 if( m_toolStack.empty() )
119 return ACTIONS::selectionTool.GetName();
120 else
121 return m_toolStack.back();
122}
123
124
125bool TOOLS_HOLDER::IsCurrentTool( const TOOL_ACTION& aAction ) const
126{
127 if( m_toolStack.empty() )
128 return &aAction == &ACTIONS::selectionTool;
129 else
130 return m_toolStack.back() == aAction.GetName();
131}
132
133
135{
136 if( !GetToolManager() )
137 return;
138
139 std::string actionName = CurrentToolName();
140 TOOL_ACTION* action = GetToolManager()->GetActionManager()->FindAction( actionName );
141
142 if( action )
143 DisplayToolMsg( action->GetFriendlyName() );
144}
145
146
158
static TOOL_ACTION selectionTool
Definition actions.h:247
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.
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:528
virtual void CommonSettingsChanged(int aFlags=0)
Notification event that some of the common (suite-wide) settings have changed.
TOOL_MANAGER * m_toolManager
bool m_immediateActions
std::string CurrentToolName() const
bool m_moveWarpsCursor
virtual void ShowChangedLanguage()
virtual void PopTool(const TOOL_EVENT &aEvent)
Pops a tool from the stack.
TOOL_DISPATCHER * m_toolDispatcher
MOUSE_DRAG_ACTION m_dragAction
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.
std::vector< std::string > m_toolStack
virtual void UnregisterUIUpdateHandler(const TOOL_ACTION &aAction)
Unregister a UI handler for an action that was registered using RegisterUIUpdateHandler.
ACTIONS * m_actions
bool IsCurrentTool(const TOOL_ACTION &aAction) const
virtual void DisplayToolMsg(const wxString &msg)
Represent a single user action.
const std::string & GetName() const
Return name of the action.
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
Get the unique ID for this action in the user interface system.
Generic, UI-independent tool event.
Definition tool_event.h:167
void SetReactivate(bool aReactivate=true)
Definition tool_event.h:270
void SetHasPosition(bool aHasPosition)
Definition tool_event.h:257
const std::string & getCommandStr() const
Definition tool_event.h:554
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
MOUSE_DRAG_ACTION
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
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
#define HOTKEYS_CHANGED