Merge https://vcs.maemo.org/git/movie-schedule
[movie-schedule] / src / ui / moviescheduledelegate.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 "moviescheduledelegate.h"
19
20 #include "movieschedulemodel.h"
21 #include "data/scheduleentry.h"
22 #include "data/cinema.h"
23 #include "scheduleentryitem.h"
24 #include "utils/timeutils.h"
25 #include "ui/styleutils.h"
26
27 #include <QPainter>
28
29 MovieScheduleDelegate::MovieScheduleDelegate(QWidget *parent)
30     : QStyledItemDelegate(parent)
31 {
32 }
33
34 void MovieScheduleDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
35                                   const QModelIndex &index) const
36 {
37     if (qVariantCanConvert<ScheduleEntryItem>(index.data())) {
38         ScheduleEntryItem item = qVariantValue<ScheduleEntryItem>(index.data());
39         painter->save();
40         painter->translate(option.rect.x(), option.rect.y());
41         if (item.IsScheduleDateItem()) {
42             _day_painter.Paint(painter, option, item.GetScheduleDate());
43         } else {
44             QRect bounding_rect;
45             QFont bigfont(option.font);
46             bigfont.setPointSizeF(bigfont.pointSizeF() * 1.5);
47             painter->setFont(bigfont);
48             painter->setPen(StyleUtils::INSTANCE()->GetScheduleTimeColor(option.palette, item.GetScheduleEntry()->GetStartTime()));
49             painter->drawText(0, 0, 1000, option.rect.height() - 4,
50                               Qt::AlignTop, TimeUtils::ToTimeString(item.GetScheduleEntry()->GetStartTime()), &bounding_rect);
51             int x = bounding_rect.right() + 8;
52             painter->translate(x, 0);
53             _cinema_painter.Paint(painter, option, item.GetScheduleEntry()->GetCinema());
54         }
55         painter->restore();
56     } else {
57         QStyledItemDelegate::paint(painter, option, index);
58     }
59 }
60
61 QSize MovieScheduleDelegate::sizeHint(const QStyleOptionViewItem &option,
62                                       const QModelIndex &index) const
63 {
64     if (qVariantCanConvert<ScheduleEntryItem>(index.data())) {
65         ScheduleEntryItem item = qVariantValue<ScheduleEntryItem>(index.data());
66         if (item.IsScheduleDateItem()) {
67             return _day_painter.GetSizeHint(option);
68         }
69         return _cinema_painter.GetSizeHint(option);
70     } else {
71         return QStyledItemDelegate::sizeHint(option, index);
72     }
73 }