3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
21 #include "iconutils.h"
22 #include "mainwindow.h"
26 void addIconFile(QIcon &icon,
27 const QString &filename,
29 QIcon::Mode mode = QIcon::Normal,
30 QIcon::State state = QIcon::Off) {
31 if (QFile::exists(filename)) {
32 icon.addFile(filename, QSize(size, size), mode, state);
37 QIcon IconUtils::fromTheme(const QString &name) {
38 static const QLatin1String symbolic("-symbolic");
39 if (name.endsWith(symbolic)) return QIcon::fromTheme(name);
40 QIcon icon = QIcon::fromTheme(name + symbolic);
41 if (icon.isNull()) return QIcon::fromTheme(name);
45 QIcon IconUtils::fromResources(const char *name) {
46 static const QLatin1String active("_active");
47 static const QLatin1String selected("_selected");
48 static const QLatin1String disabled("_disabled");
49 static const QLatin1String checked("_checked");
50 static const QLatin1String ext(".png");
52 QString path(":/icons/");
54 if (MainWindow::instance()->palette().window().color().value() > 128)
55 path += QLatin1String("light/");
57 path += QLatin1String("dark/");
61 // WARN keep these sizes updated with what we really use
62 for (int size : {16, 24, 32, 88}) {
63 const QString pathAndName = path + QString::number(size) + '/' + name;
64 QString iconFilename = pathAndName + ext;
65 if (QFile::exists(iconFilename)) {
66 addIconFile(icon, iconFilename, size);
67 addIconFile(icon, pathAndName + active + ext, size, QIcon::Active);
68 addIconFile(icon, pathAndName + selected + ext, size, QIcon::Selected);
69 addIconFile(icon, pathAndName + disabled + ext, size, QIcon::Disabled);
70 addIconFile(icon, pathAndName + checked + ext, size, QIcon::Normal, QIcon::On);
76 QIcon IconUtils::icon(const char *name) {
78 QIcon icon = fromTheme(name);
79 if (icon.isNull()) icon = fromResources(name);
82 return fromResources(name);
86 QIcon IconUtils::icon(const QVector<const char *> &names) {
88 for (auto name : names) {
89 icon = IconUtils::icon(name);
90 if (!icon.availableSizes().isEmpty()) break;
95 QPixmap IconUtils::iconPixmap(const char *name,
97 const QColor &background,
98 const qreal pixelRatio) {
99 QString path(":/icons/");
100 if (background.value() > 128)
104 path += QString::number(size) + '/' + name + QLatin1String(".png");
105 return IconUtils::pixmap(path, pixelRatio);
108 QIcon IconUtils::tintedIcon(const char *name, const QColor &color, const QVector<QSize> &sizes) {
109 QIcon i = IconUtils::icon(name);
111 // if (sizes.isEmpty()) sizes = i.availableSizes();
112 for (const QSize &size : sizes) {
113 QPixmap pixmap = i.pixmap(size);
120 QIcon IconUtils::tintedIcon(const char *name, const QColor &color, const QSize &size) {
121 return IconUtils::tintedIcon(name, color, QVector<QSize>() << size);
124 QImage IconUtils::grayscaled(const QImage &image) {
126 int pixels = img.width() * img.height();
127 unsigned int *data = (unsigned int *)img.bits();
128 for (int i = 0; i < pixels; ++i) {
129 int val = qGray(data[i]);
130 data[i] = qRgba(val, val, val, qAlpha(data[i]));
135 QImage IconUtils::tinted(const QImage &image, const QColor &color, QPainter::CompositionMode mode) {
136 QImage img(image.size(), QImage::Format_ARGB32_Premultiplied);
137 QPainter painter(&img);
138 painter.drawImage(0, 0, grayscaled(image));
139 painter.setCompositionMode(mode);
140 painter.fillRect(img.rect(), color);
142 img.setAlphaChannel(image.alphaChannel());
146 void IconUtils::tint(QPixmap &pixmap, const QColor &color, QPainter::CompositionMode mode) {
147 QPainter painter(&pixmap);
148 painter.setCompositionMode(mode);
149 painter.fillRect(pixmap.rect(), color);
152 QPixmap IconUtils::pixmap(const QString &filename, const qreal pixelRatio) {
153 // Check if a "@2x" file exists
154 if (pixelRatio > 1.0) {
155 int dotIndex = filename.lastIndexOf(QLatin1Char('.'));
156 if (dotIndex != -1) {
157 QString at2xfileName = filename;
158 at2xfileName.insert(dotIndex, QLatin1String("@2x"));
159 if (QFile::exists(at2xfileName)) {
160 QPixmap pixmap(at2xfileName);
161 pixmap.setDevicePixelRatio(pixelRatio);
166 return QPixmap(filename);