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 CONTEXT_MENU;
40 class wxWindow;
41 
50 {
51 private:
52  struct TOOL_STATE;
53 
54 public:
55  TOOL_MANAGER();
56 
57  ~TOOL_MANAGER();
58 
59  // Helper typedefs
60  typedef std::map<TOOL_BASE*, TOOL_STATE*> TOOL_STATE_MAP;
61  typedef std::map<std::string, TOOL_STATE*> NAME_STATE_MAP;
62  typedef std::map<TOOL_ID, TOOL_STATE*> ID_STATE_MAP;
63  typedef std::list<TOOL_ID> ID_LIST;
64 
68  static TOOL_ID MakeToolId( const std::string& aToolName );
69 
76  void RegisterTool( TOOL_BASE* aTool );
77 
85  bool InvokeTool( TOOL_ID aToolId );
86 
94  bool InvokeTool( const std::string& aToolName );
95 
103  void RegisterAction( TOOL_ACTION* aAction );
104 
111  void UnregisterAction( TOOL_ACTION* aAction );
112 
124  template<typename T>
125  bool RunAction( const std::string& aActionName, bool aNow = false, T aParam = NULL )
126  {
127  return RunAction( aActionName, aNow, reinterpret_cast<void*>( aParam ) );
128  }
129 
130  bool RunAction( const std::string& aActionName, bool aNow, void* aParam );
131 
132  bool RunAction( const std::string& aActionName, bool aNow = false )
133  {
134  return RunAction( aActionName, aNow, (void*) NULL );
135  }
136 
147  template<typename T>
148  void RunAction( const TOOL_ACTION& aAction, bool aNow = false, T aParam = NULL )
149  {
150  RunAction( aAction, aNow, reinterpret_cast<void*>( aParam ) );
151  }
152 
153  void RunAction( const TOOL_ACTION& aAction, bool aNow, void* aParam );
154 
155  void RunAction( const TOOL_ACTION& aAction, bool aNow = false )
156  {
157  RunAction( aAction, aNow, (void*) NULL );
158  }
159 
161  int GetHotKey( const TOOL_ACTION& aAction );
162 
164  void UpdateHotKeys();
165 
173  TOOL_BASE* FindTool( int aId ) const;
174 
182  TOOL_BASE* FindTool( const std::string& aName ) const;
183 
184  /*
185  * Function GetTool()
186  * Returns the tool of given type or NULL if there is no such tool registered.
187  */
188  template<typename T>
190  {
191  std::map<const char*, TOOL_BASE*>::iterator tool = m_toolTypes.find( typeid( T ).name() );
192 
193  if( tool != m_toolTypes.end() )
194  return static_cast<T*>( tool->second );
195 
196  return NULL;
197  }
198 
203  void DeactivateTool();
204 
209  void ResetTools( TOOL_BASE::RESET_REASON aReason );
210 
217  void InitTools();
218 
223  void ProcessEvent( const TOOL_EVENT& aEvent );
224 
229  inline void PostEvent( const TOOL_EVENT& aEvent )
230  {
231  m_eventQueue.push_back( aEvent );
232  }
233 
239  void SetEnvironment( EDA_ITEM* aModel, KIGFX::VIEW* aView,
240  KIGFX::VIEW_CONTROLS* aViewControls, wxWindow* aFrame );
241 
242  /* Accessors for the environment objects (view, model, etc.) */
244  {
245  return m_view;
246  }
247 
249  {
250  return m_viewControls;
251  }
252 
253  inline EDA_ITEM* GetModel() const
254  {
255  return m_model;
256  }
257 
258  inline wxWindow* GetEditFrame() const
259  {
260  return m_editFrame;
261  }
262 
268  inline int GetCurrentToolId() const
269  {
270  return m_activeTools.empty() ? -1 : m_activeTools.front();
271  }
272 
278  inline TOOL_BASE* GetCurrentTool() const
279  {
280  return FindTool( GetCurrentToolId() );
281  }
282 
288  {
289  auto it = m_toolIdIndex.find( GetCurrentToolId() );
290  return ( it != m_toolIdIndex.end() ) ? it->second : nullptr;
291  }
292 
301  int GetPriority( int aToolId ) const;
302 
307  void ScheduleNextState( TOOL_BASE* aTool, TOOL_STATE_FUNC& aHandler,
308  const TOOL_EVENT_LIST& aConditions );
309 
310  void RunMainStack( TOOL_BASE* aTool, std::function<void()> aFunc );
311 
318  const TOOL_EVENT_LIST& aConditions );
319 
330  void ScheduleContextMenu( TOOL_BASE* aTool, CONTEXT_MENU* aMenu,
331  CONTEXT_MENU_TRIGGER aTrigger );
332 
336  void PassEvent()
337  {
338  m_passEvent = true;
339  }
340 
346  bool SaveClipboard( const std::string& aText );
347 
352  std::string GetClipboard() const;
353 
354 private:
355  typedef std::pair<TOOL_EVENT_LIST, TOOL_STATE_FUNC> TRANSITION;
356 
361  void dispatchInternal( const TOOL_EVENT& aEvent );
362 
369  bool dispatchStandardEvents( const TOOL_EVENT& aEvent );
370 
377  bool dispatchActivation( const TOOL_EVENT& aEvent );
378 
383  void dispatchContextMenu( const TOOL_EVENT& aEvent );
384 
391  bool invokeTool( TOOL_BASE* aTool );
392 
400  bool runTool( TOOL_ID aToolId );
401 
409  bool runTool( const std::string& aName );
410 
418  bool runTool( TOOL_BASE* aTool );
419 
420  template <class Parameters>
421  void invokeTool( const std::string& aName, const Parameters& aToolParams );
422 
431  ID_LIST::iterator finishTool( TOOL_STATE* aState );
432 
440  bool isRegistered( TOOL_BASE* aTool ) const
441  {
442  return m_toolState.count( aTool ) > 0;
443  }
444 
452  bool isActive( TOOL_BASE* aTool );
453 
459  void saveViewControls( TOOL_STATE* aState );
460 
465  void applyViewControls( TOOL_STATE* aState );
466 
471  void pushViewControls();
472 
477  void popViewControls();
478 
480  void processEvent( const TOOL_EVENT& aEvent );
481 
483  TOOL_STATE_MAP m_toolState;
484 
486  NAME_STATE_MAP m_toolNameIndex;
487 
489  ID_STATE_MAP m_toolIdIndex;
490 
492  std::map<const char*, TOOL_BASE*> m_toolTypes;
493 
495  ID_LIST m_activeTools;
496 
499 
502 
506  wxWindow* m_editFrame;
507 
509  std::list<TOOL_EVENT> m_eventQueue;
510 
512  std::stack<KIGFX::VC_SETTINGS> m_vcStack;
513 
516 
519 };
520 
521 #endif
bool m_menuActive
Flag indicating whether a context menu is currently displayed.
Definition: tool_manager.h:518
void RunAction(const TOOL_ACTION &aAction, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:148
TOOL_BASE * FindTool(int aId) const
Function FindTool() Searches for a tool with given ID.
int GetHotKey(const TOOL_ACTION &aAction)
>
ACTION_MANAGER * m_actionMgr
Instance of ACTION_MANAGER that handles TOOL_ACTIONs.
Definition: tool_manager.h:498
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:268
static TOOL_ID MakeToolId(const std::string &aToolName)
Generates a unique ID from for a tool with given name.
ID_STATE_MAP m_toolIdIndex
Index of the registered tools current states, associated by tools' ID numbers.
Definition: tool_manager.h:489
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...
void RegisterAction(TOOL_ACTION *aAction)
Function RegisterAction() Registers an action that can be used to control tools (eg.
bool dispatchStandardEvents(const TOOL_EVENT &aEvent)
Function dispatchStandardEvents() Handles specific events, that are intended for TOOL_MANAGER rather ...
bool SaveClipboard(const std::string &aText)
Stores an information to the system clipboard.
T
enum T contains all this lexer's tokens.
std::map< TOOL_ID, TOOL_STATE * > ID_STATE_MAP
Definition: tool_manager.h:62
Class CONTEXT_MENU.
Definition: context_menu.h:44
VIEW_CONTROLS class definition.
wxWindow * GetEditFrame() const
Definition: tool_manager.h:258
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:61
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:125
void RunAction(const TOOL_ACTION &aAction, bool aNow=false)
Definition: tool_manager.h:155
Class ACTION_MANAGER.
void ScheduleContextMenu(TOOL_BASE *aTool, CONTEXT_MENU *aMenu, CONTEXT_MENU_TRIGGER aTrigger)
Sets behaviour of the tool's context popup menu.
void applyViewControls(TOOL_STATE *aState)
Function applyViewControls() Applies VIEW_CONTROLS settings stored in a TOOL_STATE object...
void dispatchContextMenu(const TOOL_EVENT &aEvent)
Function dispatchContextMenu() Handles context menu related events.
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...
bool m_passEvent
Flag saying if the currently processed event should be passed to other tools.
Definition: tool_manager.h:515
std::map< TOOL_BASE *, TOOL_STATE * > TOOL_STATE_MAP
Definition: tool_manager.h:60
KIGFX::VIEW_CONTROLS * GetViewControls() const
Definition: tool_manager.h:248
void PassEvent()
Allows a tool to pass the already handled event to the next tool on the stack.
Definition: tool_manager.h:336
EDA_ITEM * m_model
Definition: tool_manager.h:503
Class TOOL_MANAGER.
Definition: tool_manager.h:49
NAME_STATE_MAP m_toolNameIndex
Index of the registered tools current states, associated by tools' names.
Definition: tool_manager.h:486
int TOOL_ID
Unique identifier for tools.
Definition: tool_base.h:57
Class TOOL_EVENT_LIST.
Definition: tool_event.h:468
wxWindow * m_editFrame
Definition: tool_manager.h:506
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, wxWindow *aFrame)
Sets the work environment (model, view, view controls and the parent window).
CONTEXT_MENU_TRIGGER
Defines when a context menu is opened.
Definition: tool_event.h:150
void dispatchInternal(const TOOL_EVENT &aEvent)
Function dispatchInternal Passes an event at first to the active tools, then to all others...
Class TOOL_EVENT.
Definition: tool_event.h:162
void pushViewControls()
Function pushViewControls() Stores the current VIEW_CONTROLS settings on the stack.
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 (...
Definition: view_controls.h:94
int GetPriority(int aToolId) const
Returns priority of a given tool.
Struct describing the current execution state of a TOOL.
KIGFX::VIEW_CONTROLS * m_viewControls
Definition: tool_manager.h:505
TOOL_STATE * GetCurrentToolState() const
Returns the TOOL_STATE object representing the state of the active tool.
Definition: tool_manager.h:287
std::function< int(const TOOL_EVENT &)> TOOL_STATE_FUNC
Definition: tool_base.h:59
bool isActive(TOOL_BASE *aTool)
Function isActive() Returns information about a tool activation status.
KIGFX::VIEW * m_view
Definition: tool_manager.h:504
std::string GetClipboard() const
Returns the information currently stored in the system clipboard.
void RunMainStack(TOOL_BASE *aTool, std::function< void()> aFunc)
std::stack< KIGFX::VC_SETTINGS > m_vcStack
VIEW_CONTROLS settings stack
Definition: tool_manager.h:512
boost::optional< VECTOR2D > m_origCursor
Original cursor position, if overridden by the context menu handler.
Definition: tool_manager.h:501
void popViewControls()
Function pushViewControls() Restores VIEW_CONTROLS settings from the stack.
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:509
EDA_ITEM * GetModel() const
Definition: tool_manager.h:253
boost::optional< 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.
Class TOOL_BASE.
Definition: tool_base.h:68
Class TOOL_ACTION.
Definition: tool_action.h:46
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:278
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
bool isRegistered(TOOL_BASE *aTool) const
Function isRegistered() Returns information about a tool registration status.
Definition: tool_manager.h:440
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:80
void processEvent(const TOOL_EVENT &aEvent)
Main function for event processing.
void InitTools()
Function InitTools() Initializes all registered tools.
void ProcessEvent(const TOOL_EVENT &aEvent)
Propagates an event to tools that requested events of matching type(s).
Class VIEW.
Definition: view.h:58
ID_LIST m_activeTools
Stack of the active tools.
Definition: tool_manager.h:495
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:243
bool RunAction(const std::string &aActionName, bool aNow=false)
Definition: tool_manager.h:132
std::list< TOOL_ID > ID_LIST
Definition: tool_manager.h:63
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:229
TOOL_STATE_MAP m_toolState
Index of registered tools current states, associated by tools' objects.
Definition: tool_manager.h:483
void saveViewControls(TOOL_STATE *aState)
Function saveViewControls() Saves the VIEW_CONTROLS settings to the tool state object.
void UpdateHotKeys()
>
bool invokeTool(TOOL_BASE *aTool)
Function invokeTool() Invokes a tool by sending a proper event (in contrary to runTool, which makes the tool run for real).
void RegisterTool(TOOL_BASE *aTool)
Function RegisterTool() Adds a tool to the manager set and sets it up.
void UnregisterAction(TOOL_ACTION *aAction)
Function UnregisterAction() Unregisters an action, so it is no longer active.
std::pair< TOOL_EVENT_LIST, TOOL_STATE_FUNC > TRANSITION
Definition: tool_manager.h:355
std::map< const char *, TOOL_BASE * > m_toolTypes
Index of the registered tools to easily lookup by their type.
Definition: tool_manager.h:492