]> git.sur5r.net Git - minitube/blob - src/painterutils.cpp
71ce0124fdb7703d9b56f11821cd25dfacf6dd4c
[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(
32                 (widget->width()-textSize.width())/2,
33                 ((widget->height()-textSize.height())/2)
34                 );
35     QRect rect(topLeft, textSize);
36     painter.setOpacity(.5);
37     painter.drawText(rect, Qt::AlignCenter, message);
38 }
39
40 void PainterUtils::paintBadge(QPainter *painter, const QString &text, bool center, QColor backgroundColor) {
41     painter->save();
42
43     QRect textBox = painter->boundingRect(QRect(), Qt::AlignCenter, text);
44     int w = textBox.width() + painter->fontMetrics().width('m');
45     int x = 0;
46     if (center) x -= w / 2;
47     QRect rect(x, 0, w, textBox.height());
48     if (rect.width() < rect.height() || text.length() == 1) rect.setWidth(rect.height());
49
50     painter->setPen(Qt::NoPen);
51     painter->setBrush(backgroundColor);
52     painter->setRenderHint(QPainter::Antialiasing);
53     qreal borderRadius = rect.height()/2.;
54     painter->drawRoundedRect(rect, borderRadius, borderRadius);
55
56     painter->setPen(Qt::white);
57     painter->drawText(rect, Qt::AlignCenter, text);
58
59     painter->restore();
60 }