Skip to content

Commit c13548d

Browse files
committed
Grayscale icons in plugin-statusnotifier
1 parent 8871e26 commit c13548d

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

plugin-statusnotifier/statusnotifierbutton.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@ void StatusNotifierButton::newAttentionIcon()
105105
});
106106
}
107107

108+
QImage StatusNotifierButton::convertToGrayScale(const QImage &srcImage) {
109+
// Convert to 32bit pixel format
110+
QImage dstImage = srcImage.convertToFormat(srcImage.hasAlphaChannel() ?
111+
QImage::Format_ARGB32 : QImage::Format_RGB32);
112+
113+
unsigned int *data = (unsigned int*)dstImage.bits();
114+
int pixelCount = dstImage.width() * dstImage.height();
115+
116+
// Convert each pixel to grayscale
117+
for(int i = 0; i < pixelCount; ++i) {
118+
int val = qGray(*data);
119+
*data = qRgba(val, val, val, qAlpha(*data));
120+
++data;
121+
}
122+
123+
return dstImage;
124+
}
125+
108126
void StatusNotifierButton::refetchIcon(Status status, const QString& themePath)
109127
{
110128
QString nameProperty, pixmapProperty;
@@ -128,15 +146,19 @@ void StatusNotifierButton::refetchIcon(Status status, const QString& themePath)
128146
QIcon nextIcon;
129147
if (!iconName.isEmpty())
130148
{
131-
if (QIcon::hasThemeIcon(iconName))
132-
nextIcon = QIcon::fromTheme(iconName);
149+
if (QIcon::hasThemeIcon(iconName)){
150+
// nextIcon = QIcon::fromTheme(iconName);
151+
nextIcon.addPixmap(QPixmap::fromImage(convertToGrayScale(QIcon::fromTheme(iconName).pixmap(16, 16).toImage())));
152+
153+
}
133154
else
134155
{
135156
QDir themeDir(themePath);
136157
if (themeDir.exists())
137158
{
138159
if (themeDir.exists(iconName + QStringLiteral(".png")))
139-
nextIcon.addFile(themeDir.filePath(iconName + QStringLiteral(".png")));
160+
// nextIcon.addFile(themeDir.filePath(iconName + QStringLiteral(".png")));
161+
nextIcon.addPixmap(QPixmap::fromImage(convertToGrayScale(QImage(themeDir.filePath(iconName + QStringLiteral(".png"))))));
140162

141163
if (themeDir.cd(QStringLiteral("hicolor")) || (themeDir.cd(QStringLiteral("icons")) && themeDir.cd(QStringLiteral("hicolor"))))
142164
{
@@ -148,7 +170,9 @@ void StatusNotifierButton::refetchIcon(Status status, const QString& themePath)
148170
{
149171
QString file = themeDir.absolutePath() + QLatin1Char('/') + dir + QLatin1Char('/') + innerDir + QLatin1Char('/') + iconName + QStringLiteral(".png");
150172
if (QFile::exists(file))
151-
nextIcon.addFile(file);
173+
// nextIcon.addFile(file);
174+
nextIcon.addPixmap(QPixmap::fromImage(convertToGrayScale(QImage(file))));
175+
152176
}
153177
}
154178
}
@@ -190,7 +214,7 @@ void StatusNotifierButton::refetchIcon(Status status, const QString& themePath)
190214
for (const uchar *src = image.constBits(); src < end; src += 4, dest += 4)
191215
qToUnaligned(qToBigEndian<quint32>(qFromUnaligned<quint32>(src)), dest);
192216

193-
nextIcon.addPixmap(QPixmap::fromImage(image));
217+
nextIcon.addPixmap(QPixmap::fromImage(convertToGrayScale(image)));
194218
}
195219
}
196220

plugin-statusnotifier/statusnotifierbutton.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class StatusNotifierButton : public QToolButton
5858
Passive, Active, NeedsAttention
5959
};
6060

61+
QImage convertToGrayScale(const QImage &srcImage);
6162
public slots:
6263
void newIcon();
6364
void newAttentionIcon();
@@ -81,4 +82,4 @@ public slots:
8182
void resetIcon();
8283
};
8384

84-
#endif // STATUSNOTIFIERBUTTON_H
85+
#endif // STATUSNOTIFIERBUTTON_H

0 commit comments

Comments
 (0)