All: Reformat according to new standard
This commit is contained in:
committed by
Kacper Donat
parent
eafd18dac0
commit
25c3ba7338
@@ -20,9 +20,9 @@
|
||||
* *
|
||||
****************************************************************************/
|
||||
|
||||
# include <limits>
|
||||
# include <QShortcutEvent>
|
||||
# include <QApplication>
|
||||
#include <limits>
|
||||
#include <QShortcutEvent>
|
||||
#include <QApplication>
|
||||
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
|
||||
@@ -46,16 +46,18 @@ ShortcutManager::ShortcutManager()
|
||||
timeout = hSetting->GetInt("ShortcutTimeout", 300);
|
||||
timer.setSingleShot(true);
|
||||
|
||||
QObject::connect(&timer, &QTimer::timeout, [this](){onTimer();});
|
||||
QObject::connect(&timer, &QTimer::timeout, [this]() { onTimer(); });
|
||||
|
||||
topPriority = 0;
|
||||
for (const auto &v : hPriorities->GetIntMap()) {
|
||||
for (const auto& v : hPriorities->GetIntMap()) {
|
||||
priorities[v.first] = v.second;
|
||||
if (topPriority < v.second)
|
||||
if (topPriority < v.second) {
|
||||
topPriority = v.second;
|
||||
}
|
||||
}
|
||||
if (topPriority == 0)
|
||||
if (topPriority == 0) {
|
||||
topPriority = 100;
|
||||
}
|
||||
|
||||
QApplication::instance()->installEventFilter(this);
|
||||
}
|
||||
@@ -67,11 +69,12 @@ ShortcutManager::~ShortcutManager()
|
||||
hPriorities->Detach(this);
|
||||
}
|
||||
|
||||
static ShortcutManager *Instance;
|
||||
ShortcutManager *ShortcutManager::instance()
|
||||
static ShortcutManager* Instance;
|
||||
ShortcutManager* ShortcutManager::instance()
|
||||
{
|
||||
if (!Instance)
|
||||
if (!Instance) {
|
||||
Instance = new ShortcutManager;
|
||||
}
|
||||
return Instance;
|
||||
}
|
||||
|
||||
@@ -81,25 +84,30 @@ void ShortcutManager::destroy()
|
||||
Instance = nullptr;
|
||||
}
|
||||
|
||||
void ShortcutManager::OnChange(Base::Subject<const char*> &src, const char *reason)
|
||||
void ShortcutManager::OnChange(Base::Subject<const char*>& src, const char* reason)
|
||||
{
|
||||
if (hSetting == &src) {
|
||||
if (boost::equals(reason, "ShortcutTimeout"))
|
||||
if (boost::equals(reason, "ShortcutTimeout")) {
|
||||
timeout = hSetting->GetInt("ShortcutTimeout");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (busy)
|
||||
if (busy) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (hPriorities == &src) {
|
||||
int p = hPriorities->GetInt(reason, 0);
|
||||
if (p == 0)
|
||||
if (p == 0) {
|
||||
priorities.erase(reason);
|
||||
else
|
||||
}
|
||||
else {
|
||||
priorities[reason] = p;
|
||||
if (topPriority < p)
|
||||
}
|
||||
if (topPriority < p) {
|
||||
topPriority = p;
|
||||
}
|
||||
priorityChanged(reason, p);
|
||||
return;
|
||||
}
|
||||
@@ -108,7 +116,9 @@ void ShortcutManager::OnChange(Base::Subject<const char*> &src, const char *reas
|
||||
auto cmd = Application::Instance->commandManager().getCommandByName(reason);
|
||||
if (cmd) {
|
||||
auto accel = cmd->getAccel();
|
||||
if (!accel) accel = "";
|
||||
if (!accel) {
|
||||
accel = "";
|
||||
}
|
||||
QKeySequence oldShortcut = cmd->getShortcut();
|
||||
QKeySequence newShortcut = getShortcut(reason, accel);
|
||||
if (oldShortcut != newShortcut) {
|
||||
@@ -118,18 +128,20 @@ void ShortcutManager::OnChange(Base::Subject<const char*> &src, const char *reas
|
||||
}
|
||||
}
|
||||
|
||||
void ShortcutManager::reset(const char *cmd)
|
||||
void ShortcutManager::reset(const char* cmd)
|
||||
{
|
||||
if (!Base::Tools::isNullOrEmpty(cmd)) {
|
||||
QKeySequence oldShortcut = getShortcut(cmd);
|
||||
hShortcuts->RemoveASCII(cmd);
|
||||
if (oldShortcut != getShortcut(cmd))
|
||||
if (oldShortcut != getShortcut(cmd)) {
|
||||
shortcutChanged(cmd, oldShortcut);
|
||||
}
|
||||
|
||||
int oldPriority = getPriority(cmd);
|
||||
hPriorities->RemoveInt(cmd);
|
||||
if (oldPriority != getPriority(cmd))
|
||||
if (oldPriority != getPriority(cmd)) {
|
||||
priorityChanged(cmd, oldPriority);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,7 +154,9 @@ void ShortcutManager::resetAll()
|
||||
for (auto cmd : Application::Instance->commandManager().getAllCommands()) {
|
||||
if (cmd->getAction()) {
|
||||
auto accel = cmd->getAccel();
|
||||
if (!accel) accel = "";
|
||||
if (!accel) {
|
||||
accel = "";
|
||||
}
|
||||
cmd->setShortcut(getShortcut(nullptr, accel));
|
||||
}
|
||||
}
|
||||
@@ -151,55 +165,64 @@ void ShortcutManager::resetAll()
|
||||
priorityChanged("", 0);
|
||||
}
|
||||
|
||||
QString ShortcutManager::getShortcut(const char *cmdName, const char *accel)
|
||||
QString ShortcutManager::getShortcut(const char* cmdName, const char* accel)
|
||||
{
|
||||
if (!accel) {
|
||||
if (auto cmd = Application::Instance->commandManager().getCommandByName(cmdName)) {
|
||||
accel = cmd->getAccel();
|
||||
if (!accel)
|
||||
if (!accel) {
|
||||
accel = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
QString shortcut;
|
||||
if (cmdName)
|
||||
if (cmdName) {
|
||||
shortcut = QString::fromLatin1(hShortcuts->GetASCII(cmdName, accel).c_str());
|
||||
else
|
||||
}
|
||||
else {
|
||||
shortcut = QString::fromLatin1(accel);
|
||||
}
|
||||
return QKeySequence(shortcut).toString(QKeySequence::NativeText);
|
||||
}
|
||||
|
||||
void ShortcutManager::setShortcut(const char *cmdName, const char *accel)
|
||||
void ShortcutManager::setShortcut(const char* cmdName, const char* accel)
|
||||
{
|
||||
if (!Base::Tools::isNullOrEmpty(cmdName)) {
|
||||
setTopPriority(cmdName);
|
||||
if (!accel)
|
||||
if (!accel) {
|
||||
accel = "";
|
||||
}
|
||||
if (auto cmd = Application::Instance->commandManager().getCommandByName(cmdName)) {
|
||||
auto defaultAccel = cmd->getAccel();
|
||||
if (!defaultAccel)
|
||||
defaultAccel = "";
|
||||
if (QKeySequence(QString::fromLatin1(accel)) == QKeySequence(QString::fromLatin1(defaultAccel))) {
|
||||
if (!defaultAccel) {
|
||||
defaultAccel = "";
|
||||
}
|
||||
if (QKeySequence(QString::fromLatin1(accel))
|
||||
== QKeySequence(QString::fromLatin1(defaultAccel))) {
|
||||
hShortcuts->RemoveASCII(cmdName);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
hShortcuts->SetASCII(cmdName, accel);
|
||||
}
|
||||
}
|
||||
|
||||
bool ShortcutManager::checkShortcut(QObject *o, const QKeySequence &key)
|
||||
bool ShortcutManager::checkShortcut(QObject* o, const QKeySequence& key)
|
||||
{
|
||||
auto focus = QApplication::focusWidget();
|
||||
if (!focus)
|
||||
if (!focus) {
|
||||
return false;
|
||||
}
|
||||
auto action = qobject_cast<QAction*>(o);
|
||||
if (!action)
|
||||
if (!action) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto &index = actionMap.get<1>();
|
||||
const auto& index = actionMap.get<1>();
|
||||
auto iter = index.lower_bound(ActionKey(key));
|
||||
if (iter == index.end())
|
||||
if (iter == index.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// disable and enqueue the action in order to try other alternativeslll
|
||||
action->setEnabled(false);
|
||||
@@ -209,8 +232,9 @@ bool ShortcutManager::checkShortcut(QObject *o, const QKeySequence &key)
|
||||
bool flush = true;
|
||||
bool found = false;
|
||||
for (auto it = iter; it != index.end(); ++it) {
|
||||
if (key.matches(it->key.shortcut) == QKeySequence::NoMatch)
|
||||
if (key.matches(it->key.shortcut) == QKeySequence::NoMatch) {
|
||||
break;
|
||||
}
|
||||
if (action == it->action) {
|
||||
// There maybe more than one action with the exact same shortcut.
|
||||
// However, we only disable and enqueue the triggered action.
|
||||
@@ -225,8 +249,9 @@ bool ShortcutManager::checkShortcut(QObject *o, const QKeySequence &key)
|
||||
}
|
||||
else if (it->action && it->action->isEnabled()) {
|
||||
flush = false;
|
||||
if (found)
|
||||
if (found) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,23 +270,27 @@ bool ShortcutManager::checkShortcut(QObject *o, const QKeySequence &key)
|
||||
// unless I'm mistaken?). We'll do longest match. We've disabled all
|
||||
// shortcuts that can match the current key sequence. Now replay the sequence
|
||||
// and wait for the next keystroke.
|
||||
for (int i=0; i<key.count(); ++i) {
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
|
||||
for (int i = 0; i < key.count(); ++i) {
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
int k = key[i];
|
||||
#else
|
||||
int k = key[i].key();
|
||||
#endif
|
||||
Qt::KeyboardModifiers modifiers;
|
||||
if ((k & Qt::SHIFT) == Qt::SHIFT)
|
||||
if ((k & Qt::SHIFT) == Qt::SHIFT) {
|
||||
modifiers |= Qt::ShiftModifier;
|
||||
if ((k & Qt::CTRL) == Qt::CTRL)
|
||||
}
|
||||
if ((k & Qt::CTRL) == Qt::CTRL) {
|
||||
modifiers |= Qt::ControlModifier;
|
||||
if ((k & Qt::ALT) == Qt::ALT)
|
||||
}
|
||||
if ((k & Qt::ALT) == Qt::ALT) {
|
||||
modifiers |= Qt::AltModifier;
|
||||
if ((k & Qt::META) == Qt::META)
|
||||
}
|
||||
if ((k & Qt::META) == Qt::META) {
|
||||
modifiers |= Qt::MetaModifier;
|
||||
k &= ~(Qt::SHIFT|Qt::CTRL|Qt::ALT|Qt::META);
|
||||
QKeyEvent *kev = new QKeyEvent(QEvent::KeyPress, k, modifiers, 0, 0, 0);
|
||||
}
|
||||
k &= ~(Qt::SHIFT | Qt::CTRL | Qt::ALT | Qt::META);
|
||||
QKeyEvent* kev = new QKeyEvent(QEvent::KeyPress, k, modifiers, 0, 0, 0);
|
||||
QApplication::postEvent(focus, kev);
|
||||
kev = new QKeyEvent(QEvent::KeyRelease, k, modifiers, 0, 0, 0);
|
||||
QApplication::postEvent(focus, kev);
|
||||
@@ -270,133 +299,155 @@ bool ShortcutManager::checkShortcut(QObject *o, const QKeySequence &key)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShortcutManager::eventFilter(QObject *o, QEvent *ev)
|
||||
bool ShortcutManager::eventFilter(QObject* o, QEvent* ev)
|
||||
{
|
||||
switch(ev->type()) {
|
||||
case QEvent::KeyPress:
|
||||
lastFocus = nullptr;
|
||||
break;
|
||||
case QEvent::Shortcut:
|
||||
if (timeout > 0) {
|
||||
auto sev = static_cast<QShortcutEvent*>(ev);
|
||||
if (checkShortcut(o, sev->key())) {
|
||||
// shortcut event handled here, so filter out the event
|
||||
return true;
|
||||
} else {
|
||||
// Not handled. Clear any existing pending actions.
|
||||
timer.stop();
|
||||
for (const auto &info : pendingActions) {
|
||||
if (info.action)
|
||||
info.action->setEnabled(true);
|
||||
switch (ev->type()) {
|
||||
case QEvent::KeyPress:
|
||||
lastFocus = nullptr;
|
||||
break;
|
||||
case QEvent::Shortcut:
|
||||
if (timeout > 0) {
|
||||
auto sev = static_cast<QShortcutEvent*>(ev);
|
||||
if (checkShortcut(o, sev->key())) {
|
||||
// shortcut event handled here, so filter out the event
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
// Not handled. Clear any existing pending actions.
|
||||
timer.stop();
|
||||
for (const auto& info : pendingActions) {
|
||||
if (info.action) {
|
||||
info.action->setEnabled(true);
|
||||
}
|
||||
}
|
||||
pendingActions.clear();
|
||||
lastFocus = nullptr;
|
||||
}
|
||||
pendingActions.clear();
|
||||
lastFocus = nullptr;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case QEvent::ActionChanged:
|
||||
if (auto action = qobject_cast<QAction*>(o)) {
|
||||
auto &index = actionMap.get<0>();
|
||||
auto it = index.find(reinterpret_cast<intptr_t>(action));
|
||||
if (action->shortcut().isEmpty()) {
|
||||
break;
|
||||
case QEvent::ActionChanged:
|
||||
if (auto action = qobject_cast<QAction*>(o)) {
|
||||
auto& index = actionMap.get<0>();
|
||||
auto it = index.find(reinterpret_cast<intptr_t>(action));
|
||||
if (action->shortcut().isEmpty()) {
|
||||
if (it != index.end()) {
|
||||
QKeySequence oldShortcut = it->key.shortcut;
|
||||
index.erase(it);
|
||||
actionShortcutChanged(action, oldShortcut);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
QByteArray name;
|
||||
if (auto fcAction = qobject_cast<Action*>(action->parent())) {
|
||||
if (fcAction->command() && fcAction->command()->getName()) {
|
||||
name = fcAction->command()->getName();
|
||||
}
|
||||
}
|
||||
if (name.isEmpty()) {
|
||||
name = action->objectName().size() ? action->objectName().toUtf8()
|
||||
: action->text().toUtf8();
|
||||
if (name.isEmpty()) {
|
||||
name = "~";
|
||||
}
|
||||
else {
|
||||
name = QByteArray("~ ") + name;
|
||||
}
|
||||
}
|
||||
if (it != index.end()) {
|
||||
if (it->key.shortcut == action->shortcut() && it->key.name == name) {
|
||||
break;
|
||||
}
|
||||
QKeySequence oldShortcut = it->key.shortcut;
|
||||
index.erase(it);
|
||||
index.replace(it, ActionData {action, name});
|
||||
actionShortcutChanged(action, oldShortcut);
|
||||
}
|
||||
break;
|
||||
else {
|
||||
index.insert(ActionData {action, name});
|
||||
actionShortcutChanged(action, QKeySequence());
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray name;
|
||||
if (auto fcAction = qobject_cast<Action*>(action->parent())) {
|
||||
if (fcAction->command() && fcAction->command()->getName())
|
||||
name = fcAction->command()->getName();
|
||||
}
|
||||
if (name.isEmpty()) {
|
||||
name = action->objectName().size() ?
|
||||
action->objectName().toUtf8() : action->text().toUtf8();
|
||||
if (name.isEmpty())
|
||||
name = "~";
|
||||
else
|
||||
name = QByteArray("~ ") + name;
|
||||
}
|
||||
if (it != index.end()) {
|
||||
if (it->key.shortcut == action->shortcut() && it->key.name == name)
|
||||
break;
|
||||
QKeySequence oldShortcut = it->key.shortcut;
|
||||
index.replace(it, ActionData{action, name});
|
||||
actionShortcutChanged(action, oldShortcut);
|
||||
} else {
|
||||
index.insert(ActionData{action, name});
|
||||
actionShortcutChanged(action, QKeySequence());
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<std::pair<QByteArray, QAction*>> ShortcutManager::getActionsByShortcut(const QKeySequence &shortcut)
|
||||
std::vector<std::pair<QByteArray, QAction*>> ShortcutManager::getActionsByShortcut(
|
||||
const QKeySequence& shortcut
|
||||
)
|
||||
{
|
||||
const auto &index = actionMap.get<1>();
|
||||
const auto& index = actionMap.get<1>();
|
||||
std::vector<std::pair<QByteArray, QAction*>> res;
|
||||
std::multimap<int, const ActionData*, std::greater<>> map;
|
||||
for (auto it = index.lower_bound(ActionKey(shortcut)); it != index.end(); ++it) {
|
||||
if (it->key.shortcut != shortcut)
|
||||
if (it->key.shortcut != shortcut) {
|
||||
break;
|
||||
if (it->key.name != "~" && it->action)
|
||||
}
|
||||
if (it->key.name != "~" && it->action) {
|
||||
map.emplace(getPriority(it->key.name), &(*it));
|
||||
}
|
||||
}
|
||||
for (const auto &v : map)
|
||||
for (const auto& v : map) {
|
||||
res.emplace_back(v.second->key.name, v.second->action);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void ShortcutManager::setPriorities(const std::vector<QByteArray> &actions)
|
||||
void ShortcutManager::setPriorities(const std::vector<QByteArray>& actions)
|
||||
{
|
||||
if (actions.empty())
|
||||
if (actions.empty()) {
|
||||
return;
|
||||
}
|
||||
// Keep the same top priority of the given action, and adjust the rest. Can
|
||||
// go negative if necessary
|
||||
int current = 0;
|
||||
for (const auto &name : actions)
|
||||
for (const auto& name : actions) {
|
||||
current = std::max(current, getPriority(name));
|
||||
if (current == 0)
|
||||
}
|
||||
if (current == 0) {
|
||||
current = (int)actions.size();
|
||||
}
|
||||
setPriority(actions.front(), current);
|
||||
++current;
|
||||
for (const auto &name : actions) {
|
||||
for (const auto& name : actions) {
|
||||
int p = getPriority(name);
|
||||
if (p <= 0 || p >= current) {
|
||||
if (--current == 0)
|
||||
if (--current == 0) {
|
||||
--current;
|
||||
}
|
||||
setPriority(name, current);
|
||||
} else
|
||||
}
|
||||
else {
|
||||
current = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ShortcutManager::getPriority(const char *cmdName)
|
||||
int ShortcutManager::getPriority(const char* cmdName)
|
||||
{
|
||||
if (!cmdName)
|
||||
if (!cmdName) {
|
||||
return 0;
|
||||
}
|
||||
auto it = priorities.find(cmdName);
|
||||
if (it == priorities.end())
|
||||
if (it == priorities.end()) {
|
||||
return 0;
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
void ShortcutManager::setPriority(const char *cmdName, int p)
|
||||
void ShortcutManager::setPriority(const char* cmdName, int p)
|
||||
{
|
||||
if (p == 0)
|
||||
if (p == 0) {
|
||||
hPriorities->RemoveInt(cmdName);
|
||||
else
|
||||
}
|
||||
else {
|
||||
hPriorities->SetInt(cmdName, p);
|
||||
}
|
||||
}
|
||||
|
||||
void ShortcutManager::setTopPriority(const char *cmdName)
|
||||
void ShortcutManager::setTopPriority(const char* cmdName)
|
||||
{
|
||||
++topPriority;
|
||||
hPriorities->SetInt(cmdName, topPriority);
|
||||
@@ -406,24 +457,23 @@ void ShortcutManager::onTimer()
|
||||
{
|
||||
timer.stop();
|
||||
|
||||
QAction *found = nullptr;
|
||||
QAction* found = nullptr;
|
||||
int priority = -std::numeric_limits<int>::max();
|
||||
int seq_length = 0;
|
||||
for (const auto &info : pendingActions) {
|
||||
for (const auto& info : pendingActions) {
|
||||
if (info.action) {
|
||||
info.action->setEnabled(true);
|
||||
if (info.seq_length > seq_length
|
||||
|| (info.seq_length == seq_length
|
||||
&& info.priority > priority))
|
||||
{
|
||||
|| (info.seq_length == seq_length && info.priority > priority)) {
|
||||
priority = info.priority;
|
||||
seq_length = info.seq_length;
|
||||
found = info.action;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (found)
|
||||
if (found) {
|
||||
found->activate(QAction::Trigger);
|
||||
}
|
||||
pendingActions.clear();
|
||||
|
||||
if (lastFocus && lastFocus == QApplication::focusWidget()) {
|
||||
@@ -436,14 +486,14 @@ void ShortcutManager::onTimer()
|
||||
// our fake key strokes. So we try to fake some more obscure symbol key
|
||||
// stroke below, hoping to reset Qt's state machine.
|
||||
|
||||
const auto &index = actionMap.get<1>();
|
||||
const auto& index = actionMap.get<1>();
|
||||
static const std::string symbols = "~!@#$%^&*()_+";
|
||||
QString shortcut = pendingSequence.toString() + QStringLiteral(", Ctrl+");
|
||||
for (int s : symbols) {
|
||||
QKeySequence k(shortcut + QLatin1Char(s));
|
||||
auto it = index.lower_bound(ActionKey(k));
|
||||
if (it->key.shortcut != k) {
|
||||
QKeyEvent *kev = new QKeyEvent(QEvent::KeyPress, s, Qt::ControlModifier, 0, 0, 0);
|
||||
QKeyEvent* kev = new QKeyEvent(QEvent::KeyPress, s, Qt::ControlModifier, 0, 0, 0);
|
||||
QApplication::postEvent(lastFocus, kev);
|
||||
kev = new QKeyEvent(QEvent::KeyRelease, s, Qt::ControlModifier, 0, 0, 0);
|
||||
QApplication::postEvent(lastFocus, kev);
|
||||
|
||||
Reference in New Issue
Block a user