1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This file copyright 2007 QMUL.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "IconLoader.h"
#include <QPixmap>
#include <QApplication>
#include <QPainter>
#include <QPalette>
#include <QFile>
#include <QSvgRenderer>
#include <QSettings>
#include <QMutex>
#include <QMutexLocker>
#include <vector>
#include <set>
#include <map>
#include "base/Debug.h"
#include "base/Profiler.h"
using namespace std;
namespace sv {
static set<QString> autoInvertExceptions {
// These are the icons that look OK in their default colours, even
// in a colour scheme with a black background. (They may also be
// icons that would look worse if we tried to auto-invert them.)
// If we have icons that look bad when auto-inverted but that are
// not suitable for use without being inverted, we'll need to
// supply inverted versions -- the loader will load xx_inverse.png
// in preference to xx.png if a dark background is found.)
"fileclose",
"filenew",
"fileopen",
"fileopenaudio",
"fileopensession",
"filesave",
"filesaveas",
"filesaveas-sv",
"help",
"editcut",
"editcopy",
"editpaste",
"editdelete",
"exit",
"zoom-fit",
"zoom-in",
"zoom-out",
"zoom"
};
QIcon
IconLoader::load(QString name)
{
Profiler profiler("IconLoader::load");
static QMutex mutex;
static map<QString, QIcon> icons;
static const vector<int> sizes { 0, 16, 22, 24, 32, 48, 64, 128 };
QMutexLocker locker(&mutex);
if (icons.find(name) != icons.end()) {
return icons.at(name);
}
QIcon icon;
for (int sz: sizes) {
QPixmap pmap(loadPixmap(name, sz));
if (!pmap.isNull()) icon.addPixmap(pmap);
}
icons[name] = icon;
return icon;
}
bool
IconLoader::shouldInvert() const
{
QSettings settings;
settings.beginGroup("IconLoader");
if (!settings.value("invert-icons-on-dark-background", true).toBool()) {
return false;
}
QColor bg = QApplication::palette().window().color();
bool darkBackground = (bg.red() + bg.green() + bg.blue() <= 384);
return darkBackground;
}
bool
IconLoader::shouldAutoInvert(QString name) const
{
if (shouldInvert()) {
return (autoInvertExceptions.find(name) == autoInvertExceptions.end());
} else {
return false;
}
}
QPixmap
IconLoader::loadPixmap(QString name, int size)
{
bool invert = shouldInvert();
QString scalableName, nonScalableName;
QPixmap pmap;
// attempt to load a pixmap with the right size and inversion
nonScalableName = makeNonScalableFilename(name, size, invert);
pmap = QPixmap(nonScalableName);
if (pmap.isNull() && size > 0) {
// if that failed, load a scalable vector with the right
// inversion and scale it
scalableName = makeScalableFilename(name, invert);
pmap = loadScalable(scalableName, size);
}
if (pmap.isNull() && invert) {
// if that failed, and we were asking for an inverted pixmap,
// that may mean we don't have an inverted version of it. We
// could either auto-invert or use the uninverted version
nonScalableName = makeNonScalableFilename(name, size, false);
pmap = QPixmap(nonScalableName);
if (pmap.isNull() && size > 0) {
scalableName = makeScalableFilename(name, false);
pmap = loadScalable(scalableName, size);
}
if (!pmap.isNull() && shouldAutoInvert(name)) {
pmap = invertPixmap(pmap);
}
}
return pmap;
}
QPixmap
IconLoader::loadScalable(QString name, int size)
{
if (!QFile(name).exists()) {
// cerr << "loadScalable: no such file as: \"" << name << "\"" << endl;
return QPixmap();
}
QPixmap pmap(size, size);
pmap.fill(Qt::transparent);
QSvgRenderer renderer(name);
QPainter painter;
painter.begin(&pmap);
// cerr << "calling renderer for " << name << " at size " << size << "..." << endl;
renderer.render(&painter);
// cerr << "renderer completed" << endl;
painter.end();
return pmap;
}
QString
IconLoader::makeNonScalableFilename(QString name, int size, bool invert)
{
if (invert) {
if (size == 0) {
return QString(":icons/%1_inverse.png").arg(name);
} else {
return QString(":icons/%1-%2_inverse.png").arg(name).arg(size);
}
} else {
if (size == 0) {
return QString(":icons/%1.png").arg(name);
} else {
return QString(":icons/%1-%2.png").arg(name).arg(size);
}
}
}
QString
IconLoader::makeScalableFilename(QString name, bool invert)
{
if (invert) {
return QString(":icons/scalable/%1_inverse.svg").arg(name);
} else {
return QString(":icons/scalable/%1.svg").arg(name);
}
}
QPixmap
IconLoader::invertPixmap(QPixmap pmap)
{
// No suitable inverted icon found for black background; try to
// auto-invert the default one
QImage img = pmap.toImage().convertToFormat(QImage::Format_ARGB32);
for (int y = 0; y < img.height(); ++y) {
for (int x = 0; x < img.width(); ++x) {
QRgb rgba = img.pixel(x, y);
QColor colour = QColor
(qRed(rgba), qGreen(rgba), qBlue(rgba), qAlpha(rgba));
int alpha = colour.alpha();
if (colour.saturation() < 5 && colour.alpha() > 10) {
colour.setHsv(colour.hue(),
colour.saturation(),
255 - colour.value());
colour.setAlpha(alpha);
img.setPixel(x, y, colour.rgba());
}
}
}
pmap = QPixmap::fromImage(img);
return pmap;
}
} // end namespace sv
|