]> git.sur5r.net Git - minitube/blob - src/iconutils.cpp
b975411159ff0ef638b3d557c6086363e969c778
[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
24 QIcon IconUtils::fromTheme(const QString &name) {
25     const QLatin1String symbolic("-symbolic");
26     if (name.endsWith(symbolic)) return QIcon::fromTheme(name);
27     QIcon icon;
28     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     QIcon icon = QIcon(QString(":/images/%1.png").arg(name));
35     if (!icon.isNull()) {
36         icon.addPixmap(IconUtils::pixmap(QString(":/images/%1_active.png").arg(name)), QIcon::Active);
37         icon.addPixmap(IconUtils::pixmap(QString(":/images/%1_selected.png").arg(name)), QIcon::Selected);
38         icon.addPixmap(IconUtils::pixmap(QString(":/images/%1_disabled.png").arg(name)), QIcon::Disabled);
39     }
40     return icon;
41 }
42
43 QIcon IconUtils::icon(const QString &name) {
44 #if defined(APP_MAC) || defined(APP_WIN)
45     return fromResources(name);
46 #else
47     QIcon icon = fromTheme(name);
48     if (icon.isNull()) icon = fromResources(name);
49     return icon;
50 #endif
51 }
52
53 QIcon IconUtils::icon(const QStringList &names) {
54     QIcon icon;
55     foreach (const QString &name, names) {
56         icon = IconUtils::icon(name);
57         if (!icon.availableSizes().isEmpty()) break;
58     }
59     return icon;
60 }
61
62 QIcon IconUtils::tintedIcon(const QString &name, const QColor &color, QList<QSize> sizes) {
63     QIcon i = IconUtils::icon(name);
64     QIcon t;
65     if (sizes.isEmpty()) sizes = i.availableSizes();
66     foreach (const QSize &size, sizes) {
67         QPixmap pixmap = i.pixmap(size);
68         QImage tintedImage = tinted(pixmap.toImage(), color);
69         t.addPixmap(QPixmap::fromImage(tintedImage));
70     }
71     return t;
72 }
73
74 QIcon IconUtils::tintedIcon(const QString &name, const QColor &color, const QSize &size) {
75     return IconUtils::tintedIcon(name, color, QList<QSize>() << size);
76 }
77
78 QImage IconUtils::grayscaled(const QImage &image) {
79     QImage img = image;
80     int pixels = img.width() * img.height();
81     unsigned int *data = (unsigned int *)img.bits();
82     for (int i = 0; i < pixels; ++i) {
83         int val = qGray(data[i]);
84         data[i] = qRgba(val, val, val, qAlpha(data[i]));
85     }
86     return img;
87 }
88
89 QImage IconUtils::tinted(const QImage &image, const QColor &color,
90                          QPainter::CompositionMode mode) {
91     QImage img(image.size(), QImage::Format_ARGB32_Premultiplied);
92     QPainter painter(&img);
93     painter.drawImage(0, 0, grayscaled(image));
94     painter.setCompositionMode(mode);
95     painter.fillRect(img.rect(), color);
96     painter.end();
97     img.setAlphaChannel(image.alphaChannel());
98     return img;
99 }
100
101 void IconUtils::setupAction(QAction *action) {
102     // never autorepeat.
103     // unexperienced users tend to keep keys pressed for a "long" time
104     action->setAutoRepeat(false);
105
106     // show keyboard shortcuts in the status bar
107     if (!action->shortcut().isEmpty())
108         action->setStatusTip(action->statusTip() +
109                              " (" +
110                              action->shortcut().toString(QKeySequence::NativeText) +
111                              ")");
112 }
113
114 QPixmap IconUtils::pixmap(const QString &name) {
115     // Check if a "@2x" file exists
116     QString fileName = name;
117     if (qApp->devicePixelRatio() > 1.0) {
118         int dotIndex = fileName.lastIndexOf(QLatin1Char('.'));
119         if (dotIndex != -1) {
120             QString at2xfileName = fileName;
121             at2xfileName.insert(dotIndex, QStringLiteral("@2x"));
122             if (QFile::exists(at2xfileName))
123                 fileName = at2xfileName;
124         }
125     }
126
127     return QPixmap(fileName);
128 }