1 // Copyright 2010 Jochen Becher
3 // This file is part of MovieSchedule.
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.
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.
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/>.
18 #include "backgroundlabel.h"
25 static const int MINIMUM_FONT_SIZE = 40;
27 BackgroundLabel::BackgroundLabel(QWidget *parent)
29 _cached_label_font_size(0),
30 _cached_label_alignment(Qt::AlignHCenter | Qt::AlignTop)
34 void BackgroundLabel::SetLabelText(const QString &label_text)
36 _label_text = label_text;
37 _cached_label_font_size = 0;
41 void BackgroundLabel::paintEvent(QPaintEvent *event)
45 QPainter painter(this);
47 QFont label_font = font();
48 label_font.setWeight(QFont::Black);
50 painter.translate(rect().width(), 0);
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;
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;
80 _cached_label_font_size = font_size;
82 label_font.setPixelSize(_cached_label_font_size);
83 painter.setFont(label_font);
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);