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.

684 lines
22 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
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-2023 CERN
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  8. * @author Maciej Suminski <maciej.suminski@cern.ch>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, you may find one here:
  22. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  23. * or you may search the http://www.gnu.org website for the version 2 license,
  24. * or you may write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  26. */
  27. #ifndef TOOL_MANAGER_H
  28. #define TOOL_MANAGER_H
  29. #include <list>
  30. #include <map>
  31. #include <stack>
  32. #include <vector>
  33. #include <typeinfo>
  34. #include <type_traits>
  35. #include <tool/tool_base.h>
  36. #include <tool/tool_event.h>
  37. namespace KIGFX
  38. {
  39. class VIEW_CONTROLS;
  40. struct VC_SETTINGS;
  41. }
  42. class COMMIT;
  43. class TOOLS_HOLDER;
  44. class TOOL_ACTION;
  45. class ACTION_MANAGER;
  46. class ACTION_MENU;
  47. class APP_SETTINGS_BASE;
  48. /**
  49. * Master controller class:
  50. * - registers editing tools
  51. * - pumps UI events to tools requesting them
  52. * - manages tool state machines (transitions and wait requests)
  53. */
  54. class TOOL_MANAGER
  55. {
  56. private:
  57. struct TOOL_STATE;
  58. public:
  59. TOOL_MANAGER();
  60. ~TOOL_MANAGER();
  61. // Helper typedefs
  62. typedef std::map<TOOL_BASE*, TOOL_STATE*> TOOL_STATE_MAP;
  63. typedef std::map<std::string, TOOL_STATE*> NAME_STATE_MAP;
  64. typedef std::map<TOOL_ID, TOOL_STATE*> ID_STATE_MAP;
  65. typedef std::list<TOOL_ID> ID_LIST;
  66. /**
  67. * Generate a unique ID from for a tool with given name.
  68. */
  69. static TOOL_ID MakeToolId( const std::string& aToolName );
  70. /**
  71. * Add a tool to the manager set and sets it up. Called once for each tool during
  72. * application initialization.
  73. *
  74. * @param aTool: tool to be added. Ownership is transferred.
  75. */
  76. void RegisterTool( TOOL_BASE* aTool );
  77. /**
  78. * Call a tool by sending a tool activation event to tool of given ID.
  79. *
  80. * @param aToolId is the ID number of the requested tool.
  81. * @return True if the requested tool was invoked successfully.
  82. */
  83. bool InvokeTool( TOOL_ID aToolId );
  84. /**
  85. * Call a tool by sending a tool activation event to tool of given name.
  86. *
  87. * @param aToolName is the name of the requested tool.
  88. * @return True if the requested tool was invoked successfully.
  89. */
  90. bool InvokeTool( const std::string& aToolName );
  91. /**
  92. * Shutdown all tools with a currently registered event loop in this tool manager
  93. * by waking them up with a null event.
  94. */
  95. void ShutdownAllTools();
  96. /**
  97. * Shutdown the specified tool by waking it up with a null event to terminate
  98. * the processing loop.
  99. *
  100. * @param aTool is the tool to shutdown
  101. */
  102. void ShutdownTool( TOOL_BASE* aTool );
  103. /**
  104. * Shutdown the specified tool by waking it up with a null event to terminate
  105. * the processing loop.
  106. *
  107. * @param aToolId is the ID of the tool to shutdown
  108. */
  109. void ShutdownTool( TOOL_ID aToolId );
  110. /**
  111. * Shutdown the specified tool by waking it up with a null event to terminate
  112. * the processing loop.
  113. *
  114. * @param aToolName is name of the tool to shutdown
  115. */
  116. void ShutdownTool( const std::string& aToolName );
  117. /**
  118. * Run the specified action immediately, pausing the current action to run the new one.
  119. *
  120. * The common format for action names is "application.ToolName.Action".
  121. *
  122. * @note The type of the optional parameter must match exactly with the type the consuming
  123. * action is expecting, otherwise an assert will occur when reading the parameter.
  124. *
  125. * @param aActionName is the name of action to be invoked.
  126. * @param aParam is an optional parameter that might be used by the invoked action. Its meaning
  127. * depends on the action.
  128. * @return False if the action was not found.
  129. */
  130. template<typename T, std::enable_if_t<!std::is_convertible_v<T*, COMMIT*>>* = nullptr>
  131. bool RunAction( const std::string& aActionName, T aParam )
  132. {
  133. // Use a cast to ensure the proper type is stored inside the parameter
  134. ki::any a( static_cast<T>( aParam ) );
  135. return doRunAction( aActionName, true, a, nullptr );
  136. }
  137. bool RunAction( const std::string& aActionName )
  138. {
  139. // Default initialize the parameter argument to an empty ki_any
  140. ki::any a;
  141. return doRunAction( aActionName, true, a, nullptr );
  142. }
  143. /**
  144. * Run the specified action immediately, pausing the current action to run the new one.
  145. *
  146. * @note The type of the optional parameter must match exactly with the type the consuming
  147. * action is expecting, otherwise an assert will occur when reading the parameter.
  148. *
  149. * @param aAction is the action to be invoked.
  150. * @param aParam is an optional parameter that might be used by the invoked action. Its meaning
  151. * depends on the action.
  152. * @return True if the action was handled immediately.
  153. */
  154. template<typename T, std::enable_if_t<!std::is_convertible_v<T, COMMIT*>>* = nullptr>
  155. bool RunAction( const TOOL_ACTION& aAction, T aParam )
  156. {
  157. // Use a cast to ensure the proper type is stored inside the parameter
  158. ki::any a( static_cast<T>( aParam ) );
  159. return doRunAction( aAction, true, a, nullptr );
  160. }
  161. /**
  162. * Run the specified action immediately, pausing the current action to run the new one.
  163. *
  164. * @note The type of the optional parameter must match exactly with the type the consuming
  165. * action is expecting, otherwise an assert will occur when reading the parameter.
  166. *
  167. * @param aAction is the action to be invoked.
  168. * @param aCommit is the commit object the tool handling the action should add the new edits to
  169. * @return True if the action was handled immediately
  170. */
  171. template<typename T>
  172. bool RunSynchronousAction( const TOOL_ACTION& aAction, COMMIT* aCommit, T aParam )
  173. {
  174. // Use a cast to ensure the proper type is stored inside the parameter
  175. ki::any a( static_cast<T>( aParam ) );
  176. return doRunAction( aAction, true, a, aCommit );
  177. }
  178. bool RunSynchronousAction( const TOOL_ACTION& aAction, COMMIT* aCommit )
  179. {
  180. // Default initialize the parameter argument to an empty ki_any
  181. ki::any a;
  182. return doRunAction( aAction, true, a, aCommit );
  183. }
  184. bool RunAction( const TOOL_ACTION& aAction )
  185. {
  186. // Default initialize the parameter argument to an empty ki_any
  187. ki::any a;
  188. return doRunAction( aAction, true, a, nullptr );
  189. }
  190. /**
  191. * Run the specified action after the current action (coroutine) ends.
  192. *
  193. * The common format for action names is "application.ToolName.Action".
  194. *
  195. * @note The type of the optional parameter must match exactly with the type the consuming
  196. * action is expecting, otherwise an assert will occur when reading the parameter.
  197. *
  198. * @param aActionName is the name of action to be invoked.
  199. * @param aParam is an optional parameter that might be used by the invoked action. Its meaning
  200. * depends on the action.
  201. * @return False if the action was not found.
  202. */
  203. template<typename T>
  204. bool PostAction( const std::string& aActionName, T aParam )
  205. {
  206. // Use a cast to ensure the proper type is stored inside the parameter
  207. ki::any a( static_cast<T>( aParam ) );
  208. return doRunAction( aActionName, false, a, nullptr );
  209. }
  210. bool PostAction( const std::string& aActionName )
  211. {
  212. // Default initialize the parameter argument to an empty ki_any
  213. ki::any a;
  214. return doRunAction( aActionName, false, a, nullptr );
  215. }
  216. /**
  217. * Run the specified action after the current action (coroutine) ends.
  218. *
  219. * @nite The type of the optional parameter must match exactly with the type the consuming
  220. * action is expecting, otherwise an assert will occur when reading the parameter.
  221. *
  222. * @param aAction is the action to be invoked.
  223. * @param aParam is an optional parameter that might be used by the invoked action. Its meaning
  224. * depends on the action.
  225. */
  226. template<typename T>
  227. bool PostAction( const TOOL_ACTION& aAction, T aParam )
  228. {
  229. // Use a cast to ensure the proper type is stored inside the parameter
  230. ki::any a( static_cast<T>( aParam ) );
  231. return doRunAction( aAction, false, a, nullptr );
  232. }
  233. void PostAction( const TOOL_ACTION& aAction )
  234. {
  235. // Default initialize the parameter argument to an empty ki_any
  236. ki::any a;
  237. doRunAction( aAction, false, a, nullptr );
  238. }
  239. bool PostAction( const TOOL_ACTION& aAction, COMMIT* aCommit )
  240. {
  241. // Default initialize the parameter argument to an empty ki_any
  242. ki::any a;
  243. return doRunAction( aAction, false, a, aCommit );
  244. }
  245. /**
  246. * Send a cancel event to the tool currently at the top of the tool stack.
  247. */
  248. void CancelTool();
  249. /**
  250. * "Prime" a tool by sending a cursor left-click event with the mouse position set
  251. * to the passed in position.
  252. *
  253. * @param aPosition is the mouse position to use in the event
  254. */
  255. void PrimeTool( const VECTOR2D& aPosition );
  256. /// @copydoc ACTION_MANAGER::GetHotKey()
  257. int GetHotKey( const TOOL_ACTION& aAction ) const;
  258. ACTION_MANAGER* GetActionManager() const { return m_actionMgr; }
  259. /**
  260. * Search for a tool with given ID.
  261. *
  262. * @param aId is the ID number of the requested tool.
  263. * @return Pointer to the requested tool or NULL in case of failure.
  264. */
  265. TOOL_BASE* FindTool( int aId ) const;
  266. /**
  267. * Search for a tool with given name.
  268. *
  269. * @param aName is the name of the requested tool.
  270. * @return Pointer to the requested tool or NULL in case of failure.
  271. */
  272. TOOL_BASE* FindTool( const std::string& aName ) const;
  273. /*
  274. * Return the tool of given type or nullptr if there is no such tool registered.
  275. */
  276. template<typename T>
  277. T* GetTool()
  278. {
  279. std::map<const char*, TOOL_BASE*>::iterator tool = m_toolTypes.find( typeid( T ).name() );
  280. if( tool != m_toolTypes.end() )
  281. return static_cast<T*>( tool->second );
  282. return nullptr;
  283. }
  284. /*
  285. * Return all registered tools.
  286. */
  287. std::vector<TOOL_BASE*> Tools() { return m_toolOrder; }
  288. /**
  289. * Deactivate the currently active tool.
  290. */
  291. void DeactivateTool();
  292. /**
  293. * Return true if a tool with given id is active (executing)
  294. */
  295. bool IsToolActive( TOOL_ID aId ) const;
  296. /**
  297. * Reset all tools (i.e. calls their Reset() method).
  298. */
  299. void ResetTools( TOOL_BASE::RESET_REASON aReason );
  300. /**
  301. * Initialize all registered tools.
  302. *
  303. * If a tool fails during the initialization, it is deactivated and becomes unavailable
  304. * for further use. Initialization should be done only once.
  305. */
  306. void InitTools();
  307. /**
  308. * Propagate an event to tools that requested events of matching type(s).
  309. *
  310. * @param aEvent is the event to be processed.
  311. * @return true if the event is a managed hotkey
  312. */
  313. bool ProcessEvent( const TOOL_EVENT& aEvent );
  314. /**
  315. * Put an event to the event queue to be processed at the end of event processing cycle.
  316. *
  317. * @param aEvent is the event to be put into the queue.
  318. */
  319. void PostEvent( const TOOL_EVENT& aEvent );
  320. /**
  321. * Set the work environment (model, view, view controls and the parent window).
  322. *
  323. * These are made available to the tool. Called by the parent frame when it is set up.
  324. */
  325. void SetEnvironment( EDA_ITEM* aModel, KIGFX::VIEW* aView,
  326. KIGFX::VIEW_CONTROLS* aViewControls, APP_SETTINGS_BASE* aSettings,
  327. TOOLS_HOLDER* aFrame );
  328. /*
  329. * Accessors for the environment objects (view, model, etc.)
  330. */
  331. KIGFX::VIEW* GetView() const { return m_view; }
  332. KIGFX::VIEW_CONTROLS* GetViewControls() const { return m_viewControls; }
  333. VECTOR2D GetMousePosition() const;
  334. VECTOR2D GetCursorPosition() const;
  335. EDA_ITEM* GetModel() const { return m_model; }
  336. APP_SETTINGS_BASE* GetSettings() const { return m_settings; }
  337. TOOLS_HOLDER* GetToolHolder() const { return m_frame; }
  338. /**
  339. * Return id of the tool that is on the top of the active tools stack (was invoked the
  340. * most recently).
  341. *
  342. * @return Id of the currently used tool.
  343. */
  344. inline int GetCurrentToolId() const
  345. {
  346. return m_activeTools.empty() ? -1 : m_activeTools.front();
  347. }
  348. /**
  349. * Return the tool that is on the top of the active tools stack (was invoked the most
  350. * recently).
  351. *
  352. * @return Pointer to the currently used tool.
  353. */
  354. inline TOOL_BASE* GetCurrentTool() const
  355. {
  356. return FindTool( GetCurrentToolId() );
  357. }
  358. /**
  359. * Return the #TOOL_STATE object representing the state of the active tool. If there are no
  360. * tools active, it returns nullptr.
  361. */
  362. TOOL_STATE* GetCurrentToolState() const
  363. {
  364. auto it = m_toolIdIndex.find( GetCurrentToolId() );
  365. return ( it != m_toolIdIndex.end() ) ? it->second : nullptr;
  366. }
  367. /**
  368. * Return priority of a given tool.
  369. *
  370. * Higher number means that the tool is closer to the beginning of the active tools
  371. * queue (i.e. receives events earlier, tools with lower priority receive events later).
  372. *
  373. * @param aToolId is the id of queried tool.
  374. * @return The priority of a given tool. If returned number is negative, then it means that
  375. * the tool id is invalid or the tool is not active.
  376. */
  377. int GetPriority( int aToolId ) const;
  378. /**
  379. * Define a state transition.
  380. *
  381. * The events that cause a given handler method in the tool to be called. Called by
  382. * TOOL_INTERACTIVE::Go(). May be called from a coroutine context.
  383. */
  384. void ScheduleNextState( TOOL_BASE* aTool, TOOL_STATE_FUNC& aHandler,
  385. const TOOL_EVENT_LIST& aConditions );
  386. /**
  387. * Clear the state transition map for a tool.
  388. *
  389. * @param aTool is the tool that should have the transition map cleared.
  390. */
  391. void ClearTransitions( TOOL_BASE* aTool );
  392. void RunMainStack( TOOL_BASE* aTool, std::function<void()> aFunc );
  393. /**
  394. * Update the status bar and synchronizes toolbars.
  395. */
  396. void UpdateUI( const TOOL_EVENT& aEvent );
  397. /**
  398. * Pause execution of a given tool until one or more events matching aConditions arrives.
  399. *
  400. * The pause/resume operation is done through #COROUTINE object. Called only from coroutines.
  401. */
  402. TOOL_EVENT* ScheduleWait( TOOL_BASE* aTool, const TOOL_EVENT_LIST& aConditions );
  403. /**
  404. * Set behavior of the tool's context popup menu.
  405. *
  406. * @param aTool is the parent tool.
  407. * @param aMenu is the menu structure, defined by the tool.
  408. * @param aTrigger determines when the menu is activated:
  409. * CMENU_NOW: opens the menu right now
  410. * CMENU_BUTTON: opens the menu when RMB is pressed
  411. * CMENU_OFF: menu is disabled.
  412. * May be called from a coroutine context.
  413. */
  414. void ScheduleContextMenu( TOOL_BASE* aTool, ACTION_MENU* aMenu, CONTEXT_MENU_TRIGGER aTrigger );
  415. /**
  416. * Return the view controls settings for the current tool or the general settings if there is
  417. * no active tool.
  418. */
  419. const KIGFX::VC_SETTINGS& GetCurrentToolVC() const;
  420. /**
  421. * True while processing a context menu.
  422. */
  423. bool IsContextMenuActive() const
  424. {
  425. return m_menuActive;
  426. }
  427. /**
  428. * Disable mouse warping after the current context menu is closed.
  429. *
  430. * This must be called before invoking each context menu. It's a good idea to call this
  431. * from non-modal dialogs (e.g. DRC window).
  432. */
  433. void VetoContextMenuMouseWarp()
  434. {
  435. m_warpMouseAfterContextMenu = false;
  436. }
  437. /**
  438. * Handle context menu related events.
  439. */
  440. void DispatchContextMenu( const TOOL_EVENT& aEvent );
  441. /**
  442. * Handle specific events, that are intended for TOOL_MANAGER rather than tools.
  443. *
  444. * @param aEvent is the event to be processed.
  445. * @return true if the event was processed and should not go any further.
  446. */
  447. bool DispatchHotKey( const TOOL_EVENT& aEvent );
  448. VECTOR2D GetMenuCursorPos() const
  449. {
  450. return m_menuCursor;
  451. }
  452. private:
  453. typedef std::pair<TOOL_EVENT_LIST, TOOL_STATE_FUNC> TRANSITION;
  454. /**
  455. * Helper function to actually run an action.
  456. */
  457. bool doRunAction( const TOOL_ACTION& aAction, bool aNow, const ki::any& aParam,
  458. COMMIT* aCommit );
  459. bool doRunAction( const std::string& aActionName, bool aNow, const ki::any& aParam,
  460. COMMIT* aCommit );
  461. /**
  462. * Pass an event at first to the active tools, then to all others.
  463. */
  464. bool dispatchInternal( TOOL_EVENT& aEvent );
  465. /**
  466. * Check if it is a valid activation event and invokes a proper tool.
  467. *
  468. * @param aEvent is an event to be tested.
  469. * @return True if a tool was invoked, false otherwise.
  470. */
  471. bool dispatchActivation( const TOOL_EVENT& aEvent );
  472. /**
  473. * Invoke a tool by sending a proper event (in contrary to runTool, which makes the tool run
  474. * for real).
  475. *
  476. * @param aTool is the tool to be invoked.
  477. */
  478. bool invokeTool( TOOL_BASE* aTool );
  479. /**
  480. * Make a tool active, so it can receive events and react to them.
  481. *
  482. * The activated tool is pushed on the active tools stack, so the last activated tool
  483. * receives events first.
  484. *
  485. * @param aTool is the tool to be run.
  486. */
  487. bool runTool( TOOL_BASE* aTool );
  488. /**
  489. * Deactivate a tool and does the necessary clean up.
  490. *
  491. * @param aState is the state variable of the tool to be stopped.
  492. * @return m_activeTools iterator. If the tool has been completely deactivated, it points
  493. * to the next active tool on the list. Otherwise it is an iterator pointing to
  494. * \a aState.
  495. */
  496. ID_LIST::iterator finishTool( TOOL_STATE* aState );
  497. /**
  498. * Return information about a tool registration status.
  499. *
  500. * @param aTool is the tool to be checked.
  501. * @return true if the tool is in the registered tools list, false otherwise.
  502. */
  503. bool isRegistered( TOOL_BASE* aTool ) const
  504. {
  505. return m_toolState.count( aTool ) > 0;
  506. }
  507. /**
  508. * Return information about a tool activation status.
  509. *
  510. * @param aTool is the tool to be checked.
  511. * @return True if the tool is on the active tools stack, false otherwise.
  512. */
  513. bool isActive( TOOL_BASE* aTool ) const;
  514. /**
  515. * Save the #VIEW_CONTROLS settings to the tool state object.
  516. *
  517. * If #VIEW_CONTROLS settings are affected by #TOOL_MANAGER, the original settings are saved.
  518. */
  519. void saveViewControls( TOOL_STATE* aState );
  520. /**
  521. * Apply #VIEW_CONTROLS settings stored in a #TOOL_STATE object.
  522. */
  523. void applyViewControls( const TOOL_STATE* aState );
  524. /**
  525. * Main function for event processing.
  526. *
  527. * @return true if a hotkey was handled.
  528. */
  529. bool processEvent( const TOOL_EVENT& aEvent );
  530. /**
  531. * Save the previous active state and sets a new one.
  532. *
  533. * @param aState is the new active state. Might be null to indicate there is no new
  534. * active state.
  535. */
  536. void setActiveState( TOOL_STATE* aState );
  537. private:
  538. /// List of tools in the order they were registered.
  539. std::vector<TOOL_BASE*> m_toolOrder;
  540. /// Index of registered tools current states, associated by tools' objects.
  541. TOOL_STATE_MAP m_toolState;
  542. /// Index of the registered tools current states, associated by tools' names.
  543. NAME_STATE_MAP m_toolNameIndex;
  544. /// Index of the registered tools current states, associated by tools' ID numbers.
  545. ID_STATE_MAP m_toolIdIndex;
  546. /// Index of the registered tools to easily lookup by their type.
  547. std::map<const char*, TOOL_BASE*> m_toolTypes;
  548. /// Stack of the active tools.
  549. ID_LIST m_activeTools;
  550. /// Instance of ACTION_MANAGER that handles TOOL_ACTIONs.
  551. ACTION_MANAGER* m_actionMgr;
  552. /// Original cursor position, if overridden by the context menu handler.
  553. std::map<TOOL_ID, std::optional<VECTOR2D>> m_cursorSettings;
  554. EDA_ITEM* m_model;
  555. KIGFX::VIEW* m_view;
  556. KIGFX::VIEW_CONTROLS* m_viewControls;
  557. TOOLS_HOLDER* m_frame;
  558. APP_SETTINGS_BASE* m_settings;
  559. /// Queue that stores events to be processed at the end of the event processing cycle.
  560. std::list<TOOL_EVENT> m_eventQueue;
  561. /// Right click context menu position.
  562. VECTOR2D m_menuCursor;
  563. bool m_warpMouseAfterContextMenu;
  564. /// Flag indicating whether a context menu is currently displayed.
  565. bool m_menuActive;
  566. /// Tool currently displaying a popup menu. It is negative when there is no menu displayed.
  567. TOOL_ID m_menuOwner;
  568. /// Pointer to the state object corresponding to the currently executed tool.
  569. TOOL_STATE* m_activeState;
  570. /// True if the tool manager is shutting down (don't process additional events)
  571. bool m_shuttingDown;
  572. };
  573. #endif // TOOL_MANAGER_H