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.

144 lines
4.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013 CERN
  5. * @author Maciej Suminski <maciej.suminski@cern.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef ACTION_MANAGER_H_
  25. #define ACTION_MANAGER_H_
  26. #include <list>
  27. #include <map>
  28. #include <string>
  29. class TOOL_BASE;
  30. class TOOL_MANAGER;
  31. class TOOL_ACTION;
  32. /**
  33. * Class ACTION_MANAGER
  34. *
  35. * Takes care of TOOL_ACTION objects. Registers them and allows one to run them
  36. * using associated hot keys, names or ids.
  37. */
  38. class ACTION_MANAGER
  39. {
  40. public:
  41. /**
  42. * Constructor.
  43. * @param aToolManager is a tool manager instance that is used to pass events to tools.
  44. */
  45. ACTION_MANAGER( TOOL_MANAGER* aToolManager );
  46. /**
  47. * Destructor.
  48. * Unregisters every registered action.
  49. */
  50. ~ACTION_MANAGER();
  51. /**
  52. * Function RegisterAction()
  53. * Adds a tool action to the manager and sets it up. After that is is possible to invoke
  54. * the action using hotkeys or sending a command event with its name.
  55. * @param aAction: action to be added. Ownership is not transferred.
  56. */
  57. void RegisterAction( TOOL_ACTION* aAction );
  58. /**
  59. * Function UnregisterAction()
  60. * Removes a tool action from the manager and makes it unavailable for further usage.
  61. * @param aAction: action to be removed.
  62. */
  63. void UnregisterAction( TOOL_ACTION* aAction );
  64. /**
  65. * Generates an unique ID from for an action with given name.
  66. */
  67. static int MakeActionId( const std::string& aActionName );
  68. /**
  69. * Get a list of currently-registered actions mapped by their name.
  70. */
  71. const std::map<std::string, TOOL_ACTION*>& GetActions();
  72. /**
  73. * Function FindAction()
  74. * Finds an action with a given name (if there is one available).
  75. * @param aActionName is the searched action.
  76. * @return Pointer to a TOOL_ACTION object or NULL if there is no such action.
  77. */
  78. TOOL_ACTION* FindAction( const std::string& aActionName ) const;
  79. /**
  80. * Function RunHotKey()
  81. * Runs an action associated with a hotkey (if there is one available).
  82. * @param aHotKey is the hotkey to be handled.
  83. * @return True if there was an action associated with the hotkey, false otherwise.
  84. */
  85. bool RunHotKey( int aHotKey ) const;
  86. /**
  87. * Function GetHotKey()
  88. * Returns the hot key associated with a given action or 0 if there is none.
  89. * @param aAction is the queried action.
  90. */
  91. int GetHotKey( const TOOL_ACTION& aAction ) const;
  92. /**
  93. * Function UpdateHotKeys()
  94. * Optionally reads the hotkey config files and then rebuilds the internal hotkey maps.
  95. */
  96. void UpdateHotKeys( bool aFullUpdate );
  97. /**
  98. * Function GetActionList()
  99. * Returns list of TOOL_ACTIONs. TOOL_ACTIONs add themselves to the list upon their
  100. * creation.
  101. * @return List of TOOL_ACTIONs.
  102. */
  103. static std::list<TOOL_ACTION*>& GetActionList()
  104. {
  105. static std::list<TOOL_ACTION*> actionList;
  106. return actionList;
  107. }
  108. private:
  109. // Resolves a hotkey by applying legacy and current settings over the action's
  110. // default hotkey.
  111. int processHotKey( TOOL_ACTION* aAction, std::map<std::string, int> aLegacyMap,
  112. std::map<std::string, int> aHotKeyMap );
  113. ///> Tool manager needed to run actions
  114. TOOL_MANAGER* m_toolMgr;
  115. ///> Map for indexing actions by their names
  116. std::map<std::string, TOOL_ACTION*> m_actionNameIndex;
  117. ///> Map for indexing actions by their hotkeys
  118. typedef std::map<int, std::list<TOOL_ACTION*> > HOTKEY_LIST;
  119. HOTKEY_LIST m_actionHotKeys;
  120. ///> Quick action<->hot key lookup
  121. std::map<int, int> m_hotkeys;
  122. };
  123. #endif /* ACTION_MANAGER_H_ */