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 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_dragSelects( true ),
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  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 
127 }
128 
bool IsCurrentTool(const TOOL_ACTION &aAction) const
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
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:89
void UpdateHotKeys(bool aFullUpdate)
Function UpdateHotKeys() Optionally reads the hotkey config files and then rebuilds the internal hotk...
bool m_dragSelects
Definition: tools_holder.h:63
bool m_immediateActions
Definition: tools_holder.h:59
TOOL_ACTION * FindAction(const std::string &aActionName) const
Function FindAction() Finds an action with a given name (if there is one available).
virtual void PopTool(const std::string &actionName)
TOOL_EVENT.
Definition: tool_event.h:171
bool m_moveWarpsCursor
Definition: tools_holder.h:64
std::string CurrentToolName() const
wxString GetLabel() const
Definition: tool_action.cpp:69
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:48
see class PGM_BASE
std::vector< std::string > m_toolStack
Definition: tools_holder.h:52
ACTION_MANAGER * GetActionManager()
Definition: tool_manager.h:194
TOOL_ACTION.
Definition: tool_action.h:46
TOOL_EVENT MakeEvent() const
Function MakeEvent() Returns the event associated with the action (i.e.
Definition: tool_action.h:107
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:74
void PostEvent(const TOOL_EVENT &aEvent)
Puts an event to the event queue to be processed at the end of event processing cycle.
Definition: tool_manager.h:268
static TOOL_ACTION selectionTool
Definition: actions.h:147
void SetHasPosition(bool aHasPosition)
Definition: tool_event.h:261
const std::string & GetName() const
Function GetName() Returns name of the action.
Definition: tool_action.h:78