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 "painterutils.h"
22 #include "fontutils.h"
23 #include "iconutils.h"
25 PainterUtils::PainterUtils() { }
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));
32 (widget->width()-textSize.width())/2,
33 ((widget->height()-textSize.height())/2)
35 QRect rect(topLeft, textSize);
36 painter.setOpacity(.5);
37 painter.drawText(rect, Qt::AlignCenter, message);
40 void PainterUtils::paintBadge(QPainter *painter, const QString &text, bool center, QColor backgroundColor) {
43 QRect textBox = painter->boundingRect(QRect(), Qt::AlignCenter, text);
44 int w = textBox.width() + painter->fontMetrics().width('m');
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());
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);
56 painter->setPen(Qt::white);
57 painter->drawText(rect, Qt::AlignCenter, text);