From b657496a0ba326af0688e9935069139c002849cf Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 16 Feb 2016 12:41:11 +0100 Subject: [PATCH] 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 --- src/widgets/styles/images/cleartext-32.png | Bin 0 -> 410 bytes src/widgets/styles/qcommonstyle.cpp | 40 ++++++++++++++++----- src/widgets/styles/qstyle.qrc | 1 + src/widgets/widgets/qlineedit_p.cpp | 11 +++--- 4 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 src/widgets/styles/images/cleartext-32.png diff --git a/src/widgets/styles/images/cleartext-32.png b/src/widgets/styles/images/cleartext-32.png new file mode 100644 index 0000000000000000000000000000000000000000..ff5a2b5ec191716226458ff6c3b50628156d2929 GIT binary patch literal 410 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE3?yBabR7dy%*9TgAsieWw;%dH0CG7CJR*yM z%CCbkqm#z$3ZS55iEBhjaDG}zd16s2LwR|*US?i)adKios$PCk`s{Z$Qb0xa0X`wF z|NsB@^78Wa_4NS*AOi^e{QTe~SO7$XgoG3o6;)SPU%Ytn^5x4ve*6IHO|D~F2Q-Se zB*-tALEgwAuyOyP8#iBn{-xpdyAvp}#nZ(x#NzbY$uEVP6?j@)Sy^Lj9{ioZHCI5Y zf2PIUclrhPXB;zT)vvmGtSY}ZFaK?w%hen4Ii?R5ILiJ#_fX}!>>o~X-<|+I^P>A2 zmj7hBM4!!ZKRbV$cTBm`n!<&yD_dX9PWGC~k?l3{pW(Z|jz4`|V)sn7UUrc4owIPl zfsWi{J03={IS=e~825$A*&V+eR{JBpLBh!=NGCXF%8jg_n~uDm#_lF#S()*4<8=9R d&C9>9jypR?F*5#izc0}J44$rjF6*2UngD|AqhA03 literal 0 HcmV?d00001 diff --git a/src/widgets/styles/qcommonstyle.cpp b/src/widgets/styles/qcommonstyle.cpp index 355a3d2c3f..748cef20c1 100644 --- a/src/widgets/styles/qcommonstyle.cpp +++ b/src/widgets/styles/qcommonstyle.cpp @@ -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: diff --git a/src/widgets/styles/qstyle.qrc b/src/widgets/styles/qstyle.qrc index c063ec5036..d8c73dd6fa 100644 --- a/src/widgets/styles/qstyle.qrc +++ b/src/widgets/styles/qstyle.qrc @@ -1,6 +1,7 @@ images/cleartext-16.png + images/cleartext-32.png images/filelink-16.png images/filelink-32.png images/filelink-128.png diff --git a/src/widgets/widgets/qlineedit_p.cpp b/src/widgets/widgets/qlineedit_p.cpp index ff4d0fec47..ff80c45c9a 100644 --- a/src/widgets/widgets/qlineedit_p.cpp +++ b/src/widgets/widgets/qlineedit_p.cpp @@ -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)