summaryrefslogtreecommitdiff
path: root/include/tool/action_manager.h
blob: 34809e0a4d23abb181e8c028e6cefd8081feebb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 CERN
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#ifndef ACTION_MANAGER_H_
#define ACTION_MANAGER_H_

#include <list>
#include <map>
#include <string>

class TOOL_BASE;
class TOOL_MANAGER;
class TOOL_ACTION;

/**
 * Class ACTION_MANAGER
 *
 * Takes care of TOOL_ACTION objects. Registers them and allows to run them using associated
 * hot keys, names or ids.
 */
class ACTION_MANAGER
{
public:
    /**
     * Constructor.
     * @param aToolManager is a tool manager instance that is used to pass events to tools.
     */
    ACTION_MANAGER( TOOL_MANAGER* aToolManager );

    /**
     * Destructor.
     * Unregisters every registered action.
     */
    ~ACTION_MANAGER();

    /**
     * Function RegisterAction()
     * Adds a tool action to the manager and sets it up. After that is is possible to invoke
     * the action using hotkeys or sending a command event with its name.
     * @param aAction: action to be added. Ownership is not transferred.
     */
    void RegisterAction( TOOL_ACTION* aAction );

    /**
     * Function UnregisterAction()
     * Removes a tool action from the manager and makes it unavailable for further usage.
     * @param aAction: action to be removed.
     */
    void UnregisterAction( TOOL_ACTION* aAction );

    /**
     * Generates an unique ID from for an action with given name.
     */
    static int MakeActionId( const std::string& aActionName );

    /**
     * Function FindAction()
     * Finds an action with a given name (if there is one available).
     * @param aActionName is the searched action.
     * @return Pointer to a TOOL_ACTION object or NULL if there is no such action.
     */
    TOOL_ACTION* FindAction( const std::string& aActionName ) const;

    /**
     * Function RunHotKey()
     * Runs an action associated with a hotkey (if there is one available).
     * @param aHotKey is the hotkey to be handled.
     * @return True if there was an action associated with the hotkey, false otherwise.
     */
    bool RunHotKey( int aHotKey ) const;

    /**
     * Function GetHotKey()
     * Returns the hot key associated with a given action or 0 if there is none.
     * @param aAction is the queried action.
     */
    int GetHotKey( const TOOL_ACTION& aAction ) const;

    /**
     * Function UpdateHotKeys()
     * Updates TOOL_ACTIONs hot key assignment according to the current frame's Hot Key Editor settings.
     */
    void UpdateHotKeys();

    /**
     * Function GetActionList()
     * Returns list of TOOL_ACTIONs. TOOL_ACTIONs add themselves to the list upon their
     * creation.
     * @return List of TOOL_ACTIONs.
     */
    static std::list<TOOL_ACTION*>& GetActionList()
    {
        static std::list<TOOL_ACTION*> actionList;

        return actionList;
    }

private:
    ///> Resolves a reference to legacy hot key settings to a particular hot key.
    ///> @param aAction is the action to be resolved.
    int processHotKey( TOOL_ACTION* aAction );

    ///> Tool manager needed to run actions
    TOOL_MANAGER* m_toolMgr;

    ///> Map for indexing actions by their names
    std::map<std::string, TOOL_ACTION*> m_actionNameIndex;

    ///> Map for indexing actions by their hotkeys
    typedef std::map<int, std::list<TOOL_ACTION*> > HOTKEY_LIST;
    HOTKEY_LIST m_actionHotKeys;

    ///> Quick action<->hot key lookup
    std::map<int, int> m_hotkeys;
};

#endif /* ACTION_MANAGER_H_ */