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 * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 * @author Maciej Suminski <[email protected]>
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
28#ifndef __TOOL_MANAGER_H
29#define __TOOL_MANAGER_H
30
31#include <typeinfo>
32#include <map>
33#include <list>
34#include <stack>
35
36#include <tool/tool_base.h>
37#include <tool/tool_event.h>
38#include <view/view_controls.h>
39
40class TOOLS_HOLDER;
41class TOOL_ACTION;
42class TOOL_BASE;
43class ACTION_MANAGER;
44class ACTION_MENU;
46
47
55{
56private:
57 struct TOOL_STATE;
58
59public:
61
63
64 // Helper typedefs
65 typedef std::map<TOOL_BASE*, TOOL_STATE*> TOOL_STATE_MAP;
66 typedef std::map<std::string, TOOL_STATE*> NAME_STATE_MAP;
67 typedef std::map<TOOL_ID, TOOL_STATE*> ID_STATE_MAP;
68 typedef std::list<TOOL_ID> ID_LIST;
69
73 static TOOL_ID MakeToolId( const std::string& aToolName );
74
81 void RegisterTool( TOOL_BASE* aTool );
82
89 bool InvokeTool( TOOL_ID aToolId );
90
97 bool InvokeTool( const std::string& aToolName );
98
103 void ShutdownAllTools();
104
111 void ShutdownTool( TOOL_BASE* aTool );
112
119 void ShutdownTool( TOOL_ID aToolId );
120
127 void ShutdownTool( const std::string& aToolName );
128
141 template<typename T>
142 bool RunAction( const std::string& aActionName, bool aNow = false, T aParam = NULL )
143 {
144 return RunAction( aActionName, aNow, reinterpret_cast<void*>( aParam ) );
145 }
146
147 bool RunAction( const std::string& aActionName, bool aNow, void* aParam );
148
149 bool RunAction( const std::string& aActionName, bool aNow = false )
150 {
151 return RunAction( aActionName, aNow, (void*) NULL );
152 }
153
167 template <typename T>
168 bool RunAction( const TOOL_ACTION& aAction, bool aNow = false, T aParam = NULL )
169 {
170 return RunAction( aAction, aNow, reinterpret_cast<void*>( aParam ) );
171 }
172
173 bool RunAction( const TOOL_ACTION& aAction, bool aNow, void* aParam );
174
175 bool RunAction( const TOOL_ACTION& aAction, bool aNow = false )
176 {
177 return RunAction( aAction, aNow, (void*) NULL );
178 }
179
183 void CancelTool();
184
191 void PrimeTool( const VECTOR2D& aPosition );
192
194 int GetHotKey( const TOOL_ACTION& aAction ) const;
195
197
204 TOOL_BASE* FindTool( int aId ) const;
205
212 TOOL_BASE* FindTool( const std::string& aName ) const;
213
214 /*
215 * Return the tool of given type or nullptr if there is no such tool registered.
216 */
217 template<typename T>
219 {
220 std::map<const char*, TOOL_BASE*>::iterator tool = m_toolTypes.find( typeid( T ).name() );
221
222 if( tool != m_toolTypes.end() )
223 return static_cast<T*>( tool->second );
224
225 return nullptr;
226 }
227
228 /*
229 * Return all registered tools.
230 */
231 std::vector<TOOL_BASE*> Tools() { return m_toolOrder; }
232
236 void DeactivateTool();
237
238
242 bool IsToolActive( TOOL_ID aId ) const;
243
244
248 void ResetTools( TOOL_BASE::RESET_REASON aReason );
249
256 void InitTools();
257
264 bool ProcessEvent( const TOOL_EVENT& aEvent );
265
271 void PostEvent( const TOOL_EVENT& aEvent );
272
278 void SetEnvironment( EDA_ITEM* aModel, KIGFX::VIEW* aView,
279 KIGFX::VIEW_CONTROLS* aViewControls, APP_SETTINGS_BASE* aSettings,
280 TOOLS_HOLDER* aFrame );
281
282 /*
283 * Accessors for the environment objects (view, model, etc.)
284 */
285 KIGFX::VIEW* GetView() const { return m_view; }
286
288
291
292 EDA_ITEM* GetModel() const { return m_model; }
293
295
296 TOOLS_HOLDER* GetToolHolder() const { return m_frame; }
297
304 inline int GetCurrentToolId() const
305 {
306 return m_activeTools.empty() ? -1 : m_activeTools.front();
307 }
308
315 inline TOOL_BASE* GetCurrentTool() const
316 {
317 return FindTool( GetCurrentToolId() );
318 }
319
325 {
326 auto it = m_toolIdIndex.find( GetCurrentToolId() );
327 return ( it != m_toolIdIndex.end() ) ? it->second : nullptr;
328 }
329
340 int GetPriority( int aToolId ) const;
341
348 void ScheduleNextState( TOOL_BASE* aTool, TOOL_STATE_FUNC& aHandler,
349 const TOOL_EVENT_LIST& aConditions );
350
356 void ClearTransitions( TOOL_BASE* aTool );
357
358 void RunMainStack( TOOL_BASE* aTool, std::function<void()> aFunc );
359
363 void UpdateUI( const TOOL_EVENT& aEvent );
364
370 TOOL_EVENT* ScheduleWait( TOOL_BASE* aTool, const TOOL_EVENT_LIST& aConditions );
371
383 void ScheduleContextMenu( TOOL_BASE* aTool, ACTION_MENU* aMenu, CONTEXT_MENU_TRIGGER aTrigger );
384
392 bool SaveClipboard( const std::string& aTextUTF8 );
393
402 std::string GetClipboardUTF8() const;
403
409
414 {
415 return m_menuActive;
416 }
417
425 {
427 }
428
432 void DispatchContextMenu( const TOOL_EVENT& aEvent );
433
440 bool DispatchHotKey( const TOOL_EVENT& aEvent );
441
443 {
444 return m_menuCursor;
445 }
446
447private:
448 typedef std::pair<TOOL_EVENT_LIST, TOOL_STATE_FUNC> TRANSITION;
449
453 bool dispatchInternal( TOOL_EVENT& aEvent );
454
461 bool dispatchActivation( const TOOL_EVENT& aEvent );
462
469 bool invokeTool( TOOL_BASE* aTool );
470
479 bool runTool( TOOL_BASE* aTool );
480
489 ID_LIST::iterator finishTool( TOOL_STATE* aState );
490
497 bool isRegistered( TOOL_BASE* aTool ) const
498 {
499 return m_toolState.count( aTool ) > 0;
500 }
501
508 bool isActive( TOOL_BASE* aTool ) const;
509
515 void saveViewControls( TOOL_STATE* aState );
516
520 void applyViewControls( const TOOL_STATE* aState );
521
527 bool processEvent( const TOOL_EVENT& aEvent );
528
535 void setActiveState( TOOL_STATE* aState );
536
538 std::vector<TOOL_BASE*> m_toolOrder;
539
542
545
548
550 std::map<const char*, TOOL_BASE*> m_toolTypes;
551
554
557
559 std::map<TOOL_ID, std::optional<VECTOR2D>> m_cursorSettings;
560
566
568 std::list<TOOL_EVENT> m_eventQueue;
569
572
574
577
580
583
586};
587
588#endif // __TOOL_MANAGER_H
const char * name
Definition: DXF_plotter.cpp:56
Manage TOOL_ACTION objects.
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:49
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:101
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
An interface for classes handling user events controlling the view behavior such as zooming,...
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:69
Represent a single user action.
Definition: tool_action.h:68
Base abstract interface for all kinds of tools.
Definition: tool_base.h:66
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
A list of TOOL_EVENTs, with overloaded || operators allowing for concatenating TOOL_EVENTs with littl...
Definition: tool_event.h:564
Generic, UI-independent tool event.
Definition: tool_event.h:156
Master controller class:
Definition: tool_manager.h:55
void applyViewControls(const TOOL_STATE *aState)
Apply #VIEW_CONTROLS settings stored in a #TOOL_STATE object.
int GetPriority(int aToolId) const
Return priority of a given tool.
std::map< std::string, TOOL_STATE * > NAME_STATE_MAP
Definition: tool_manager.h:66
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagate an event to tools that requested events of matching type(s).
TOOL_STATE * m_activeState
True if the tool manager is shutting down (don't process additional events)
Definition: tool_manager.h:582
void UpdateUI(const TOOL_EVENT &aEvent)
Update the status bar and synchronizes toolbars.
std::map< TOOL_ID, std::optional< VECTOR2D > > m_cursorSettings
Definition: tool_manager.h:559
void PostEvent(const TOOL_EVENT &aEvent)
Put an event to the event queue to be processed at the end of event processing cycle.
void ScheduleNextState(TOOL_BASE *aTool, TOOL_STATE_FUNC &aHandler, const TOOL_EVENT_LIST &aConditions)
Define a state transition.
std::map< const char *, TOOL_BASE * > m_toolTypes
Stack of the active tools.
Definition: tool_manager.h:550
bool isRegistered(TOOL_BASE *aTool) const
Return information about a tool registration status.
Definition: tool_manager.h:497
APP_SETTINGS_BASE * m_settings
Queue that stores events to be processed at the end of the event processing cycle.
Definition: tool_manager.h:565
TOOL_STATE * GetCurrentToolState() const
Return the #TOOL_STATE object representing the state of the active tool.
Definition: tool_manager.h:324
VECTOR2D GetMenuCursorPos() const
Definition: tool_manager.h:442
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:142
VECTOR2D GetCursorPosition() const
std::list< TOOL_EVENT > m_eventQueue
Right click context menu position.
Definition: tool_manager.h:568
std::pair< TOOL_EVENT_LIST, TOOL_STATE_FUNC > TRANSITION
Definition: tool_manager.h:448
void setActiveState(TOOL_STATE *aState)
Save the previous active state and sets a new one.
void DeactivateTool()
Deactivate the currently active tool.
bool dispatchInternal(TOOL_EVENT &aEvent)
Pass an event at first to the active tools, then to all others.
void PrimeTool(const VECTOR2D &aPosition)
"Prime" a tool by sending a cursor left-click event with the mouse position set to the passed in posi...
bool runTool(TOOL_BASE *aTool)
Make a tool active, so it can receive events and react to them.
bool InvokeTool(TOOL_ID aToolId)
Call a tool by sending a tool activation event to tool of given ID.
NAME_STATE_MAP m_toolNameIndex
Index of the registered tools current states, associated by tools' ID numbers.
Definition: tool_manager.h:544
TOOLS_HOLDER * m_frame
Definition: tool_manager.h:564
std::string GetClipboardUTF8() const
Return the information currently stored in the system clipboard.
void CancelTool()
Send a cancel event to the tool currently at the top of the tool stack.
bool m_warpMouseAfterContextMenu
Flag indicating whether a context menu is currently displayed.
Definition: tool_manager.h:573
KIGFX::VIEW_CONTROLS * m_viewControls
Definition: tool_manager.h:563
ACTION_MANAGER * m_actionMgr
Original cursor position, if overridden by the context menu handler.
Definition: tool_manager.h:556
int GetCurrentToolId() const
Return id of the tool that is on the top of the active tools stack (was invoked the most recently).
Definition: tool_manager.h:304
const KIGFX::VC_SETTINGS & GetCurrentToolVC() const
Return the view controls settings for the current tool or the general settings if there is no active ...
void RunMainStack(TOOL_BASE *aTool, std::function< void()> aFunc)
TOOLS_HOLDER * GetToolHolder() const
Definition: tool_manager.h:296
ACTION_MANAGER * GetActionManager() const
Definition: tool_manager.h:196
VECTOR2D GetMousePosition() const
TOOL_BASE * GetCurrentTool() const
Return the tool that is on the top of the active tools stack (was invoked the most recently).
Definition: tool_manager.h:315
bool processEvent(const TOOL_EVENT &aEvent)
Main function for event processing.
std::list< TOOL_ID > ID_LIST
Definition: tool_manager.h:68
KIGFX::VIEW_CONTROLS * GetViewControls() const
Definition: tool_manager.h:287
ID_LIST::iterator finishTool(TOOL_STATE *aState)
Deactivate a tool and does the necessary clean up.
APP_SETTINGS_BASE * GetSettings() const
Definition: tool_manager.h:294
bool dispatchActivation(const TOOL_EVENT &aEvent)
Check if it is a valid activation event and invokes a proper tool.
ID_STATE_MAP m_toolIdIndex
Index of the registered tools to easily lookup by their type.
Definition: tool_manager.h:547
TOOL_ID m_menuOwner
Pointer to the state object corresponding to the currently executed tool.
Definition: tool_manager.h:579
KIGFX::VIEW * m_view
Definition: tool_manager.h:562
void ClearTransitions(TOOL_BASE *aTool)
Clear the state transition map for a tool.
bool RunAction(const TOOL_ACTION &aAction, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:168
bool SaveClipboard(const std::string &aTextUTF8)
Store information to the system clipboard.
bool m_shuttingDown
Definition: tool_manager.h:585
void saveViewControls(TOOL_STATE *aState)
Save the #VIEW_CONTROLS settings to the tool state object.
int GetHotKey(const TOOL_ACTION &aAction) const
bool m_menuActive
Tool currently displaying a popup menu. It is negative when there is no menu displayed.
Definition: tool_manager.h:576
bool IsContextMenuActive() const
True while processing a context menu.
Definition: tool_manager.h:413
TOOL_STATE_MAP m_toolState
Index of the registered tools current states, associated by tools' names.
Definition: tool_manager.h:541
void ShutdownTool(TOOL_BASE *aTool)
Shutdown the specified tool by waking it up with a null event to terminate the processing loop.
std::map< TOOL_BASE *, TOOL_STATE * > TOOL_STATE_MAP
Definition: tool_manager.h:65
EDA_ITEM * GetModel() const
Definition: tool_manager.h:292
void VetoContextMenuMouseWarp()
Disable mouse warping after the current context menu is closed.
Definition: tool_manager.h:424
ID_LIST m_activeTools
Instance of ACTION_MANAGER that handles TOOL_ACTIONs.
Definition: tool_manager.h:553
void ResetTools(TOOL_BASE::RESET_REASON aReason)
Reset all tools (i.e.
EDA_ITEM * m_model
Definition: tool_manager.h:561
std::map< TOOL_ID, TOOL_STATE * > ID_STATE_MAP
Definition: tool_manager.h:67
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:285
bool RunAction(const TOOL_ACTION &aAction, bool aNow=false)
Definition: tool_manager.h:175
bool DispatchHotKey(const TOOL_EVENT &aEvent)
Handle specific events, that are intended for TOOL_MANAGER rather than tools.
static TOOL_ID MakeToolId(const std::string &aToolName)
Generates a unique ID from for a tool with given name.
bool invokeTool(TOOL_BASE *aTool)
Invoke a tool by sending a proper event (in contrary to runTool, which makes the tool run for real).
void DispatchContextMenu(const TOOL_EVENT &aEvent)
Handle context menu related events.
std::vector< TOOL_BASE * > m_toolOrder
Index of registered tools current states, associated by tools' objects.
Definition: tool_manager.h:538
TOOL_BASE * FindTool(int aId) const
Search for a tool with given ID.
std::vector< TOOL_BASE * > Tools()
Definition: tool_manager.h:231
void ScheduleContextMenu(TOOL_BASE *aTool, ACTION_MENU *aMenu, CONTEXT_MENU_TRIGGER aTrigger)
Set behavior of the tool's context popup menu.
TOOL_EVENT * ScheduleWait(TOOL_BASE *aTool, const TOOL_EVENT_LIST &aConditions)
Pause execution of a given tool until one or more events matching aConditions arrives.
bool IsToolActive(TOOL_ID aId) const
Return true if a tool with given id is active (executing)
bool isActive(TOOL_BASE *aTool) const
Return information about a tool activation status.
void RegisterTool(TOOL_BASE *aTool)
Add a tool to the manager set and sets it up.
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, APP_SETTINGS_BASE *aSettings, TOOLS_HOLDER *aFrame)
Set the work environment (model, view, view controls and the parent window).
void InitTools()
Initializes all registered tools.
bool RunAction(const std::string &aActionName, bool aNow=false)
Definition: tool_manager.h:149
VECTOR2D m_menuCursor
Definition: tool_manager.h:571
void ShutdownAllTools()
Shutdown all tools with a currently registered event loop in this tool manager by waking them up with...
Structure to keep VIEW_CONTROLS settings for easy store/restore operations.
Definition: view_controls.h:42
Struct describing the current execution state of a TOOL.
std::function< int(const TOOL_EVENT &)> TOOL_STATE_FUNC
Definition: tool_base.h:58
int TOOL_ID
Unique identifier for tools.
Definition: tool_base.h:56
CONTEXT_MENU_TRIGGER
Defines when a context menu is opened.
Definition: tool_event.h:146