File: action_manager.cpp

package info (click to toggle)
kicad 9.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 770,320 kB
  • sloc: cpp: 961,692; ansic: 121,001; xml: 66,428; python: 18,387; sh: 1,010; awk: 301; asm: 292; makefile: 227; javascript: 167; perl: 10
file content (336 lines) | stat: -rw-r--r-- 11,028 bytes parent folder | download | duplicates (3)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013-2023 CERN
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 * @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
 */

#include <eda_draw_frame.h>
#include <tool/action_manager.h>
#include <tool/tool_action.h>
#include <tool/tool_manager.h>
#include <trace_helpers.h>
#include <wx/log.h>

#include <hotkeys_basic.h>
#include <cctype>


ACTION_MANAGER::ACTION_MANAGER( TOOL_MANAGER* aToolManager ) :
    m_toolMgr( aToolManager )
{
    // Register known actions
    std::list<TOOL_ACTION*>& actionList = GetActionList();

    for( TOOL_ACTION* action : actionList )
    {
        if( action->m_id == -1 )
            action->m_id = MakeActionId( action->m_name );

        int         groupID   = 0;
        std::string groupName = "none";

        std::optional<TOOL_ACTION_GROUP> group = action->GetActionGroup();

        if( group.has_value() )
        {
            groupID   = group.value().GetGroupID();
            groupName = group.value().GetName();
        }

        wxLogTrace( kicadTraceToolStack,
                    "ACTION_MANAGER::ACTION_MANAGER: Registering action %s with ID %d, UI ID %d, "
                    "and group %s(%d)",
                    action->m_name, action->m_id, action->GetUIId(), groupName, groupID );

        RegisterAction( action );
    }
}


ACTION_MANAGER::~ACTION_MANAGER()
{
}


void ACTION_MANAGER::RegisterAction( TOOL_ACTION* aAction )
{
    // TOOL_ACTIONs are supposed to be named [appName.]toolName.actionName (with dots between)
    // action name without specifying at least toolName is not valid
    wxASSERT( aAction->GetName().find( '.', 0 ) != std::string::npos );

    // TOOL_ACTIONs must have unique names & ids
    wxASSERT( m_actionNameIndex.find( aAction->m_name ) == m_actionNameIndex.end() );

    m_actionNameIndex[aAction->m_name] = aAction;

    if( aAction->HasCustomUIId() )
        m_customUIIdIndex[aAction->GetUIId()] = aAction;
}


void ACTION_MANAGER::SetConditions( const TOOL_ACTION& aAction,
                                    const ACTION_CONDITIONS& aConditions )
{
    // Remove any existing handlers with the old conditions to ensure the UI layer doesn't have
    // stale data.
    if( m_toolMgr )
        m_toolMgr->GetToolHolder()->UnregisterUIUpdateHandler( aAction );

    m_uiConditions[aAction.GetId()] = aConditions;

    wxLogTrace( kicadTraceToolStack,
                wxS( "ACTION_MANAGER::SetConditions: Registering conditions for ID %d - %s" ),
                aAction.GetId(), aAction.GetName() );

    // Register a new handler with the new conditions
    if( m_toolMgr )
        m_toolMgr->GetToolHolder()->RegisterUIUpdateHandler( aAction, aConditions );
}


const ACTION_CONDITIONS* ACTION_MANAGER::GetCondition( const TOOL_ACTION& aAction ) const
{
    const auto it = m_uiConditions.find( aAction.GetId() );

    // If the action doesn't have something registered, then return null
    if( it == m_uiConditions.end() )
        return nullptr;
    else
        return &it->second;
}


int ACTION_MANAGER::MakeActionId( const std::string& aActionName )
{
    static int currentActionId = 1;

    return currentActionId++;
}


TOOL_ACTION* ACTION_MANAGER::FindAction( const std::string& aActionName ) const
{
    std::map<std::string, TOOL_ACTION*>::const_iterator it = m_actionNameIndex.find( aActionName );

    if( it != m_actionNameIndex.end() )
        return it->second;

    return nullptr;
}


