tool_action.h 6.13 KB
Newer Older
1 2 3 4 5
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 CERN
 * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6
 * @author Maciej Suminski <maciej.suminski@cern.ch>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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
 */

26 27 28 29
#ifndef __TOOL_ACTION_H
#define __TOOL_ACTION_H

#include <string>
30
#include <cassert>
31

32
#include <tool/tool_manager.h>
33

34 35 36 37 38 39 40 41 42 43
/**
 * Class TOOL_ACTION
 *
 * Represents a single user action. For instance:
 * - changing layer to top by pressing PgUp
 * - running the DRC from the menu
 * and so on, and so forth....
 * Action class groups all necessary properties of an action, including explanation,
 * icons, hotkeys,.menu items, etc.
 */
44
class TOOL_ACTION
45
{
46
public:
Maciej Suminski's avatar
Maciej Suminski committed
47
    TOOL_ACTION( const std::string& aName, TOOL_ACTION_SCOPE aScope = AS_CONTEXT,
48
            int aDefaultHotKey = 0, const std::string& aMenuItem = std::string( "" ),
49
            const std::string& aMenuDesc = std::string( "" ), TOOL_ACTION_FLAGS aFlags = AF_NONE ) :
50 51
        m_name( aName ), m_scope( aScope ), m_defaultHotKey( aDefaultHotKey ),
        m_currentHotKey( aDefaultHotKey ), m_menuItem( aMenuItem ),
52
        m_menuDescription( aMenuDesc ), m_id( -1 ), m_flags( aFlags )
53
    {
54
        TOOL_MANAGER::GetActionList().push_back( this );
55 56 57 58
    }

    ~TOOL_ACTION()
    {
59
        TOOL_MANAGER::GetActionList().remove( this );
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
    }

    bool operator==( const TOOL_ACTION& aRhs ) const
    {
        return m_id == aRhs.m_id;
    }

    bool operator!=( const TOOL_ACTION& aRhs ) const
    {
        return m_id != aRhs.m_id;
    }

    /**
     * Function GetName()
     * Returns name of the action. It is the same one that is contained in TOOL_EVENT that is
     * sent by activating the TOOL_ACTION.
     *
     * @return Name of the action.
     */
    const std::string& GetName() const
    {
        return m_name;
    }

    /**
     * Function GetId()
     * Returns the unique id of the TOOL_ACTION object. It is valid only after registering the
     * TOOL_ACTION by ACTION_MANAGER.
     *
     * @return The unique identification number. If the number is negative, then it is not valid.
     */
    int GetId() const
    {
        return m_id;
    }

96 97 98 99 100 101 102 103 104
    /**
     * Function GetHotKey()
     * Returns the associated hot key.
     */
    int GetHotKey() const
    {
        return m_currentHotKey;
    }

105 106 107 108 109 110 111 112
    /**
     * Function ChangeHotKey()
     * Assigns a new hot key.
     *
     * @param aNewHotKey is the new hot key.
     */
    void ChangeHotKey( int aNewHotKey )
    {
113 114
        assert( false );
        // hotkey has to be changed in the ACTION_MANAGER, or change the implementation
115 116 117 118 119 120 121 122 123
        m_currentHotKey = aNewHotKey;
    }

    /**
     * Function RestoreHotKey()
     * Changes the assigned hot key to the default one.
     */
    void RestoreHotKey()
    {
124 125
        assert( false );
        // hotkey has to be changed in the ACTION_MANAGER, or change the implementation
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        m_currentHotKey = m_defaultHotKey;
    }

    /**
     * Function HasHotKey()
     * Checks if the action has a hot key assigned.
     *
     * @return True if there is a hot key assigned, false otherwise.
     */
    bool HasHotKey() const
    {
        return m_currentHotKey > 0;
    }

    /**
141
     * Function MakeEvent()
142
     * Returns the event associated with the action (i.e. the event that will be sent after
143 144 145 146
     * activating the action).
     *
     * @return The event associated with the action.
     */
147
    TOOL_EVENT MakeEvent() const
148
    {
149 150 151 152
        if( IsActivation() )
            return TOOL_EVENT( TC_COMMAND, TA_ACTIVATE, m_name, m_scope );
        else
            return TOOL_EVENT( TC_COMMAND, TA_ACTION, m_name, m_scope );
153 154
    }

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    const std::string& GetMenuItem() const
    {
        return m_menuItem;
    }

    void SetMenuItem( const std::string& aItem )
    {
        m_menuItem = aItem;
    }

    const std::string& GetDescription() const
    {
        return m_menuDescription;
    }

    void SetDescription( const std::string& aDescription )
    {
        m_menuDescription = aDescription;
    }

175 176 177 178 179 180 181 182 183 184
    TOOL_ACTION_SCOPE GetScope() const
    {
        return m_scope;
    }

    /**
     * Returns name of the tool associated with the action. It is basically the action name
     * stripped of the last part (e.g. for "pcbnew.InteractiveDrawing.drawCircle" it is
     * "pcbnew.InteractiveDrawing").
     */
185 186 187 188 189 190
    std::string GetToolName() const;

    /**
     * Returns true if the action is intended to activate a tool.
     */
    bool IsActivation() const
191
    {
192
        return m_flags & AF_ACTIVATE;
193 194
    }

195 196 197 198 199 200
private:
    friend class ACTION_MANAGER;

    /// Name of the action (convention is: app.[tool.]action.name)
    std::string m_name;

201
    /// Scope of the action (i.e. the event that is issued after activation).
Maciej Suminski's avatar
Maciej Suminski committed
202
    TOOL_ACTION_SCOPE m_scope;
203 204 205 206 207 208 209 210

    /// Default hot key that activates the action.
    const int m_defaultHotKey;

    /// Custom assigned hot key that activates the action.
    int m_currentHotKey;

    /// Menu entry text
211
    std::string m_menuItem;
212 213

    /// Pop-up help
214
    std::string m_menuDescription;
215 216

    // Icon for menu entry
217
    // KiBitmap m_bitmap;
218 219 220 221

    /// Unique ID for fast matching. Assigned by ACTION_MANAGER.
    int m_id;

222 223 224
    /// Action flags
    TOOL_ACTION_FLAGS m_flags;

225
    /// Origin of the action
226
    // const TOOL_BASE* m_origin;
227 228

    /// Originating UI object
229
    // wxWindow* m_uiOrigin;
230
};
231

232
#endif