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 
131  template<typename T>
132  void RunAction( const TOOL_ACTION& aAction, bool aNow = false, T aParam = NULL )
133  {
134  RunAction( aAction, aNow, reinterpret_cast<void*>( aParam ) );
135  }
136 
137  void RunAction( const TOOL_ACTION& aAction, bool aNow, void* aParam );
138 
139  void RunAction( const TOOL_ACTION& aAction, bool aNow = false )
140  {
141  RunAction( aAction, aNow, (void*) NULL );
142  }
143 
144  const std::map<std::string, TOOL_ACTION*>& GetActions();
145 
147  int GetHotKey( const TOOL_ACTION& aAction );
148 
150 
158  TOOL_BASE* FindTool( int aId ) const;
159 
167  TOOL_BASE* FindTool( const std::string& aName ) const;
168 
169  /*
170  * Function GetTool()
171  * Returns the tool of given type or NULL if there is no such tool registered.
172  */
173  template<typename T>
174  T* GetTool()
175  {
176  std::map<const char*, TOOL_BASE*>::iterator tool = m_toolTypes.find( typeid( T ).name() );
177 
178  if( tool != m_toolTypes.end() )
179  return static_cast<T*>( tool->second );
180 
181  return NULL;
182  }
183 
188  void DeactivateTool();
189 
190 
195  bool IsToolActive( TOOL_ID aId ) const;
196 
197 
202  void ResetTools( TOOL_BASE::RESET_REASON aReason );
203 
210  void InitTools();
211 
217  bool ProcessEvent( const TOOL_EVENT& aEvent );
218 
223  inline void PostEvent( const TOOL_EVENT& aEvent )
224  {
225  m_eventQueue.push_back( aEvent );
226  }
227 
232  void SetEnvironment( EDA_ITEM* aModel, KIGFX::VIEW* aView,
233  KIGFX::VIEW_CONTROLS* aViewControls, EDA_BASE_FRAME* aFrame );
234 
235  /* Accessors for the environment objects (view, model, etc.) */
237  {
238  return m_view;
239  }
240 
242  {
243  return m_viewControls;
244  }
245 
246  inline EDA_ITEM* GetModel() const
247  {
248  return m_model;
249  }
250 
251  inline EDA_BASE_FRAME* GetEditFrame() const
252  {
253  return m_frame;
254  }
255 
261  inline int GetCurrentToolId() const
262  {
263  return m_activeTools.empty() ? -1 : m_activeTools.front();
264  }
265 
271  inline TOOL_BASE* GetCurrentTool() const
272  {
273  return FindTool( GetCurrentToolId() );
274  }
275 
281  {
282  auto it = m_toolIdIndex.find( GetCurrentToolId() );
283  return ( it != m_toolIdIndex.end() ) ? it->second : nullptr;
284  }
285 
294  int GetPriority( int aToolId ) const;
295 
300  void ScheduleNextState( TOOL_BASE* aTool, TOOL_STATE_FUNC& aHandler,
301  const TOOL_EVENT_LIST& aConditions );
302 
307  void ClearTransitions( TOOL_BASE* aTool );
308 
309  void RunMainStack( TOOL_BASE* aTool, std::function<void()> aFunc );
310 
314  void UpdateUI( const TOOL_EVENT& aEvent );
315 
321  TOOL_EVENT* ScheduleWait( TOOL_BASE* aTool, const TOOL_EVENT_LIST& aConditions );
322 
333  void ScheduleContextMenu( TOOL_BASE* aTool, ACTION_MENU* aMenu, CONTEXT_MENU_TRIGGER aTrigger );
334 
340  bool SaveClipboard( const std::string& aText );
341 
346  std::string GetClipboard() const;
347 
352  const KIGFX::VC_SETTINGS& GetCurrentToolVC() const;
353 
358  {
359  return m_menuActive;
360  }
361 
368  {
370  }
371 
376  void DispatchContextMenu( const TOOL_EVENT& aEvent );
377 
378 private:
379  typedef std::pair<TOOL_EVENT_LIST, TOOL_STATE_FUNC> TRANSITION;
380 
385  bool dispatchInternal( const TOOL_EVENT& aEvent );
386 
393  bool dispatchHotKey( const TOOL_EVENT& aEvent );
394 
401  bool dispatchActivation( const TOOL_EVENT& aEvent );
402 
409  bool invokeTool( TOOL_BASE* aTool );
410 
418  bool runTool( TOOL_ID aToolId );
419 
427  bool runTool( const std::string& aName );
428 
436  bool runTool( TOOL_BASE* aTool );
437 
438  template <class Parameters>
439  void invokeTool( const std::string& aName, const Parameters& aToolParams );
440 
449  ID_LIST::iterator finishTool( TOOL_STATE* aState );
450 
458  bool isRegistered( TOOL_BASE* aTool ) const
459  {
460  return m_toolState.count( aTool ) > 0;
461  }
462 
470  bool isActive( TOOL_BASE* aTool );
471 
477  void saveViewControls( TOOL_STATE* aState );
478 
483  void applyViewControls( TOOL_STATE* aState );
484 
487  bool processEvent( const TOOL_EVENT& aEvent );
488 
494  void setActiveState( TOOL_STATE* aState );
495 
498 
501 
504 
506  std::map<const char*, TOOL_BASE*> m_toolTypes;
507 
510 
513 
515  std::map<TOOL_ID, OPT<VECTOR2D>> m_cursorSettings;
516 
521 
523  std::list<TOOL_EVENT> m_eventQueue;
524 
527 
529 
532 
535 
538 };
539 
540 #endif
bool m_menuActive
Flag indicating whether a context menu is currently displayed.
Definition: tool_manager.h:531
void RunAction(const TOOL_ACTION &aAction, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:132
void DispatchContextMenu(const TOOL_EVENT &aEvent)
Function DispatchContextMenu() Handles context menu related events.
VECTOR2D m_menuCursor
Right click context menu position.
Definition: tool_manager.h:526
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:236
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:512
EDA_BASE_FRAME * GetEditFrame() const
Definition: tool_manager.h:251
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:503
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.
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:515
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 RunAction(const TOOL_ACTION &aAction, bool aNow=false)
Definition: tool_manager.h:139
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:357
EDA_ITEM * GetModel() const
Definition: tool_manager.h:246
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:520
std::map< TOOL_BASE *, TOOL_STATE * > TOOL_STATE_MAP
Definition: tool_manager.h:61
EDA_ITEM * m_model
Definition: tool_manager.h:517
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:500
int TOOL_ID
Unique identifier for tools.
Definition: tool_base.h:56
Class TOOL_EVENT_LIST.
Definition: tool_event.h:556
TOOL_ID m_menuOwner
Tool currently displaying a popup menu. It is negative when there is no menu displayed.
Definition: tool_manager.h:534
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:156
Class TOOL_EVENT.
Definition: tool_event.h:168
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:519
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:271
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:280
bool isActive(TOOL_BASE *aTool)
Function isActive() Returns information about a tool activation status.
KIGFX::VIEW * m_view
Definition: tool_manager.h:518
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:261
void RunMainStack(TOOL_BASE *aTool, std::function< void()> aFunc)
bool m_warpMouseAfterContextMenu
Definition: tool_manager.h:528
bool isRegistered(TOOL_BASE *aTool) const
Function isRegistered() Returns information about a tool registration status.
Definition: tool_manager.h:458
KIGFX::VIEW_CONTROLS * GetViewControls() const
Definition: tool_manager.h:241
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:523
const char * name
Definition: DXF_plotter.cpp:61
ACTION_MANAGER * GetActionManager()
Definition: tool_manager.h:149
TOOL_STATE * m_activeState
Pointer to the state object corresponding to the currently executed tool.
Definition: tool_manager.h:537
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:367
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:150
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.
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:509
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:223
TOOL_STATE_MAP m_toolState
Index of registered tools current states, associated by tools' objects.
Definition: tool_manager.h:497
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 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:379
std::map< const char *, TOOL_BASE * > m_toolTypes
Index of the registered tools to easily lookup by their type.
Definition: tool_manager.h:506