QItemDelegate::doLayout: only call QStyle::pixelMetric() once

QStyle::pixelMetric(QStyle::PM_FocusFrameHMargin) was called three times
in the worst case but it returns a constant value. Therefore cache this
value and call pixelMetric() only once.
Since this code is duplicated from QCommonStylePrivate::viewItemLayout()
also change it there.

Change-Id: I6d5f0a8d2b1373bd36f0520f404e6a3cb0794f12
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Reviewed-by: Thorbjørn Lund Martsum <tmartsum@gmail.com>
bb10
Christian Ehrlicher 2018-01-14 09:34:56 +01:00
parent ea7a9d694b
commit 0aecac1dcf
2 changed files with 16 additions and 10 deletions

View File

@ -803,11 +803,14 @@ void QItemDelegate::doLayout(const QStyleOptionViewItem &option,
const bool hasCheck = checkRect->isValid();
const bool hasPixmap = pixmapRect->isValid();
const bool hasText = textRect->isValid();
const int textMargin = hasText ? style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0;
const int pixmapMargin = hasPixmap ? style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0;
const int checkMargin = hasCheck ? style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0;
int x = option.rect.left();
int y = option.rect.top();
const bool hasMargin = (hasText | hasPixmap | hasCheck);
const int frameHMargin = hasMargin ?
style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0;
const int textMargin = hasText ? frameHMargin : 0;
const int pixmapMargin = hasPixmap ? frameHMargin : 0;
const int checkMargin = hasCheck ? frameHMargin : 0;
const int x = option.rect.left();
const int y = option.rect.top();
int w, h;
textRect->adjust(-textMargin, 0, textMargin, 0); // add width padding

View File

@ -994,11 +994,14 @@ void QCommonStylePrivate::viewItemLayout(const QStyleOptionViewItem *opt, QRect
const bool hasCheck = checkRect->isValid();
const bool hasPixmap = pixmapRect->isValid();
const bool hasText = textRect->isValid();
const int textMargin = hasText ? proxyStyle->pixelMetric(QStyle::PM_FocusFrameHMargin, opt, widget) + 1 : 0;
const int pixmapMargin = hasPixmap ? proxyStyle->pixelMetric(QStyle::PM_FocusFrameHMargin, opt, widget) + 1 : 0;
const int checkMargin = hasCheck ? proxyStyle->pixelMetric(QStyle::PM_FocusFrameHMargin, opt, widget) + 1 : 0;
int x = opt->rect.left();
int y = opt->rect.top();
const bool hasMargin = (hasText | hasPixmap | hasCheck);
const int frameHMargin = hasMargin ?
proxyStyle->pixelMetric(QStyle::PM_FocusFrameHMargin, opt, widget) + 1 : 0;
const int textMargin = hasText ? frameHMargin : 0;
const int pixmapMargin = hasPixmap ? frameHMargin : 0;
const int checkMargin = hasCheck ? frameHMargin : 0;
const int x = opt->rect.left();
const int y = opt->rect.top();
int w, h;
if (textRect->height() == 0 && (!hasPixmap || !sizehint)) {