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