KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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 <list>
32#include <map>
33#include <stack>
34#include <vector>
35#include <typeinfo>
36#include <type_traits>
37
38#include <tool/tool_base.h>
39#include <tool/tool_event.h>
40
41namespace KIGFX
42{
43class VIEW_CONTROLS;
44struct VC_SETTINGS;
45}
46
47class COMMIT;
48class TOOLS_HOLDER;
49class TOOL_ACTION;
50class ACTION_MANAGER;
51class ACTION_MENU;
53
54
62{
63private:
64 struct TOOL_STATE;
65
66public:
68
70
71 // Helper typedefs
72 typedef std::map<TOOL_BASE*, TOOL_STATE*> TOOL_STATE_MAP;
73 typedef std::map<std::string, TOOL_STATE*> NAME_STATE_MAP;
74 typedef std::map<TOOL_ID, TOOL_STATE*> ID_STATE_MAP;
75 typedef std::list<TOOL_ID> ID_LIST;
76
80 static TOOL_ID MakeToolId( const std::string& aToolName );
81
88 void RegisterTool( TOOL_BASE* aTool );
89
96 bool InvokeTool( TOOL_ID aToolId );
97
104 bool InvokeTool( const std::string& aToolName );
105
110 void ShutdownAllTools();
111
118 void ShutdownTool( TOOL_BASE* aTool );
119
126 void ShutdownTool( TOOL_ID aToolId );
127
134 void ShutdownTool( const std::string& aToolName );
135
149 template<typename T, std::enable_if_t<!std::is_convertible_v<T*, COMMIT*>>* = nullptr>
150 bool RunAction( const std::string& aActionName, T aParam )
151 {
152 // Use a cast to ensure the proper type is stored inside the parameter
153 std::any a( static_cast<T>( aParam ) );
154
155 return doRunAction( aActionName, true, a, nullptr );
156 }
157
158 bool RunAction( const std::string& aActionName )
159 {
160 // Default initialize the parameter argument to an empty std::any
161 std::any a;
162
163 return doRunAction( aActionName, true, a, nullptr );
164 }
165
177 template<typename T, std::enable_if_t<!std::is_convertible_v<T, COMMIT*>>* = nullptr>
178 bool RunAction( const TOOL_ACTION& aAction, T aParam )
179 {
180 // Use a cast to ensure the proper type is stored inside the parameter
181 std::any a( static_cast<T>( aParam ) );
182
183 return doRunAction( aAction, true, a, nullptr );
184 }
185
196 template<typename T>
197 bool RunSynchronousAction( const TOOL_ACTION& aAction, COMMIT* aCommit, T aParam )
198 {
199 // Use a cast to ensure the proper type is stored inside the parameter
200 std::any a( static_cast<T>( aParam ) );
201
202 return doRunAction( aAction, true, a, aCommit );
203 }
204
205 bool RunSynchronousAction( const TOOL_ACTION& aAction, COMMIT* aCommit )
206 {
207 // Default initialize the parameter argument to an empty std::any
208 std::any a;
209
210 return doRunAction( aAction, true, a, aCommit );
211 }
212
213 bool RunAction( const TOOL_ACTION& aAction )
214 {
215 // Default initialize the parameter argument to an empty std::any
216 std::any a;
217
218 return doRunAction( aAction, true, a, nullptr );
219 }
220
234 template<typename T>
235 bool PostAction( const std::string& aActionName, T aParam )
236 {
237 // Use a cast to ensure the proper type is stored inside the parameter
238 std::any a( static_cast<T>( aParam ) );
239
240 return doRunAction( aActionName, false, a, nullptr );
241 }
242
243 bool PostAction( const std::string& aActionName )
244 {
245 // Default initialize the parameter argument to an empty std::any
246 std::any a;
247
248 return doRunAction( aActionName, false, a, nullptr );
249 }
250
261 template<typename T>
262 bool PostAction( const TOOL_ACTION& aAction, T aParam )
263 {
264 // Use a cast to ensure the proper type is stored inside the parameter
265 std::any a( static_cast<T>( aParam ) );
266
267 return doRunAction( aAction, false, a, nullptr );
268 }
269
270 void PostAction( const TOOL_ACTION& aAction )
271 {
272 // Default initialize the parameter argument to an empty std::any
273 std::any a;
274
275 doRunAction( aAction, false, a, nullptr );
276 }
277
278 bool PostAction( const TOOL_ACTION& aAction, COMMIT* aCommit )
279 {
280 // Default initialize the parameter argument to an empty std::any
281 std::any a;
282
283 return doRunAction( aAction, false, a, aCommit );
284 }
285
289 void CancelTool();
290
297 void PrimeTool( const VECTOR2D& aPosition );
298
300 int GetHotKey( const TOOL_ACTION& aAction ) const;
301
303
310 TOOL_BASE* FindTool( int aId ) const;
311
318 TOOL_BASE* FindTool( const std::string& aName ) const;
319
320 /*
321 * Return the tool of given type or nullptr if there is no such tool registered.
322 */
323 template<typename T>
325 {
326 std::map<const char*, TOOL_BASE*>::iterator tool = m_toolTypes.find( typeid( T ).name() );
327
328 if( tool != m_toolTypes.end() )
329 return static_cast<T*>( tool->second );
330
331 return nullptr;
332 }
333
334 /*
335 * Return all registered tools.
336 */
337 std::vector<TOOL_BASE*> Tools() { return m_toolOrder; }
338
342 void DeactivateTool();
343
344
348 bool IsToolActive( TOOL_ID aId ) const;
349
350
354 void ResetTools( TOOL_BASE::RESET_REASON aReason );
355
362 void InitTools();
363
370 bool ProcessEvent( const TOOL_EVENT& aEvent );
371
377 void PostEvent( const TOOL_EVENT& aEvent );
378
384 void SetEnvironment( EDA_ITEM* aModel, KIGFX::VIEW* aView,
385 KIGFX::VIEW_CONTROLS* aViewControls, APP_SETTINGS_BASE* aSettings,
386 TOOLS_HOLDER* aFrame );
387
388 /*
389 * Accessors for the environment objects (view, model, etc.)
390 */
391 KIGFX::VIEW* GetView() const { return m_view; }
392
394
397
398 EDA_ITEM* GetModel() const { return m_model; }
399
401
402 TOOLS_HOLDER* GetToolHolder() const { return m_frame; }
403
410 inline int GetCurrentToolId() const
411 {
412 return m_activeTools.empty() ? -1 : m_activeTools.front();
413 }
414
421 inline TOOL_BASE* GetCurrentTool() const
422 {
423 return FindTool( GetCurrentToolId() );
424 }
425
431 {
432 auto it = m_toolIdIndex.find( GetCurrentToolId() );
433 return ( it != m_toolIdIndex.end() ) ? it->second : nullptr;
434 }
435
446 int GetPriority( int aToolId ) const;
447
454 void ScheduleNextState( TOOL_BASE* aTool, TOOL_STATE_FUNC& aHandler,
455 const TOOL_EVENT_LIST& aConditions );
456
462 void ClearTransitions( TOOL_BASE* aTool );
463
464 void RunMainStack( TOOL_BASE* aTool, std::function<void()> aFunc );
465
469 void UpdateUI( const TOOL_EVENT& aEvent );
470
476 TOOL_EVENT* ScheduleWait( TOOL_BASE* aTool, const TOOL_EVENT_LIST& aConditions );
477
489 void ScheduleContextMenu( TOOL_BASE* aTool, ACTION_MENU* aMenu, CONTEXT_MENU_TRIGGER aTrigger );
490
498 bool SaveClipboard( const std::string& aTextUTF8 );
499
508 std::string GetClipboardUTF8() const;
509
515
520 {
521 return m_menuActive;
522 }
523
531 {
533 }
534
538 void DispatchContextMenu( const TOOL_EVENT& aEvent );
539
546 bool DispatchHotKey( const TOOL_EVENT& aEvent );
547
549 {
550 return m_menuCursor;
551 }
552
553private:
554 typedef std::pair<TOOL_EVENT_LIST, TOOL_STATE_FUNC> TRANSITION;
555
559 bool doRunAction( const TOOL_ACTION& aAction, bool aNow, const std::any& aParam, COMMIT* aCommit );
560 bool doRunAction( const std::string& aActionName, bool aNow, const std::any& aParam, COMMIT* aCommit );
561
565 bool dispatchInternal( TOOL_EVENT& aEvent );
566
573 bool dispatchActivation( const TOOL_EVENT& aEvent );
574
581 bool invokeTool( TOOL_BASE* aTool );
582
591 bool runTool( TOOL_BASE* aTool );
592
601 ID_LIST::iterator finishTool( TOOL_STATE* aState );
602
609 bool isRegistered( TOOL_BASE* aTool ) const
610 {
611 return m_toolState.count( aTool ) > 0;
612 }
613
620 bool isActive( TOOL_BASE* aTool ) const;
621
627 void saveViewControls( TOOL_STATE* aState );
628
632 void applyViewControls( const TOOL_STATE* aState );
633
639 bool processEvent( const TOOL_EVENT& aEvent );
640
647 void setActiveState( TOOL_STATE* aState );
648
649private:
651 std::vector<TOOL_BASE*> m_toolOrder;
652
655
658
661
663 std::map<const char*, TOOL_BASE*> m_toolTypes;
664
667
670
672 std::map<TOOL_ID, std::optional<VECTOR2D>> m_cursorSettings;
673
679
681 std::list<TOOL_EVENT> m_eventQueue;
682
685
687
690
693
696
699};
700
701#endif // TOOL_MANAGER_H
const char * name
Definition: DXF_plotter.cpp:57
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:92
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition: commit.h:74
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:88
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:68
Represent a single user action.
Definition: tool_action.h:269
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:636
Generic, UI-independent tool event.
Definition: tool_event.h:167
Master controller class:
Definition: tool_manager.h:62
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:73
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:695
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:672
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:663
bool isRegistered(TOOL_BASE *aTool) const
Return information about a tool registration status.
Definition: tool_manager.h:609
APP_SETTINGS_BASE * m_settings
Queue that stores events to be processed at the end of the event processing cycle.
Definition: tool_manager.h:678
TOOL_STATE * GetCurrentToolState() const
Return the #TOOL_STATE object representing the state of the active tool.
Definition: tool_manager.h:430
VECTOR2D GetMenuCursorPos() const
Definition: tool_manager.h:548
VECTOR2D GetCursorPosition() const
std::list< TOOL_EVENT > m_eventQueue
Right click context menu position.
Definition: tool_manager.h:681
bool RunAction(const std::string &aActionName)
Definition: tool_manager.h:158
std::pair< TOOL_EVENT_LIST, TOOL_STATE_FUNC > TRANSITION
Definition: tool_manager.h:554
void setActiveState(TOOL_STATE *aState)
Save the previous active state and sets a new one.
void DeactivateTool()
Deactivate the currently active tool.
bool RunAction(const TOOL_ACTION &aAction, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:178
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:150
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:657
bool RunAction(const TOOL_ACTION &aAction)
Definition: tool_manager.h:213
TOOLS_HOLDER * m_frame
Definition: tool_manager.h:677
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:686
KIGFX::VIEW_CONTROLS * m_viewControls
Definition: tool_manager.h:676
bool doRunAction(const TOOL_ACTION &aAction, bool aNow, const std::any &aParam, COMMIT *aCommit)
Helper function to actually run an action.
ACTION_MANAGER * m_actionMgr
Original cursor position, if overridden by the context menu handler.
Definition: tool_manager.h:669
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:410
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:402
ACTION_MANAGER * GetActionManager() const
Definition: tool_manager.h:302
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:421
bool processEvent(const TOOL_EVENT &aEvent)
Main function for event processing.
std::list< TOOL_ID > ID_LIST
Definition: tool_manager.h:75
KIGFX::VIEW_CONTROLS * GetViewControls() const
Definition: tool_manager.h:393
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:400
bool PostAction(const std::string &aActionName, T aParam)
Run the specified action after the current action (coroutine) ends.
Definition: tool_manager.h:235
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:660
TOOL_ID m_menuOwner
Pointer to the state object corresponding to the currently executed tool.
Definition: tool_manager.h:692
KIGFX::VIEW * m_view
Definition: tool_manager.h:675
void ClearTransitions(TOOL_BASE *aTool)
Clear the state transition map for a tool.
bool SaveClipboard(const std::string &aTextUTF8)
Store information to the system clipboard.
bool m_shuttingDown
Definition: tool_manager.h:698
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:689
bool IsContextMenuActive() const
True while processing a context menu.
Definition: tool_manager.h:519
bool RunSynchronousAction(const TOOL_ACTION &aAction, COMMIT *aCommit, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:197
TOOL_STATE_MAP m_toolState
Index of the registered tools current states, associated by tools' names.
Definition: tool_manager.h:654
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:72
EDA_ITEM * GetModel() const
Definition: tool_manager.h:398
void VetoContextMenuMouseWarp()
Disable mouse warping after the current context menu is closed.
Definition: tool_manager.h:530
ID_LIST m_activeTools
Instance of ACTION_MANAGER that handles TOOL_ACTIONs.
Definition: tool_manager.h:666
void ResetTools(TOOL_BASE::RESET_REASON aReason)
Reset all tools (i.e.
EDA_ITEM * m_model
Definition: tool_manager.h:674
std::map< TOOL_ID, TOOL_STATE * > ID_STATE_MAP
Definition: tool_manager.h:74
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:391
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
< List of tools in the order they were registered
Definition: tool_manager.h:651
void PostAction(const TOOL_ACTION &aAction)
Definition: tool_manager.h:270
TOOL_BASE * FindTool(int aId) const
Search for a tool with given ID.
std::vector< TOOL_BASE * > Tools()
Definition: tool_manager.h:337
void ScheduleContextMenu(TOOL_BASE *aTool, ACTION_MENU *aMenu, CONTEXT_MENU_TRIGGER aTrigger)
Set behavior of the tool's context popup menu.
bool PostAction(const TOOL_ACTION &aAction, T aParam)
Run the specified action after the current action (coroutine) ends.
Definition: tool_manager.h:262
bool PostAction(const TOOL_ACTION &aAction, COMMIT *aCommit)
Definition: tool_manager.h:278
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 PostAction(const std::string &aActionName)
Definition: tool_manager.h:243
bool RunSynchronousAction(const TOOL_ACTION &aAction, COMMIT *aCommit)
Definition: tool_manager.h:205
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.
VECTOR2D m_menuCursor
Definition: tool_manager.h:684
void ShutdownAllTools()
Shutdown all tools with a currently registered event loop in this tool manager by waking them up with...
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
Structure to keep VIEW_CONTROLS settings for easy store/restore operations.
Definition: view_controls.h:43
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:150