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 <typeinfo>
35#include <type_traits>
36
37#include <tool/tool_base.h>
38#include <tool/tool_event.h>
39#include <view/view_controls.h>
40
41class COMMIT;
42class TOOLS_HOLDER;
43class TOOL_ACTION;
44class TOOL_BASE;
45class ACTION_MANAGER;
46class ACTION_MENU;
48
49
57{
58private:
59 struct TOOL_STATE;
60
61public:
63
65
66 // Helper typedefs
67 typedef std::map<TOOL_BASE*, TOOL_STATE*> TOOL_STATE_MAP;
68 typedef std::map<std::string, TOOL_STATE*> NAME_STATE_MAP;
69 typedef std::map<TOOL_ID, TOOL_STATE*> ID_STATE_MAP;
70 typedef std::list<TOOL_ID> ID_LIST;
71
75 static TOOL_ID MakeToolId( const std::string& aToolName );
76
83 void RegisterTool( TOOL_BASE* aTool );
84
91 bool InvokeTool( TOOL_ID aToolId );
92
99 bool InvokeTool( const std::string& aToolName );
100
105 void ShutdownAllTools();
106
113 void ShutdownTool( TOOL_BASE* aTool );
114
121 void ShutdownTool( TOOL_ID aToolId );
122
129 void ShutdownTool( const std::string& aToolName );
130
144 template<typename T, std::enable_if_t<!std::is_convertible_v<T*, COMMIT*>>* = nullptr>
145 bool RunAction( const std::string& aActionName, T aParam )
146 {
147 // Use a cast to ensure the proper type is stored inside the parameter
148 std::any a( static_cast<T>( aParam ) );
149
150 return doRunAction( aActionName, true, a, nullptr );
151 }
152
153 bool RunAction( const std::string& aActionName )
154 {
155 // Default initialize the parameter argument to an empty std::any
156 std::any a;
157
158 return doRunAction( aActionName, true, a, nullptr );
159 }
160
172 template<typename T, std::enable_if_t<!std::is_convertible_v<T, COMMIT*>>* = nullptr>
173 bool RunAction( const TOOL_ACTION& aAction, T aParam )
174 {
175 // Use a cast to ensure the proper type is stored inside the parameter
176 std::any a( static_cast<T>( aParam ) );
177
178 return doRunAction( aAction, true, a, nullptr );
179 }
180
191 template<typename T>
192 bool RunSynchronousAction( const TOOL_ACTION& aAction, COMMIT* aCommit, T aParam )
193 {
194 // Use a cast to ensure the proper type is stored inside the parameter
195 std::any a( static_cast<T>( aParam ) );
196
197 return doRunAction( aAction, true, a, aCommit );
198 }
199
200 bool RunSynchronousAction( const TOOL_ACTION& aAction, COMMIT* aCommit )
201 {
202 // Default initialize the parameter argument to an empty std::any
203 std::any a;
204
205 return doRunAction( aAction, true, a, aCommit );
206 }
207
208 bool RunAction( const TOOL_ACTION& aAction )
209 {
210 // Default initialize the parameter argument to an empty std::any
211 std::any a;
212
213 return doRunAction( aAction, true, a, nullptr );
214 }
215
229 template<typename T>
230 bool PostAction( const std::string& aActionName, T aParam )
231 {
232 // Use a cast to ensure the proper type is stored inside the parameter
233 std::any a( static_cast<T>( aParam ) );
234
235 return doRunAction( aActionName, false, a, nullptr );
236 }
237
238 bool PostAction( const std::string& aActionName )
239 {
240 // Default initialize the parameter argument to an empty std::any
241 std::any a;
242
243 return doRunAction( aActionName, false, a, nullptr );
244 }
245
256 template<typename T>
257 bool PostAction( const TOOL_ACTION& aAction, T aParam )
258 {
259 // Use a cast to ensure the proper type is stored inside the parameter
260 std::any a( static_cast<T>( aParam ) );
261
262 return doRunAction( aAction, false, a, nullptr );
263 }
264
265 void PostAction( const TOOL_ACTION& aAction )
266 {
267 // Default initialize the parameter argument to an empty std::any
268 std::any a;
269
270 doRunAction( aAction, false, a, nullptr );
271 }
272
273 bool PostAction( const TOOL_ACTION& aAction, COMMIT* aCommit )
274 {
275 // Default initialize the parameter argument to an empty std::any
276 std::any a;
277
278 return doRunAction( aAction, false, a, aCommit );
279 }
280
284 void CancelTool();
285
292 void PrimeTool( const VECTOR2D& aPosition );
293
295 int GetHotKey( const TOOL_ACTION& aAction ) const;
296
298
305 TOOL_BASE* FindTool( int aId ) const;
306
313 TOOL_BASE* FindTool( const std::string& aName ) const;
314
315 /*
316 * Return the tool of given type or nullptr if there is no such tool registered.
317 */
318 template<typename T>
320 {
321 std::map<const char*, TOOL_BASE*>::iterator tool = m_toolTypes.find( typeid( T ).name() );
322
323 if( tool != m_toolTypes.end() )
324 return static_cast<T*>( tool->second );
325
326 return nullptr;
327 }
328
329 /*
330 * Return all registered tools.
331 */
332 std::vector<TOOL_BASE*> Tools() { return m_toolOrder; }
333
337 void DeactivateTool();
338
339
343 bool IsToolActive( TOOL_ID aId ) const;
344
345
349 void ResetTools( TOOL_BASE::RESET_REASON aReason );
350
357 void InitTools();
358
365 bool ProcessEvent( const TOOL_EVENT& aEvent );
366
372 void PostEvent( const TOOL_EVENT& aEvent );
373
379 void SetEnvironment( EDA_ITEM* aModel, KIGFX::VIEW* aView,
380 KIGFX::VIEW_CONTROLS* aViewControls, APP_SETTINGS_BASE* aSettings,
381 TOOLS_HOLDER* aFrame );
382
383 /*
384 * Accessors for the environment objects (view, model, etc.)
385 */
386 KIGFX::VIEW* GetView() const { return m_view; }
387
389
392
393 EDA_ITEM* GetModel() const { return m_model; }
394
396
397 TOOLS_HOLDER* GetToolHolder() const { return m_frame; }
398
405 inline int GetCurrentToolId() const
406 {
407 return m_activeTools.empty() ? -1 : m_activeTools.front();
408 }
409
416 inline TOOL_BASE* GetCurrentTool() const
417 {
418 return FindTool( GetCurrentToolId() );
419 }
420
426 {
427 auto it = m_toolIdIndex.find( GetCurrentToolId() );
428 return ( it != m_toolIdIndex.end() ) ? it->second : nullptr;
429 }
430
441 int GetPriority( int aToolId ) const;
442
449 void ScheduleNextState( TOOL_BASE* aTool, TOOL_STATE_FUNC& aHandler,
450 const TOOL_EVENT_LIST& aConditions );
451
457 void ClearTransitions( TOOL_BASE* aTool );
458
459 void RunMainStack( TOOL_BASE* aTool, std::function<void()> aFunc );
460
464 void UpdateUI( const TOOL_EVENT& aEvent );
465
471 TOOL_EVENT* ScheduleWait( TOOL_BASE* aTool, const TOOL_EVENT_LIST& aConditions );
472
484 void ScheduleContextMenu( TOOL_BASE* aTool, ACTION_MENU* aMenu, CONTEXT_MENU_TRIGGER aTrigger );
485
493 bool SaveClipboard( const std::string& aTextUTF8 );
494
503 std::string GetClipboardUTF8() const;
504
510
515 {
516 return m_menuActive;
517 }
518
526 {
528 }
529
533 void DispatchContextMenu( const TOOL_EVENT& aEvent );
534
541 bool DispatchHotKey( const TOOL_EVENT& aEvent );
542
544 {
545 return m_menuCursor;
546 }
547
548private:
549 typedef std::pair<TOOL_EVENT_LIST, TOOL_STATE_FUNC> TRANSITION;
550
554 bool doRunAction( const TOOL_ACTION& aAction, bool aNow, const std::any& aParam, COMMIT* aCommit );
555 bool doRunAction( const std::string& aActionName, bool aNow, const std::any& aParam, COMMIT* aCommit );
556
560 bool dispatchInternal( TOOL_EVENT& aEvent );
561
568 bool dispatchActivation( const TOOL_EVENT& aEvent );
569
576 bool invokeTool( TOOL_BASE* aTool );
577
586 bool runTool( TOOL_BASE* aTool );
587
596 ID_LIST::iterator finishTool( TOOL_STATE* aState );
597
604 bool isRegistered( TOOL_BASE* aTool ) const
605 {
606 return m_toolState.count( aTool ) > 0;
607 }
608
615 bool isActive( TOOL_BASE* aTool ) const;
616
622 void saveViewControls( TOOL_STATE* aState );
623
627 void applyViewControls( const TOOL_STATE* aState );
628
634 bool processEvent( const TOOL_EVENT& aEvent );
635
642 void setActiveState( TOOL_STATE* aState );
643
645 std::vector<TOOL_BASE*> m_toolOrder;
646
649
652
655
657 std::map<const char*, TOOL_BASE*> m_toolTypes;
658
661
664
666 std::map<TOOL_ID, std::optional<VECTOR2D>> m_cursorSettings;
667
673
675 std::list<TOOL_EVENT> m_eventQueue;
676
679
681
684
687
690
693};
694
695#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:57
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:68
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:689
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:666
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:657
bool isRegistered(TOOL_BASE *aTool) const
Return information about a tool registration status.
Definition: tool_manager.h:604
APP_SETTINGS_BASE * m_settings
Queue that stores events to be processed at the end of the event processing cycle.
Definition: tool_manager.h:672
TOOL_STATE * GetCurrentToolState() const
Return the #TOOL_STATE object representing the state of the active tool.
Definition: tool_manager.h:425
VECTOR2D GetMenuCursorPos() const
Definition: tool_manager.h:543
VECTOR2D GetCursorPosition() const
std::list< TOOL_EVENT > m_eventQueue
Right click context menu position.
Definition: tool_manager.h:675
bool RunAction(const std::string &aActionName)
Definition: tool_manager.h:153
std::pair< TOOL_EVENT_LIST, TOOL_STATE_FUNC > TRANSITION
Definition: tool_manager.h:549
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:173
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:145
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:651
bool RunAction(const TOOL_ACTION &aAction)
Definition: tool_manager.h:208
TOOLS_HOLDER * m_frame
Definition: tool_manager.h:671
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:680
KIGFX::VIEW_CONTROLS * m_viewControls
Definition: tool_manager.h:670
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:663
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:405
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:397
ACTION_MANAGER * GetActionManager() const
Definition: tool_manager.h:297
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:416
bool processEvent(const TOOL_EVENT &aEvent)
Main function for event processing.
std::list< TOOL_ID > ID_LIST
Definition: tool_manager.h:70
KIGFX::VIEW_CONTROLS * GetViewControls() const
Definition: tool_manager.h:388
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:395
bool PostAction(const std::string &aActionName, T aParam)
Run the specified action after the current action (coroutine) ends.
Definition: tool_manager.h:230
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:654
TOOL_ID m_menuOwner
Pointer to the state object corresponding to the currently executed tool.
Definition: tool_manager.h:686
KIGFX::VIEW * m_view
Definition: tool_manager.h:669
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:692
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:683
bool IsContextMenuActive() const
True while processing a context menu.
Definition: tool_manager.h:514
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:192
TOOL_STATE_MAP m_toolState
Index of the registered tools current states, associated by tools' names.
Definition: tool_manager.h:648
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:67
EDA_ITEM * GetModel() const
Definition: tool_manager.h:393
void VetoContextMenuMouseWarp()
Disable mouse warping after the current context menu is closed.
Definition: tool_manager.h:525
ID_LIST m_activeTools
Instance of ACTION_MANAGER that handles TOOL_ACTIONs.
Definition: tool_manager.h:660
void ResetTools(TOOL_BASE::RESET_REASON aReason)
Reset all tools (i.e.
EDA_ITEM * m_model
Definition: tool_manager.h:668
std::map< TOOL_ID, TOOL_STATE * > ID_STATE_MAP
Definition: tool_manager.h:69
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:386
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:645
void PostAction(const TOOL_ACTION &aAction)
Definition: tool_manager.h:265
TOOL_BASE * FindTool(int aId) const
Search for a tool with given ID.
std::vector< TOOL_BASE * > Tools()
Definition: tool_manager.h:332
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:257
bool PostAction(const TOOL_ACTION &aAction, COMMIT *aCommit)
Definition: tool_manager.h:273
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:238
bool RunSynchronousAction(const TOOL_ACTION &aAction, COMMIT *aCommit)
Definition: tool_manager.h:200
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:678
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: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