1
0
mirror of https://github.com/KDE/latte-dock.git synced 2025-03-20 06:50:28 +03:00

support unity colors in Latte::IconItem

--iconitem now can provide a background and glow
color for its icon when the propery providesColors
is enabled
This commit is contained in:
Michail Vourlakos 2019-02-12 18:18:12 +02:00
parent 7772ee2855
commit 9eb5e25a2c
2 changed files with 151 additions and 19 deletions

View File

@ -61,6 +61,9 @@ IconItem::IconItem(QQuickItem *parent)
this, &IconItem::schedulePixmapUpdate);
connect(this, SIGNAL(overlaysChanged()),
this, SLOT(schedulePixmapUpdate()));
connect(this, SIGNAL(providesColorsChanged()),
this, SLOT(schedulePixmapUpdate()));
//initialize implicit size to the Dialog size
setImplicitWidth(KIconLoader::global()->currentSize(KIconLoader::Dialog));
setImplicitHeight(KIconLoader::global()->currentSize(KIconLoader::Dialog));
@ -238,6 +241,21 @@ void IconItem::setActive(bool active)
emit activeChanged();
}
bool IconItem::providesColors() const
{
return m_providesColors;
}
void IconItem::setProvidesColors(const bool provides)
{
if (m_providesColors == provides) {
return;
}
m_providesColors = provides;
emit providesColorsChanged();
}
void IconItem::setSmooth(const bool smooth)
{
if (smooth == m_smooth) {
@ -337,6 +355,86 @@ void IconItem::enabledChanged()
schedulePixmapUpdate();
}
QColor IconItem::backgroundColor() const
{
return m_backgroundColor;
}
void IconItem::setBackgroundColor(QColor background)
{
if (m_backgroundColor == background) {
return;
}
m_backgroundColor = background;
emit backgroundColorChanged();
}
QColor IconItem::glowColor() const
{
return m_glowColor;
}
void IconItem::setGlowColor(QColor glow)
{
if (m_glowColor == glow) {
return;
}
m_glowColor = glow;
emit glowColorChanged();
}
void IconItem::updateColors()
{
QImage icon = m_iconPixmap.toImage();
if (icon.format() != QImage::Format_Invalid) {
float rtotal = 0, gtotal = 0, btotal = 0;
float total = 0.0f;
for(int row=0; row<icon.height(); ++row) {
QRgb *line = (QRgb *)icon.scanLine(row);
for(int col=0; col<icon.width(); ++col) {
QRgb pix = line[col];
int r = qRed(pix);
int g = qGreen(pix);
int b = qBlue(pix);
int a = qAlpha(pix);
float saturation = (qMax(r, qMax(g, b)) - qMin(r, qMin(g, b))) / 255.0f;
float relevance = .1 + .9 * (a / 255.0f) * saturation;
rtotal += (float)(r * relevance);
gtotal += (float)(g * relevance);
btotal += (float)(b * relevance);
total += relevance * 255;
}
}
int nr = (rtotal / total) * 255;
int ng = (gtotal / total) * 255;
int nb = (btotal / total) * 255;
QColor tempColor(nr, ng, nb);
if (tempColor.hsvSaturationF() > 0.15f) {
tempColor.setHsvF(tempColor.hueF(), 0.65f, tempColor.valueF());
}
tempColor.setHsvF(tempColor.hueF(), tempColor.saturationF(), 0.55f); //original 0.90f ???
setBackgroundColor(tempColor);
tempColor.setHsvF(tempColor.hueF(), tempColor.saturationF(), 1.0f);
setGlowColor(tempColor);
}
}
void IconItem::loadPixmap()
{
if (!isComponentComplete()) {
@ -412,6 +510,12 @@ void IconItem::loadPixmap()
}
m_iconPixmap = result;
if (m_providesColors && m_lastValidSourceName != m_lastColorsSourceName) {
m_lastColorsSourceName = m_lastValidSourceName;
updateColors();
}
m_textureChanged = true;
//don't animate initial setting
update();

View File

@ -92,11 +92,18 @@ class IconItem : public QQuickItem
*/
Q_PROPERTY(bool usesPlasmaTheme READ usesPlasmaTheme WRITE setUsesPlasmaTheme NOTIFY usesPlasmaThemeChanged)
/**
* If set, icon will provide a background and glow color
*/
Q_PROPERTY(bool providesColors READ providesColors WRITE setProvidesColors NOTIFY providesColorsChanged)
/**
* Contains the last valid icon name
*/
Q_PROPERTY(QString lastValidSourceName READ lastValidSourceName NOTIFY lastValidSourceNameChanged)
Q_PROPERTY(QColor backgroundColor READ backgroundColor NOTIFY backgroundColorChanged)
Q_PROPERTY(QColor glowColor READ glowColor NOTIFY glowColorChanged)
public:
IconItem(QQuickItem *parent = nullptr);
virtual ~IconItem();
@ -115,14 +122,21 @@ public:
bool isValid() const;
int paintedWidth() const;
int paintedHeight() const;
bool providesColors() const;
void setProvidesColors(const bool provides);
bool usesPlasmaTheme() const;
void setUsesPlasmaTheme(bool usesPlasmaTheme);
int paintedWidth() const;
int paintedHeight() const;
QString lastValidSourceName();
QColor backgroundColor() const;
QColor glowColor() const;
void updatePolish() Q_DECL_OVERRIDE;
QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData) override;
@ -132,14 +146,17 @@ public:
void componentComplete() Q_DECL_OVERRIDE;
signals:
void overlaysChanged();
void activeChanged();
void backgroundColorChanged();
void glowColorChanged();
void lastValidSourceNameChanged();
void sourceChanged();
void smoothChanged();
void validChanged();
void overlaysChanged();
void paintedSizeChanged();
void providesColorsChanged();
void smoothChanged();
void sourceChanged();
void usesPlasmaThemeChanged();
void validChanged();
private slots:
void schedulePixmapUpdate();
@ -147,27 +164,38 @@ private slots:
private:
void loadPixmap();
void updateColors();
void setLastValidSourceName(QString name);
void setBackgroundColor(QColor background);
void setGlowColor(QColor glow);
QIcon m_icon;
QPixmap m_iconPixmap;
QImage m_imageIcon;
std::unique_ptr<Plasma::Svg> m_svgIcon;
QString m_lastValidSourceName;
QString m_svgIconName;
QStringList m_overlays;
//this contains the raw variant it was passed
QVariant m_source;
QSizeF m_implicitSize;
bool m_smooth;
private:
bool m_active;
bool m_providesColors{false};
bool m_smooth;
bool m_textureChanged;
bool m_sizeChanged;
bool m_usesPlasmaTheme;
QColor m_backgroundColor;
QColor m_glowColor;
QIcon m_icon;
QPixmap m_iconPixmap;
QImage m_imageIcon;
std::unique_ptr<Plasma::Svg> m_svgIcon;
QString m_svgIconName;
QString m_lastValidSourceName;
QString m_lastColorsSourceName;
QStringList m_overlays;
//this contains the raw variant it was passed
QVariant m_source;
QSizeF m_implicitSize;
};
}