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));
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);
38 void PainterUtils::paintBadge(QPainter *painter,
41 const QColor &backgroundColor,
45 QRect textBox = painter->boundingRect(QRect(), Qt::AlignCenter, text);
46 int w = textBox.width() + painter->fontMetrics().width('m');
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());
52 painter->setPen(Qt::NoPen);
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);
63 painter->setFont(FontUtils::small());
64 painter->setPen(Qt::white);
65 painter->drawText(rect, Qt::AlignCenter, text);