Simplify QAction privates
List primary and alternative key sequences and ids together, making the logic cleaner. Change-Id: I4eb07f9828f2b15dc66c34ceb2c4f800df73e800 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
parent
45a0c68c8c
commit
00807f1fe5
@ -115,44 +115,28 @@ void QActionPrivate::sendDataChanged()
|
|||||||
void QActionPrivate::redoGrab(QShortcutMap &map)
|
void QActionPrivate::redoGrab(QShortcutMap &map)
|
||||||
{
|
{
|
||||||
Q_Q(QAction);
|
Q_Q(QAction);
|
||||||
if (shortcutId)
|
for (int id : qAsConst(shortcutIds)) {
|
||||||
map.removeShortcut(shortcutId, q);
|
if (id)
|
||||||
if (shortcut.isEmpty())
|
|
||||||
return;
|
|
||||||
shortcutId = map.addShortcut(q, shortcut, shortcutContext, contextMatcher());
|
|
||||||
if (!enabled)
|
|
||||||
map.setShortcutEnabled(false, shortcutId, q);
|
|
||||||
if (!autorepeat)
|
|
||||||
map.setShortcutAutoRepeat(false, shortcutId, q);
|
|
||||||
}
|
|
||||||
|
|
||||||
void QActionPrivate::redoGrabAlternate(QShortcutMap &map)
|
|
||||||
{
|
|
||||||
Q_Q(QAction);
|
|
||||||
for(int i = 0; i < alternateShortcutIds.count(); ++i) {
|
|
||||||
if (const int id = alternateShortcutIds.at(i))
|
|
||||||
map.removeShortcut(id, q);
|
map.removeShortcut(id, q);
|
||||||
}
|
}
|
||||||
alternateShortcutIds.clear();
|
|
||||||
if (alternateShortcuts.isEmpty())
|
shortcutIds.clear();
|
||||||
return;
|
for (const QKeySequence &shortcut : qAsConst(shortcuts)) {
|
||||||
for(int i = 0; i < alternateShortcuts.count(); ++i) {
|
if (!shortcut.isEmpty())
|
||||||
const QKeySequence& alternate = alternateShortcuts.at(i);
|
shortcutIds.append(map.addShortcut(q, shortcut, shortcutContext, contextMatcher()));
|
||||||
if (!alternate.isEmpty())
|
|
||||||
alternateShortcutIds.append(map.addShortcut(q, alternate, shortcutContext, contextMatcher()));
|
|
||||||
else
|
else
|
||||||
alternateShortcutIds.append(0);
|
shortcutIds.append(0);
|
||||||
}
|
}
|
||||||
if (!enabled) {
|
if (!enabled) {
|
||||||
for(int i = 0; i < alternateShortcutIds.count(); ++i) {
|
for (int id : qAsConst(shortcutIds)) {
|
||||||
const int id = alternateShortcutIds.at(i);
|
if (id)
|
||||||
map.setShortcutEnabled(false, id, q);
|
map.setShortcutEnabled(false, id, q);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!autorepeat) {
|
if (!autorepeat) {
|
||||||
for(int i = 0; i < alternateShortcutIds.count(); ++i) {
|
for (int id : qAsConst(shortcutIds)) {
|
||||||
const int id = alternateShortcutIds.at(i);
|
if (id)
|
||||||
map.setShortcutAutoRepeat(false, id, q);
|
map.setShortcutAutoRepeat(false, id, q);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -160,10 +144,8 @@ void QActionPrivate::redoGrabAlternate(QShortcutMap &map)
|
|||||||
void QActionPrivate::setShortcutEnabled(bool enable, QShortcutMap &map)
|
void QActionPrivate::setShortcutEnabled(bool enable, QShortcutMap &map)
|
||||||
{
|
{
|
||||||
Q_Q(QAction);
|
Q_Q(QAction);
|
||||||
if (shortcutId)
|
for (int id : qAsConst(shortcutIds)) {
|
||||||
map.setShortcutEnabled(enable, shortcutId, q);
|
if (id)
|
||||||
for(int i = 0; i < alternateShortcutIds.count(); ++i) {
|
|
||||||
if (const int id = alternateShortcutIds.at(i))
|
|
||||||
map.setShortcutEnabled(enable, id, q);
|
map.setShortcutEnabled(enable, id, q);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -344,44 +326,36 @@ QAction::QAction(QActionPrivate &dd, QObject *parent)
|
|||||||
Valid keycodes for this property can be found in \l Qt::Key and
|
Valid keycodes for this property can be found in \l Qt::Key and
|
||||||
\l Qt::Modifier. There is no default shortcut key.
|
\l Qt::Modifier. There is no default shortcut key.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Sets \a shortcut as the sole shortcut that triggers the action.
|
||||||
|
|
||||||
|
\sa shortcut, setShortcuts()
|
||||||
|
*/
|
||||||
void QAction::setShortcut(const QKeySequence &shortcut)
|
void QAction::setShortcut(const QKeySequence &shortcut)
|
||||||
{
|
{
|
||||||
QAPP_CHECK("setShortcut");
|
if (shortcut.isEmpty())
|
||||||
|
setShortcuts({});
|
||||||
Q_D(QAction);
|
else
|
||||||
if (d->shortcut == shortcut)
|
setShortcuts({ shortcut });
|
||||||
return;
|
|
||||||
|
|
||||||
d->shortcut = shortcut;
|
|
||||||
d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
|
|
||||||
d->sendDataChanged();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Sets \a shortcuts as the list of shortcuts that trigger the
|
Sets \a shortcuts as the list of shortcuts that trigger the
|
||||||
action. The first element of the list is the primary shortcut.
|
action. The first element of the list is the primary shortcut.
|
||||||
|
|
||||||
\sa shortcut
|
\sa shortcut, setShortcut()
|
||||||
*/
|
*/
|
||||||
void QAction::setShortcuts(const QList<QKeySequence> &shortcuts)
|
void QAction::setShortcuts(const QList<QKeySequence> &shortcuts)
|
||||||
{
|
{
|
||||||
|
QAPP_CHECK("setShortcuts");
|
||||||
Q_D(QAction);
|
Q_D(QAction);
|
||||||
|
|
||||||
QList <QKeySequence> listCopy = shortcuts;
|
if (d->shortcuts == shortcuts)
|
||||||
|
|
||||||
QKeySequence primary;
|
|
||||||
if (!listCopy.isEmpty())
|
|
||||||
primary = listCopy.takeFirst();
|
|
||||||
|
|
||||||
if (d->shortcut == primary && d->alternateShortcuts == listCopy)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
QAPP_CHECK("setShortcuts");
|
d->shortcuts = shortcuts;
|
||||||
|
|
||||||
d->shortcut = primary;
|
|
||||||
d->alternateShortcuts = listCopy;
|
|
||||||
d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
|
d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
|
||||||
d->redoGrabAlternate(QGuiApplicationPrivate::instance()->shortcutMap);
|
|
||||||
d->sendDataChanged();
|
d->sendDataChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -407,7 +381,9 @@ void QAction::setShortcuts(QKeySequence::StandardKey key)
|
|||||||
QKeySequence QAction::shortcut() const
|
QKeySequence QAction::shortcut() const
|
||||||
{
|
{
|
||||||
Q_D(const QAction);
|
Q_D(const QAction);
|
||||||
return d->shortcut;
|
if (d->shortcuts.isEmpty())
|
||||||
|
return QKeySequence();
|
||||||
|
return d->shortcuts.first();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -419,12 +395,7 @@ QKeySequence QAction::shortcut() const
|
|||||||
QList<QKeySequence> QAction::shortcuts() const
|
QList<QKeySequence> QAction::shortcuts() const
|
||||||
{
|
{
|
||||||
Q_D(const QAction);
|
Q_D(const QAction);
|
||||||
QList <QKeySequence> shortcuts;
|
return d->shortcuts;
|
||||||
if (!d->shortcut.isEmpty())
|
|
||||||
shortcuts << d->shortcut;
|
|
||||||
if (!d->alternateShortcuts.isEmpty())
|
|
||||||
shortcuts << d->alternateShortcuts;
|
|
||||||
return shortcuts;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -442,7 +413,6 @@ void QAction::setShortcutContext(Qt::ShortcutContext context)
|
|||||||
QAPP_CHECK("setShortcutContext");
|
QAPP_CHECK("setShortcutContext");
|
||||||
d->shortcutContext = context;
|
d->shortcutContext = context;
|
||||||
d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
|
d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
|
||||||
d->redoGrabAlternate(QGuiApplicationPrivate::instance()->shortcutMap);
|
|
||||||
d->sendDataChanged();
|
d->sendDataChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -469,7 +439,6 @@ void QAction::setAutoRepeat(bool on)
|
|||||||
QAPP_CHECK("setAutoRepeat");
|
QAPP_CHECK("setAutoRepeat");
|
||||||
d->autorepeat = on;
|
d->autorepeat = on;
|
||||||
d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
|
d->redoGrab(QGuiApplicationPrivate::instance()->shortcutMap);
|
||||||
d->redoGrabAlternate(QGuiApplicationPrivate::instance()->shortcutMap);
|
|
||||||
d->sendDataChanged();
|
d->sendDataChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -522,10 +491,11 @@ QAction::~QAction()
|
|||||||
if (d->group)
|
if (d->group)
|
||||||
d->group->removeAction(this);
|
d->group->removeAction(this);
|
||||||
#if QT_CONFIG(shortcut)
|
#if QT_CONFIG(shortcut)
|
||||||
if (d->shortcutId && qApp) {
|
if (qApp) {
|
||||||
QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(d->shortcutId, this);
|
for (int id : qAsConst(d->shortcutIds)) {
|
||||||
for (int id : qAsConst(d->alternateShortcutIds))
|
if (id)
|
||||||
QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(id, this);
|
QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(id, this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -1095,7 +1065,7 @@ bool QAction::event(QEvent *e)
|
|||||||
#if QT_CONFIG(shortcut)
|
#if QT_CONFIG(shortcut)
|
||||||
if (e->type() == QEvent::Shortcut) {
|
if (e->type() == QEvent::Shortcut) {
|
||||||
QShortcutEvent *se = static_cast<QShortcutEvent *>(e);
|
QShortcutEvent *se = static_cast<QShortcutEvent *>(e);
|
||||||
Q_ASSERT_X(se->key() == d_func()->shortcut || d_func()->alternateShortcuts.contains(se->key()),
|
Q_ASSERT_X(d_func()->shortcutIds.contains(se->shortcutId()),
|
||||||
"QAction::event",
|
"QAction::event",
|
||||||
"Received shortcut event from incorrect shortcut");
|
"Received shortcut event from incorrect shortcut");
|
||||||
if (se->isAmbiguous())
|
if (se->isAmbiguous())
|
||||||
@ -1370,8 +1340,8 @@ Q_GUI_EXPORT QDebug operator<<(QDebug d, const QAction *action)
|
|||||||
if (action->isCheckable())
|
if (action->isCheckable())
|
||||||
d << " checked=" << action->isChecked();
|
d << " checked=" << action->isChecked();
|
||||||
#if QT_CONFIG(shortcut)
|
#if QT_CONFIG(shortcut)
|
||||||
if (!action->shortcut().isEmpty())
|
if (!action->shortcuts().isEmpty())
|
||||||
d << " shortcut=" << action->shortcut();
|
d << " shortcuts=" << action->shortcuts();
|
||||||
#endif
|
#endif
|
||||||
d << " menuRole=";
|
d << " menuRole=";
|
||||||
QtDebugUtils::formatQEnum(d, action->menuRole());
|
QtDebugUtils::formatQEnum(d, action->menuRole());
|
||||||
|
@ -94,8 +94,7 @@ public:
|
|||||||
QString statustip;
|
QString statustip;
|
||||||
QString whatsthis;
|
QString whatsthis;
|
||||||
#if QT_CONFIG(shortcut)
|
#if QT_CONFIG(shortcut)
|
||||||
QKeySequence shortcut;
|
QList<QKeySequence> shortcuts;
|
||||||
QList<QKeySequence> alternateShortcuts;
|
|
||||||
#endif
|
#endif
|
||||||
QVariant userData;
|
QVariant userData;
|
||||||
|
|
||||||
@ -104,8 +103,7 @@ public:
|
|||||||
virtual void setMenu(QObject *menu);
|
virtual void setMenu(QObject *menu);
|
||||||
|
|
||||||
#if QT_CONFIG(shortcut)
|
#if QT_CONFIG(shortcut)
|
||||||
int shortcutId = 0;
|
QList<int> shortcutIds;
|
||||||
QList<int> alternateShortcutIds;
|
|
||||||
Qt::ShortcutContext shortcutContext = Qt::WindowShortcut;
|
Qt::ShortcutContext shortcutContext = Qt::WindowShortcut;
|
||||||
uint autorepeat : 1;
|
uint autorepeat : 1;
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user