2013-09-20 16:21:01 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
2021-07-15 19:26:35 +00:00
|
|
|
* Copyright (C) 2019-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-09-20 16:21:01 +00:00
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2019-07-27 14:11:41 +00:00
|
|
|
#include <eda_draw_frame.h>
|
2013-09-20 16:21:01 +00:00
|
|
|
#include <tool/action_manager.h>
|
|
|
|
#include <tool/tool_action.h>
|
2019-07-27 14:11:41 +00:00
|
|
|
#include <tool/tool_manager.h>
|
|
|
|
#include <trace_helpers.h>
|
2021-06-03 12:11:15 +00:00
|
|
|
#include <wx/log.h>
|
2015-05-05 18:39:42 +00:00
|
|
|
|
|
|
|
#include <hotkeys_basic.h>
|
2016-05-28 16:46:29 +00:00
|
|
|
#include <cctype>
|
2013-09-20 16:21:01 +00:00
|
|
|
|
|
|
|
ACTION_MANAGER::ACTION_MANAGER( TOOL_MANAGER* aToolManager ) :
|
|
|
|
m_toolMgr( aToolManager )
|
|
|
|
{
|
2015-05-05 18:39:41 +00:00
|
|
|
// Register known actions
|
|
|
|
std::list<TOOL_ACTION*>& actionList = GetActionList();
|
2015-07-28 08:29:00 +00:00
|
|
|
|
2016-06-29 20:07:55 +00:00
|
|
|
for( TOOL_ACTION* action : actionList )
|
2015-07-28 08:29:00 +00:00
|
|
|
{
|
|
|
|
if( action->m_id == -1 )
|
|
|
|
action->m_id = MakeActionId( action->m_name );
|
|
|
|
|
2019-06-14 10:55:54 +00:00
|
|
|
RegisterAction( action );
|
2015-07-28 08:29:00 +00:00
|
|
|
}
|
2013-09-20 16:21:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-28 14:24:19 +00:00
|
|
|
ACTION_MANAGER::~ACTION_MANAGER()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-20 16:21:01 +00:00
|
|
|
void ACTION_MANAGER::RegisterAction( TOOL_ACTION* aAction )
|
|
|
|
{
|
2014-04-09 15:05:05 +00:00
|
|
|
// TOOL_ACTIONs are supposed to be named [appName.]toolName.actionName (with dots between)
|
|
|
|
// action name without specifying at least toolName is not valid
|
2018-04-12 06:47:09 +00:00
|
|
|
wxASSERT( aAction->GetName().find( '.', 0 ) != std::string::npos );
|
2014-04-09 15:05:05 +00:00
|
|
|
|
2014-05-14 14:29:53 +00:00
|
|
|
// TOOL_ACTIONs must have unique names & ids
|
2018-04-12 06:47:09 +00:00
|
|
|
wxASSERT( m_actionNameIndex.find( aAction->m_name ) == m_actionNameIndex.end() );
|
2013-09-20 16:21:01 +00:00
|
|
|
|
|
|
|
m_actionNameIndex[aAction->m_name] = aAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
void ACTION_MANAGER::SetConditions( const TOOL_ACTION& aAction,
|
|
|
|
const ACTION_CONDITIONS& aConditions )
|
2020-07-27 22:42:23 +00:00
|
|
|
{
|
2021-07-15 19:26:35 +00:00
|
|
|
// Remove any existing handlers with the old conditions to ensure the UI layer doesn't have
|
|
|
|
// stale data.
|
2020-07-27 22:42:23 +00:00
|
|
|
if( m_toolMgr )
|
|
|
|
m_toolMgr->GetToolHolder()->UnregisterUIUpdateHandler( aAction );
|
|
|
|
|
|
|
|
m_uiConditions[aAction.GetId()] = aConditions;
|
|
|
|
|
2020-08-14 01:33:25 +00:00
|
|
|
wxLogTrace( kicadTraceToolStack,
|
2023-01-17 12:42:30 +00:00
|
|
|
wxS( "ACTION_MANAGER::SetConditions: Registering conditions for ID %d - %s" ),
|
2020-08-14 01:33:25 +00:00
|
|
|
aAction.GetId(), aAction.GetName() );
|
|
|
|
|
2020-07-27 22:42:23 +00:00
|
|
|
// 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
|
|
|
|
{
|
2020-08-14 01:33:25 +00:00
|
|
|
const auto it = m_uiConditions.find( aAction.GetId() );
|
2020-07-27 22:42:23 +00:00
|
|
|
|
|
|
|
// If the action doesn't have something registered, then return null
|
|
|
|
if( it == m_uiConditions.end() )
|
|
|
|
return nullptr;
|
|
|
|
else
|
|
|
|
return &it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-20 16:21:01 +00:00
|
|
|
int ACTION_MANAGER::MakeActionId( const std::string& aActionName )
|
|
|
|
{
|
2013-09-27 18:52:34 +00:00
|
|
|
static int currentActionId = 1;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-20 16:21:01 +00:00
|
|
|
return currentActionId++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-09 14:25:50 +00:00
|
|
|
TOOL_ACTION* ACTION_MANAGER::FindAction( const std::string& aActionName ) const
|
2013-09-20 16:21:01 +00:00
|
|
|
{
|
|
|
|
std::map<std::string, TOOL_ACTION*>::const_iterator it = m_actionNameIndex.find( aActionName );
|
|
|
|
|
2014-07-09 14:25:50 +00:00
|
|
|
if( it != m_actionNameIndex.end() )
|
|
|
|
return it->second;
|
2013-09-20 16:21:01 +00:00
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
return nullptr;
|
2014-02-28 14:46:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-23 10:21:16 +00:00
|
|
|
bool ACTION_MANAGER::RunHotKey( int aHotKey ) const
|
2013-09-20 16:21:01 +00:00
|
|
|
{
|
2015-06-16 12:22:12 +00:00
|
|
|
int key = aHotKey & ~MD_MODIFIER_MASK;
|
2014-04-09 15:33:22 +00:00
|
|
|
int mod = aHotKey & MD_MODIFIER_MASK;
|
2013-09-20 16:21:01 +00:00
|
|
|
|
2015-07-24 07:42:45 +00:00
|
|
|
if( key >= 'a' && key <= 'z' )
|
|
|
|
key = std::toupper( key );
|
2015-06-16 12:22:12 +00:00
|
|
|
|
2023-01-17 12:42:30 +00:00
|
|
|
wxLogTrace( kicadTraceToolStack, wxS( "ACTION_MANAGER::RunHotKey Key: %s" ),
|
2020-05-02 13:36:29 +00:00
|
|
|
KeyNameFromKeyCode( aHotKey ) );
|
2019-07-27 14:11:41 +00:00
|
|
|
|
2014-04-09 15:33:22 +00:00
|
|
|
HOTKEY_LIST::const_iterator it = m_actionHotKeys.find( key | mod );
|
|
|
|
|
2015-03-16 09:34:45 +00:00
|
|
|
// If no luck, try without Shift, to handle keys that require it
|
2014-04-09 15:33:22 +00:00
|
|
|
// 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
|
2013-09-20 16:21:01 +00:00
|
|
|
if( it == m_actionHotKeys.end() )
|
2014-04-09 15:33:22 +00:00
|
|
|
{
|
2019-07-27 14:11:41 +00:00
|
|
|
wxLogTrace( kicadTraceToolStack,
|
2023-01-17 12:42:30 +00:00
|
|
|
wxS( "ACTION_MANAGER::RunHotKey No actions found, searching with key: %s" ),
|
2020-05-02 13:36:29 +00:00
|
|
|
KeyNameFromKeyCode( key | ( mod & ~MD_SHIFT ) ) );
|
|
|
|
|
2015-06-16 12:51:39 +00:00
|
|
|
it = m_actionHotKeys.find( key | ( mod & ~MD_SHIFT ) );
|
2014-04-09 15:33:22 +00:00
|
|
|
|
|
|
|
if( it == m_actionHotKeys.end() )
|
|
|
|
return false; // no appropriate action found for the hotkey
|
|
|
|
}
|
2013-09-20 16:21:01 +00:00
|
|
|
|
2014-04-09 15:05:05 +00:00
|
|
|
const std::list<TOOL_ACTION*>& actions = it->second;
|
2013-09-20 16:21:01 +00:00
|
|
|
|
2014-04-09 15:05:05 +00:00
|
|
|
// 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;
|
2021-07-15 19:26:35 +00:00
|
|
|
const TOOL_ACTION* context = nullptr; // pointer to context action of the highest priority tool
|
2019-08-08 23:10:31 +00:00
|
|
|
std::vector<const TOOL_ACTION*> global; // pointers to global actions
|
|
|
|
// if there is no context action
|
2014-04-09 15:05:05 +00:00
|
|
|
|
2016-06-29 20:07:55 +00:00
|
|
|
for( const TOOL_ACTION* action : actions )
|
2014-04-09 15:05:05 +00:00
|
|
|
{
|
|
|
|
if( action->GetScope() == AS_GLOBAL )
|
|
|
|
{
|
2019-06-12 10:22:23 +00:00
|
|
|
// Store the global action in case there are no context actions to run
|
2019-08-08 23:10:31 +00:00
|
|
|
global.emplace_back( action );
|
2014-04-09 15:05:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
TOOL_BASE* tool = m_toolMgr->FindTool( action->GetToolName() );
|
|
|
|
|
|
|
|
if( tool )
|
|
|
|
{
|
2014-07-09 14:25:50 +00:00
|
|
|
// Choose the action that goes to the tool with highest priority
|
|
|
|
// (i.e. is on the top of active tools stack)
|
2014-04-09 15:05:05 +00:00
|
|
|
priority = m_toolMgr->GetPriority( tool->GetId() );
|
|
|
|
|
|
|
|
if( priority >= 0 && priority > highestPriority )
|
|
|
|
{
|
|
|
|
highestPriority = priority;
|
|
|
|
context = action;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-20 16:21:01 +00:00
|
|
|
|
2020-08-14 01:33:25 +00:00
|
|
|
// Get the selection to use to test if the action is enabled
|
|
|
|
SELECTION& sel = m_toolMgr->GetToolHolder()->GetCurrentSelection();
|
|
|
|
|
2014-04-09 15:05:05 +00:00
|
|
|
if( context )
|
2014-07-09 14:25:50 +00:00
|
|
|
{
|
2020-08-14 01:33:25 +00:00
|
|
|
bool runAction = true;
|
|
|
|
|
|
|
|
if( const ACTION_CONDITIONS* aCond = GetCondition( *context ) )
|
|
|
|
runAction = aCond->enableCondition( sel );
|
|
|
|
|
2019-07-27 14:11:41 +00:00
|
|
|
wxLogTrace( kicadTraceToolStack,
|
2023-01-17 12:42:30 +00:00
|
|
|
wxS( "ACTION_MANAGER::RunHotKey %s context action: %s for hotkey %s" ),
|
|
|
|
runAction ? wxS( "Running" ) : wxS( "Not running" ),
|
2020-05-02 13:36:29 +00:00
|
|
|
context->GetName(),
|
|
|
|
KeyNameFromKeyCode( aHotKey ) );
|
2019-07-27 14:11:41 +00:00
|
|
|
|
2020-08-14 01:33:25 +00:00
|
|
|
if( runAction )
|
|
|
|
return m_toolMgr->RunAction( *context, true );
|
2014-07-09 14:25:50 +00:00
|
|
|
}
|
2019-08-08 23:10:31 +00:00
|
|
|
else if( !global.empty() )
|
2014-07-09 14:25:50 +00:00
|
|
|
{
|
2022-07-29 21:02:35 +00:00
|
|
|
for( const TOOL_ACTION* act : global )
|
2019-08-08 23:10:31 +00:00
|
|
|
{
|
2020-08-14 01:33:25 +00:00
|
|
|
bool runAction = true;
|
|
|
|
|
|
|
|
if( const ACTION_CONDITIONS* aCond = GetCondition( *act ) )
|
|
|
|
runAction = aCond->enableCondition( sel );
|
|
|
|
|
2019-08-08 23:10:31 +00:00
|
|
|
wxLogTrace( kicadTraceToolStack,
|
2023-01-17 12:42:30 +00:00
|
|
|
wxS( "ACTION_MANAGER::RunHotKey %s global action: %s for hotkey %s" ),
|
|
|
|
runAction ? wxS( "Running" ) : wxS( "Not running" ),
|
2020-05-02 13:36:29 +00:00
|
|
|
act->GetName(),
|
|
|
|
KeyNameFromKeyCode( aHotKey ) );
|
2019-07-27 14:11:41 +00:00
|
|
|
|
2020-08-14 01:33:25 +00:00
|
|
|
if( runAction && m_toolMgr->RunAction( *act, true ) )
|
2019-08-08 23:10:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-07-09 14:25:50 +00:00
|
|
|
}
|
2014-04-09 15:05:05 +00:00
|
|
|
|
2020-05-02 13:36:29 +00:00
|
|
|
wxLogTrace( kicadTraceToolStack,
|
2023-01-17 12:42:30 +00:00
|
|
|
wxS( "ACTION_MANAGER::RunHotKey No action found for key %s" ),
|
2020-05-02 13:36:29 +00:00
|
|
|
KeyNameFromKeyCode( aHotKey ) );
|
2019-07-27 14:11:41 +00:00
|
|
|
|
2014-07-09 14:25:50 +00:00
|
|
|
return false;
|
2013-12-05 13:48:44 +00:00
|
|
|
}
|
2015-05-05 18:39:42 +00:00
|
|
|
|
|
|
|
|
2020-10-27 11:03:35 +00:00
|
|
|
const std::map<std::string, TOOL_ACTION*>& ACTION_MANAGER::GetActions() const
|
2019-06-09 21:57:23 +00:00
|
|
|
{
|
|
|
|
return m_actionNameIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-28 08:29:00 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-12 10:22:23 +00:00
|
|
|
void ACTION_MANAGER::UpdateHotKeys( bool aFullUpdate )
|
2015-05-05 18:39:42 +00:00
|
|
|
{
|
2022-06-02 23:35:19 +00:00
|
|
|
static std::map<std::string, int> legacyHotKeyMap;
|
|
|
|
static std::map<std::string, int> userHotKeyMap;
|
|
|
|
static bool mapsInitialized = false;
|
2019-06-09 21:57:23 +00:00
|
|
|
|
2015-05-05 18:39:42 +00:00
|
|
|
m_actionHotKeys.clear();
|
2015-07-28 08:29:00 +00:00
|
|
|
m_hotkeys.clear();
|
2019-06-12 10:22:23 +00:00
|
|
|
|
2022-06-02 23:35:19 +00:00
|
|
|
if( aFullUpdate && !mapsInitialized && m_toolMgr->GetToolHolder() )
|
2019-06-12 10:22:23 +00:00
|
|
|
{
|
2020-03-24 01:01:23 +00:00
|
|
|
ReadLegacyHotkeyConfig( m_toolMgr->GetToolHolder()->ConfigBaseName(), legacyHotKeyMap );
|
2019-06-12 10:22:23 +00:00
|
|
|
ReadHotKeyConfig( wxEmptyString, userHotKeyMap );
|
2022-06-02 23:35:19 +00:00
|
|
|
mapsInitialized = true;
|
2019-06-12 10:22:23 +00:00
|
|
|
}
|
2015-05-05 18:39:42 +00:00
|
|
|
|
2019-06-14 10:55:54 +00:00
|
|
|
for( const auto& ii : m_actionNameIndex )
|
2015-05-05 18:39:42 +00:00
|
|
|
{
|
2019-06-14 10:55:54 +00:00
|
|
|
TOOL_ACTION* action = ii.second;
|
2019-06-12 10:22:23 +00:00
|
|
|
int hotkey = 0;
|
2015-05-05 18:39:42 +00:00
|
|
|
|
2019-06-12 10:22:23 +00:00
|
|
|
if( aFullUpdate )
|
|
|
|
hotkey = processHotKey( action, legacyHotKeyMap, userHotKeyMap );
|
|
|
|
else
|
|
|
|
hotkey = action->GetHotKey();
|
2015-05-05 18:39:42 +00:00
|
|
|
|
2019-06-12 10:22:23 +00:00
|
|
|
if( hotkey > 0 )
|
|
|
|
m_actionHotKeys[hotkey].push_back( action );
|
2019-06-14 10:55:54 +00:00
|
|
|
|
|
|
|
m_hotkeys[action->GetId()] = hotkey;
|
2015-05-05 18:39:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-02 23:35:19 +00:00
|
|
|
int ACTION_MANAGER::processHotKey( TOOL_ACTION* aAction,
|
|
|
|
const std::map<std::string, int>& aLegacyMap,
|
|
|
|
const std::map<std::string, int>& aHotKeyMap )
|
2015-05-05 18:39:42 +00:00
|
|
|
{
|
2019-06-09 21:57:23 +00:00
|
|
|
aAction->m_hotKey = aAction->m_defaultHotKey;
|
2019-06-26 11:17:03 +00:00
|
|
|
|
2019-06-09 21:57:23 +00:00
|
|
|
if( !aAction->m_legacyName.empty() && aLegacyMap.count( aAction->m_legacyName ) )
|
2022-06-02 23:35:19 +00:00
|
|
|
aAction->SetHotKey( aLegacyMap.at( aAction->m_legacyName ) );
|
2019-06-26 11:17:03 +00:00
|
|
|
|
2019-06-09 21:57:23 +00:00
|
|
|
if( aHotKeyMap.count( aAction->m_name ) )
|
2022-06-02 23:35:19 +00:00
|
|
|
aAction->SetHotKey( aHotKeyMap.at( aAction->m_name ) );
|
2019-06-26 11:17:03 +00:00
|
|
|
|
2019-06-09 21:57:23 +00:00
|
|
|
return aAction->m_hotKey;
|
2015-05-05 18:39:42 +00:00
|
|
|
}
|