From 582f5ebad1686d47168a3246e2aff5beefb59121 Mon Sep 17 00:00:00 2001 From: David Redondo Date: Mon, 30 Mar 2020 14:56:12 +0200 Subject: [PATCH] Apply the correct palette to icons Summary: Our icons can be recolored. However there is a difference between custom colors on widgets and icons. We will respect the palette but KIconLoader that creates the icon pixmaps operates on an application wide palette basis. This can create miscolored icons when a widget has a custom palette. A helper function is introduced to load the pixmaps that switches the palette of the global KIconLoader if necessary and resets it appropriately. Test Plan: Before: {F8205856} After: {F8205857} Reviewers: #breeze, ndavis, cblack, hpereiradacosta, mart Reviewed By: cblack, mart Subscribers: plasma-devel Tags: #plasma Differential Revision: https://phabricator.kde.org/D28433 --- kstyle/CMakeLists.txt | 3 ++- kstyle/breezehelper.cpp | 16 ++++++++++++++++ kstyle/breezehelper.h | 4 ++++ kstyle/breezestyle.cpp | 23 ++++++++++++----------- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/kstyle/CMakeLists.txt b/kstyle/CMakeLists.txt index c9e34331..27a81fe4 100644 --- a/kstyle/CMakeLists.txt +++ b/kstyle/CMakeLists.txt @@ -4,6 +4,7 @@ find_package(KF5 REQUIRED COMPONENTS I18n Config GuiAddons + IconThemes ConfigWidgets WindowSystem) @@ -101,7 +102,7 @@ target_link_libraries(breeze Qt5::Core Qt5::Gui Qt5::Widgets Qt5::DBus) if( BREEZE_HAVE_QTQUICK ) target_link_libraries(breeze Qt5::Quick) endif() -target_link_libraries(breeze KF5::ConfigCore KF5::ConfigWidgets KF5::GuiAddons KF5::WindowSystem) +target_link_libraries(breeze KF5::ConfigCore KF5::ConfigWidgets KF5::GuiAddons KF5::IconThemes KF5::WindowSystem) target_link_libraries(breeze breezecommon5) if(KF5FrameworkIntegration_FOUND) diff --git a/kstyle/breezehelper.cpp b/kstyle/breezehelper.cpp index 8eed5151..7757c4ee 100644 --- a/kstyle/breezehelper.cpp +++ b/kstyle/breezehelper.cpp @@ -23,6 +23,7 @@ #include "breezestyleconfigdata.h" #include +#include #include #include @@ -1595,4 +1596,19 @@ namespace Breeze return pixmap.devicePixelRatio(); } + QPixmap Helper::coloredIcon(const QIcon& icon, const QPalette& palette, const QSize &size, QIcon::Mode mode, QIcon::State state) + { + const QPalette activePalette = KIconLoader::global()->customPalette(); + const bool changePalette = activePalette != palette; + if (changePalette) { + KIconLoader::global()->setCustomPalette(palette); + } + const QPixmap pixmap = icon.pixmap(size, mode, state); + if (changePalette && activePalette == QPalette()) { + KIconLoader::global()->resetPalette(); + } else { + KIconLoader::global()->setCustomPalette(palette); + } + return pixmap; + } } diff --git a/kstyle/breezehelper.h b/kstyle/breezehelper.h index 13898829..efc1341b 100644 --- a/kstyle/breezehelper.h +++ b/kstyle/breezehelper.h @@ -29,6 +29,7 @@ #include #include +#include #include namespace Breeze @@ -281,6 +282,9 @@ namespace Breeze //* return a QRectF with the appropriate size for a rectangle with a pen stroke QRectF strokedRect( const QRect &rect, const int penWidth = PenWidth::Frame ) const; + + QPixmap coloredIcon(const QIcon &icon, const QPalette& palette, const QSize &size, + QIcon::Mode mode = QIcon::Normal, QIcon::State state = QIcon::Off); protected: diff --git a/kstyle/breezestyle.cpp b/kstyle/breezestyle.cpp index 3eb3bca9..8bbc3ffa 100644 --- a/kstyle/breezestyle.cpp +++ b/kstyle/breezestyle.cpp @@ -1276,9 +1276,9 @@ namespace Breeze const auto pixmapSize( button->icon().actualSize( button->iconSize() ) ); const QRect pixmapRect( QPoint( offset.x(), button->description().isEmpty() ? (button->height() - pixmapSize.height())/2:offset.y() ), pixmapSize ); - const QPixmap pixmap( button->icon().pixmap(pixmapSize, + const QPixmap pixmap(_helper->coloredIcon(button->icon(), button->palette(), pixmapSize, enabled ? QIcon::Normal : QIcon::Disabled, - button->isChecked() ? QIcon::On : QIcon::Off) ); + button->isChecked() ? QIcon::On : QIcon::Off)); drawItemPixmap( &painter, pixmapRect, Qt::AlignCenter, pixmap ); offset.rx() += pixmapSize.width() + Metrics::Button_ItemSpacing; @@ -3888,7 +3888,7 @@ namespace Breeze const QSize iconSize( iconWidth, iconWidth ); // get pixmap - const QPixmap pixmap( icon.pixmap( iconSize, iconMode, iconState ) ); + const QPixmap pixmap(_helper->coloredIcon(icon, option->palette, iconSize, iconMode, iconState)); // render drawItemPixmap( painter, option->rect, Qt::AlignCenter, pixmap ); @@ -4203,7 +4203,7 @@ namespace Breeze else if( mouseOver && flat ) iconMode = QIcon::Active; else iconMode = QIcon::Normal; - const auto pixmap = buttonOption->icon.pixmap( iconSize, iconMode, iconState ); + const auto pixmap = _helper->coloredIcon(buttonOption->icon, buttonOption->palette, iconSize, iconMode, iconState); drawItemPixmap( painter, iconRect, Qt::AlignCenter, pixmap ); } @@ -4329,7 +4329,7 @@ namespace Breeze else if( mouseOver && flat ) iconMode = QIcon::Active; else iconMode = QIcon::Normal; - const QPixmap pixmap = toolButtonOption->icon.pixmap( iconSize, iconMode, iconState ); + const QPixmap pixmap = _helper->coloredIcon(toolButtonOption->icon, toolButtonOption->palette, iconSize, iconMode, iconState); drawItemPixmap( painter, iconRect, Qt::AlignCenter, pixmap ); } @@ -4378,7 +4378,7 @@ namespace Breeze if( !buttonOption->icon.isNull() ) { const QIcon::Mode mode( enabled ? QIcon::Normal : QIcon::Disabled ); - const QPixmap pixmap( buttonOption->icon.pixmap( buttonOption->iconSize, mode ) ); + const QPixmap pixmap(_helper->coloredIcon(buttonOption->icon, buttonOption->palette, buttonOption->iconSize, mode)); drawItemPixmap( painter, rect, textFlags, pixmap ); // adjust rect (copied from QCommonStyle) @@ -4472,7 +4472,8 @@ namespace Breeze #endif } - const auto pixmap = cb->currentIcon.pixmap(window, cb->iconSize, mode); + auto pixmap = _helper->coloredIcon(cb->currentIcon,cb->palette, cb->iconSize * window->devicePixelRatio(), mode); + pixmap.setDevicePixelRatio(window->devicePixelRatio()); auto iconRect(editRect); iconRect.setWidth(cb->iconSize.width() + 4); iconRect = alignedRect(cb->direction, @@ -4557,7 +4558,7 @@ namespace Breeze } - const auto pixmap = menuItemOption->icon.pixmap( iconSize, iconMode, iconState ); + const auto pixmap = _helper->coloredIcon(menuItemOption->icon, menuItemOption->palette, iconRect.size(), iconMode, iconState); drawItemPixmap( painter, iconRect, Qt::AlignCenter, pixmap ); // render outline @@ -4736,7 +4737,7 @@ namespace Breeze // icon state const QIcon::State iconState( sunken ? QIcon::On:QIcon::Off ); - const QPixmap icon = menuItemOption->icon.pixmap( iconRect.size(), mode, iconState ); + const QPixmap icon = _helper->coloredIcon(menuItemOption->icon, menuItemOption->palette, iconRect.size(), mode, iconState); painter->drawPixmap( iconRect, icon ); } @@ -5716,7 +5717,7 @@ namespace Breeze iconRect = visualRect( option, iconRect ); const QIcon::Mode mode( enabled ? QIcon::Normal : QIcon::Disabled ); - const QPixmap pixmap( toolBoxOption->icon.pixmap( iconSize, mode ) ); + const QPixmap pixmap(_helper->coloredIcon(toolBoxOption->icon, toolBoxOption->palette, iconRect.size(), mode)); drawItemPixmap( painter, iconRect, textFlags, pixmap ); } @@ -6598,7 +6599,7 @@ namespace Breeze } // get pixmap and render - const QPixmap pixmap = icon.pixmap( iconSize, iconMode, iconState ); + const QPixmap pixmap = _helper->coloredIcon(icon, option->palette, iconSize, iconMode, iconState); painter->drawPixmap( iconRect, pixmap ); }