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 "theaterschedulemodel.h"
20 #include "scheduleentryitem.h"
21 #include "data/cinemaschedule.h"
22 #include "utils/assertedlocker.h"
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)
33 void TheaterScheduleModel::Update()
35 AssertedReadLocker locker(_cinema_schedule->GetLock());
36 const Cinema *cinema = _cinema_schedule->FindCinema(_cinema_key);
38 _schedule_dates = _cinema_schedule->GetScheduleDates(cinema).toList();
39 _schedule_keys = _cinema_schedule->GetScheduleKeys(_cinema_key).toList();
43 int TheaterScheduleModel::rowCount(const QModelIndex &parent) const
46 return _schedule_dates.size() + _schedule_keys.size();
49 QVariant TheaterScheduleModel::data(const QModelIndex &index, int role) const
51 if (!(index.isValid() && index.row() >= 0 && index.row() < _schedule_dates.size() + _schedule_keys.size())) {
52 std::cout << "TheaterScheduleModel: invalid index" << std::endl;
55 if (index.row() < _schedule_dates.size()) {
56 QDate date = _schedule_dates[index.row()];
59 return qVariantFromValue(ScheduleEntryItem(date));
61 return QVariant(date.toString("yyyyMMdd"));
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()) {
70 case TheaterScheduleItemRole:
71 return qVariantFromValue(ScheduleEntryItem(schedule_entry));
72 case TheaterScheduleKeyRole:
73 return qVariantFromValue(schedule_entry_key);
75 return QVariant(schedule_entry.GetDate().toString("yyyyMMdd") + schedule_entry.GetStartTime().toString("hhmm")
76 + schedule_entry.GetMovie()->GetName());