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
276 void CancelTool();
277
284 void PrimeTool( const VECTOR2D& aPosition );
285
287 int GetHotKey( const TOOL_ACTION& aAction ) const;
288
290
297 TOOL_BASE* FindTool( int aId ) const;
298
305 TOOL_BASE* FindTool( const std::string& aName ) const;
306
307 /*
308 * Return the tool of given type or nullptr if there is no such tool registered.
309 */
310 template<typename T>
312 {
313 std::map<const char*, TOOL_BASE*>::iterator tool = m_toolTypes.find( typeid( T ).name() );
314
315 if( tool != m_toolTypes.end() )
316 return static_cast<T*>( tool->second );
317
318 return nullptr;
319 }
320
321 /*
322 * Return all registered tools.
323 */
324 std::vector<TOOL_BASE*> Tools() { return m_toolOrder; }
325
329 void DeactivateTool();
330
331
335 bool IsToolActive( TOOL_ID aId ) const;
336
337
341 void ResetTools( TOOL_BASE::RESET_REASON aReason );
342
349 void InitTools();
350
357 bool ProcessEvent( const TOOL_EVENT& aEvent );
358
364 void PostEvent( const TOOL_EVENT& aEvent );
365
371 void SetEnvironment( EDA_ITEM* aModel, KIGFX::VIEW* aView,
372 KIGFX::VIEW_CONTROLS* aViewControls, APP_SETTINGS_BASE* aSettings,
373 TOOLS_HOLDER* aFrame );
374
375 /*
376 * Accessors for the environment objects (view, model, etc.)
377 */
378 KIGFX::VIEW* GetView() const { return m_view; }
379
381
384
385 EDA_ITEM* GetModel() const { return m_model; }
386
388
389 TOOLS_HOLDER* GetToolHolder() const { return m_frame; }
390
397 inline int GetCurrentToolId() const
398 {
399 return m_activeTools.empty() ? -1 : m_activeTools.front();
400 }
401
408 inline TOOL_BASE* GetCurrentTool() const
409 {
410 return FindTool( GetCurrentToolId() );
411 }
412
418 {
419 auto it = m_toolIdIndex.find( GetCurrentToolId() );
420 return ( it != m_toolIdIndex.end() ) ? it->second : nullptr;
421 }
422
433 int GetPriority( int aToolId ) const;
434
441 void ScheduleNextState( TOOL_BASE* aTool, TOOL_STATE_FUNC& aHandler,
442 const TOOL_EVENT_LIST& aConditions );
443
449 void ClearTransitions( TOOL_BASE* aTool );
450
451 void RunMainStack( TOOL_BASE* aTool, std::function<void()> aFunc );
452
456 void UpdateUI( const TOOL_EVENT& aEvent );
457
463 TOOL_EVENT* ScheduleWait( TOOL_BASE* aTool, const TOOL_EVENT_LIST& aConditions );
464
476 void ScheduleContextMenu( TOOL_BASE* aTool, ACTION_MENU* aMenu, CONTEXT_MENU_TRIGGER aTrigger );
477
485 bool SaveClipboard( const std::string& aTextUTF8 );
486
495 std::string GetClipboardUTF8() const;
496
502
507 {
508 return m_menuActive;
509 }
510
518 {
520 }
521
525 void DispatchContextMenu( const TOOL_EVENT& aEvent );
526
533 bool DispatchHotKey( const TOOL_EVENT& aEvent );
534
536 {
537 return m_menuCursor;
538 }
539
540private:
541 typedef std::pair<TOOL_EVENT_LIST, TOOL_STATE_FUNC> TRANSITION;
542
546 bool doRunAction( const TOOL_ACTION& aAction, bool aNow, const std::any& aParam, COMMIT* aCommit );
547 bool doRunAction( const std::string& aActionName, bool aNow, const std::any& aParam, COMMIT* aCommit );
548
552 bool dispatchInternal( TOOL_EVENT& aEvent );
553
560 bool dispatchActivation( const TOOL_EVENT& aEvent );
561
568 bool invokeTool( TOOL_BASE* aTool );
569
578 bool runTool( TOOL_BASE* aTool );
579
588 ID_LIST::iterator finishTool( TOOL_STATE* aState );
589
596 bool isRegistered( TOOL_BASE* aTool ) const
597 {
598 return m_toolState.count( aTool ) > 0;
599 }
600
607 bool isActive( TOOL_BASE* aTool ) const;
608
614 void saveViewControls( TOOL_STATE* aState );
615
619 void applyViewControls( const TOOL_STATE* aState );
620
626 bool processEvent( const TOOL_EVENT& aEvent );
627
634 void setActiveState( TOOL_STATE* aState );
635
637 std::vector<TOOL_BASE*> m_toolOrder;
638
641
644
647
649 std::map<const char*, TOOL_BASE*> m_toolTypes;
650
653
656
658 std::map<TOOL_ID, std::optional<VECTOR2D>> m_cursorSettings;
659
665
667 std::list<TOOL_EVENT> m_eventQueue;
668
671
673
676
679
682
685};
686
687#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: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: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:681
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:658
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:649
bool isRegistered(TOOL_BASE *aTool) const
Return information about a tool registration status.
Definition: tool_manager.h:596
APP_SETTINGS_BASE * m_settings
Queue that stores events to be processed at the end of the event processing cycle.
Definition: tool_manager.h:664
TOOL_STATE * GetCurrentToolState() const
Return the #TOOL_STATE object representing the state of the active tool.
Definition: tool_manager.h:417
VECTOR2D GetMenuCursorPos() const
Definition: tool_manager.h:535
VECTOR2D GetCursorPosition() const
std::list< TOOL_EVENT > m_eventQueue
Right click context menu position.
Definition: tool_manager.h:667
bool RunAction(const std::string &aActionName)
Definition: tool_manager.h:153
std::pair< TOOL_EVENT_LIST, TOOL_STATE_FUNC > TRANSITION
Definition: tool_manager.h:541
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:643
bool RunAction(const TOOL_ACTION &aAction)
Definition: tool_manager.h:208
TOOLS_HOLDER * m_frame
Definition: tool_manager.h:663
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:672
KIGFX::VIEW_CONTROLS * m_viewControls
Definition: tool_manager.h:662
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:655
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:397
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:389
ACTION_MANAGER * GetActionManager() const
Definition: tool_manager.h:289
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:408
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:380
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:387
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:646
TOOL_ID m_menuOwner
Pointer to the state object corresponding to the currently executed tool.
Definition: tool_manager.h:678
KIGFX::VIEW * m_view
Definition: tool_manager.h:661
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:684
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:675
bool IsContextMenuActive() const
True while processing a context menu.
Definition: tool_manager.h:506
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:640
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:385
void VetoContextMenuMouseWarp()
Disable mouse warping after the current context menu is closed.
Definition: tool_manager.h:517
ID_LIST m_activeTools
Instance of ACTION_MANAGER that handles TOOL_ACTIONs.
Definition: tool_manager.h:652
void ResetTools(TOOL_BASE::RESET_REASON aReason)
Reset all tools (i.e.
EDA_ITEM * m_model
Definition: tool_manager.h:660
std::map< TOOL_ID, TOOL_STATE * > ID_STATE_MAP
Definition: tool_manager.h:69
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:378
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:637
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:324
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
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:670
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