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"
23 static const int MINIMUM_FONT_SIZE = 40;
25 BackgroundLabel::BackgroundLabel(QWidget *parent)
27 _cached_label_font_size(0),
28 _cached_label_alignment(Qt::AlignHCenter | Qt::AlignTop)
32 void BackgroundLabel::SetLabelText(const QString &label_text)
34 _label_text = label_text;
35 _cached_label_font_size = 0;
39 void BackgroundLabel::paintEvent(QPaintEvent *event)
43 QPainter painter(this);
45 QFont label_font = font();
46 label_font.setWeight(QFont::Black);
48 painter.translate(rect().width(), 0);
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;
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;
77 _cached_label_font_size = font_size;
79 label_font.setPixelSize(_cached_label_font_size);
80 painter.setFont(label_font);
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);