| 1 |
/**************************************************************************** |
| 2 |
* Copyright (c) 2022 Zheng Lei (realthunder) <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
****************************************************************************/ |
| 22 |
|
| 23 |
#ifndef GUI_SHORTCUT_MANAGER_H |
| 24 |
#define GUI_SHORTCUT_MANAGER_H |
| 25 |
|
| 26 |
#include <unordered_map> |
| 27 |
|
| 28 |
#include <boost/multi_index_container.hpp> |
| 29 |
#include <boost/multi_index/hashed_index.hpp> |
| 30 |
#include <boost/multi_index/ordered_index.hpp> |
| 31 |
#include <boost/multi_index/member.hpp> |
| 32 |
|
| 33 |
#include <QAction> |
| 34 |
#include <QKeySequence> |
| 35 |
#include <QPointer> |
| 36 |
#include <QTimer> |
| 37 |
|
| 38 |
#include <Base/Parameter.h> |
| 39 |
|
| 40 |
namespace Gui |
| 41 |
{ |
| 42 |
|
| 43 |
class Command; |
| 44 |
namespace bmi = boost::multi_index; |
| 45 |
|
| 46 |
class GuiExport ShortcutManager: public QObject, public ParameterGrp::ObserverType |
| 47 |
{ |
| 48 |
Q_OBJECT |
| 49 |
|
| 50 |
public: |
| 51 |
ShortcutManager(); |
| 52 |
~ShortcutManager() override; |
| 53 |
|
| 54 |
static ShortcutManager* instance(); |
| 55 |
static void destroy(); |
| 56 |
|
| 57 |
void OnChange(Base::Subject<const char*>&, const char* reason) override; |
| 58 |
|
| 59 |
/// Clear all user defined shortcut |
| 60 |
void resetAll(); |
| 61 |
/// Clear the user defined shortcut of a given command |
| 62 |
void reset(const char* cmd); |
| 63 |
/// Set shortcut of a given command |
| 64 |
void setShortcut(const char* cmd, const char* accel); |
| 65 |
/** Get shortcut of a given command |
| 66 |
* @param cmd: command name |
| 67 |
* @param defaultAccel: default shortcut |
| 68 |
*/ |
| 69 |
QString getShortcut(const char* cmd, const char* defaultAccel = nullptr); |
| 70 |
|
| 71 |
/// Return actions having a given shortcut in order of decreasing priority |
| 72 |
std::vector<std::pair<QByteArray, QAction*>> getActionsByShortcut(const QKeySequence& shortcut); |
| 73 |
|
| 74 |
/// Set properties for a given list of actions in order of decreasing priority |
| 75 |
void setPriorities(const std::vector<QByteArray>& actions); |
| 76 |
|
| 77 |
/** Set the priority of a given command |
| 78 |
* @param cmd: command name |
| 79 |
* @param priority: priority of the command, bigger value means higher priority |
| 80 |
*/ |
| 81 |
void setPriority(const char* cmd, int priority); |
| 82 |
|
| 83 |
/// Get the priority of a given command |
| 84 |
int getPriority(const char* cmd); |
| 85 |
|
| 86 |
/** Set the top priority of a given command |
| 87 |
* Make the given command the top priority of all commands. |
| 88 |
* |
| 89 |
* @param cmd: command name |
| 90 |
*/ |
| 91 |
void setTopPriority(const char* cmd); |
| 92 |
|
| 93 |
Q_SIGNALS: |
| 94 |
void shortcutChanged(const char* name, const QKeySequence& oldShortcut); |
| 95 |
void actionShortcutChanged(QAction*, const QKeySequence& oldShortcut); |
| 96 |
void priorityChanged(const char* name, int priority); |
| 97 |
|
| 98 |
protected: |
| 99 |
bool eventFilter(QObject*, QEvent* ev) override; |
| 100 |
bool checkShortcut(QObject* o, const QKeySequence& key); |
| 101 |
void onTimer(); |
| 102 |
|
| 103 |
private: |
| 104 |
ParameterGrp::handle hShortcuts; |
| 105 |
ParameterGrp::handle hPriorities; |
| 106 |
ParameterGrp::handle hSetting; |
| 107 |
bool busy = false; |
| 108 |
|
| 109 |
struct ActionKey |
| 110 |
{ |
| 111 |
QKeySequence shortcut; |
| 112 |
QByteArray name; |
| 113 |
explicit ActionKey(const QKeySequence& shortcut, const char* name = "") |
| 114 |
: shortcut(shortcut) |
| 115 |
, name(name) |
| 116 |
{} |
| 117 |
bool operator<(const ActionKey& other) const |
| 118 |
{ |
| 119 |
if (shortcut > other.shortcut) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
if (shortcut < other.shortcut) { |
| 123 |
return true; |
| 124 |
} |
| 125 |
return name < other.name; |
| 126 |
} |
| 127 |
}; |
| 128 |
struct ActionData |
| 129 |
{ |
| 130 |
ActionKey key; |
| 131 |
intptr_t pointer; |
| 132 |
QPointer<QAction> action; |
| 133 |
|
| 134 |
explicit ActionData(QAction* action, const char* name = "") |
| 135 |
: key(action->shortcut(), name) |
| 136 |
, pointer(reinterpret_cast<intptr_t>(action)) |
| 137 |
, action(action) |
| 138 |
{} |
| 139 |
}; |
| 140 |
bmi::multi_index_container< |
| 141 |
ActionData, |
| 142 |
bmi::indexed_by< |
| 143 |
// hashed index on ActionData::Action pointer |
| 144 |
bmi::hashed_unique<bmi::member<ActionData, intptr_t, &ActionData::pointer>>, |
| 145 |
// ordered index on shortcut + name |
| 146 |
bmi::ordered_non_unique<bmi::member<ActionData, ActionKey, &ActionData::key>>>> |
| 147 |
actionMap; |
| 148 |
|
| 149 |
std::unordered_map<std::string, int> priorities; |
| 150 |
int topPriority; |
| 151 |
|
| 152 |
struct ActionInfo |
| 153 |
{ |
| 154 |
QPointer<QAction> action; |
| 155 |
int seq_length; |
| 156 |
int priority; |
| 157 |
|
| 158 |
ActionInfo(QAction* action, int l, int p) |
| 159 |
: action(action) |
| 160 |
, seq_length(l) |
| 161 |
, priority(p) |
| 162 |
{} |
| 163 |
}; |
| 164 |
std::vector<ActionInfo> pendingActions; |
| 165 |
|
| 166 |
QKeySequence pendingSequence; |
| 167 |
|
| 168 |
QPointer<QWidget> lastFocus; |
| 169 |
|
| 170 |
QTimer timer; |
| 171 |
int timeout; |
| 172 |
}; |
| 173 |
|
| 174 |
} // namespace Gui |
| 175 |
|
| 176 |
#endif // GUI_SHORTCUT_MANAGER_H |