]> git.sur5r.net Git - minitube/blob - src/painterutils.cpp
HiDPI support
[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 #include "iconutils.h"
24
25 PainterUtils::PainterUtils() { }
26
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(
32                 (widget->width()-textSize.width())/2,
33                 ((widget->height()-textSize.height())/2)
34                 );
35     QRect rect(topLeft, textSize);
36
37     /*
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);
42     */
43
44     QPen textPen;
45     textPen.setBrush(widget->palette().mid());
46     painter.setPen(textPen);
47     painter.drawText(rect, Qt::AlignCenter, message);
48 }
49
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));
59         }
60     }
61     QRect rect = widget->rect();
62     QPainter p(widget);
63     p.fillRect(rect.x(), rect.y(), rect.width(), shadowHeight, QBrush(shadow));
64 }
65
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");
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     const int w = badge.width() / badge.devicePixelRatio();
79     const int h = badge.height() / badge.devicePixelRatio();
80
81     int x = 0;
82     if (center) x -= w / 2;
83
84     QRect rect(x, 0, w, h);
85     painter->drawPixmap(rect, badge);
86
87     QFont f = painter->font();
88     f.setPixelSize(11);
89     f.setHintingPreference(QFont::PreferNoHinting);
90 #ifdef APP_MAC
91     f.setFamily("Helvetica");
92 #elif APP_WIN
93     rect.adjust(0, -2, 0, 0);
94 #else
95     rect.adjust(0, -1, 0, 0);
96 #endif
97     painter->save();
98     painter->setFont(f);
99
100     rect.adjust(0, 1, 0, 0);
101     painter->setPen(QColor(0, 0, 0, 64));
102     painter->drawText(rect, Qt::AlignCenter, text);
103
104     rect.adjust(0, -1, 0, 0);
105     painter->setPen(Qt::white);
106     painter->drawText(rect, Qt::AlignCenter, text);
107
108     painter->restore();
109 }