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 #include "mainwindow.h"
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);
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);
39 QLatin1String active("_active");
40 QLatin1String selected("_selected");
41 QLatin1String disabled("_disabled");
42 QLatin1String checked("_checked");
43 QLatin1String twoX("@2x");
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);
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);
59 QIcon IconUtils::icon(const QString &name) {
61 QIcon icon = fromTheme(name);
62 if (icon.isNull()) icon = fromResources(name);
65 return fromResources(name);
69 QIcon IconUtils::icon(const QStringList &names) {
71 for (const QString &name : names) {
72 icon = IconUtils::icon(name);
73 if (!icon.availableSizes().isEmpty()) break;
78 QIcon IconUtils::tintedIcon(const QString &name, const QColor &color, QList<QSize> sizes) {
79 QIcon i = IconUtils::icon(name);
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));
90 QIcon IconUtils::tintedIcon(const QString &name, const QColor &color, const QSize &size) {
91 return IconUtils::tintedIcon(name, color, QList<QSize>() << size);
94 QImage IconUtils::grayscaled(const QImage &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]));
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);
113 img.setAlphaChannel(image.alphaChannel());
117 void IconUtils::setupAction(QAction *action) {
119 // unexperienced users tend to keep keys pressed for a "long" time
120 action->setAutoRepeat(false);
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) +
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);
145 return QPixmap(name);
148 qreal IconUtils::pixelRatio() {
149 #if QT_VERSION >= 0x050600
150 return MainWindow::instance()->devicePixelRatioF();
152 return MainWindow::instance()->devicePixelRatio();