2 * Copyright (C) 2010 Ixonos Plc.
4 * This file is part of fosdem-schedule.
6 * fosdem-schedule is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 2 of the License, or (at your option)
11 * fosdem-schedule is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * fosdem-schedule. If not, see <http://www.gnu.org/licenses/>.
19 #include "eventmodel.h"
20 #include <conference.h>
24 const QString EventModel::COMMA_SEPARATOR = ", ";
26 EventModel::EventModel()
31 void EventModel::createTimeGroups()
41 const int timeSpan = 5400;
43 QTime startTime = mEvents.first().start().time();
44 mGroups << EventModel::Group(QString("%1 - %2").arg(startTime.toString("HH:mm"),
45 startTime.addSecs(timeSpan).toString("HH:mm")), 0);
46 QTime nextGroupTime = mEvents.first().start().time().addSecs(timeSpan);
48 for (int i=0; i<mEvents.count(); i++)
50 QTime eventTime = mEvents.at(i).start().time();
52 if (nextGroupTime <= eventTime)
54 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
55 mGroups << EventModel::Group(QString("%1 - %2").arg(nextGroupTime.toString("HH:mm"),
56 nextGroupTime.addSecs(timeSpan).toString("HH:mm")), i);
57 nextGroupTime = nextGroupTime.addSecs(timeSpan);
60 // add parent-child relation
61 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
64 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
67 void EventModel::createTrackGroups() {
74 int trackId = mEvents.first().trackId();
76 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), 0);
77 int nextTrackId = trackId;
79 for (int i=0; i<mEvents.count(); i++)
81 trackId = mEvents.at(i).trackId();
82 if (nextTrackId != trackId)
84 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
85 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), i);
86 nextTrackId = trackId;
88 // add parent-child relation
89 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
91 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
94 void EventModel::createRoomGroups()
102 int roomId = mEvents.first().roomId();
104 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), 0);
105 int nextRoomId = roomId;
107 QList<Event>::iterator event = mEvents.begin();
109 while (event != mEvents.end())
111 roomId = event->roomId();
112 if (nextRoomId != roomId)
114 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
115 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), i);
118 mParents[event->id()] = mGroups.count() - 1;
122 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
125 QVariant EventModel::data(const QModelIndex& index, int role) const
127 if (index.isValid() && role == Qt::DisplayRole)
129 if (index.internalId() == 0)
131 return mGroups.at(index.row()).mTitle;
135 return static_cast<Event*>(index.internalPointer())->id();
142 QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
144 // TODO: add checks for out of range rows
146 if (!parent.isValid())
148 return createIndex(row, column, 0);
150 else if (parent.internalId() == 0)
152 const Group& group = mGroups.at(parent.row());
153 Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
154 return createIndex(row, column, reinterpret_cast<void*>(event));
158 return QModelIndex();
162 QModelIndex EventModel::parent(const QModelIndex & index) const
166 if (index.internalId() == 0)
168 return QModelIndex();
171 Event * event = static_cast<Event*>(index.internalPointer());
173 return createIndex(mParents[event->id()], 0, 0);
176 return QModelIndex();
179 int EventModel::columnCount(const QModelIndex & parent) const
185 int EventModel::rowCount (const QModelIndex & parent) const
187 if (!parent.isValid())
189 return mGroups.count();
192 if (parent.internalId() == 0)
194 return mGroups.at(parent.row()).mChildCount;
200 void EventModel::clearModel()
202 for(int i = 0;i < mGroups.count();i++){
203 QModelIndex idx = index(i, 0);
204 Group group = mGroups[i];
205 beginRemoveRows(idx, 0, group.mChildCount - 1);
206 /*bool ok =*/ removeRows(0, group.mChildCount, idx);
208 //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok;
213 void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
216 // check for existence of the conference in the DB
217 if(Conference::getAll().count())
219 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
220 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
225 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
228 // check for existence of the conference in the DB
229 if(Conference::getAll().count())
231 qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
232 mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
237 int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId)
240 // check for existence of the conference in the DB
241 if(Conference::getAll().count())
243 qDebug() << "Loading search result Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
245 mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
247 catch( OrmException &e ){
248 qDebug() << "Event::getSearchResultByDate failed: " << e.text();
251 qDebug() << "Event::getSearchResultByDate failed";
258 return mEvents.count();
261 void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId)
264 if (Conference::getAll().count())
266 qDebug() << "Loading Conference Data (by Track): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
267 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start");
272 void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId)
275 if (Conference::getAll().count())
277 qDebug() << "Loading Conference Data (by Room): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
278 mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
283 void EventModel::loadNowEvents(int aConferenceId)
286 // check for existence of the conference in the DB
287 if(Conference::getAll().count())
289 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] scheduled NOW";
290 mEvents = Event::nowEvents(aConferenceId, "start");
295 void EventModel::loadConflictEvents(int aEventId, int aConferenceId)
298 // check for existence of the conference in the DB
299 if(Conference::getAll().count())
301 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] in conflict with " << aEventId;
302 mEvents = Event::conflictEvents(aEventId, aConferenceId);
307 void EventModel::updateModel(int aEventId)
309 for(int i=0; i<mEvents.count(); i++)
311 if(mEvents[i].id() == aEventId)
312 mEvents[i] = Event::getById(aEventId,Conference::activeConference());
315 // find the ModelIndex for given aEventId
316 for(int i=0; i<mGroups.count(); i++)
318 QModelIndex groupIndex = index(i,0,QModelIndex());
319 for(int j=0; j<mGroups[i].mChildCount; j++)
321 QModelIndex eventIndex = index(j,0,groupIndex);
322 if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
324 emit(dataChanged(groupIndex,groupIndex));
325 emit(dataChanged(eventIndex,eventIndex));