svgui  1.9
IconLoader.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2007 QMUL.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "IconLoader.h"
00017 
00018 #include <QPixmap>
00019 #include <QApplication>
00020 #include <QPainter>
00021 #include <QPalette>
00022 
00023 static const char *autoInvertExceptions[] = {
00024     // These are the icons that look OK in their default colours, even
00025     // in a colour scheme with a black background.  (They may also be
00026     // icons that would look worse if we tried to auto-invert them.)
00027     // If we have icons that look bad when auto-inverted but that are
00028     // not suitable for use without being inverted, we'll need to
00029     // supply inverted versions -- the loader will load xx_inverse.png
00030     // in preference to xx.png if a dark background is found.)
00031     "fileclose",
00032     "filenew-22",
00033     "filenew",
00034     "fileopen-22",
00035     "fileopen",
00036     "fileopenaudio",
00037     "fileopensession",
00038     "filesave-22",
00039     "filesave",
00040     "filesaveas-22",
00041     "filesaveas",
00042     "help",
00043     "editcut",
00044     "editcopy",
00045     "editpaste",
00046     "editdelete",
00047     "exit",
00048     "zoom-fit",
00049     "zoom-in",
00050     "zoom-out",
00051     "zoom"
00052 };
00053 
00054 QIcon
00055 IconLoader::load(QString name)
00056 {
00057     QPixmap pmap(loadPixmap(name));
00058     if (pmap.isNull()) return QIcon();
00059     else return QIcon(pmap);
00060 }
00061 
00062 QPixmap
00063 IconLoader::loadPixmap(QString name)
00064 {
00065     QColor bg = QApplication::palette().window().color();
00066     if (bg.red() + bg.green() + bg.blue() > 384) { // light background
00067         QPixmap pmap(QString(":icons/%1").arg(name));
00068         if (pmap.isNull()) {
00069             pmap = QPixmap(QString(":icons/%1.png").arg(name));
00070         }
00071         return pmap;
00072     }
00073 
00074     QPixmap pmap(QString(":icons/%1").arg(name));
00075     if (pmap.isNull()) {
00076         pmap = QPixmap(QString(":icons/%1_inverse.png").arg(name));
00077         if (pmap.isNull()) {
00078             pmap = QPixmap(QString(":icons/%1.png").arg(name));
00079         }
00080     }
00081     if (pmap.isNull()) return pmap;
00082 
00083     for (int i = 0; i < int(sizeof(autoInvertExceptions)/
00084                             sizeof(autoInvertExceptions[0])); ++i) {
00085         if (autoInvertExceptions[i] == name) {
00086             return pmap;
00087         }
00088     }
00089 
00090     // No suitable inverted icon found for black background; try to
00091     // auto-invert the default one
00092 
00093     QImage img = pmap.toImage().convertToFormat(QImage::Format_ARGB32);
00094 
00095     for (int y = 0; y < img.height(); ++y) {
00096         for (int x = 0; x < img.width(); ++x) {
00097 
00098             QRgb rgba = img.pixel(x, y);
00099             QColor colour = QColor
00100                 (qRed(rgba), qGreen(rgba), qBlue(rgba), qAlpha(rgba));
00101 
00102             int alpha = colour.alpha();
00103             if (colour.saturation() < 5 && colour.alpha() > 10) {
00104                 colour.setHsv(colour.hue(),
00105                               colour.saturation(),
00106                               255 - colour.value());
00107                 colour.setAlpha(alpha);
00108                 img.setPixel(x, y, colour.rgba());
00109             }
00110         }
00111     }
00112 
00113     pmap = QPixmap::fromImage(img);
00114     return pmap;
00115 }
00116