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 
152  int GetHotKey( const TOOL_ACTION& aAction );
153 
155 
163  TOOL_BASE* FindTool( int aId ) const;
164 
172  TOOL_BASE* FindTool( const std::string& aName ) const;
173 
174  /*
175  * Function GetTool()
176  * Returns the tool of given type or NULL if there is no such tool registered.
177  */
178  template<typename T>
179  T* GetTool()
180  {
181  std::map<const char*, TOOL_BASE*>::iterator tool = m_toolTypes.find( typeid( T ).name() );
182 
183  if( tool != m_toolTypes.end() )
184  return static_cast<T*>( tool->second );
185 
186  return NULL;
187  }
188 
193  void DeactivateTool();
194 
195 
200  bool IsToolActive( TOOL_ID aId ) const;
201 
202 
207  void ResetTools( TOOL_BASE::RESET_REASON aReason );
208 
215  void InitTools();
216 
222  bool ProcessEvent( const TOOL_EVENT& aEvent );
223 
228  inline void PostEvent( const TOOL_EVENT& aEvent )
229  {
230  m_eventQueue.push_back( aEvent );
231  }
232 
237  void SetEnvironment( EDA_ITEM* aModel, KIGFX::VIEW* aView,
238  KIGFX::VIEW_CONTROLS* aViewControls, EDA_BASE_FRAME* aFrame );
239 
240  /* Accessors for the environment objects (view, model, etc.) */
242  {
243  return m_view;
244  }
245 
247  {
248  return m_viewControls;
249  }
250 
251  inline EDA_ITEM* GetModel() const
252  {
253  return m_model;
254  }
255 
256  inline EDA_BASE_FRAME* GetEditFrame() const
257  {
258  return m_frame;
259  }
260 
266  inline int GetCurrentToolId() const
267  {
268  return m_activeTools.empty() ? -1 : m_activeTools.front();
269  }
270 
276  inline TOOL_BASE* GetCurrentTool() const
277  {
278  return FindTool( GetCurrentToolId() );
279  }
280 
286  {
287  auto it = m_toolIdIndex.find( GetCurrentToolId() );
288  return ( it != m_toolIdIndex.end() ) ? it->second : nullptr;
289  }
290 
299  int GetPriority( int aToolId ) const;
300 
305  void ScheduleNextState( TOOL_BASE* aTool, TOOL_STATE_FUNC& aHandler,
306  const TOOL_EVENT_LIST& aConditions );
307 
312  void ClearTransitions( TOOL_BASE* aTool );
313 
314  void RunMainStack( TOOL_BASE* aTool, std::function<void()> aFunc );
315 
319  void UpdateUI( const TOOL_EVENT& aEvent );
320 
326  TOOL_EVENT* ScheduleWait( TOOL_BASE* aTool, const TOOL_EVENT_LIST& aConditions );
327 
338  void ScheduleContextMenu( TOOL_BASE* aTool, ACTION_MENU* aMenu, CONTEXT_MENU_TRIGGER aTrigger );
339 
345  bool SaveClipboard( const std::string& aText );
346 
351  std::string GetClipboard() const;
352 
357  const KIGFX::VC_SETTINGS& GetCurrentToolVC() const;
358 
363  {
364  return m_menuActive;
365  }
366 
373  {
375  }
376 
381  void DispatchContextMenu( const TOOL_EVENT& aEvent );
382 
383 private:
384  typedef std::pair<TOOL_EVENT_LIST, TOOL_STATE_FUNC> TRANSITION;
385 
390  bool dispatchInternal( const TOOL_EVENT& aEvent );
391 
398  bool dispatchHotKey( const TOOL_EVENT& aEvent );
399 
406  bool dispatchActivation( const TOOL_EVENT& aEvent );
407 
414  bool invokeTool( TOOL_BASE* aTool );
415 
423  bool runTool( TOOL_ID aToolId );
424 
432  bool runTool( const std::string& aName );
433 
441  bool runTool( TOOL_BASE* aTool );
442 
443  template <class Parameters>
444  void invokeTool( const std::string& aName, const Parameters& aToolParams );
445 
454  ID_LIST::iterator finishTool( TOOL_STATE* aState );
455 
463  bool isRegistered( TOOL_BASE* aTool ) const
464  {
465  return m_toolState.count( aTool ) > 0;
466  }
467 
475  bool isActive( TOOL_BASE* aTool );
476 
482  void saveViewControls( TOOL_STATE* aState );
483 
488  void applyViewControls( TOOL_STATE* aState );
489 
492  bool processEvent( const TOOL_EVENT& aEvent );
493 
499  void setActiveState( TOOL_STATE* aState );
500 
503 
506 
509 
511  std::map<const char*, TOOL_BASE*> m_toolTypes;
512 
515 
518 
520  std::map<TOOL_ID, OPT<VECTOR2D>> m_cursorSettings;
521 
526 
528  std::list<TOOL_EVENT> m_eventQueue;
529 
532 
534 
537 
540 
543 };
544 
545 #endif
bool m_menuActive
Flag indicating whether a context menu is currently displayed.
Definition: tool_manager.h:536
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:531
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:241
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:517
EDA_BASE_FRAME * GetEditFrame() const
Definition: tool_manager.h:256
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:508
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:520
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
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:362
EDA_ITEM * GetModel() const
Definition: tool_manager.h:251
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:525
std::map< TOOL_BASE *, TOOL_STATE * > TOOL_STATE_MAP
Definition: tool_manager.h:61
EDA_ITEM * m_model
Definition: tool_manager.h:522
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:505
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:539
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:524
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:276
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:285
bool isActive(TOOL_BASE *aTool)
Function isActive() Returns information about a tool activation status.
KIGFX::VIEW * m_view
Definition: tool_manager.h:523
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:266
void RunMainStack(TOOL_BASE *aTool, std::function< void()> aFunc)
bool m_warpMouseAfterContextMenu
Definition: tool_manager.h:533
bool isRegistered(TOOL_BASE *aTool) const
Function isRegistered() Returns information about a tool registration status.
Definition: tool_manager.h:463
KIGFX::VIEW_CONTROLS * GetViewControls() const
Definition: tool_manager.h:246
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:528
const char * name
Definition: DXF_plotter.cpp:61
ACTION_MANAGER * GetActionManager()
Definition: tool_manager.h:154
TOOL_STATE * m_activeState
Pointer to the state object corresponding to the currently executed tool.
Definition: tool_manager.h:542
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:372
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.
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:514
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:228
TOOL_STATE_MAP m_toolState
Index of registered tools current states, associated by tools' objects.
Definition: tool_manager.h:502
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:384
std::map< const char *, TOOL_BASE * > m_toolTypes
Index of the registered tools to easily lookup by their type.
Definition: tool_manager.h:511