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()
31 void EventModel::Group::setTitle(const QList<Event>& mEvents) {
32 QTime startTime = mEvents.at(mFirstEventIndex).start().time();
34 for (int i = mFirstEventIndex; i != mFirstEventIndex + mChildCount; ++i) {
35 endTime = qMax(mEvents.at(i).start().time().addSecs(mEvents.at(i).duration()), endTime);
37 mTitle = QString("%1 - %2").arg(startTime.toString("HH:mm")).arg(endTime.toString("HH:mm"));
41 // We want to group the events into "time slots/time groups" that
42 // should start at full hours and have the duration of either
43 // one hour or (if less than 3 events are in one time slot)
44 // multiple of one hour.
45 void EventModel::createTimeGroups()
49 if (mEvents.empty()) return;
51 const int minTimeSpan = 3600; // one hour
52 const int minChildCount = 3; // minimum number of events in one group
54 // Create the first time group. The events have to be sorted by start time at this point!
55 QTime groupStartTime(mEvents.first().start().time().hour(), 0);
56 QTime groupEndTime = groupStartTime.addSecs(mEvents.first().duration());
57 mGroups << EventModel::Group("", 0);
58 int timeSpan = minTimeSpan;
60 for (int i = 0; i != mEvents.count(); ++i) {
61 QTime eventStartTime = mEvents.at(i).start().time();
62 QTime eventEndTime = eventStartTime.addSecs(mEvents.at(i).duration());
64 if (eventStartTime >= groupStartTime.addSecs(timeSpan)) {
65 // a new group could be necessary
66 if (mGroups.last().mChildCount < minChildCount) {
67 // too few events in the group => no new group
68 // except a gap in time would occur that is longer than minTimeSpan
69 if (i > 0 && qMax(mEvents.at(i-1).start().time().addSecs(mEvents.at(i-1).duration()), groupEndTime).secsTo(eventStartTime) < minTimeSpan) {
70 timeSpan += minTimeSpan;
72 continue; // repeat with the same event
76 // a new group is necessary
77 mGroups.last().setTitle(mEvents);
78 groupStartTime = groupStartTime.addSecs(timeSpan);
79 groupEndTime = groupStartTime.addSecs(mEvents.at(i).duration());
80 mGroups << EventModel::Group("", i);
81 timeSpan = minTimeSpan;
84 // insert event into current group
85 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
86 mGroups.last().mChildCount += 1;
87 groupEndTime = qMax(eventEndTime, groupEndTime);
90 // the last group needs a title as well
91 mGroups.last().setTitle(mEvents);
96 void EventModel::createTrackGroups() {
103 int trackId = mEvents.first().trackId();
105 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), 0);
106 int nextTrackId = trackId;
108 for (int i=0; i<mEvents.count(); i++)
110 trackId = mEvents.at(i).trackId();
111 if (nextTrackId != trackId)
113 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
114 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), i);
115 nextTrackId = trackId;
117 // add parent-child relation
118 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
120 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
123 void EventModel::createRoomGroups()
131 int roomId = mEvents.first().roomId();
133 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), 0);
134 int nextRoomId = roomId;
136 QList<Event>::iterator event = mEvents.begin();
138 while (event != mEvents.end())
140 roomId = event->roomId();
141 if (nextRoomId != roomId)
143 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
144 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), i);
147 mParents[event->id()] = mGroups.count() - 1;
151 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
154 QVariant EventModel::data(const QModelIndex& index, int role) const
156 if (index.isValid() && role == Qt::DisplayRole)
158 if (index.internalId() == 0)
160 return mGroups.at(index.row()).mTitle;
164 return static_cast<Event*>(index.internalPointer())->id();
171 QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
173 // TODO: add checks for out of range rows
175 if (!parent.isValid())
177 return createIndex(row, column, 0);
179 else if (parent.internalId() == 0)
181 const Group& group = mGroups.at(parent.row());
182 Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
183 return createIndex(row, column, reinterpret_cast<void*>(event));
187 return QModelIndex();
191 QModelIndex EventModel::parent(const QModelIndex & index) const
195 if (index.internalId() == 0)
197 return QModelIndex();
200 Event * event = static_cast<Event*>(index.internalPointer());
202 return createIndex(mParents[event->id()], 0, 0);
205 return QModelIndex();
208 int EventModel::columnCount(const QModelIndex & parent) const
214 int EventModel::rowCount (const QModelIndex & parent) const
216 if (!parent.isValid())
218 return mGroups.count();
221 if (parent.internalId() == 0)
223 return mGroups.at(parent.row()).mChildCount;
229 void EventModel::clearModel()
238 void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
241 // check for existence of the conference in the DB
242 if(Conference::getAll().count())
244 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
249 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
252 // check for existence of the conference in the DB
253 if(Conference::getAll().count())
255 mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
260 int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId)
263 // check for existence of the conference in the DB
264 if(Conference::getAll().count())
267 mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
269 catch( OrmException &e ){
270 qDebug() << "Event::getSearchResultByDate failed: " << e.text();
273 qDebug() << "Event::getSearchResultByDate failed";
280 return mEvents.count();
283 void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId)
286 if (Conference::getAll().count())
288 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start");
293 void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId)
296 if (Conference::getAll().count())
298 mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
304 void EventModel::loadConflictEvents(int aEventId, int aConferenceId) {
306 // check for existence of the conference in the DB
307 if(Conference::getAll().count())
309 mEvents = Event::conflictEvents(aEventId, aConferenceId);
314 void EventModel::updateModel(int aEventId)
316 for(int i=0; i<mEvents.count(); i++)
318 if(mEvents[i].id() == aEventId)
319 mEvents[i] = Event::getById(aEventId,Conference::activeConference());
322 // find the ModelIndex for given aEventId
323 for(int i=0; i<mGroups.count(); i++)
325 QModelIndex groupIndex = index(i,0,QModelIndex());
326 for(int j=0; j<mGroups[i].mChildCount; j++)
328 QModelIndex eventIndex = index(j,0,groupIndex);
329 if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
331 emit(dataChanged(groupIndex,groupIndex));
332 emit(dataChanged(eventIndex,eventIndex));