QLineEdit: Use High DPI pixmap for clear button.

Add a 32x32-pixmap to the style and factor out a function from
QStyle::standardIcon() to assemble the icon. The 32x32 pixmap may also be used
for the 16x16 case with devicePixelRatio=2.

Change QLineEditIconButton to use QStyle::standardIcon() instead of
QStyle::standardPixmap passing the QWindow to obtain the correct pixmap
from the icon.

Task-number: QTBUG-49374
Change-Id: I9895230f66911752cc13b7212609141610df0977
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
This commit is contained in:
Friedemann Kleint 2016-02-16 12:41:11 +01:00
parent a125b2c4c7
commit b657496a0b
4 changed files with 39 additions and 13 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 410 B

View File

@ -5198,6 +5198,30 @@ static QPixmap cachedPixmapFromXPM(const char * const *xpm)
return result;
}
static QIcon clearTextIcon(bool rtl)
{
const QString directionalThemeName = rtl
? QStringLiteral("edit-clear-locationbar-ltr") : QStringLiteral("edit-clear-locationbar-rtl");
if (QIcon::hasThemeIcon(directionalThemeName))
return QIcon::fromTheme(directionalThemeName);
const QString themeName = QStringLiteral("edit-clear");
if (QIcon::hasThemeIcon(themeName))
return QIcon::fromTheme(themeName);
QIcon icon;
#ifndef QT_NO_IMAGEFORMAT_PNG
QPixmap clearText16(QStringLiteral(":/qt-project.org/styles/commonstyle/images/cleartext-16.png"));
Q_ASSERT(!clearText16.size().isEmpty());
icon.addPixmap(clearText16);
QPixmap clearText32(QStringLiteral(":/qt-project.org/styles/commonstyle/images/cleartext-32.png"));
Q_ASSERT(!clearText32.size().isEmpty());
icon.addPixmap(clearText32);
clearText32.setDevicePixelRatio(2); // The 32x32 pixmap can also be used for 16x16/devicePixelRatio=2
icon.addPixmap(clearText32);
#endif // !QT_NO_IMAGEFORMAT_PNG
return icon;
}
/*! \reimp */
QPixmap QCommonStyle::standardPixmap(StandardPixmap sp, const QStyleOption *option,
const QWidget *widget) const
@ -5370,12 +5394,8 @@ QPixmap QCommonStyle::standardPixmap(StandardPixmap sp, const QStyleOption *opti
}
}
break;
case SP_LineEditClearButton: {
QString themeName = rtl ? QStringLiteral("edit-clear-locationbar-ltr") : QStringLiteral("edit-clear-locationbar-rtl");
if (!QIcon::hasThemeIcon(themeName))
themeName = QStringLiteral("edit-clear");
pixmap = QIcon::fromTheme(themeName).pixmap(16);
}
case SP_LineEditClearButton:
pixmap = clearTextIcon(rtl).pixmap(16);
break;
default:
break;
@ -5505,8 +5525,6 @@ QPixmap QCommonStyle::standardPixmap(StandardPixmap sp, const QStyleOption *opti
return QPixmap(QLatin1String(":/qt-project.org/styles/commonstyle/images/media-volume-16.png"));
case SP_MediaVolumeMuted:
return QPixmap(QLatin1String(":/qt-project.org/styles/commonstyle/images/media-volume-muted-16.png"));
case SP_LineEditClearButton:
return QPixmap(QStringLiteral(":/qt-project.org/styles/commonstyle/images/cleartext-16.png"));
#endif // QT_NO_IMAGEFORMAT_PNG
default:
break;
@ -5556,6 +5574,8 @@ QIcon QCommonStyle::standardIcon(StandardPixmap standardIcon, const QStyleOption
const QWidget *widget) const
{
QIcon icon;
const bool rtl = (option && option->direction == Qt::RightToLeft) || (!option && QApplication::isRightToLeft());
#ifdef Q_OS_WIN
switch (standardIcon) {
case SP_DriveCDIcon:
@ -5597,6 +5617,9 @@ QIcon QCommonStyle::standardIcon(StandardPixmap standardIcon, const QStyleOption
icon.addPixmap(pixmap);
}
break;
case SP_LineEditClearButton:
icon = clearTextIcon(rtl);
break;
default:
break;
}
@ -5605,7 +5628,6 @@ QIcon QCommonStyle::standardIcon(StandardPixmap standardIcon, const QStyleOption
#endif
const bool rtl = (option && option->direction == Qt::RightToLeft) || (!option && QApplication::isRightToLeft());
if (QApplication::desktopSettingsAware() && !QIcon::themeName().isEmpty()) {
switch (standardIcon) {
case SP_DirHomeIcon:

View File

@ -1,6 +1,7 @@
<RCC>
<qresource prefix="/qt-project.org/styles/commonstyle">
<file>images/cleartext-16.png</file>
<file>images/cleartext-32.png</file>
<file>images/filelink-16.png</file>
<file>images/filelink-32.png</file>
<file>images/filelink-128.png</file>

View File

@ -317,14 +317,17 @@ QLineEditIconButton::QLineEditIconButton(QWidget *parent)
void QLineEditIconButton::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QWindow *window = Q_NULLPTR;
if (const QWidget *nativeParent = nativeParentWidget())
window = nativeParent->windowHandle();
// Note isDown should really use the active state but in most styles
// this has no proper feedback
QIcon::Mode state = QIcon::Disabled;
if (isEnabled())
state = isDown() ? QIcon::Selected : QIcon::Normal;
const QPixmap iconPixmap = icon().pixmap(QSize(IconButtonSize, IconButtonSize),
state, QIcon::Off);
QRect pixmapRect = QRect(QPoint(0, 0), iconPixmap.size() / iconPixmap.devicePixelRatio());
const QSize iconSize(IconButtonSize, IconButtonSize);
const QPixmap iconPixmap = icon().pixmap(window, iconSize, state, QIcon::Off);
QRect pixmapRect = QRect(QPoint(0, 0), iconSize);
pixmapRect.moveCenter(rect().center());
painter.setOpacity(m_opacity);
painter.drawPixmap(pixmapRect, iconPixmap);
@ -416,7 +419,7 @@ QIcon QLineEditPrivate::clearButtonIcon() const
Q_Q(const QLineEdit);
QStyleOptionFrame styleOption;
q->initStyleOption(&styleOption);
return QIcon(q->style()->standardPixmap(QStyle::SP_LineEditClearButton, &styleOption, q));
return q->style()->standardIcon(QStyle::SP_LineEditClearButton, &styleOption, q);
}
void QLineEditPrivate::setClearButtonEnabled(bool enabled)