util_icon.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <QFile>
00025 #include <QPixmap>
00026 #include <QRgb>
00027
00028 #include "util_icon.h"
00029
00030 #include "prefsmanager.h"
00031 #include "scpaths.h"
00032 #include "scpixmapcache.h"
00033
00034
00035 QPixmap loadIcon(const QString nam, bool forceUseColor)
00036 {
00037 static ScPixmapCache<QString> pxCache;
00038 if (pxCache.contains(nam))
00039 return *pxCache[nam];
00040
00041 QString iconFilePath(QString("%1%2").arg(ScPaths::instance().iconDir()).arg(nam));
00042 QPixmap *pm = new QPixmap();
00043
00044 if (!QFile::exists(iconFilePath))
00045 qWarning("Unable to load icon %s: File not found", iconFilePath.toAscii().constData());
00046 else
00047 {
00048 pm->load(iconFilePath);
00049 if (pm->isNull())
00050 qWarning("Unable to load icon %s: Got null pixmap", iconFilePath.toAscii().constData());
00051 if (PrefsManager::instance()->appPrefs.grayscaleIcons && !forceUseColor)
00052 {
00053 QImage qi(pm->toImage());
00054 int h=qi.height();
00055 int w=qi.width();
00056 QRgb c_rgb;
00057 for (int i=0;i<w;++i)
00058 {
00059 for (int j=0;j<h;++j)
00060 {
00061 c_rgb=qi.pixel(i,j);
00062 int k = qMin(qRound(0.3 * qRed(c_rgb) + 0.59 * qGreen(c_rgb) + 0.11 * qBlue(c_rgb)), 255);
00063 qi.setPixel(i, j, qRgba(k, k, k, qAlpha(c_rgb)));
00064 }
00065 }
00066 *pm=QPixmap::fromImage(qi);
00067 }
00068 }
00069 pxCache.insert(nam, pm);
00070 return *pm;
00071 }