Minor fix for another change in Google's movie pages and a fix in
[movie-schedule] / src / ui / theaterschedulemodel.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 "theaterschedulemodel.h"
19
20 #include "scheduleentryitem.h"
21 #include "data/cinemaschedule.h"
22 #include "utils/assertedlocker.h"
23
24 #include <iostream>
25
26 TheaterScheduleModel::TheaterScheduleModel(const CinemaSchedule *cinema_schedule, const CinemaKey &cinema_key, QObject *parent)
27     : QAbstractListModel(parent),
28     _cinema_schedule(cinema_schedule),
29     _cinema_key(cinema_key)
30 {
31 }
32
33 void TheaterScheduleModel::Update()
34 {
35     AssertedReadLocker locker(_cinema_schedule->GetLock());
36     const Cinema *cinema = _cinema_schedule->FindCinema(_cinema_key);
37     if (cinema != 0) {
38         _schedule_dates = _cinema_schedule->GetScheduleDates(cinema).toList();
39         _schedule_keys = _cinema_schedule->GetScheduleKeys(_cinema_key).toList();
40     }
41 }
42
43 int TheaterScheduleModel::rowCount(const QModelIndex &parent) const
44 {
45     Q_UNUSED(parent);
46     return _schedule_dates.size() + _schedule_keys.size();
47 }
48
49 QVariant TheaterScheduleModel::data(const QModelIndex &index, int role) const
50 {
51     if (!(index.isValid() && index.row() >= 0 && index.row() < _schedule_dates.size() + _schedule_keys.size())) {
52         std::cout << "TheaterScheduleModel: invalid index" << std::endl;
53         return QVariant();
54     }
55     if (index.row() < _schedule_dates.size()) {
56         QDate date = _schedule_dates[index.row()];
57         switch (role) {
58         case Qt::DisplayRole:
59             return qVariantFromValue(ScheduleEntryItem(date));
60         case SortRole:
61             return QVariant(date.toString("yyyyMMdd"));
62         }
63     } else {
64         AssertedReadLocker locker(_cinema_schedule->GetLock());
65         const ScheduleEntryKey &schedule_entry_key = _schedule_keys.operator [](index.row() - _schedule_dates.size());
66         const ScheduleEntry schedule_entry = _cinema_schedule->FindScheduleEntry(schedule_entry_key);
67         if (schedule_entry.IsValid()) {
68             switch (role) {
69             case Qt::DisplayRole:
70             case TheaterScheduleItemRole:
71                 return qVariantFromValue(ScheduleEntryItem(schedule_entry));
72             case TheaterScheduleKeyRole:
73                 return qVariantFromValue(schedule_entry_key);
74             case SortRole:
75                 return QVariant(schedule_entry.GetDate().toString("yyyyMMdd") + schedule_entry.GetStartTime().toString("hhmm")
76                                 + schedule_entry.GetMovie()->GetName());
77             }
78         }
79     }
80     return QVariant();
81 }