QMenu: Simplify check for SH_Menu_AllowActiveAndDisabled

The check for SH_Menu_AllowActiveAndDisabled is done in eight places.
Simplify it by added a helper function which checks if an action should
be considered (i.e. is not a separator, is enabled or
SH_Menu_AllowActiveAndDisabled is enabled).

Pick-to: 6.9 6.8
Task-number: QTBUG-56952
Change-Id: I65c6dc67b804dd4c5cf463767a525ec6f785bf1a
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
This commit is contained in:
Christian Ehrlicher 2024-11-05 20:43:37 +01:00
parent 8e843ddba8
commit e0a44cd32e
2 changed files with 22 additions and 30 deletions

View File

@ -683,7 +683,6 @@ void QMenuPrivate::setSyncAction()
void QMenuPrivate::setFirstActionActive() void QMenuPrivate::setFirstActionActive()
{ {
Q_Q(QMenu);
updateActionRects(); updateActionRects();
for(int i = 0, saccum = 0; i < actions.size(); i++) { for(int i = 0, saccum = 0; i < actions.size(); i++) {
const QRect &rect = actionRects.at(i); const QRect &rect = actionRects.at(i);
@ -695,9 +694,7 @@ void QMenuPrivate::setFirstActionActive()
continue; continue;
} }
QAction *act = actions.at(i); QAction *act = actions.at(i);
if (!act->isSeparator() && if (considerAction(act)) {
(q->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, nullptr, q)
|| act->isEnabled())) {
setCurrentAction(act); setCurrentAction(act);
break; break;
} }
@ -710,9 +707,7 @@ void QMenuPrivate::setCurrentAction(QAction *action, int popup, SelectionReason
Q_Q(QMenu); Q_Q(QMenu);
tearoffHighlighted = 0; tearoffHighlighted = 0;
if (action if (!considerAction(action))
&& (action->isSeparator()
|| (!action->isEnabled() && !q->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, nullptr, q))))
action = nullptr; action = nullptr;
// Reselect the currently active action in case mouse moved over other menu items when // Reselect the currently active action in case mouse moved over other menu items when
@ -1236,16 +1231,13 @@ void QMenuPrivate::scrollMenu(QAction *action, QMenuScroller::ScrollLocation loc
void QMenuPrivate::scrollMenu(QMenuScroller::ScrollLocation location, bool active) void QMenuPrivate::scrollMenu(QMenuScroller::ScrollLocation location, bool active)
{ {
Q_Q(QMenu);
updateActionRects(); updateActionRects();
if (location == QMenuScroller::ScrollBottom) { if (location == QMenuScroller::ScrollBottom) {
for(int i = actions.size()-1; i >= 0; --i) { for(int i = actions.size()-1; i >= 0; --i) {
QAction *act = actions.at(i);
if (actionRects.at(i).isNull()) if (actionRects.at(i).isNull())
continue; continue;
if (!act->isSeparator() && QAction *act = actions.at(i);
(q->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, nullptr, q) if (considerAction(act)) {
|| act->isEnabled())) {
if (scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollDown) if (scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollDown)
scrollMenu(act, QMenuPrivate::QMenuScroller::ScrollBottom, active); scrollMenu(act, QMenuPrivate::QMenuScroller::ScrollBottom, active);
else if (active) else if (active)
@ -1255,12 +1247,10 @@ void QMenuPrivate::scrollMenu(QMenuScroller::ScrollLocation location, bool activ
} }
} else if (location == QMenuScroller::ScrollTop) { } else if (location == QMenuScroller::ScrollTop) {
for(int i = 0; i < actions.size(); ++i) { for(int i = 0; i < actions.size(); ++i) {
QAction *act = actions.at(i);
if (actionRects.at(i).isNull()) if (actionRects.at(i).isNull())
continue; continue;
if (!act->isSeparator() && QAction *act = actions.at(i);
(q->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, nullptr, q) if (considerAction(act)) {
|| act->isEnabled())) {
if (scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp) if (scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp)
scrollMenu(act, QMenuPrivate::QMenuScroller::ScrollTop, active); scrollMenu(act, QMenuPrivate::QMenuScroller::ScrollTop, active);
else if (active) else if (active)
@ -3147,24 +3137,20 @@ void QMenu::keyPressEvent(QKeyEvent *e)
if (!d->currentAction) { if (!d->currentAction) {
if (key == Qt::Key_Down) { if (key == Qt::Key_Down) {
for(int i = 0; i < d->actions.size(); ++i) { for(int i = 0; i < d->actions.size(); ++i) {
QAction *act = d->actions.at(i);
if (d->actionRects.at(i).isNull()) if (d->actionRects.at(i).isNull())
continue; continue;
if (!act->isSeparator() && QAction *act = d->actions.at(i);
(style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, nullptr, this) if (d->considerAction(act)) {
|| act->isEnabled())) {
nextAction = act; nextAction = act;
break; break;
} }
} }
} else { } else {
for(int i = d->actions.size()-1; i >= 0; --i) { for(int i = d->actions.size()-1; i >= 0; --i) {
QAction *act = d->actions.at(i);
if (d->actionRects.at(i).isNull()) if (d->actionRects.at(i).isNull())
continue; continue;
if (!act->isSeparator() && QAction *act = d->actions.at(i);
(style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, nullptr, this) if (d->considerAction(act)) {
|| act->isEnabled())) {
nextAction = act; nextAction = act;
break; break;
} }
@ -3188,9 +3174,7 @@ void QMenu::keyPressEvent(QKeyEvent *e)
break; break;
if (d->actionRects.at(next_i).isNull()) if (d->actionRects.at(next_i).isNull())
continue; continue;
if (next->isSeparator() || if (!d->considerAction(next))
(!next->isEnabled() &&
!style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, nullptr, this)))
continue; continue;
nextAction = next; nextAction = next;
if (d->scroll && (d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp)) { if (d->scroll && (d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp)) {
@ -3219,9 +3203,7 @@ void QMenu::keyPressEvent(QKeyEvent *e)
break; break;
if (d->actionRects.at(next_i).isNull()) if (d->actionRects.at(next_i).isNull())
continue; continue;
if (next->isSeparator() || if (!d->considerAction(next))
(!next->isEnabled() &&
!style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, nullptr, this)))
continue; continue;
nextAction = next; nextAction = next;
if (d->scroll && (d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollDown)) { if (d->scroll && (d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollDown)) {

View File

@ -464,6 +464,16 @@ public:
void drawTearOff(QPainter *painter, const QRect &rect); void drawTearOff(QPainter *painter, const QRect &rect);
QRect rect() const; QRect rect() const;
bool considerAction(const QAction *action) const
{
Q_Q(const QMenu);
if (!action || action->isSeparator())
return false;
if (action->isEnabled())
return true;
return q->style()->styleHint(QStyle::SH_Menu_AllowActiveAndDisabled, nullptr, q);
}
mutable uint maxIconWidth = 0; mutable uint maxIconWidth = 0;
mutable uint tabWidth = 0; mutable uint tabWidth = 0;
int motions = 0; int motions = 0;