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-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
42
43// TODO: Implement an RAII mechanism for the stack PushTool/PopTool pairs
45{
46 const std::string& actionName = aEvent.getCommandStr();
47
48 wxASSERT_MSG( !actionName.empty(), wxS( "Pushed Empty Tool Name!" ) );
49
50 m_toolStack.push_back( actionName );
51
52 // Human cognitive stacking is very shallow; deeper tool stacks just get annoying
53 if( m_toolStack.size() > 3 )
54 m_toolStack.erase( m_toolStack.begin() );
55
56 TOOL_ACTION* action = m_toolManager->GetActionManager()->FindAction( actionName );
57
58 if( action )
59 DisplayToolMsg( action->GetLabel() );
60 else
61 DisplayToolMsg( actionName );
62}
63
64
65void TOOLS_HOLDER::PopTool( const TOOL_EVENT& aEvent )
66{
67 const std::string& actionName = aEvent.getCommandStr();
68
69 wxASSERT_MSG( !aEvent.getCommandStr().empty(), wxS( "Popped Empty Tool Name!" ) );
70
71 // Push/pop events can get out of order (such as when they're generated by the Simulator
72 // frame but not processed until the mouse is back in the Schematic frame), so make sure
73 // we're popping the right stack frame.
74
75 for( int i = (int) m_toolStack.size() - 1; i >= 0; --i )
76 {
77 if( m_toolStack[ i ] == actionName )
78 {
79 m_toolStack.erase( m_toolStack.begin() + i );
80
81 // If there's something underneath us, and it's now the top of the stack, then
82 // re-activate it
83 if( ( --i ) >= 0 && i == (int)m_toolStack.size() - 1 )
84 {
85 std::string back = m_toolStack[ i ];
87
88 if( action )
89 {
90 // Pop the action as running it will push it back onto the stack
91 m_toolStack.pop_back();
92
93 TOOL_EVENT evt = action->MakeEvent();
94 evt.SetHasPosition( false );
95 evt.SetReactivate( true );
96 GetToolManager()->PostEvent( evt );
97 }
98 }
99 else
101
102 return;
103 }
104 }
105
106 wxASSERT_MSG( false, wxS( "Popped a Tool Not on the Tool Stack!" ) );
107}
108
109
111{
112 if( m_toolStack.empty() )
114 else
115 return m_toolStack.back();
116}
117
118
119bool TOOLS_HOLDER::IsCurrentTool( const TOOL_ACTION& aAction ) const
120{
121 if( m_toolStack.empty() )
122 return &aAction == &ACTIONS::selectionTool;
123 else
124 return m_toolStack.back() == aAction.GetName();
125}
126
127
129{
130 if( !GetToolManager() )
131 return;
132
133 std::string actionName = CurrentToolName();
134 TOOL_ACTION* action = GetToolManager()->GetActionManager()->FindAction( actionName );
135
136 if( action )
137 DisplayToolMsg( action->GetLabel() );
138}
139
140
141void TOOLS_HOLDER::CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVarsChanged )
142{
143 if( GetToolManager() )
145
146 COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
147
149 m_dragAction = settings->m_Input.drag_left;
151}
152
static TOOL_ACTION selectionTool
Definition: actions.h:156
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:170
bool m_immediateActions
Definition: tools_holder.h:183
std::string CurrentToolName() const
bool m_moveWarpsCursor
Definition: tools_holder.h:189
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:187
virtual void PushTool(const TOOL_EVENT &aEvent)
NB: the definition of "tool" is different at the user level.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:54
std::vector< std::string > m_toolStack
Definition: tools_holder.h:176
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:133
Represent a single user action.
Definition: tool_action.h:68
wxString GetLabel() const
Definition: tool_action.cpp:83
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:101
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
Definition: tool_action.cpp:72
Generic, UI-independent tool event.
Definition: tool_event.h:156
void SetReactivate(bool aReactivate=true)
Returns information about difference between current mouse cursor position and the place where draggi...
Definition: tool_event.h:256
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:244
const std::string & getCommandStr() const
Definition: tool_event.h:482
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:196
MOUSE_DRAG_ACTION
wxString GetLabel(EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Get the units string for a given units type.
Definition: eda_units.cpp:125
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:111
MOUSE_DRAG_ACTION drag_left