b9c622b8d4c7dd31f1041f196285e56b4a0e12b9
[movie-schedule] / src / ui / backgroundlabel.cpp
1 // Copyright 2010 Jochen Becher
2 //
3 // This file is part of MovieSchedule.
4 //
5 // MovieSchedule is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // MovieSchedule is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with MovieSchedule.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "backgroundlabel.h"
19
20 #include <QPainter>
21 #include <QFont>
22
23 static const int MINIMUM_FONT_SIZE = 40;
24
25 BackgroundLabel::BackgroundLabel(QWidget *parent)
26     : QWidget(parent),
27     _cached_label_font_size(0),
28     _cached_label_alignment(Qt::AlignHCenter | Qt::AlignTop)
29 {
30 }
31
32 void BackgroundLabel::SetLabelText(const QString &label_text)
33 {
34     _label_text = label_text;
35     _cached_label_font_size = 0;
36     update();
37 }
38
39 void BackgroundLabel::paintEvent(QPaintEvent *event)
40 {
41     Q_UNUSED(event);
42
43     QPainter painter(this);
44
45     QFont label_font = font();
46     label_font.setWeight(QFont::Black);
47
48     painter.translate(rect().width(), 0);
49     painter.rotate(90);
50
51     QRect widget_rect = painter.worldTransform().inverted().mapRect(rect());
52     if (_cached_label_font_size <= 0 || widget_rect != _cached_widget_rect) {
53         _cached_widget_rect = widget_rect;
54         _cached_label_rect = widget_rect;
55         _cached_label_rect.setTop(_cached_label_rect.top() + 20);
56         _cached_label_alignment = Qt::AlignHCenter | Qt::AlignTop;
57         int font_size = 80;
58
59         for (;;) {
60             label_font.setPixelSize(font_size);
61             painter.setFont(label_font);
62             QRect label_rect = painter.boundingRect(_cached_label_rect, _cached_label_alignment, _label_text);
63             if (label_rect.x() < _cached_widget_rect.x()
64                 || label_rect.y() < _cached_widget_rect.y()
65                 || label_rect.width() > _cached_widget_rect.width()
66                 || label_rect.height() > _cached_widget_rect.height()) {
67                 font_size = font_size * 0.9;
68                 if (font_size < MINIMUM_FONT_SIZE) {
69                     font_size = MINIMUM_FONT_SIZE;
70                     _cached_label_alignment = Qt::AlignLeft | Qt::AlignTop;
71                     break;
72                 }
73             } else {
74                 break;
75             }
76         }
77         _cached_label_font_size = font_size;
78     } else {
79         label_font.setPixelSize(_cached_label_font_size);
80         painter.setFont(label_font);
81     }
82     QColor text_color = palette().color(QPalette::WindowText);
83     text_color.setAlpha(32);
84     painter.setPen(text_color);
85     painter.drawText(_cached_label_rect, _cached_label_alignment, _label_text);
86 }