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

View File

@ -464,6 +464,16 @@ public:
void drawTearOff(QPainter *painter, const QRect &rect);
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 tabWidth = 0;
int motions = 0;