2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
5 * This file is part of ConfClerk.
7 * ConfClerk is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 2 of the License, or (at your option)
12 * ConfClerk is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * ConfClerk. If not, see <http://www.gnu.org/licenses/>.
20 #include "eventmodel.h"
21 #include <conference.h>
25 const QString EventModel::COMMA_SEPARATOR = ", ";
27 EventModel::EventModel()
30 void EventModel::createTimeGroups()
40 const int timeSpan = 5400;
42 QTime startTime = mEvents.first().start().time();
43 mGroups << EventModel::Group(QString("%1 - %2").arg(startTime.toString("HH:mm"),
44 startTime.addSecs(timeSpan).toString("HH:mm")), 0);
45 QTime nextGroupTime = mEvents.first().start().time().addSecs(timeSpan);
47 for (int i=0; i<mEvents.count(); i++)
49 QTime eventTime = mEvents.at(i).start().time();
51 if (nextGroupTime <= eventTime)
53 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
54 mGroups << EventModel::Group(QString("%1 - %2").arg(nextGroupTime.toString("HH:mm"),
55 nextGroupTime.addSecs(timeSpan).toString("HH:mm")), i);
56 nextGroupTime = nextGroupTime.addSecs(timeSpan);
59 // add parent-child relation
60 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
63 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
68 void EventModel::createTrackGroups() {
75 int trackId = mEvents.first().trackId();
77 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), 0);
78 int nextTrackId = trackId;
80 for (int i=0; i<mEvents.count(); i++)
82 trackId = mEvents.at(i).trackId();
83 if (nextTrackId != trackId)
85 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
86 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), i);
87 nextTrackId = trackId;
89 // add parent-child relation
90 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
92 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
95 void EventModel::createRoomGroups()
103 int roomId = mEvents.first().roomId();
105 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), 0);
106 int nextRoomId = roomId;
108 QList<Event>::iterator event = mEvents.begin();
110 while (event != mEvents.end())
112 roomId = event->roomId();
113 if (nextRoomId != roomId)
115 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
116 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), i);
119 mParents[event->id()] = mGroups.count() - 1;
123 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
126 QVariant EventModel::data(const QModelIndex& index, int role) const
128 if (index.isValid() && role == Qt::DisplayRole)
130 if (index.internalId() == 0)
132 return mGroups.at(index.row()).mTitle;
136 return static_cast<Event*>(index.internalPointer())->id();
143 QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
145 // TODO: add checks for out of range rows
147 if (!parent.isValid())
149 return createIndex(row, column, 0);
151 else if (parent.internalId() == 0)
153 const Group& group = mGroups.at(parent.row());
154 Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
155 return createIndex(row, column, reinterpret_cast<void*>(event));
159 return QModelIndex();
163 QModelIndex EventModel::parent(const QModelIndex & index) const
167 if (index.internalId() == 0)
169 return QModelIndex();
172 Event * event = static_cast<Event*>(index.internalPointer());
174 return createIndex(mParents[event->id()], 0, 0);
177 return QModelIndex();
180 int EventModel::columnCount(const QModelIndex & parent) const
186 int EventModel::rowCount (const QModelIndex & parent) const
188 if (!parent.isValid())
190 return mGroups.count();
193 if (parent.internalId() == 0)
195 return mGroups.at(parent.row()).mChildCount;
201 void EventModel::clearModel()
203 // qDebug() << __PRETTY_FUNCTION__ << this << mEvents.count();
211 void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
214 // check for existence of the conference in the DB
215 if(Conference::getAll().count())
217 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
218 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
223 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
226 // check for existence of the conference in the DB
227 if(Conference::getAll().count())
229 qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
230 mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
235 int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId)
238 // check for existence of the conference in the DB
239 if(Conference::getAll().count())
241 // qDebug() << "Loading search result Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
243 mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
245 catch( OrmException &e ){
246 qDebug() << "Event::getSearchResultByDate failed: " << e.text();
249 qDebug() << "Event::getSearchResultByDate failed";
256 return mEvents.count();
259 void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId)
262 if (Conference::getAll().count())
264 qDebug() << "Loading Conference Data (by Track): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
265 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start");
270 void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId)
273 if (Conference::getAll().count())
275 qDebug() << "Loading Conference Data (by Room): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
276 mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
281 void EventModel::loadNowEvents(int aConferenceId)
284 // check for existence of the conference in the DB
285 if(Conference::getAll().count())
287 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] scheduled NOW";
288 mEvents = Event::nowEvents(aConferenceId, "start");
293 void EventModel::loadConflictEvents(int aEventId, int aConferenceId)
296 // check for existence of the conference in the DB
297 if(Conference::getAll().count())
299 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] in conflict with " << aEventId;
300 mEvents = Event::conflictEvents(aEventId, aConferenceId);
305 void EventModel::updateModel(int aEventId)
307 for(int i=0; i<mEvents.count(); i++)
309 if(mEvents[i].id() == aEventId)
310 mEvents[i] = Event::getById(aEventId,Conference::activeConference());
313 // find the ModelIndex for given aEventId
314 for(int i=0; i<mGroups.count(); i++)
316 QModelIndex groupIndex = index(i,0,QModelIndex());
317 for(int j=0; j<mGroups[i].mChildCount; j++)
319 QModelIndex eventIndex = index(j,0,groupIndex);
320 if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
322 emit(dataChanged(groupIndex,groupIndex));
323 emit(dataChanged(eventIndex,eventIndex));