KiCad PCB EDA Suite
tool_manager.h
Go to the documentation of this file.
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 
26 #ifndef __TOOL_MANAGER_H
27 #define __TOOL_MANAGER_H
28 
29 #include <typeinfo>
30 #include <map>
31 #include <list>
32 #include <stack>
33 
34 #include <tool/tool_base.h>
35 #include <view/view_controls.h>
36 
37 class TOOL_BASE;
38 class ACTION_MANAGER;
39 class ACTION_MENU;
40 class EDA_BASE_FRAME;
41 
42 
51 {
52 private:
53  struct TOOL_STATE;
54 
55 public:
56  TOOL_MANAGER();
57 
58  ~TOOL_MANAGER();
59 
60  // Helper typedefs
61  typedef std::map<TOOL_BASE*, TOOL_STATE*> TOOL_STATE_MAP;
62  typedef std::map<std::string, TOOL_STATE*> NAME_STATE_MAP;
63  typedef std::map<TOOL_ID, TOOL_STATE*> ID_STATE_MAP;
64  typedef std::list<TOOL_ID> ID_LIST;
65 
69  static TOOL_ID MakeToolId( const std::string& aToolName );
70 
77  void RegisterTool( TOOL_BASE* aTool );
78 
86  bool InvokeTool( TOOL_ID aToolId );
87 
95  bool InvokeTool( const std::string& aToolName );
96 
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 
114  bool RunAction( const std::string& aActionName, bool aNow, void* aParam );
115 
116  bool RunAction( const std::string& aActionName, bool aNow = false )
117  {
118  return RunAction( aActionName, aNow, (void*) NULL );
119  }
120 
136  template <typename T>
137  bool RunAction( const TOOL_ACTION& aAction, bool aNow = false, T aParam = NULL )
138  {
139  return RunAction( aAction, aNow, reinterpret_cast<void*>( aParam ) );
140  }
141 
142  bool RunAction( const TOOL_ACTION& aAction, bool aNow, void* aParam );
143 
144  bool RunAction( const TOOL_ACTION& aAction, bool aNow = false )
145  {
146  return RunAction( aAction, aNow, (void*) NULL );
147  }
148 
149  const std::map<std::string, TOOL_ACTION*>& GetActions();
150 
158  void PrimeTool( const VECTOR2D& aPosition );
159 
161  int GetHotKey( const TOOL_ACTION& aAction );
162 
164 
172  TOOL_BASE* FindTool( int aId ) const;
173 
181  TOOL_BASE* FindTool( const std::string& aName ) const;
182 
183  /*
184  * Function GetTool()
185  * Returns the tool of given type or NULL if there is no such tool registered.
186  */
187  template<typename T>
188  T* GetTool()
189  {
190  std::map<const char*, TOOL_BASE*>::iterator tool = m_toolTypes.find( typeid( T ).name() );
191 
192  if( tool != m_toolTypes.end() )
193  return static_cast<T*>( tool->second );
194 
195  return NULL;
196  }
197 
202  void DeactivateTool();
203 
204 
209  bool IsToolActive( TOOL_ID aId ) const;
210 
211 
216  void ResetTools( TOOL_BASE::RESET_REASON aReason );
217 
224  void InitTools();
225 
231  bool ProcessEvent( const TOOL_EVENT& aEvent );
232 
237  inline void PostEvent( const TOOL_EVENT& aEvent )
238  {
239  m_eventQueue.push_back( aEvent );
240  }
241 
246  void SetEnvironment( EDA_ITEM* aModel, KIGFX::VIEW* aView,
247  KIGFX::VIEW_CONTROLS* aViewControls, EDA_BASE_FRAME* aFrame );
248 
249  /* Accessors for the environment objects (view, model, etc.) */
251  {
252  return m_view;
253  }
254 
256  {
257  return m_viewControls;
258  }
259 
262 
263  inline EDA_ITEM* GetModel() const
264  {
265  return m_model;
266  }
267 
268  inline EDA_BASE_FRAME* GetEditFrame() const
269  {
270  return m_frame;
271  }
272 
278  inline int GetCurrentToolId() const
279  {
280  return m_activeTools.empty() ? -1 : m_activeTools.front();
281  }
282 
288  inline TOOL_BASE* GetCurrentTool() const
289  {
290  return FindTool( GetCurrentToolId() );
291  }
292 
298  {
299  auto it = m_toolIdIndex.find( GetCurrentToolId() );
300  return ( it != m_toolIdIndex.end() ) ? it->second : nullptr;
301  }
302 
311  int GetPriority( int aToolId ) const;
312 
317  void ScheduleNextState( TOOL_BASE* aTool, TOOL_STATE_FUNC& aHandler,
318  const TOOL_EVENT_LIST& aConditions );
319 
324  void ClearTransitions( TOOL_BASE* aTool );
325 
326  void RunMainStack( TOOL_BASE* aTool, std::function<void()> aFunc );
327 
331  void UpdateUI( const TOOL_EVENT& aEvent );
332 
338  TOOL_EVENT* ScheduleWait( TOOL_BASE* aTool, const TOOL_EVENT_LIST& aConditions );
339 
350  void ScheduleContextMenu( TOOL_BASE* aTool, ACTION_MENU* aMenu, CONTEXT_MENU_TRIGGER aTrigger );
351 
357  bool SaveClipboard( const std::string& aText );
358 
363  std::string GetClipboard() const;
364 
369  const KIGFX::VC_SETTINGS& GetCurrentToolVC() const;
370 
375  {
376  return m_menuActive;
377  }
378 
385  {
387  }
388 
393  void DispatchContextMenu( const TOOL_EVENT& aEvent );
394 
395 private:
396  typedef std::pair<TOOL_EVENT_LIST, TOOL_STATE_FUNC> TRANSITION;
397 
402  bool dispatchInternal( const TOOL_EVENT& aEvent );
403 
410  bool dispatchHotKey( const TOOL_EVENT& aEvent );
411 
418  bool dispatchActivation( const TOOL_EVENT& aEvent );
419 
426  bool invokeTool( TOOL_BASE* aTool );
427 
435  bool runTool( TOOL_ID aToolId );
436 
444  bool runTool( const std::string& aName );
445 
453  bool runTool( TOOL_BASE* aTool );
454 
455  template <class Parameters>
456  void invokeTool( const std::string& aName, const Parameters& aToolParams );
457 
466  ID_LIST::iterator finishTool( TOOL_STATE* aState );
467 
475  bool isRegistered( TOOL_BASE* aTool ) const
476  {
477  return m_toolState.count( aTool ) > 0;
478  }
479 
487  bool isActive( TOOL_BASE* aTool );
488 
494  void saveViewControls( TOOL_STATE* aState );
495 
500  void applyViewControls( TOOL_STATE* aState );
501 
504  bool processEvent( const TOOL_EVENT& aEvent );
505 
511  void setActiveState( TOOL_STATE* aState );
512 
515 
518 
521 
523  std::map<const char*, TOOL_BASE*> m_toolTypes;
524 
527 
530 
532  std::map<TOOL_ID, OPT<VECTOR2D>> m_cursorSettings;
533 
538 
540  std::list<TOOL_EVENT> m_eventQueue;
541 
544 
546 
549 
552 
555 };
556 
557 #endif
bool m_menuActive
Flag indicating whether a context menu is currently displayed.
Definition: tool_manager.h:548
void DispatchContextMenu(const TOOL_EVENT &aEvent)
Function DispatchContextMenu() Handles context menu related events.
VECTOR2D GetCursorPosition()
VECTOR2D m_menuCursor
Right click context menu position.
Definition: tool_manager.h:543
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:250
void UpdateUI(const TOOL_EVENT &aEvent)
Updates the status bar and synchronizes toolbars.
int GetHotKey(const TOOL_ACTION &aAction)
ACTION_MANAGER * m_actionMgr
Instance of ACTION_MANAGER that handles TOOL_ACTIONs.
Definition: tool_manager.h:529
EDA_BASE_FRAME * GetEditFrame() const
Definition: tool_manager.h:268
bool processEvent(const TOOL_EVENT &aEvent)
Main function for event processing.
static TOOL_ID MakeToolId(const std::string &aToolName)
Generates a unique ID from for a tool with given name.
int GetPriority(int aToolId) const
Returns priority of a given tool.
Class ACTION_MENU.
Definition: action_menu.h:43
ID_STATE_MAP m_toolIdIndex
Index of the registered tools current states, associated by tools' ID numbers.
Definition: tool_manager.h:520
void ScheduleNextState(TOOL_BASE *aTool, TOOL_STATE_FUNC &aHandler, const TOOL_EVENT_LIST &aConditions)
Defines a state transition - the events that cause a given handler method in the tool to be called.
bool SaveClipboard(const std::string &aText)
Stores an information to the system clipboard.
bool RunAction(const TOOL_ACTION &aAction, bool aNow=false)
Definition: tool_manager.h:144
std::map< TOOL_ID, TOOL_STATE * > ID_STATE_MAP
Definition: tool_manager.h:63
VIEW_CONTROLS class definition.
std::map< TOOL_ID, OPT< VECTOR2D > > m_cursorSettings
Original cursor position, if overridden by the context menu handler.
Definition: tool_manager.h:532
ID_LIST::iterator finishTool(TOOL_STATE *aState)
Function finishTool() Deactivates a tool and does the necessary clean up.
bool dispatchActivation(const TOOL_EVENT &aEvent)
Function dispatchActivation() Checks if it is a valid activation event and invokes a proper tool.
std::map< std::string, TOOL_STATE * > NAME_STATE_MAP
Definition: tool_manager.h:62
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:109
void PrimeTool(const VECTOR2D &aPosition)
Function PrimeTool() "Primes" a tool by sending a cursor left-click event with the mouse position set...
Class ACTION_MANAGER.
TOOL_BASE * FindTool(int aId) const
Function FindTool() Searches for a tool with given ID.
TOOL_EVENT * ScheduleWait(TOOL_BASE *aTool, const TOOL_EVENT_LIST &aConditions)
Pauses execution of a given tool until one or more events matching aConditions arrives.
void applyViewControls(TOOL_STATE *aState)
Function applyViewControls() Applies VIEW_CONTROLS settings stored in a TOOL_STATE object.
bool IsContextMenuActive()
True while processing a context menu.
Definition: tool_manager.h:374
EDA_ITEM * GetModel() const
Definition: tool_manager.h:263
bool runTool(TOOL_ID aToolId)
Function runTool() Makes a tool active, so it can receive events and react to them.
void DeactivateTool()
Function DeactivateTool() Deactivates the currently active tool.
bool InvokeTool(TOOL_ID aToolId)
Function InvokeTool() Calls a tool by sending a tool activation event to tool of given ID.
EDA_BASE_FRAME * m_frame
Definition: tool_manager.h:537
std::map< TOOL_BASE *, TOOL_STATE * > TOOL_STATE_MAP
Definition: tool_manager.h:61
EDA_ITEM * m_model
Definition: tool_manager.h:534
Class TOOL_MANAGER.
Definition: tool_manager.h:50
NAME_STATE_MAP m_toolNameIndex
Index of the registered tools current states, associated by tools' names.
Definition: tool_manager.h:517
int TOOL_ID
Unique identifier for tools.
Definition: tool_base.h:56
Class TOOL_EVENT_LIST.
Definition: tool_event.h:564
TOOL_ID m_menuOwner
Tool currently displaying a popup menu. It is negative when there is no menu displayed.
Definition: tool_manager.h:551
const std::map< std::string, TOOL_ACTION * > & GetActions()
bool dispatchHotKey(const TOOL_EVENT &aEvent)
Function dispatchStandardEvents() Handles specific events, that are intended for TOOL_MANAGER rather ...
void setActiveState(TOOL_STATE *aState)
Saves the previous active state and sets a new one.
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagates an event to tools that requested events of matching type(s).
CONTEXT_MENU_TRIGGER
Defines when a context menu is opened.
Definition: tool_event.h:159
Class TOOL_EVENT.
Definition: tool_event.h:171
const KIGFX::VC_SETTINGS & GetCurrentToolVC() const
Returns the view controls settings for the current tool or the general settings if there is no active...
Structure to keep VIEW_CONTROLS settings for easy store/restore operations
Definition: view_controls.h:44
void ResetTools(TOOL_BASE::RESET_REASON aReason)
Function ResetTools() Resets all tools (i.e.
Class VIEW_CONTROLS is an interface for classes handling user events controlling the view behaviour (...
Struct describing the current execution state of a TOOL.
KIGFX::VIEW_CONTROLS * m_viewControls
Definition: tool_manager.h:536
TOOL_BASE * GetCurrentTool() const
Returns the tool that is on the top of the active tools stack (was invoked the most recently).
Definition: tool_manager.h:288
std::function< int(const TOOL_EVENT &)> TOOL_STATE_FUNC
Definition: tool_base.h:58
TOOL_STATE * GetCurrentToolState() const
Returns the TOOL_STATE object representing the state of the active tool.
Definition: tool_manager.h:297
bool isActive(TOOL_BASE *aTool)
Function isActive() Returns information about a tool activation status.
KIGFX::VIEW * m_view
Definition: tool_manager.h:535
int GetCurrentToolId() const
Returns id of the tool that is on the top of the active tools stack (was invoked the most recently).
Definition: tool_manager.h:278
void RunMainStack(TOOL_BASE *aTool, std::function< void()> aFunc)
bool m_warpMouseAfterContextMenu
Definition: tool_manager.h:545
bool isRegistered(TOOL_BASE *aTool) const
Function isRegistered() Returns information about a tool registration status.
Definition: tool_manager.h:475
KIGFX::VIEW_CONTROLS * GetViewControls() const
Definition: tool_manager.h:255
std::list< TOOL_EVENT > m_eventQueue
Queue that stores events to be processed at the end of the event processing cycle.
Definition: tool_manager.h:540
const char * name
Definition: DXF_plotter.cpp:61
ACTION_MANAGER * GetActionManager()
Definition: tool_manager.h:163
TOOL_STATE * m_activeState
Pointer to the state object corresponding to the currently executed tool.
Definition: tool_manager.h:554
Class TOOL_BASE.
Definition: tool_base.h:67
The base frame for deriving all KiCad main window classes.
Class TOOL_ACTION.
Definition: tool_action.h:46
std::string GetClipboard() const
Returns the information currently stored in the system clipboard.
void VetoContextMenuMouseWarp()
Disables mouse warping after the current context menu is closed.
Definition: tool_manager.h:384
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:163
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, EDA_BASE_FRAME *aFrame)
Sets the work environment (model, view, view controls and the parent window).
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:79
void InitTools()
Function InitTools() Initializes all registered tools.
VECTOR2D GetMousePosition()
bool IsToolActive(TOOL_ID aId) const
Function IsToolActive() Returns true if a tool with given id is active (executing)
Class VIEW.
Definition: view.h:61
void ClearTransitions(TOOL_BASE *aTool)
Clears the state transition map for a tool.
ID_LIST m_activeTools
Stack of the active tools.
Definition: tool_manager.h:526
bool RunAction(const std::string &aActionName, bool aNow=false)
Definition: tool_manager.h:116
std::list< TOOL_ID > ID_LIST
Definition: tool_manager.h:64
void PostEvent(const TOOL_EVENT &aEvent)
Puts an event to the event queue to be processed at the end of event processing cycle.
Definition: tool_manager.h:237
TOOL_STATE_MAP m_toolState
Index of registered tools current states, associated by tools' objects.
Definition: tool_manager.h:514
void ScheduleContextMenu(TOOL_BASE *aTool, ACTION_MENU *aMenu, CONTEXT_MENU_TRIGGER aTrigger)
Sets behaviour of the tool's context popup menu.
void saveViewControls(TOOL_STATE *aState)
Function saveViewControls() Saves the VIEW_CONTROLS settings to the tool state object.
bool RunAction(const TOOL_ACTION &aAction, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:137
bool invokeTool(TOOL_BASE *aTool)
Function invokeTool() Invokes a tool by sending a proper event (in contrary to runTool,...
void RegisterTool(TOOL_BASE *aTool)
Function RegisterTool() Adds a tool to the manager set and sets it up.
bool dispatchInternal(const TOOL_EVENT &aEvent)
Function dispatchInternal Passes an event at first to the active tools, then to all others.
std::pair< TOOL_EVENT_LIST, TOOL_STATE_FUNC > TRANSITION
Definition: tool_manager.h:396
std::map< const char *, TOOL_BASE * > m_toolTypes
Index of the registered tools to easily lookup by their type.
Definition: tool_manager.h:523