annotate widgets/IconLoader.cpp @ 854:c17719e488c9

Fix some potential null-pointer derefs, and simplify some logic where loops were used with an unconditional "break" that meant they could only happen once (from coverity scan)
author Chris Cannam
date Wed, 03 Sep 2014 12:04:22 +0100
parents e4773943c9c1
children 4b5efd76a55c
rev   line source
Chris@289 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@289 2
Chris@289 3 /*
Chris@289 4 Sonic Visualiser
Chris@289 5 An audio file viewer and annotation editor.
Chris@289 6 Centre for Digital Music, Queen Mary, University of London.
Chris@289 7 This file copyright 2007 QMUL.
Chris@289 8
Chris@289 9 This program is free software; you can redistribute it and/or
Chris@289 10 modify it under the terms of the GNU General Public License as
Chris@289 11 published by the Free Software Foundation; either version 2 of the
Chris@289 12 License, or (at your option) any later version. See the file
Chris@289 13 COPYING included with this distribution for more information.
Chris@289 14 */
Chris@289 15
Chris@289 16 #include "IconLoader.h"
Chris@289 17
Chris@289 18 #include <QPixmap>
Chris@289 19 #include <QApplication>
Chris@289 20 #include <QPainter>
Chris@289 21 #include <QPalette>
Chris@289 22
Chris@289 23 static const char *autoInvertExceptions[] = {
Chris@289 24 // These are the icons that look OK in their default colours, even
Chris@289 25 // in a colour scheme with a black background. (They may also be
Chris@289 26 // icons that would look worse if we tried to auto-invert them.)
Chris@289 27 // If we have icons that look bad when auto-inverted but that are
Chris@289 28 // not suitable for use without being inverted, we'll need to
Chris@289 29 // supply inverted versions -- the loader will load xx_inverse.png
Chris@289 30 // in preference to xx.png if a dark background is found.)
Chris@289 31 "fileclose",
Chris@289 32 "filenew-22",
Chris@289 33 "filenew",
Chris@289 34 "fileopen-22",
Chris@289 35 "fileopen",
Chris@289 36 "fileopenaudio",
Chris@289 37 "fileopensession",
Chris@289 38 "filesave-22",
Chris@289 39 "filesave",
Chris@289 40 "filesaveas-22",
Chris@289 41 "filesaveas",
Chris@289 42 "help",
Chris@289 43 "editcut",
Chris@289 44 "editcopy",
Chris@289 45 "editpaste",
Chris@289 46 "editdelete",
Chris@289 47 "exit",
Chris@289 48 "zoom-fit",
Chris@289 49 "zoom-in",
Chris@289 50 "zoom-out",
Chris@289 51 "zoom"
Chris@289 52 };
Chris@289 53
Chris@289 54 QIcon
Chris@289 55 IconLoader::load(QString name)
Chris@289 56 {
Chris@289 57 QPixmap pmap(loadPixmap(name));
Chris@289 58 if (pmap.isNull()) return QIcon();
Chris@289 59 else return QIcon(pmap);
Chris@289 60 }
Chris@289 61
Chris@289 62 QPixmap
Chris@289 63 IconLoader::loadPixmap(QString name)
Chris@289 64 {
Chris@289 65 QColor bg = QApplication::palette().window().color();
Chris@289 66 if (bg.red() + bg.green() + bg.blue() > 384) { // light background
Chris@289 67 QPixmap pmap(QString(":icons/%1").arg(name));
Chris@289 68 if (pmap.isNull()) {
Chris@289 69 pmap = QPixmap(QString(":icons/%1.png").arg(name));
Chris@289 70 }
Chris@289 71 return pmap;
Chris@289 72 }
Chris@289 73
Chris@289 74 QPixmap pmap(QString(":icons/%1").arg(name));
Chris@289 75 if (pmap.isNull()) {
Chris@289 76 pmap = QPixmap(QString(":icons/%1_inverse.png").arg(name));
Chris@289 77 if (pmap.isNull()) {
Chris@289 78 pmap = QPixmap(QString(":icons/%1.png").arg(name));
Chris@289 79 }
Chris@289 80 }
Chris@289 81 if (pmap.isNull()) return pmap;
Chris@289 82
Chris@807 83 for (int i = 0; i < int(sizeof(autoInvertExceptions)/
Chris@807 84 sizeof(autoInvertExceptions[0])); ++i) {
Chris@289 85 if (autoInvertExceptions[i] == name) {
Chris@289 86 return pmap;
Chris@289 87 }
Chris@289 88 }
Chris@289 89
Chris@289 90 // No suitable inverted icon found for black background; try to
Chris@289 91 // auto-invert the default one
Chris@289 92
Chris@289 93 QImage img = pmap.toImage().convertToFormat(QImage::Format_ARGB32);
Chris@289 94
Chris@289 95 for (int y = 0; y < img.height(); ++y) {
Chris@289 96 for (int x = 0; x < img.width(); ++x) {
Chris@289 97
Chris@289 98 QRgb rgba = img.pixel(x, y);
Chris@289 99 QColor colour = QColor
Chris@289 100 (qRed(rgba), qGreen(rgba), qBlue(rgba), qAlpha(rgba));
Chris@289 101
Chris@289 102 int alpha = colour.alpha();
Chris@289 103 if (colour.saturation() < 5 && colour.alpha() > 10) {
Chris@289 104 colour.setHsv(colour.hue(),
Chris@289 105 colour.saturation(),
Chris@289 106 255 - colour.value());
Chris@289 107 colour.setAlpha(alpha);
Chris@289 108 img.setPixel(x, y, colour.rgba());
Chris@289 109 }
Chris@289 110 }
Chris@289 111 }
Chris@289 112
Chris@289 113 pmap = QPixmap::fromImage(img);
Chris@289 114 return pmap;
Chris@289 115 }
Chris@289 116