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);
38 rect.adjust(0, -1, 0, 0);
39 painter.setPen(QColor(0, 0, 0, 128));
40 painter.drawText(rect, Qt::AlignCenter, message);
41 rect.adjust(0, 1, 0, 0);
45 textPen.setBrush(widget->palette().mid());
46 painter.setPen(textPen);
47 painter.drawText(rect, Qt::AlignCenter, message);
50 void PainterUtils::topShadow(QWidget *widget) {
51 static QLinearGradient shadow;
52 static const int shadowHeight = 10;
53 if (shadow.stops().count() == 2) {
54 shadow.setFinalStop(0, shadowHeight);
55 const qreal initialOpacity = 96;
56 for (qreal i = 0; i <= 1; i += 1.0/shadowHeight) {
57 qreal opacity = qPow(initialOpacity, (1.0 - i)) - 1;
58 shadow.setColorAt(i, QColor(0x00, 0x00, 0x00, opacity));
61 QRect rect = widget->rect();
63 p.fillRect(rect.x(), rect.y(), rect.width(), shadowHeight, QBrush(shadow));
66 void PainterUtils::paintBadge(QPainter *painter, const QString &text, bool center) {
67 const QPixmap badge1 = IconUtils::pixmap(":/images/badge.png");
68 const QPixmap badge3 = IconUtils::pixmap(":/images/badge3.png");
69 const QPixmap badge4 = IconUtils::pixmap(":/images/badge4.png");
71 const int textSize = text.size();
74 if (textSize < 3) badge = badge1;
75 else if (textSize == 3) badge = badge3;
78 const int w = badge.width() / badge.devicePixelRatio();
79 const int h = badge.height() / badge.devicePixelRatio();
82 if (center) x -= w / 2;
84 QRect rect(x, 0, w, h);
85 painter->drawPixmap(rect, badge);
87 QFont f = painter->font();
89 f.setHintingPreference(QFont::PreferNoHinting);
91 f.setFamily("Helvetica");
93 rect.adjust(0, -2, 0, 0);
95 rect.adjust(0, -1, 0, 0);
100 rect.adjust(0, 1, 0, 0);
101 painter->setPen(QColor(0, 0, 0, 64));
102 painter->drawText(rect, Qt::AlignCenter, text);
104 rect.adjust(0, -1, 0, 0);
105 painter->setPen(Qt::white);
106 painter->drawText(rect, Qt::AlignCenter, text);