Files
o3de/Gems/EMotionFX/Code/MysticQt/Source/KeyboardShortcutManager.h
T
Chris Burel 13c7b06308 Handle EMotionFX hotkeys with QActions instead of in keyPressEvent (#514)
EMotionFX has user-customizable hotkeys. These hotkeys are registered by
individual plugins, and then the user can set what they want the hotkey to
be. The way this was implemented was by reimplementing `keyPressEvent` and
`keyReleaseEvent` for each widget that used customizable hotkeys, and in
there call `KeyboardShortcutManager::Check` to see if key press matched any
existing hotkey mapping.

However, the main Editor has behavior that prevents events from reaching
EMotionFX's `keyPressEvent` method, if a keypress matches a hotkey that is
also used by the main Editor. This is due to the global event filter
defined in `ShortcutDispatcher::eventFilter`. This event filter takes a Qt
`Shortcut` event, and will re-dispatch that event to all parent widgets of
a given receiver. So if a parent widget, like the main Editor, *does* have
a QAction that matches a given key sequence, that widget will receive the
event, the event is marked as processed, and no `KeyPress` event is ever
sent to the original widget where the event occurred.

All this means that processing hotkeys in a `keyPressEvent` won't work
reliably. The main editor defines a hotkey for the `delete` key, so that
can never be received in a `keyPressEvent` by any child widget of the
Editor.

This change removes all the hotkey logic from the `keyPressEvent` methods,
and replaces them with `QAction` instances. Hotkeys are defined with
`QAction::setShortcut`, and added to each widget that they apply to.

In addition, the `KeyboardShortcutManager` class had to be adjusted to suit
this new way of defining the hotkeys. It now has a pointer to each
`QAction*` that can have a customizable hotkey. It has also been greatly
simplified, since it can use a `QKeySequence` instead of separate variables
for `int key; bool hasCtrlModifier; bool hasAltModifier`.

Applying user-defined hotkeys now has to be done after the hotkeys are
registered from a plugin. It is the plugin's responsibility to reload the
user-defined hotkeys after registering all of its actions.
2021-05-03 15:00:40 -07:00

86 lines
3.0 KiB
C++

/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#if !defined(Q_MOC_RUN)
#include <QKeySequence>
#include <QAction>
#include <AzCore/std/smart_ptr/unique_ptr.h>
#include <AzCore/std/string/string.h>
#include <AzCore/std/containers/vector.h>
#include <MCore/Source/StandardHeaders.h>
#include "MysticQtConfig.h"
#endif
class QKeyEvent;
class QSettings;
namespace MysticQt
{
class MYSTICQT_API KeyboardShortcutManager
: public QObject
{
public:
struct Action
{
QAction* m_qaction;
QKeySequence m_defaultKeySequence;
bool m_local;
Action(QAction* qaction, bool local)
: m_qaction(qaction)
, m_defaultKeySequence(qaction->shortcut())
, m_local(local)
{
}
};
class Group
{
public:
Group(AZStd::string_view groupName)
: m_name(groupName)
{
}
void AddAction(AZStd::unique_ptr<Action> action) { m_actions.emplace_back(AZStd::move(action)); }
void RemoveAction(QAction* action, bool local);
size_t GetNumActions() const { return m_actions.size(); }
Action* GetAction(size_t index) { return m_actions[index].get(); }
const AZStd::vector<AZStd::unique_ptr<Action>>& GetActions() const { return m_actions; }
const AZStd::string& GetName() const { return m_name; }
Action* FindActionByName(const QString& actionName, bool local) const;
private:
AZStd::string m_name;
AZStd::vector<AZStd::unique_ptr<Action>> m_actions;
};
void RegisterKeyboardShortcut(QAction* qaction, AZStd::string_view groupName, bool local);
void UnregisterKeyboardShortcut(QAction* qaction, AZStd::string_view groupName, bool local);
Action* FindShortcut(QKeySequence keySequence, Group* group) const;
Action* FindAction(const QString& actionName, AZStd::string_view groupName) const;
Group* FindGroupForShortcut(Action* action) const;
size_t GetNumGroups() const { return m_groups.size(); }
Group* GetGroup(size_t index) const { return m_groups[index].get(); }
void Save(QSettings* settings);
void Load(QSettings* settings);
private:
AZStd::vector<AZStd::unique_ptr<Group>> m_groups;
Group* FindGroupByName(AZStd::string_view groupName) const;
};
} // namespace MysticQt