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"
23 QIcon IconUtils::fromTheme(const QString &name) {
24 const QLatin1String symbolic("-symbolic");
25 if (name.endsWith(symbolic)) return QIcon::fromTheme(name);
27 icon = QIcon::fromTheme(name + symbolic);
28 if (icon.isNull()) return QIcon::fromTheme(name);
32 QIcon IconUtils::fromResources(const QString &name) {
33 QIcon icon = QIcon(QString(":/images/%1.png").arg(name));
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);
42 QIcon IconUtils::icon(const QString &name) {
43 #if defined(APP_MAC) || defined(APP_WIN)
44 return fromResources(name);
46 QIcon icon = fromTheme(name);
47 if (icon.isNull()) icon = fromResources(name);
52 QIcon IconUtils::icon(const QStringList &names) {
54 foreach (QString name, names) {
55 icon = IconUtils::icon(name);
56 if (!icon.availableSizes().isEmpty()) break;
61 QIcon IconUtils::tintedIcon(const QString &name, const QColor &color, QList<QSize> sizes) {
62 QIcon i = IconUtils::icon(name);
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));
73 QIcon IconUtils::tintedIcon(const QString &name, const QColor &color, const QSize &size) {
74 return IconUtils::tintedIcon(name, color, QList<QSize>() << size);
77 QImage IconUtils::grayscaled(const QImage &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]));
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);
96 img.setAlphaChannel(image.alphaChannel());
100 void IconUtils::setupAction(QAction *action) {
102 // unexperienced users tend to keep keys pressed for a "long" time
103 action->setAutoRepeat(false);
105 // show keyboard shortcuts in the status bar
106 if (!action->shortcut().isEmpty())
107 action->setStatusTip(action->statusTip() +
109 action->shortcut().toString(QKeySequence::NativeText) +