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"
24 PainterUtils::PainterUtils() { }
26 void PainterUtils::centeredMessage(const QString &message, QWidget* widget) {
27 QPainter painter(widget);
28 painter.setFont(FontUtils::big());
29 QSize textSize(QFontMetrics(painter.font()).size(Qt::TextSingleLine, message));
31 (widget->width()-textSize.width())/2,
32 ((widget->height()-textSize.height())/2)
34 QRect rect(topLeft, textSize);
37 rect.adjust(0, -1, 0, 0);
38 painter.setPen(QColor(0, 0, 0, 128));
39 painter.drawText(rect, Qt::AlignCenter, message);
40 rect.adjust(0, 1, 0, 0);
44 textPen.setBrush(widget->palette().mid());
45 painter.setPen(textPen);
46 painter.drawText(rect, Qt::AlignCenter, message);
49 void PainterUtils::topShadow(QWidget *widget) {
50 static QLinearGradient shadow;
51 static const int shadowHeight = 10;
52 if (shadow.stops().count() == 2) {
53 shadow.setFinalStop(0, shadowHeight);
54 const qreal initialOpacity = 96;
55 for (qreal i = 0; i <= 1; i += 1.0/shadowHeight) {
56 qreal opacity = qPow(initialOpacity, (1.0 - i)) - 1;
57 shadow.setColorAt(i, QColor(0x00, 0x00, 0x00, opacity));
60 QRect rect = widget->rect();
62 p.fillRect(rect.x(), rect.y(), rect.width(), shadowHeight, QBrush(shadow));
65 void PainterUtils::paintBadge(QPainter *painter, const QString &text, bool center) {
66 static const QPixmap badge1 = QPixmap(":/images/badge.png");
67 static const QPixmap badge3 = QPixmap(":/images/badge3.png");
68 static const QPixmap badge4 = QPixmap(":/images/badge4.png");
69 static const int size = badge1.height();
71 const int textSize = text.size();
74 if (textSize < 3) badge = badge1;
75 else if (textSize == 3) badge = badge3;
79 if (center) x -= badge.width() / 2;
81 QRect rect(x, 0, badge.width(), size);
82 painter->drawPixmap(rect, badge);
84 QFont f = painter->font();
86 f.setHintingPreference(QFont::PreferNoHinting);
88 f.setFamily("Helvetica");
91 rect.adjust(0, -2, 0, 0);
94 rect.adjust(0, -1, 0, 0);
99 rect.adjust(0, 1, 0, 0);
100 painter->setPen(QColor(0, 0, 0, 64));
101 painter->drawText(rect, Qt::AlignCenter, text);
103 rect.adjust(0, -1, 0, 0);
104 painter->setPen(Qt::white);
105 painter->drawText(rect, Qt::AlignCenter, text);