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