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.

185 lines
5.6 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013 CERN
  5. * @author Tomasz Wlostowski <tomasz.wlostowski@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 __TOOL_MANAGER_H
  25. #define __TOOL_MANAGER_H
  26. #include <cstdio>
  27. #include <map>
  28. #include <vector>
  29. #include <deque>
  30. #include <boost/unordered_map.hpp>
  31. #include <math/vector2d.h>
  32. #include <tool/tool_event.h>
  33. #include <tool/tool_base.h>
  34. class TOOL_BASE;
  35. class CONTEXT_MENU;
  36. class wxWindow;
  37. /**
  38. * Class TOOL_MANAGER.
  39. * Master controller class:
  40. * - registers editing tools
  41. * - pumps UI events to tools requesting them
  42. * - manages tool state machines (transitions and wait requests)
  43. */
  44. class TOOL_MANAGER
  45. {
  46. public:
  47. TOOL_MANAGER();
  48. ~TOOL_MANAGER();
  49. /**
  50. * Generates an unique ID from for a tool with given name.
  51. */
  52. static TOOL_ID MakeToolId( const std::string& aToolName );
  53. /**
  54. * Function RegisterTool()
  55. * Adds a tool to the manager set and sets it up. Called once for
  56. * each tool during application initialization.
  57. * @param aTool: tool to be added. Ownership is transferred.
  58. */
  59. void RegisterTool( TOOL_BASE* aTool );
  60. /**
  61. * Function InvokeTool()
  62. * Calls a tool by sending a tool activation event to tool of given ID or name.
  63. * An user-defined parameter object can be also passed
  64. */
  65. bool InvokeTool( TOOL_ID aToolId );
  66. bool InvokeTool( const std::string& aName );
  67. template <class Parameters>
  68. void InvokeTool( const std::string& aName, const Parameters& aToolParams );
  69. /**
  70. * Function FindTool()
  71. * Searches for a tool with given name or ID
  72. */
  73. TOOL_BASE* FindTool( int aId ) const;
  74. TOOL_BASE* FindTool( const std::string& aName ) const;
  75. /**
  76. * Resets the state of a given tool by clearing its wait and
  77. * transition lists and calling tool's internal Reset() method.
  78. */
  79. void ResetTool( TOOL_BASE* aTool );
  80. /**
  81. * Takes an event from the TOOL_DISPATCHER and propagates it to
  82. * tools that requested events of matching type(s)
  83. */
  84. bool ProcessEvent( TOOL_EVENT& aEvent );
  85. /**
  86. * Sets the work environment (model, view, view controls and the parent window).
  87. * These are made available to the tool. Called by the parent frame (PCB_EDIT_FRAME)
  88. * when the board is set up
  89. */
  90. void SetEnvironment( EDA_ITEM* aModel, KiGfx::VIEW* aView,
  91. KiGfx::VIEW_CONTROLS* aViewControls, wxWindow* aFrame );
  92. /* Accessors for the environment objects (view, model, etc.) */
  93. KiGfx::VIEW* GetView()
  94. {
  95. return m_view;
  96. }
  97. KiGfx::VIEW_CONTROLS* GetViewControls()
  98. {
  99. return m_viewControls;
  100. }
  101. EDA_ITEM* GetModel()
  102. {
  103. return m_model;
  104. }
  105. wxWindow* GetEditFrame()
  106. {
  107. return m_editFrame;
  108. }
  109. /**
  110. * Defines a state transition - the events that cause a given handler method in the tool
  111. * to be called. Called by TOOL_INTERACTIVE::Go(). May be called from a coroutine context.
  112. */
  113. void ScheduleNextState( TOOL_BASE* aTool, TOOL_STATE_FUNC& aHandler,
  114. const TOOL_EVENT_LIST& aConditions );
  115. /**
  116. * Pauses execution of a given tool until one or more events matching aConditions arrives.
  117. * The pause/resume operation is done through COROUTINE object.
  118. * Called only from coroutines.
  119. */
  120. boost::optional<TOOL_EVENT> ScheduleWait( TOOL_BASE* aTool,
  121. const TOOL_EVENT_LIST& aConditions );
  122. /**
  123. * Sets behaviour of the tool's context popup menu.
  124. * @param aMenu - the menu structure, defined by the tool
  125. * @param aTrigger - when the menu is activated:
  126. * CMENU_NOW: opens the menu right now
  127. * CMENU_BUTTON: opens the menu when RMB is pressed
  128. * CMENU_OFF: menu is disabled.
  129. * May be called from a coroutine context.
  130. */
  131. void ScheduleContextMenu( TOOL_BASE* aTool, CONTEXT_MENU* aMenu,
  132. TOOL_ContextMenuTrigger aTrigger );
  133. /**
  134. * Allows a tool pass the already handled event to be passed to the next tool on the stack.
  135. */
  136. void PassEvent()
  137. {
  138. m_passEvent = true;
  139. }
  140. private:
  141. struct ToolState;
  142. typedef std::pair<TOOL_EVENT_LIST, TOOL_STATE_FUNC> Transition;
  143. void dispatchInternal( TOOL_EVENT& aEvent );
  144. void finishTool( ToolState* aState );
  145. std::map<TOOL_BASE*, ToolState*> m_toolState;
  146. std::map<std::string, ToolState*> m_toolNameIndex;
  147. std::map<TOOL_ID, ToolState*> m_toolIdIndex;
  148. std::deque<TOOL_ID> m_activeTools;
  149. EDA_ITEM* m_model;
  150. KiGfx::VIEW* m_view;
  151. KiGfx::VIEW_CONTROLS* m_viewControls;
  152. wxWindow* m_editFrame;
  153. bool m_passEvent;
  154. ToolState* m_currentTool;
  155. };
  156. #endif