bool ACTION_MANAGER::RunHotKey( int aHotKey ) const
{
    int key = aHotKey & ~MD_MODIFIER_MASK;
    int mod = aHotKey & MD_MODIFIER_MASK;

    if( key >= 'a' && key <= 'z' )
        key = std::toupper( key );

    wxLogTrace( kicadTraceToolStack, wxS( "ACTION_MANAGER::RunHotKey Key: %s" ),
                KeyNameFromKeyCode( aHotKey ) );

    HOTKEY_LIST::const_iterator it = m_actionHotKeys.find( key | mod );

    // If no luck, try without Shift, to handle keys that require it
    // e.g. to get ? you need to press Shift+/ without US keyboard layout
    // Hardcoding ? as Shift+/ is a bad idea, as on another layout you may need to press a
    // different combination.
    // This doesn't apply for letters, as we already handled case normalisation.
    if( it == m_actionHotKeys.end() && !std::isalpha( key ) )
    {
        wxLogTrace( kicadTraceToolStack,
                    wxS( "ACTION_MANAGER::RunHotKey No actions found, searching with key: %s" ),
                    KeyNameFromKeyCode( key | ( mod & ~MD_SHIFT ) ) );

        it = m_actionHotKeys.find( key | ( mod & ~MD_SHIFT ) );
    }

    // Still no luck, we're done without a match
    if( it == m_actionHotKeys.end() )
        return false; // no appropriate action found for the hotkey

    const std::list<TOOL_ACTION*>& actions = it->second;

    // Choose the action that has the highest priority on the active tools stack
    // If there is none, run the global action associated with the hot key
    int highestPriority = -1, priority = -1;
    const TOOL_ACTION* context = nullptr; // pointer to context action of the highest priority tool
    std::vector<const TOOL_ACTION*> global; // pointers to global actions
                                            // if there is no context action

    for( const TOOL_ACTION* action : actions )
    {
        if( action->GetScope() == AS_GLOBAL )
        {
            // Store the global action in case there are no context actions to run
            global.emplace_back( action );
            continue;
        }

        TOOL_BASE* tool = m_toolMgr->FindTool( action->GetToolName() );

        if( tool )
        {
            // Choose the action that goes to the tool with highest priority
            // (i.e. is on the top of active tools stack)
            priority = m_toolMgr->GetPriority( tool->GetId() );

            if( priority >= 0 && priority > highestPriority )
            {
                highestPriority = priority;
                context = action;
            }
        }
    }

    // Get the selection to use to test if the action is enabled
    SELECTION& sel = m_toolMgr->GetToolHolder()->GetCurrentSelection();

    if( context )
    {
        bool runAction = true;

        if( const ACTION_CONDITIONS* aCond = GetCondition( *context ) )
            runAction = aCond->enableCondition( sel );

        wxLogTrace( kicadTraceToolStack,
                    wxS( "ACTION_MANAGER::RunHotKey %s context action: %s for hotkey %s" ),
                    runAction ? wxS( "Running" ) : wxS( "Not running" ),
                    context->GetName(),
                    KeyNameFromKeyCode( aHotKey ) );

        if( runAction )
            return m_toolMgr->RunAction( *context );
    }
    else if( !global.empty() )
    {
        for( const TOOL_ACTION* act : global )
        {
            bool runAction = true;

            if( const ACTION_CONDITIONS* aCond = GetCondition( *act ) )
                runAction = aCond->enableCondition( sel );

            wxLogTrace( kicadTraceToolStack,
                        wxS( "ACTION_MANAGER::RunHotKey %s global action: %s for hotkey %s" ),
                        runAction ? wxS( "Running" ) : wxS( "Not running" ),
                        act->GetName(),
                        KeyNameFromKeyCode( aHotKey ) );

            if( runAction && m_toolMgr->RunAction( *act ) )
                return true;
        }
    }

    wxLogTrace( kicadTraceToolStack,
                wxS( "ACTION_MANAGER::RunHotKey No action found for key %s" ),
                KeyNameFromKeyCode( aHotKey ) );

    return false;
}


bool ACTION_MANAGER::IsActionUIId( int aId ) const
{
    // Automatically assigned IDs are always in this range
    if( aId >= TOOL_ACTION::GetBaseUIId() )
        return true;

    // Search the custom assigned UI IDs
    auto it = m_customUIIdIndex.find( aId );

    return ( it != m_customUIIdIndex.end() );
}


const std::map<std::string, TOOL_ACTION*>& ACTION_MANAGER::GetActions() const
{
    return m_actionNameIndex;
}


int ACTION_MANAGER::GetHotKey( const TOOL_ACTION& aAction ) const
{
    std::map<int, int>::const_iterator it = m_hotkeys.find( aAction.GetId() );

    if( it == m_hotkeys.end() )
        return 0;

    return it->second;
}


void ACTION_MANAGER::UpdateHotKeys( bool aFullUpdate )
{
    static std::map<std::string, int>                 legacyHotKeyMap;
    static std::map<std::string, std::pair<int, int>> userHotKeyMap;
    static bool                                       mapsInitialized = false;

    m_actionHotKeys.clear();
    m_hotkeys.clear();

    if( m_toolMgr->GetToolHolder() && ( aFullUpdate || !mapsInitialized ) )
    {
        ReadLegacyHotkeyConfig( m_toolMgr->GetToolHolder()->ConfigBaseName(), legacyHotKeyMap );
        ReadHotKeyConfig( wxEmptyString, userHotKeyMap );
        mapsInitialized = true;
    }

    for( const auto& ii : m_actionNameIndex )
    {
        TOOL_ACTION* action = ii.second;
        int          hotkey = 0;
        int          alt    = 0;

        if( aFullUpdate )
            processHotKey( action, legacyHotKeyMap, userHotKeyMap );

        hotkey = action->GetHotKey();
        alt    = action->GetHotKeyAlt();

        if( hotkey > 0 )
            m_actionHotKeys[hotkey].push_back( action );

        if( alt > 0 )
            m_actionHotKeys[alt].push_back( action );

        m_hotkeys[action->GetId()] = hotkey;
    }
}


void ACTION_MANAGER::processHotKey( TOOL_ACTION*                                      aAction,
                                    const std::map<std::string, int>&                 aLegacyMap,
                                    const std::map<std::string, std::pair<int, int>>& aHotKeyMap )
{
    aAction->m_hotKey = aAction->m_defaultHotKey;

    if( !aAction->m_legacyName.empty() && aLegacyMap.count( aAction->m_legacyName ) )
        aAction->SetHotKey( aLegacyMap.at( aAction->m_legacyName ) );

    if( aHotKeyMap.count( aAction->m_name ) )
    {
        std::pair<int, int> keys = aHotKeyMap.at( aAction->m_name );
        aAction->SetHotKey( keys.first, keys.second );
    }
}