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"
24 QIcon IconUtils::fromTheme(const QString &name) {
25 const QLatin1String symbolic("-symbolic");
26 if (name.endsWith(symbolic)) return QIcon::fromTheme(name);
28 icon = QIcon::fromTheme(name + symbolic);
29 if (icon.isNull()) return QIcon::fromTheme(name);
33 QIcon IconUtils::fromResources(const QString &name) {
34 QIcon icon = QIcon(QString(":/images/%1.png").arg(name));
36 icon.addPixmap(QString(":/images/%1_active.png").arg(name), QIcon::Active);
37 icon.addPixmap(QString(":/images/%1_selected.png").arg(name), QIcon::Selected);
38 icon.addPixmap(QString(":/images/%1_disabled.png").arg(name), QIcon::Disabled);
43 QIcon IconUtils::icon(const QString &name) {
44 #if defined(APP_MAC) || defined(APP_WIN)
45 return fromResources(name);
47 QIcon icon = fromTheme(name);
48 if (icon.isNull()) icon = fromResources(name);
53 QIcon IconUtils::icon(const QStringList &names) {
55 foreach (const QString &name, names) {
56 icon = IconUtils::icon(name);
57 if (!icon.availableSizes().isEmpty()) break;
62 QIcon IconUtils::tintedIcon(const QString &name, const QColor &color, QList<QSize> sizes) {
63 QIcon i = IconUtils::icon(name);
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));
74 QIcon IconUtils::tintedIcon(const QString &name, const QColor &color, const QSize &size) {
75 return IconUtils::tintedIcon(name, color, QList<QSize>() << size);
78 QImage IconUtils::grayscaled(const QImage &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]));
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);
97 img.setAlphaChannel(image.alphaChannel());
101 void IconUtils::setupAction(QAction *action) {
103 // unexperienced users tend to keep keys pressed for a "long" time
104 action->setAutoRepeat(false);
106 // show keyboard shortcuts in the status bar
107 if (!action->shortcut().isEmpty())
108 action->setStatusTip(action->statusTip() +
110 action->shortcut().toString(QKeySequence::NativeText) +