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.

486 lines
16 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 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
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
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. * @author Maciej Suminski <maciej.suminski@cern.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #ifndef __TOOL_MANAGER_H
  26. #define __TOOL_MANAGER_H
  27. #include <deque>
  28. #include <typeinfo>
  29. #include <map>
  30. #include <list>
  31. #include <tool/tool_base.h>
  32. class TOOL_BASE;
  33. class ACTION_MANAGER;
  34. class CONTEXT_MENU;
  35. class wxWindow;
  36. /**
  37. * Class TOOL_MANAGER.
  38. * Master controller class:
  39. * - registers editing tools
  40. * - pumps UI events to tools requesting them
  41. * - manages tool state machines (transitions and wait requests)
  42. */
  43. class TOOL_MANAGER
  44. {
  45. private:
  46. struct TOOL_STATE;
  47. public:
  48. TOOL_MANAGER();
  49. ~TOOL_MANAGER();
  50. // Helper typedefs
  51. typedef std::map<TOOL_BASE*, TOOL_STATE*> TOOL_STATE_MAP;
  52. typedef std::map<std::string, TOOL_STATE*> NAME_STATE_MAP;
  53. typedef std::map<TOOL_ID, TOOL_STATE*> ID_STATE_MAP;
  54. typedef std::list<TOOL_ID> ID_LIST;
  55. /**
  56. * Generates a unique ID from for a tool with given name.
  57. */
  58. static TOOL_ID MakeToolId( const std::string& aToolName );
  59. /**
  60. * Function RegisterTool()
  61. * Adds a tool to the manager set and sets it up. Called once for
  62. * each tool during application initialization.
  63. * @param aTool: tool to be added. Ownership is transferred.
  64. */
  65. void RegisterTool( TOOL_BASE* aTool );
  66. /**
  67. * Function InvokeTool()
  68. * Calls a tool by sending a tool activation event to tool of given ID.
  69. *
  70. * @param aToolId is the ID number of the requested tool.
  71. * @return True if the requested tool was invoked successfully.
  72. */
  73. bool InvokeTool( TOOL_ID aToolId );
  74. /**
  75. * Function InvokeTool()
  76. * Calls a tool by sending a tool activation event to tool of given name.
  77. *
  78. * @param aToolName is the name of the requested tool.
  79. * @return True if the requested tool was invoked successfully.
  80. */
  81. bool InvokeTool( const std::string& aToolName );
  82. /**
  83. * Function RegisterAction()
  84. * Registers an action that can be used to control tools (eg. invoke, trigger specific
  85. * behaviours).
  86. *
  87. * @param aAction is the action to be registered.
  88. */
  89. void RegisterAction( TOOL_ACTION* aAction );
  90. /**
  91. * Function UnregisterAction()
  92. * Unregisters an action, so it is no longer active.
  93. *
  94. * @param aAction is the action to be unregistered.
  95. */
  96. void UnregisterAction( TOOL_ACTION* aAction );
  97. /**
  98. * Function RunAction()
  99. * Runs the specified action. The common format for action names is "application.ToolName.Action".
  100. *
  101. * @param aActionName is the name of action to be invoked.
  102. * @param aNow decides if the action has to be run immediately or after the current coroutine
  103. * is preemptied.
  104. * @param aParam is an optional parameter that might be used by the invoked action. Its meaning
  105. * depends on the action.
  106. * @return False if the action was not found.
  107. */
  108. template<typename T>
  109. bool RunAction( const std::string& aActionName, bool aNow = false, T aParam = NULL )
  110. {
  111. return RunAction( aActionName, aNow, reinterpret_cast<void*>( aParam ) );
  112. }
  113. bool RunAction( const std::string& aActionName, bool aNow, void* aParam );
  114. bool RunAction( const std::string& aActionName, bool aNow = false )
  115. {
  116. return RunAction( aActionName, aNow, (void*) NULL );
  117. }
  118. /**
  119. * Function RunAction()
  120. * Runs the specified action.
  121. *
  122. * @param aAction is the action to be invoked.
  123. * @param aNow decides if the action has to be run immediately or after the current coroutine
  124. * is preemptied.
  125. * @param aParam is an optional parameter that might be used by the invoked action. Its meaning
  126. * depends on the action.
  127. */
  128. template<typename T>
  129. void RunAction( const TOOL_ACTION& aAction, bool aNow = false, T aParam = NULL )
  130. {
  131. RunAction( aAction, aNow, reinterpret_cast<void*>( aParam ) );
  132. }
  133. void RunAction( const TOOL_ACTION& aAction, bool aNow, void* aParam );
  134. void RunAction( const TOOL_ACTION& aAction, bool aNow = false )
  135. {
  136. RunAction( aAction, aNow, (void*) NULL );
  137. }
  138. ///> @copydoc ACTION_MANAGER::GetHotKey()
  139. int GetHotKey( const TOOL_ACTION& aAction );
  140. ///> @copydoc ACTION_MANAGER::UpdateHotKeys()
  141. void UpdateHotKeys();
  142. /**
  143. * Function FindTool()
  144. * Searches for a tool with given ID.
  145. *
  146. * @param aId is the ID number of the requested tool.
  147. * @return Pointer to the requested tool or NULL in case of failure.
  148. */
  149. TOOL_BASE* FindTool( int aId ) const;
  150. /**
  151. * Function FindTool()
  152. * Searches for a tool with given name.
  153. *
  154. * @param aName is the name of the requested tool.
  155. * @return Pointer to the requested tool or NULL in case of failure.
  156. */
  157. TOOL_BASE* FindTool( const std::string& aName ) const;
  158. /*
  159. * Function GetTool()
  160. * Returns the tool of given type or NULL if there is no such tool registered.
  161. */
  162. template<typename T>
  163. T* GetTool()
  164. {
  165. std::map<const char*, TOOL_BASE*>::iterator tool = m_toolTypes.find( typeid( T ).name() );
  166. if( tool != m_toolTypes.end() )
  167. return static_cast<T*>( tool->second );
  168. return NULL;
  169. }
  170. /**
  171. * Function DeactivateTool()
  172. * Deactivates the currently active tool.
  173. */
  174. void DeactivateTool();
  175. /**
  176. * Function ResetTools()
  177. * Resets all tools (i.e. calls their Reset() method).
  178. */
  179. void ResetTools( TOOL_BASE::RESET_REASON aReason );
  180. /**
  181. * Function InitTools()
  182. * Initializes all registered tools. If a tool fails during the initialization, it is
  183. * deactivated and becomes unavailable for further use. Initialization should be done
  184. * only once.
  185. */
  186. void InitTools();
  187. /**
  188. * Propagates an event to tools that requested events of matching type(s).
  189. * @param aEvent is the event to be processed.
  190. */
  191. bool ProcessEvent( const TOOL_EVENT& aEvent );
  192. /**
  193. * Puts an event to the event queue to be processed at the end of event processing cycle.
  194. * @param aEvent is the event to be put into the queue.
  195. */
  196. inline void PostEvent( const TOOL_EVENT& aEvent )
  197. {
  198. m_eventQueue.push_back( aEvent );
  199. }
  200. /**
  201. * Sets the work environment (model, view, view controls and the parent window).
  202. * These are made available to the tool. Called by the parent frame (PCB_EDIT_FRAME)
  203. * when the board is set up.
  204. */
  205. void SetEnvironment( EDA_ITEM* aModel, KIGFX::VIEW* aView,
  206. KIGFX::VIEW_CONTROLS* aViewControls, wxWindow* aFrame );
  207. /* Accessors for the environment objects (view, model, etc.) */
  208. KIGFX::VIEW* GetView() const
  209. {
  210. return m_view;
  211. }
  212. inline KIGFX::VIEW_CONTROLS* GetViewControls() const
  213. {
  214. return m_viewControls;
  215. }
  216. inline EDA_ITEM* GetModel() const
  217. {
  218. return m_model;
  219. }
  220. inline wxWindow* GetEditFrame() const
  221. {
  222. return m_editFrame;
  223. }
  224. /**
  225. * Returns id of the tool that is on the top of the active tools stack
  226. * (was invoked the most recently).
  227. * @return Id of the currently used tool.
  228. */
  229. inline int GetCurrentToolId() const
  230. {
  231. return m_activeTools.empty() ? -1 : m_activeTools.front();
  232. }
  233. /**
  234. * Returns the tool that is on the top of the active tools stack
  235. * (was invoked the most recently).
  236. * @return Pointer to the currently used tool.
  237. */
  238. inline TOOL_BASE* GetCurrentTool() const
  239. {
  240. return FindTool( GetCurrentToolId() );
  241. }
  242. /**
  243. * Returns the TOOL_STATE object representing the state of the active tool. If there are no
  244. * tools active, it returns nullptr.
  245. */
  246. TOOL_STATE* GetCurrentToolState() const
  247. {
  248. auto it = m_toolIdIndex.find( GetCurrentToolId() );
  249. return ( it != m_toolIdIndex.end() ) ? it->second : nullptr;
  250. }
  251. /**
  252. * Returns priority of a given tool. Higher number means that the tool is closer to the
  253. * beginning of the active tools queue (i.e. receives events earlier, tools with lower
  254. * priority receive events later).
  255. * @param aToolId is the id of queried tool.
  256. * @return The priority of a given tool. If returned number is negative, then it means that
  257. * the tool id is invalid or the tool is not active.
  258. */
  259. int GetPriority( int aToolId ) const;
  260. /**
  261. * Defines a state transition - the events that cause a given handler method in the tool
  262. * to be called. Called by TOOL_INTERACTIVE::Go(). May be called from a coroutine context.
  263. */
  264. void ScheduleNextState( TOOL_BASE* aTool, TOOL_STATE_FUNC& aHandler,
  265. const TOOL_EVENT_LIST& aConditions );
  266. void RunMainStack( TOOL_BASE* aTool, std::function<void()> aFunc );
  267. /**
  268. * Pauses execution of a given tool until one or more events matching aConditions arrives.
  269. * The pause/resume operation is done through COROUTINE object.
  270. * Called only from coroutines.
  271. */
  272. boost::optional<TOOL_EVENT> ScheduleWait( TOOL_BASE* aTool,
  273. const TOOL_EVENT_LIST& aConditions );
  274. /**
  275. * Sets behaviour of the tool's context popup menu.
  276. * @param aTool - the parent tool
  277. * @param aMenu - the menu structure, defined by the tool
  278. * @param aTrigger - when the menu is activated:
  279. * CMENU_NOW: opens the menu right now
  280. * CMENU_BUTTON: opens the menu when RMB is pressed
  281. * CMENU_OFF: menu is disabled.
  282. * May be called from a coroutine context.
  283. */
  284. void ScheduleContextMenu( TOOL_BASE* aTool, CONTEXT_MENU* aMenu,
  285. CONTEXT_MENU_TRIGGER aTrigger );
  286. /**
  287. * Allows a tool to pass the already handled event to the next tool on the stack.
  288. */
  289. void PassEvent()
  290. {
  291. m_passEvent = true;
  292. }
  293. /**
  294. * Stores an information to the system clipboard.
  295. * @param aText is the information to be stored.
  296. * @return False if error occured.
  297. */
  298. bool SaveClipboard( const std::string& aText );
  299. /**
  300. * Returns the information currently stored in the system clipboard. If data stored in the
  301. * clipboard is in non-text format, empty string is returned.
  302. */
  303. std::string GetClipboard() const;
  304. private:
  305. typedef std::pair<TOOL_EVENT_LIST, TOOL_STATE_FUNC> TRANSITION;
  306. /**
  307. * Function dispatchInternal
  308. * Passes an event at first to the active tools, then to all others.
  309. */
  310. void dispatchInternal( const TOOL_EVENT& aEvent );
  311. /**
  312. * Function dispatchStandardEvents()
  313. * Handles specific events, that are intended for TOOL_MANAGER rather than tools.
  314. * @param aEvent is the event to be processed.
  315. * @return False if the event was processed and should not go any further.
  316. */
  317. bool dispatchStandardEvents( const TOOL_EVENT& aEvent );
  318. /**
  319. * Function dispatchActivation()
  320. * Checks if it is a valid activation event and invokes a proper tool.
  321. * @param aEvent is an event to be tested.
  322. * @return True if a tool was invoked, false otherwise.
  323. */
  324. bool dispatchActivation( const TOOL_EVENT& aEvent );
  325. /**
  326. * Function dispatchContextMenu()
  327. * Handles context menu related events.
  328. */
  329. void dispatchContextMenu( const TOOL_EVENT& aEvent );
  330. /**
  331. * Function invokeTool()
  332. * Invokes a tool by sending a proper event (in contrary to runTool, which makes the tool run
  333. * for real).
  334. * @param aTool is the tool to be invoked.
  335. */
  336. bool invokeTool( TOOL_BASE* aTool );
  337. /**
  338. * Function runTool()
  339. * Makes a tool active, so it can receive events and react to them. Activated tool is pushed
  340. * on the active tools stack, so the last activated tool receives events first.
  341. *
  342. * @param aToolId is the ID number of tool to be run.
  343. */
  344. bool runTool( TOOL_ID aToolId );
  345. /**
  346. * Function runTool()
  347. * Makes a tool active, so it can receive events and react to them. Activated tool is pushed
  348. * on the active tools stack, so the last activated tool receives events first.
  349. *
  350. * @param aName is the name of tool to be run.
  351. */
  352. bool runTool( const std::string& aName );
  353. /**
  354. * Function runTool()
  355. * Makes a tool active, so it can receive events and react to them. Activated tool is pushed
  356. * on the active tools stack, so the last activated tool receives events first.
  357. *
  358. * @param aTool is the tool to be run.
  359. */
  360. bool runTool( TOOL_BASE* aTool );
  361. template <class Parameters>
  362. void invokeTool( const std::string& aName, const Parameters& aToolParams );
  363. /**
  364. * Function finishTool()
  365. * Deactivates a tool and does the necessary clean up.
  366. *
  367. * @param aState is the state variable of the tool to be stopped.
  368. * @return m_activeTools iterator. If the tool has been completely deactivated, it points
  369. * to the next active tool on the list. Otherwise it is an iterator pointing to aState.
  370. */
  371. ID_LIST::iterator finishTool( TOOL_STATE* aState );
  372. /**
  373. * Function isRegistered()
  374. * Returns information about a tool registration status.
  375. *
  376. * @param aTool is the tool to be checked.
  377. * @return true if the tool is in the registered tools list, false otherwise.
  378. */
  379. bool isRegistered( TOOL_BASE* aTool ) const
  380. {
  381. return m_toolState.count( aTool ) > 0;
  382. }
  383. /**
  384. * Function isActive()
  385. * Returns information about a tool activation status.
  386. *
  387. * @param aTool is the tool to be checked.
  388. * @return True if the tool is on the active tools stack, false otherwise.
  389. */
  390. bool isActive( TOOL_BASE* aTool );
  391. /// Index of registered tools current states, associated by tools' objects.
  392. TOOL_STATE_MAP m_toolState;
  393. /// Index of the registered tools current states, associated by tools' names.
  394. NAME_STATE_MAP m_toolNameIndex;
  395. /// Index of the registered tools current states, associated by tools' ID numbers.
  396. ID_STATE_MAP m_toolIdIndex;
  397. /// Index of the registered tools to easily lookup by their type.
  398. std::map<const char*, TOOL_BASE*> m_toolTypes;
  399. /// Stack of the active tools
  400. ID_LIST m_activeTools;
  401. /// Instance of ACTION_MANAGER that handles TOOL_ACTIONs
  402. ACTION_MANAGER* m_actionMgr;
  403. /// Original cursor position, if overridden by the context menu handler
  404. boost::optional<VECTOR2D> m_origCursor;
  405. EDA_ITEM* m_model;
  406. KIGFX::VIEW* m_view;
  407. KIGFX::VIEW_CONTROLS* m_viewControls;
  408. wxWindow* m_editFrame;
  409. /// Queue that stores events to be processed at the end of the event processing cycle.
  410. std::list<TOOL_EVENT> m_eventQueue;
  411. /// Flag saying if the currently processed event should be passed to other tools.
  412. bool m_passEvent;
  413. };
  414. #endif