Minor fix for another change in Google's movie pages and a fix in
[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 #include "uiutils.h"
20
21 #include <QPainter>
22 #include <QFont>
23 #include <iostream>
24
25 static const int MINIMUM_FONT_SIZE = 40;
26
27 BackgroundLabel::BackgroundLabel(QWidget *parent)
28     : QWidget(parent),
29     _cached_label_font_size(0),
30     _cached_label_alignment(Qt::AlignHCenter | Qt::AlignTop)
31 {
32 }
33
34 void BackgroundLabel::SetLabelText(const QString &label_text)
35 {
36     _label_text = label_text;
37     _cached_label_font_size = 0;
38     update();
39 }
40
41 void BackgroundLabel::paintEvent(QPaintEvent *event)
42 {
43     Q_UNUSED(event);
44
45     QPainter painter(this);
46
47     QFont label_font = font();
48     label_font.setWeight(QFont::Black);
49
50     painter.translate(rect().width(), 0);
51     painter.rotate(90);
52
53     QRect widget_rect = painter.worldTransform().inverted().mapRect(rect());
54     if (_cached_label_font_size <= 0 || widget_rect != _cached_widget_rect) {
55         bool landscape = rect().width() >= rect().height();
56         _cached_widget_rect = widget_rect;
57         _cached_label_rect = widget_rect;
58         _cached_label_rect.setTop(_cached_label_rect.top() + (landscape ? 20 : 10));
59         _cached_label_alignment = Qt::AlignHCenter | Qt::AlignTop;
60         int font_size = 80;
61
62         for (;;) {
63             label_font.setPixelSize(font_size);
64             painter.setFont(label_font);
65             QRect label_rect = painter.boundingRect(_cached_label_rect, _cached_label_alignment, _label_text);
66             if (label_rect.x() < _cached_widget_rect.x()
67                 || label_rect.y() < _cached_widget_rect.y()
68                 || label_rect.width() > _cached_widget_rect.width()
69                 || label_rect.height() > _cached_widget_rect.height()) {
70                 font_size = font_size * 0.9;
71                 if (font_size < MINIMUM_FONT_SIZE) {
72                     font_size = MINIMUM_FONT_SIZE;
73                     _cached_label_alignment = Qt::AlignLeft | Qt::AlignTop;
74                     break;
75                 }
76             } else {
77                 break;
78             }
79         }
80         _cached_label_font_size = font_size;
81     } else {
82         label_font.setPixelSize(_cached_label_font_size);
83         painter.setFont(label_font);
84     }
85     QColor text_color = palette().color(QPalette::WindowText);
86     text_color.setAlpha(32);
87     painter.setPen(text_color);
88     painter.drawText(_cached_label_rect, _cached_label_alignment, _label_text);
89 }