]> git.sur5r.net Git - minitube/blob - src/painterutils.cpp
54ee39ee26d1cb6365c70691e3b6ee8240a94a6e
[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
24 PainterUtils::PainterUtils() { }
25
26 void PainterUtils::centeredMessage(QString message, QWidget* widget) {
27     QPainter painter(widget);
28     painter.setFont(FontUtils::bigBold());
29     QSize textSize(QFontMetrics(painter.font()).size(Qt::TextSingleLine, message));
30     QPoint topLeft(
31                 (widget->width()-textSize.width())/2,
32                 ((widget->height()-textSize.height())/2)
33                 );
34     QRect rect(topLeft, textSize);
35
36     /*
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);
41     */
42
43     QPen textPen;
44     textPen.setBrush(widget->palette().mid());
45     painter.setPen(textPen);
46     painter.drawText(rect, Qt::AlignCenter, message);
47 }
48
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));
58         }
59     }
60     QRect rect = widget->rect();
61     QPainter p(widget);
62     p.fillRect(rect.x(), rect.y(), rect.width(), shadowHeight, QBrush(shadow));
63 }
64
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();
70
71     const int textSize = text.size();
72
73     QPixmap badge;
74     if (textSize < 3) badge = badge1;
75     else if (textSize == 3) badge = badge3;
76     else badge = badge4;
77
78     int x = 0;
79     if (center) x -= badge.width() / 2;
80
81     QRect rect(x, 0, badge.width(), size);
82     painter->drawPixmap(rect, badge);
83
84     QFont f = painter->font();
85     f.setPixelSize(11);
86     f.setHintingPreference(QFont::PreferNoHinting);
87 #ifdef APP_MAC
88     f.setFamily("Helvetica");
89 #endif
90 #ifdef APP_WIN
91     rect.adjust(0, -2, 0, 0);
92 #endif
93 #ifdef Q_WS_X11
94     rect.adjust(0, -1, 0, 0);
95 #endif
96     painter->save();
97     painter->setFont(f);
98
99     rect.adjust(0, 1, 0, 0);
100     painter->setPen(QColor(0, 0, 0, 64));
101     painter->drawText(rect, Qt::AlignCenter, text);
102
103     rect.adjust(0, -1, 0, 0);
104     painter->setPen(Qt::white);
105     painter->drawText(rect, Qt::AlignCenter, text);
106
107     painter->restore();
108 }