2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2013 Philipp Spitzer, gregor herrmann, Stefan Stahl
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 QDateTime startTime = mEvents.at(mFirstEventIndex).start();
33 QDateTime endTime(startTime);
34 for (int i = mFirstEventIndex; i != mFirstEventIndex + mChildCount; ++i) {
35 endTime = qMax(mEvents.at(i).start().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 // minimum duration of a group in seconds
52 const int minChildCount = 3; // minimum number of events in one group
54 QDateTime groupStartDateTime(mEvents.first().start().date(), QTime(mEvents.first().start().time().hour(), 0), mEvents.first().start().timeSpec());
55 QDateTime groupEndDateTime = groupStartDateTime.addSecs(mEvents.first().duration());
56 mGroups << EventModel::Group("", 0);
57 int timeSpan = minTimeSpan;
59 for (int i = 0; i != mEvents.count(); ++i) {
60 QDateTime eventStartDateTime = mEvents.at(i).start();
61 QDateTime eventEndDateTime = eventStartDateTime.addSecs(mEvents.at(i).duration());
63 if (eventStartDateTime >= groupStartDateTime.addSecs(timeSpan)) {
64 // a new group could be necessary
65 if (mGroups.last().mChildCount < minChildCount) {
66 // too few events in the group => no new group
67 // except a gap in time would occur that is longer than minTimeSpan
68 QDateTime prevEventStartDateTime = mEvents.at(i).start();
69 if (i > 0 && qMax(prevEventStartDateTime.addSecs(mEvents.at(i-1).duration()), groupEndDateTime).secsTo(eventStartDateTime) < minTimeSpan) {
70 timeSpan += minTimeSpan;
72 continue; // repeat with the same event
76 // a new group is necessary
77 mGroups.last().setTitle(mEvents);
78 groupStartDateTime = groupStartDateTime.addSecs(timeSpan);
79 groupEndDateTime = groupStartDateTime.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 groupEndDateTime = qMax(eventEndDateTime, groupEndDateTime);
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()
239 void EventModel::loadEvents(const QDate &aDate, int aConferenceId) {
241 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start, duration");
246 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId) {
248 mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
253 int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId) {
256 mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start, duration");
258 catch( OrmException &e ){
259 qDebug() << "Event::getSearchResultByDate failed: " << e.text();
262 qDebug() << "Event::getSearchResultByDate failed";
267 return mEvents.count();
271 void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId) {
273 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start, duration");
278 void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId) {
280 mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
285 void EventModel::loadConflictEvents(int aEventId, int aConferenceId) {
287 mEvents = Event::conflictEvents(aEventId, aConferenceId);
292 void EventModel::updateModel(int aEventId)
294 for(int i=0; i<mEvents.count(); i++)
296 if(mEvents[i].id() == aEventId)
297 mEvents[i] = Event::getById(aEventId,Conference::activeConference());
300 // find the ModelIndex for given aEventId
301 for(int i=0; i<mGroups.count(); i++)
303 QModelIndex groupIndex = index(i,0,QModelIndex());
304 for(int j=0; j<mGroups[i].mChildCount; j++)
306 QModelIndex eventIndex = index(j,0,groupIndex);
307 if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
309 emit(dataChanged(groupIndex,groupIndex));
310 emit(dataChanged(eventIndex,eventIndex));