]> git.sur5r.net Git - minitube/blob - src/iconutils.cpp
New upstream version 3.1
[minitube] / src / iconutils.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
21 #include "iconutils.h"
22 #include "mainwindow.h"
23 #include <QAction>
24
25 namespace {
26 void addIconFile(QIcon &icon,
27                  const QString &filename,
28                  int size,
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);
33     }
34 }
35 } // namespace
36
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);
42     return icon;
43 }
44
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");
51
52     QString path(":/icons/");
53
54     if (MainWindow::instance()->palette().window().color().value() > 128)
55         path += QLatin1String("light/");
56     else
57         path += QLatin1String("dark/");
58
59     QIcon icon;
60
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);
71         }
72     }
73     return icon;
74 }
75
76 QIcon IconUtils::icon(const char *name) {
77 #ifdef APP_LINUX
78     QIcon icon = fromTheme(name);
79     if (icon.isNull()) icon = fromResources(name);
80     return icon;
81 #else
82     return fromResources(name);
83 #endif
84 }
85
86 QIcon IconUtils::icon(const QVector<const char *> &names) {
87     QIcon icon;
88     for (auto name : names) {
89         icon = IconUtils::icon(name);
90         if (!icon.availableSizes().isEmpty()) break;
91     }
92     return icon;
93 }
94
95 QPixmap IconUtils::iconPixmap(const char *name,
96                               int size,
97                               const QColor &background,
98                               const qreal pixelRatio) {
99     QString path(":/icons/");
100     if (background.value() > 128)
101         path += "light/";
102     else
103         path += "dark/";
104     path += QString::number(size) + '/' + name + QLatin1String(".png");
105     return IconUtils::pixmap(path, pixelRatio);
106 }
107
108 QIcon IconUtils::tintedIcon(const char *name, const QColor &color, const QVector<QSize> &sizes) {
109     QIcon i = IconUtils::icon(name);
110     QIcon t;
111     // if (sizes.isEmpty()) sizes = i.availableSizes();
112     for (const QSize &size : sizes) {
113         QPixmap pixmap = i.pixmap(size);
114         tint(pixmap, color);
115         t.addPixmap(pixmap);
116     }
117     return t;
118 }
119
120 QIcon IconUtils::tintedIcon(const char *name, const QColor &color, const QSize &size) {
121     return IconUtils::tintedIcon(name, color, QVector<QSize>() << size);
122 }
123
124 QImage IconUtils::grayscaled(const QImage &image) {
125     QImage img = 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]));
131     }
132     return img;
133 }
134
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);
141     painter.end();
142     img.setAlphaChannel(image.alphaChannel());
143     return img;
144 }
145
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);
150 }
151
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);
162                 return pixmap;
163             }
164         }
165     }
166     return QPixmap(filename);
167 }