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
wilder-5.19
David Redondo 6 years ago
parent 885c778378
commit 582f5ebad1
  1. 3
      kstyle/CMakeLists.txt
  2. 16
      kstyle/breezehelper.cpp
  3. 4
      kstyle/breezehelper.h
  4. 23
      kstyle/breezestyle.cpp

@ -4,6 +4,7 @@ find_package(KF5 REQUIRED COMPONENTS
I18n I18n
Config Config
GuiAddons GuiAddons
IconThemes
ConfigWidgets ConfigWidgets
WindowSystem) WindowSystem)
@ -101,7 +102,7 @@ target_link_libraries(breeze Qt5::Core Qt5::Gui Qt5::Widgets Qt5::DBus)
if( BREEZE_HAVE_QTQUICK ) if( BREEZE_HAVE_QTQUICK )
target_link_libraries(breeze Qt5::Quick) target_link_libraries(breeze Qt5::Quick)
endif() 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) target_link_libraries(breeze breezecommon5)
if(KF5FrameworkIntegration_FOUND) if(KF5FrameworkIntegration_FOUND)

@ -23,6 +23,7 @@
#include "breezestyleconfigdata.h" #include "breezestyleconfigdata.h"
#include <KColorUtils> #include <KColorUtils>
#include <KIconLoader>
#include <KWindowSystem> #include <KWindowSystem>
#include <QApplication> #include <QApplication>
@ -1595,4 +1596,19 @@ namespace Breeze
return pixmap.devicePixelRatio(); 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;
}
} }

@ -29,6 +29,7 @@
#include <KSharedConfig> #include <KSharedConfig>
#include <QPainterPath> #include <QPainterPath>
#include <QIcon>
#include <QWidget> #include <QWidget>
namespace Breeze namespace Breeze
@ -281,6 +282,9 @@ namespace Breeze
//* return a QRectF with the appropriate size for a rectangle with a pen stroke //* 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; 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: protected:

@ -1276,9 +1276,9 @@ namespace Breeze
const auto pixmapSize( button->icon().actualSize( button->iconSize() ) ); 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 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, enabled ? QIcon::Normal : QIcon::Disabled,
button->isChecked() ? QIcon::On : QIcon::Off) ); button->isChecked() ? QIcon::On : QIcon::Off));
drawItemPixmap( &painter, pixmapRect, Qt::AlignCenter, pixmap ); drawItemPixmap( &painter, pixmapRect, Qt::AlignCenter, pixmap );
offset.rx() += pixmapSize.width() + Metrics::Button_ItemSpacing; offset.rx() += pixmapSize.width() + Metrics::Button_ItemSpacing;
@ -3888,7 +3888,7 @@ namespace Breeze
const QSize iconSize( iconWidth, iconWidth ); const QSize iconSize( iconWidth, iconWidth );
// get pixmap // get pixmap
const QPixmap pixmap( icon.pixmap( iconSize, iconMode, iconState ) ); const QPixmap pixmap(_helper->coloredIcon(icon, option->palette, iconSize, iconMode, iconState));
// render // render
drawItemPixmap( painter, option->rect, Qt::AlignCenter, pixmap ); drawItemPixmap( painter, option->rect, Qt::AlignCenter, pixmap );
@ -4203,7 +4203,7 @@ namespace Breeze
else if( mouseOver && flat ) iconMode = QIcon::Active; else if( mouseOver && flat ) iconMode = QIcon::Active;
else iconMode = QIcon::Normal; 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 ); drawItemPixmap( painter, iconRect, Qt::AlignCenter, pixmap );
} }
@ -4329,7 +4329,7 @@ namespace Breeze
else if( mouseOver && flat ) iconMode = QIcon::Active; else if( mouseOver && flat ) iconMode = QIcon::Active;
else iconMode = QIcon::Normal; 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 ); drawItemPixmap( painter, iconRect, Qt::AlignCenter, pixmap );
} }
@ -4378,7 +4378,7 @@ namespace Breeze
if( !buttonOption->icon.isNull() ) if( !buttonOption->icon.isNull() )
{ {
const QIcon::Mode mode( enabled ? QIcon::Normal : QIcon::Disabled ); 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 ); drawItemPixmap( painter, rect, textFlags, pixmap );
// adjust rect (copied from QCommonStyle) // adjust rect (copied from QCommonStyle)
@ -4472,7 +4472,8 @@ namespace Breeze
#endif #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); auto iconRect(editRect);
iconRect.setWidth(cb->iconSize.width() + 4); iconRect.setWidth(cb->iconSize.width() + 4);
iconRect = alignedRect(cb->direction, 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 ); drawItemPixmap( painter, iconRect, Qt::AlignCenter, pixmap );
// render outline // render outline
@ -4736,7 +4737,7 @@ namespace Breeze
// icon state // icon state
const QIcon::State iconState( sunken ? QIcon::On:QIcon::Off ); 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 ); painter->drawPixmap( iconRect, icon );
} }
@ -5716,7 +5717,7 @@ namespace Breeze
iconRect = visualRect( option, iconRect ); iconRect = visualRect( option, iconRect );
const QIcon::Mode mode( enabled ? QIcon::Normal : QIcon::Disabled ); 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 ); drawItemPixmap( painter, iconRect, textFlags, pixmap );
} }
@ -6598,7 +6599,7 @@ namespace Breeze
} }
// get pixmap and render // 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 ); painter->drawPixmap( iconRect, pixmap );
} }

Loading…
Cancel
Save