You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.4 KiB

  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. #include <pgm_base.h>
  24. #include <settings/common_settings.h>
  25. #include <tool/action_manager.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. TOOLS_HOLDER::TOOLS_HOLDER() :
  31. m_toolManager( nullptr ),
  32. m_actions( nullptr ),
  33. m_toolDispatcher( nullptr ),
  34. m_immediateActions( true ),
  35. m_dragAction( MOUSE_DRAG_ACTION::SELECT ),
  36. m_moveWarpsCursor( true )
  37. { }
  38. // TODO: Implement an RAII mechanism for the stack PushTool/PopTool pairs
  39. void TOOLS_HOLDER::PushTool( const std::string& actionName )
  40. {
  41. m_toolStack.push_back( actionName );
  42. // Human cognitive stacking is very shallow; deeper tool stacks just get annoying
  43. if( m_toolStack.size() > 3 )
  44. m_toolStack.erase( m_toolStack.begin() );
  45. TOOL_ACTION* action = m_toolManager->GetActionManager()->FindAction( actionName );
  46. if( action )
  47. DisplayToolMsg( action->GetLabel() );
  48. else
  49. DisplayToolMsg( actionName );
  50. }
  51. void TOOLS_HOLDER::PopTool( const std::string& actionName )
  52. {
  53. // Push/pop events can get out of order (such as when they're generated by the Simulator
  54. // frame but not processed until the mouse is back in the Schematic frame), so make sure
  55. // we're popping the right stack frame.
  56. for( int i = (int) m_toolStack.size() - 1; i >= 0; --i )
  57. {
  58. if( m_toolStack[ i ] == actionName )
  59. {
  60. m_toolStack.erase( m_toolStack.begin() + i );
  61. // If there's something underneath us, and it's now the top of the stack, then
  62. // re-activate it
  63. if( ( --i ) >= 0 && i == (int)m_toolStack.size() - 1 )
  64. {
  65. std::string back = m_toolStack[ i ];
  66. TOOL_ACTION* action = m_toolManager->GetActionManager()->FindAction( back );
  67. if( action )
  68. {
  69. // Pop the action as running it will push it back onto the stack
  70. m_toolStack.pop_back();
  71. TOOL_EVENT evt = action->MakeEvent();
  72. evt.SetHasPosition( false );
  73. evt.SetReactivate( true );
  74. GetToolManager()->PostEvent( evt );
  75. }
  76. }
  77. else
  78. DisplayToolMsg( ACTIONS::selectionTool.GetLabel() );
  79. return;
  80. }
  81. }
  82. }
  83. std::string TOOLS_HOLDER::CurrentToolName() const
  84. {
  85. if( m_toolStack.empty() )
  86. return ACTIONS::selectionTool.GetName();
  87. else
  88. return m_toolStack.back();
  89. }
  90. bool TOOLS_HOLDER::IsCurrentTool( const TOOL_ACTION& aAction ) const
  91. {
  92. if( m_toolStack.empty() )
  93. return &aAction == &ACTIONS::selectionTool;
  94. else
  95. return m_toolStack.back() == aAction.GetName();
  96. }
  97. void TOOLS_HOLDER::ShowChangedLanguage()
  98. {
  99. std::string actionName = CurrentToolName();
  100. TOOL_ACTION* action = m_toolManager->GetActionManager()->FindAction( actionName );
  101. if( action )
  102. DisplayToolMsg( action->GetLabel() );
  103. }
  104. void TOOLS_HOLDER::CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVarsChanged )
  105. {
  106. if( GetToolManager() )
  107. GetToolManager()->GetActionManager()->UpdateHotKeys( false );
  108. COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
  109. m_moveWarpsCursor = settings->m_Input.warp_mouse_on_move;
  110. m_dragAction = settings->m_Input.drag_left;
  111. m_immediateActions = settings->m_Input.immediate_actions;
  112. }