]> git.sur5r.net Git - minitube/blob - src/painterutils.cpp
New upstream version 3.1
[minitube] / src / painterutils.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 "painterutils.h"
22 #include "fontutils.h"
23 #include "iconutils.h"
24
25 PainterUtils::PainterUtils() {}
26
27 void PainterUtils::centeredMessage(const QString &message, QWidget *widget) {
28     QPainter painter(widget);
29     painter.setFont(FontUtils::big());
30     QSize textSize(QFontMetrics(painter.font()).size(Qt::TextSingleLine, message));
31     QPoint topLeft((widget->width() - textSize.width()) / 2,
32                    ((widget->height() - textSize.height()) / 2));
33     QRect rect(topLeft, textSize);
34     painter.setOpacity(.5);
35     painter.drawText(rect, Qt::AlignCenter, message);
36 }
37
38 void PainterUtils::paintBadge(QPainter *painter,
39                               const QString &text,
40                               bool center,
41                               const QColor &backgroundColor,
42                               bool literalColor) {
43     painter->save();
44
45     QRect textBox = painter->boundingRect(QRect(), Qt::AlignCenter, text);
46     int w = textBox.width() + painter->fontMetrics().width('m');
47     int x = 0;
48     if (center) x -= w / 2;
49     QRect rect(x, 0, w, textBox.height());
50     if (rect.width() < rect.height() || text.length() == 1) rect.setWidth(rect.height());
51
52     painter->setPen(Qt::NoPen);
53     QColor bg;
54     if (literalColor)
55         bg = backgroundColor;
56     else
57         bg = backgroundColor.value() > 128 ? QColor(0, 0, 0, 64) : QColor(255, 255, 255, 64);
58     painter->setBrush(bg);
59     painter->setRenderHint(QPainter::Antialiasing);
60     qreal borderRadius = rect.height() / 2.;
61     painter->drawRoundedRect(rect, borderRadius, borderRadius);
62
63     painter->setFont(FontUtils::small());
64     painter->setPen(Qt::white);
65     painter->drawText(rect, Qt::AlignCenter, text);
66
67     painter->restore();
68 }