2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2017 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()
51 if (mEvents.empty()) return;
53 const int minTimeSpan = 3600; // one hour // minimum duration of a group in seconds
54 const int minChildCount = 3; // minimum number of events in one group
56 QDateTime groupStartDateTime(mEvents.first().start().date(), QTime(mEvents.first().start().time().hour(), 0), mEvents.first().start().timeSpec());
57 QDateTime groupEndDateTime = groupStartDateTime.addSecs(mEvents.first().duration());
58 mGroups << EventModel::Group("", 0);
59 int timeSpan = minTimeSpan;
61 for (int i = 0; i != mEvents.count(); ++i) {
62 QDateTime eventStartDateTime = mEvents.at(i).start();
63 QDateTime eventEndDateTime = eventStartDateTime.addSecs(mEvents.at(i).duration());
65 if (eventStartDateTime >= groupStartDateTime.addSecs(timeSpan)) {
66 // a new group could be necessary
67 if (mGroups.last().mChildCount < minChildCount) {
68 // too few events in the group => no new group
69 // except a gap in time would occur that is longer than minTimeSpan
70 QDateTime prevEventStartDateTime = mEvents.at(i).start();
71 if (i > 0 && qMax(prevEventStartDateTime.addSecs(mEvents.at(i-1).duration()), groupEndDateTime).secsTo(eventStartDateTime) < minTimeSpan) {
72 timeSpan += minTimeSpan;
74 continue; // repeat with the same event
78 // a new group is necessary
79 mGroups.last().setTitle(mEvents);
80 groupStartDateTime = groupStartDateTime.addSecs(timeSpan);
81 groupEndDateTime = groupStartDateTime.addSecs(mEvents.at(i).duration());
82 mGroups << EventModel::Group("", i);
83 timeSpan = minTimeSpan;
86 // insert event into current group
87 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
88 mGroups.last().mChildCount += 1;
89 groupEndDateTime = qMax(eventEndDateTime, groupEndDateTime);
92 // the last group needs a title as well
93 mGroups.last().setTitle(mEvents);
98 void EventModel::createTrackGroups() {
105 int trackId = mEvents.first().trackId();
107 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), 0);
108 int nextTrackId = trackId;
110 for (int i=0; i<mEvents.count(); i++)
112 trackId = mEvents.at(i).trackId();
113 if (nextTrackId != trackId)
115 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
116 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), i);
117 nextTrackId = trackId;
119 // add parent-child relation
120 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
122 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
125 void EventModel::createRoomGroups()
133 int roomId = mEvents.first().roomId();
135 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), 0);
136 int nextRoomId = roomId;
138 QList<Event>::iterator event = mEvents.begin();
140 while (event != mEvents.end())
142 roomId = event->roomId();
143 if (nextRoomId != roomId)
145 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
146 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), i);
149 mParents[event->id()] = mGroups.count() - 1;
153 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
156 QVariant EventModel::data(const QModelIndex& index, int role) const
158 if (index.isValid() && role == Qt::DisplayRole)
160 if (index.internalId() == 0)
162 return mGroups.at(index.row()).mTitle;
166 return static_cast<Event*>(index.internalPointer())->id();
173 QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
175 // TODO: add checks for out of range rows
177 if (!parent.isValid())
179 return createIndex(row, column);
181 else if (parent.internalId() == 0)
183 const Group& group = mGroups.at(parent.row());
184 Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
185 return createIndex(row, column, reinterpret_cast<void*>(event));
189 return QModelIndex();
193 QModelIndex EventModel::parent(const QModelIndex & index) const
197 if (index.internalId() == 0)
199 return QModelIndex();
202 Event * event = static_cast<Event*>(index.internalPointer());
204 return createIndex(mParents[event->id()], 0);
207 return QModelIndex();
210 int EventModel::columnCount(const QModelIndex & parent) const
216 int EventModel::rowCount (const QModelIndex & parent) const
218 if (!parent.isValid())
220 return mGroups.count();
223 if (parent.internalId() == 0)
225 return mGroups.at(parent.row()).mChildCount;
231 void EventModel::clearModel()
241 void EventModel::loadEvents(const QDate &aDate, int aConferenceId) {
243 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start, duration");
248 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId) {
250 mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
255 int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId) {
258 mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start, duration");
260 catch( OrmException &e ){
261 qDebug() << "Event::getSearchResultByDate failed: " << e.text();
264 qDebug() << "Event::getSearchResultByDate failed";
269 return mEvents.count();
273 void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId) {
275 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start, duration");
280 void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId) {
282 mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
287 void EventModel::loadConflictEvents(int aEventId, int aConferenceId) {
289 mEvents = Event::conflictEvents(aEventId, aConferenceId);
294 void EventModel::updateModel(int aEventId)
296 for(int i=0; i<mEvents.count(); i++)
298 if(mEvents[i].id() == aEventId)
299 mEvents[i] = Event::getById(aEventId,Conference::activeConference());
302 // find the ModelIndex for given aEventId
303 for(int i=0; i<mGroups.count(); i++)
305 QModelIndex groupIndex = index(i,0,QModelIndex());
306 for(int j=0; j<mGroups[i].mChildCount; j++)
308 QModelIndex eventIndex = index(j,0,groupIndex);
309 if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
311 emit(dataChanged(groupIndex,groupIndex));
312 emit(dataChanged(eventIndex,eventIndex));