2 * Copyright (C) 2010 Ixonos Plc.
4 * This file is part of ConfClerk.
6 * ConfClerk 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 * ConfClerk 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 * ConfClerk. 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()
29 void EventModel::createTimeGroups()
39 const int timeSpan = 5400;
41 QTime startTime = mEvents.first().start().time();
42 mGroups << EventModel::Group(QString("%1 - %2").arg(startTime.toString("HH:mm"),
43 startTime.addSecs(timeSpan).toString("HH:mm")), 0);
44 QTime nextGroupTime = mEvents.first().start().time().addSecs(timeSpan);
46 for (int i=0; i<mEvents.count(); i++)
48 QTime eventTime = mEvents.at(i).start().time();
50 if (nextGroupTime <= eventTime)
52 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
53 mGroups << EventModel::Group(QString("%1 - %2").arg(nextGroupTime.toString("HH:mm"),
54 nextGroupTime.addSecs(timeSpan).toString("HH:mm")), i);
55 nextGroupTime = nextGroupTime.addSecs(timeSpan);
58 // add parent-child relation
59 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
62 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 // qDebug() << __PRETTY_FUNCTION__ << this << mEvents.count();
210 void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
213 // check for existence of the conference in the DB
214 if(Conference::getAll().count())
216 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
217 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
222 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
225 // check for existence of the conference in the DB
226 if(Conference::getAll().count())
228 qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
229 mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
234 int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId)
237 // check for existence of the conference in the DB
238 if(Conference::getAll().count())
240 qDebug() << "Loading search result Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
242 mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
244 catch( OrmException &e ){
245 qDebug() << "Event::getSearchResultByDate failed: " << e.text();
248 qDebug() << "Event::getSearchResultByDate failed";
255 return mEvents.count();
258 void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId)
261 if (Conference::getAll().count())
263 qDebug() << "Loading Conference Data (by Track): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
264 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start");
269 void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId)
272 if (Conference::getAll().count())
274 qDebug() << "Loading Conference Data (by Room): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
275 mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
280 void EventModel::loadNowEvents(int aConferenceId)
283 // check for existence of the conference in the DB
284 if(Conference::getAll().count())
286 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] scheduled NOW";
287 mEvents = Event::nowEvents(aConferenceId, "start");
292 void EventModel::loadConflictEvents(int aEventId, int aConferenceId)
295 // check for existence of the conference in the DB
296 if(Conference::getAll().count())
298 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] in conflict with " << aEventId;
299 mEvents = Event::conflictEvents(aEventId, aConferenceId);
304 void EventModel::updateModel(int aEventId)
306 for(int i=0; i<mEvents.count(); i++)
308 if(mEvents[i].id() == aEventId)
309 mEvents[i] = Event::getById(aEventId,Conference::activeConference());
312 // find the ModelIndex for given aEventId
313 for(int i=0; i<mGroups.count(); i++)
315 QModelIndex groupIndex = index(i,0,QModelIndex());
316 for(int j=0; j<mGroups[i].mChildCount; j++)
318 QModelIndex eventIndex = index(j,0,groupIndex);
319 if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
321 emit(dataChanged(groupIndex,groupIndex));
322 emit(dataChanged(eventIndex,eventIndex));