QSS/MenuItem: only draw checkbox if no item is available

QTBUG-66380 introduced a behavior change drawing a checkable menu item
with an icon. After this path the checkmark and icon was drawn.
Revert this regression so the stylesheet style matches the behavior of
all other styles. A checkable item should have two different icons
(QIcon::On and QIcon::Off).

Fixes: QTBUG-74655
Task-number: QTBUG-66380
Change-Id: I32ac2f397087a1c3d5d07400372109703c00c1ba
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
Christian Ehrlicher 2019-06-11 21:12:47 +02:00
parent 8e528d8bd0
commit b568e93149

View File

@ -3703,17 +3703,6 @@ void QStyleSheetStyle::drawControl(ControlElement ce, const QStyleOption *opt, Q
bool dis = !(opt->state & QStyle::State_Enabled), bool dis = !(opt->state & QStyle::State_Enabled),
act = opt->state & QStyle::State_Selected; act = opt->state & QStyle::State_Selected;
int checkableOffset = 0;
if (checkable) {
QRenderRule subSubRule = renderRule(w, opt, PseudoElement_MenuCheckMark);
QStyleOptionMenuItem newMi = mi;
newMi.rect = positionRect(w, subRule, subSubRule, PseudoElement_MenuCheckMark, opt->rect, opt->direction);
// align with icons if there are some
checkableOffset = std::max(m->maxIconWidth, newMi.rect.width());
if (subSubRule.hasDrawable() || checked)
drawPrimitive(PE_IndicatorMenuCheckMark, &newMi, p, w);
}
if (!mi.icon.isNull()) { if (!mi.icon.isNull()) {
QIcon::Mode mode = dis ? QIcon::Disabled : QIcon::Normal; QIcon::Mode mode = dis ? QIcon::Disabled : QIcon::Normal;
if (act && !dis) if (act && !dis)
@ -3730,24 +3719,28 @@ void QStyleSheetStyle::drawControl(ControlElement ce, const QStyleOption *opt, Q
} }
QRect iconRect = positionRect(w, subRule, iconRule, PseudoElement_MenuIcon, opt->rect, opt->direction); QRect iconRect = positionRect(w, subRule, iconRule, PseudoElement_MenuIcon, opt->rect, opt->direction);
if (opt->direction == Qt::LeftToRight) if (opt->direction == Qt::LeftToRight)
iconRect.moveLeft(iconRect.left() + checkableOffset); iconRect.moveLeft(iconRect.left());
else else
iconRect.moveRight(iconRect.right() - checkableOffset); iconRect.moveRight(iconRect.right());
iconRule.drawRule(p, iconRect); iconRule.drawRule(p, iconRect);
QRect pmr(0, 0, pixw, pixh); QRect pmr(0, 0, pixw, pixh);
pmr.moveCenter(iconRect.center()); pmr.moveCenter(iconRect.center());
p->drawPixmap(pmr.topLeft(), pixmap); p->drawPixmap(pmr.topLeft(), pixmap);
} else if (checkable) {
QRenderRule subSubRule = renderRule(w, opt, PseudoElement_MenuCheckMark);
if (subSubRule.hasDrawable() || checked) {
QStyleOptionMenuItem newMi = mi;
if (!dis)
newMi.state |= State_Enabled;
if (act)
newMi.state |= State_On;
newMi.rect = positionRect(w, subRule, subSubRule, PseudoElement_MenuCheckMark, opt->rect, opt->direction);
drawPrimitive(PE_IndicatorMenuCheckMark, &newMi, p, w);
}
} }
int textOffset = 0;
// padding overrules it all
if (!subRule.hasBox() || subRule.box()->paddings[LeftEdge] == 0) {
textOffset = checkableOffset;
if (!m->icon.isNull() || !checkable)
textOffset += m->maxIconWidth;
}
QRect textRect = subRule.contentsRect(opt->rect); QRect textRect = subRule.contentsRect(opt->rect);
textRect.setLeft(textRect.left() + textOffset); textRect.setLeft(textRect.left() + m->maxIconWidth);
textRect.setWidth(textRect.width() - mi.tabWidth); textRect.setWidth(textRect.width() - mi.tabWidth);
const QRect vTextRect = visualRect(opt->direction, m->rect, textRect); const QRect vTextRect = visualRect(opt->direction, m->rect, textRect);
@ -5091,27 +5084,20 @@ QSize QStyleSheetStyle::sizeFromContents(ContentsType ct, const QStyleOption *op
if ((pe == PseudoElement_MenuSeparator) && subRule.hasContentsSize()) { if ((pe == PseudoElement_MenuSeparator) && subRule.hasContentsSize()) {
return QSize(sz.width(), subRule.size().height()); return QSize(sz.width(), subRule.size().height());
} else if ((pe == PseudoElement_Item) && (subRule.hasBox() || subRule.hasBorder())) { } else if ((pe == PseudoElement_Item) && (subRule.hasBox() || subRule.hasBorder())) {
int width = csz.width(); QSize sz(csz);
if (mi->text.contains(QLatin1Char('\t'))) if (mi->text.contains(QLatin1Char('\t')))
width += 12; //as in QCommonStyle sz.rwidth() += 12; //as in QCommonStyle
bool checkable = mi->checkType != QStyleOptionMenuItem::NotCheckable; bool checkable = mi->checkType != QStyleOptionMenuItem::NotCheckable;
int checkableWidth = 0; if (!mi->icon.isNull()) {
if (checkable) { const int pmSmall = pixelMetric(PM_SmallIconSize);
const QSize pmSize = mi->icon.actualSize(QSize(pmSmall, pmSmall));
sz.rwidth() += pmSize.width() + 4;
} else if (checkable) {
QRenderRule subSubRule = renderRule(w, opt, PseudoElement_MenuCheckMark); QRenderRule subSubRule = renderRule(w, opt, PseudoElement_MenuCheckMark);
QRect checkmarkRect = positionRect(w, subRule, subSubRule, PseudoElement_MenuCheckMark, opt->rect, opt->direction); QRect checkmarkRect = positionRect(w, subRule, subSubRule, PseudoElement_MenuCheckMark, opt->rect, opt->direction);
checkableWidth = std::max(mi->maxIconWidth, checkmarkRect.width()); sz.rwidth() += std::max(mi->maxIconWidth, checkmarkRect.width()) + 4;
} }
if (!mi->icon.isNull()) { return subRule.boxSize(subRule.adjustSize(sz));
QPixmap pixmap = mi->icon.pixmap(pixelMetric(PM_SmallIconSize));
width += pixmap.width();
}
// padding overrules it all
if (!subRule.hasBox() || subRule.box()->paddings[LeftEdge] == 0) {
width += checkableWidth;
if (!mi->icon.isNull() || !checkable)
width += mi->maxIconWidth;
}
return subRule.boxSize(subRule.adjustSize(QSize(width, csz.height())));
} }
} }
break; break;