]> git.sur5r.net Git - minitube/blob - src/iconutils.cpp
b09d400b2e4e024079ec776900d2f3f88586d578
[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 <QAction>
23 #include "mainwindow.h"
24
25 QIcon IconUtils::fromTheme(const QString &name) {
26     const QLatin1String symbolic("-symbolic");
27     if (name.endsWith(symbolic)) return QIcon::fromTheme(name);
28     QIcon icon = QIcon::fromTheme(name + symbolic);
29     if (icon.isNull()) return QIcon::fromTheme(name);
30     return icon;
31 }
32
33 QIcon IconUtils::fromResources(const QString &name) {
34     QLatin1String path(":/images/");
35     QLatin1String ext(".png");
36     const QString pathAndName = path + name;
37     QIcon icon = QIcon(pathAndName + ext);
38     if (!icon.isNull()) {
39         QLatin1String active("_active");
40         QLatin1String selected("_selected");
41         QLatin1String disabled("_disabled");
42         QLatin1String checked("_checked");
43         QLatin1String twoX("@2x");
44
45         icon.addPixmap(QPixmap(pathAndName + active + ext), QIcon::Active);
46         icon.addPixmap(QPixmap(pathAndName + selected + ext), QIcon::Selected);
47         icon.addPixmap(QPixmap(pathAndName + disabled + ext), QIcon::Disabled);
48         icon.addPixmap(QPixmap(pathAndName + checked + ext), QIcon::Normal, QIcon::On);
49
50         const QString twoXAndExt = twoX + ext;
51         icon.addPixmap(QPixmap(pathAndName + active + twoXAndExt), QIcon::Active);
52         icon.addPixmap(QPixmap(pathAndName + selected + twoXAndExt), QIcon::Selected);
53         icon.addPixmap(QPixmap(pathAndName + disabled + twoXAndExt), QIcon::Disabled);
54         icon.addPixmap(QPixmap(pathAndName + checked + twoXAndExt), QIcon::Normal, QIcon::On);
55     }
56     return icon;
57 }
58
59 QIcon IconUtils::icon(const QString &name) {
60 #ifdef APP_LINUX
61     QIcon icon = fromTheme(name);
62     if (icon.isNull()) icon = fromResources(name);
63     return icon;
64 #else
65     return fromResources(name);
66 #endif
67 }
68
69 QIcon IconUtils::icon(const QStringList &names) {
70     QIcon icon;
71     for (const QString &name : names) {
72         icon = IconUtils::icon(name);
73         if (!icon.availableSizes().isEmpty()) break;
74     }
75     return icon;
76 }
77
78 QIcon IconUtils::tintedIcon(const QString &name, const QColor &color, QList<QSize> sizes) {
79     QIcon i = IconUtils::icon(name);
80     QIcon t;
81     if (sizes.isEmpty()) sizes = i.availableSizes();
82     for (const QSize &size : sizes) {
83         QPixmap pixmap = i.pixmap(size);
84         QImage tintedImage = tinted(pixmap.toImage(), color);
85         t.addPixmap(QPixmap::fromImage(tintedImage));
86     }
87     return t;
88 }
89
90 QIcon IconUtils::tintedIcon(const QString &name, const QColor &color, const QSize &size) {
91     return IconUtils::tintedIcon(name, color, QList<QSize>() << size);
92 }
93
94 QImage IconUtils::grayscaled(const QImage &image) {
95     QImage img = image;
96     int pixels = img.width() * img.height();
97     unsigned int *data = (unsigned int *)img.bits();
98     for (int i = 0; i < pixels; ++i) {
99         int val = qGray(data[i]);
100         data[i] = qRgba(val, val, val, qAlpha(data[i]));
101     }
102     return img;
103 }
104
105 QImage IconUtils::tinted(const QImage &image, const QColor &color,
106                          QPainter::CompositionMode mode) {
107     QImage img(image.size(), QImage::Format_ARGB32_Premultiplied);
108     QPainter painter(&img);
109     painter.drawImage(0, 0, grayscaled(image));
110     painter.setCompositionMode(mode);
111     painter.fillRect(img.rect(), color);
112     painter.end();
113     img.setAlphaChannel(image.alphaChannel());
114     return img;
115 }
116
117 void IconUtils::setupAction(QAction *action) {
118     // never autorepeat.
119     // unexperienced users tend to keep keys pressed for a "long" time
120     action->setAutoRepeat(false);
121
122     // show keyboard shortcuts in the status bar
123     if (!action->shortcut().isEmpty())
124         action->setStatusTip(action->statusTip() +
125                              QLatin1String(" (") +
126                              action->shortcut().toString(QKeySequence::NativeText) +
127                              QLatin1String(")"));
128 }
129
130 QPixmap IconUtils::pixmap(const QString &name) {
131     // Check if a "@2x" file exists
132     const qreal pixelRatio = IconUtils::pixelRatio();
133     if (pixelRatio > 1.0) {
134         int dotIndex = name.lastIndexOf(QLatin1Char('.'));
135         if (dotIndex != -1) {
136             QString at2xfileName = name;
137             at2xfileName.insert(dotIndex, QLatin1String("@2x"));
138             if (QFile::exists(at2xfileName)) {
139                 QPixmap pixmap(at2xfileName);
140                 pixmap.setDevicePixelRatio(pixelRatio);
141                 return pixmap;
142             }
143         }
144     }
145     return QPixmap(name);
146 }
147
148 qreal IconUtils::pixelRatio() {
149 #if QT_VERSION >= 0x050600
150     return MainWindow::instance()->devicePixelRatioF();
151 #else
152     return MainWindow::instance()->devicePixelRatio();
153 #endif
154 }