2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2021 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 #include <application.h>
27 const QString EventModel::COMMA_SEPARATOR = ", ";
29 EventModel::EventModel()
33 void EventModel::Group::setTitle(const QList<Event>& mEvents) {
34 QDateTime startTime = mEvents.at(mFirstEventIndex).start();
35 QDateTime endTime(startTime);
36 for (int i = mFirstEventIndex; i != mFirstEventIndex + mChildCount; ++i) {
37 endTime = qMax(mEvents.at(i).start().addSecs(mEvents.at(i).duration()), endTime);
39 Conference& conference = ((Application*) qApp)->activeConference();
40 QTime s = conference.shiftTime(startTime.time());
41 QTime e = conference.shiftTime(endTime.time());
42 mTitle = QString("%1 - %2").arg(s.toString("HH:mm")).arg(e.toString("HH:mm"));
46 // We want to group the events into "time slots/time groups" that
47 // should start at full hours and have the duration of either
48 // one hour or (if less than 3 events are in one time slot)
49 // multiple of one hour.
50 void EventModel::createTimeGroups()
56 if (mEvents.empty()) return;
58 const int minTimeSpan = 3600; // one hour // minimum duration of a group in seconds
59 const int minChildCount = 3; // minimum number of events in one group
61 QDateTime groupStartDateTime(mEvents.first().start().date(), QTime(mEvents.first().start().time().hour(), 0), mEvents.first().start().timeSpec());
62 QDateTime groupEndDateTime = groupStartDateTime.addSecs(mEvents.first().duration());
63 mGroups << EventModel::Group("", 0);
64 int timeSpan = minTimeSpan;
66 for (int i = 0; i != mEvents.count(); ++i) {
67 QDateTime eventStartDateTime = mEvents.at(i).start();
68 QDateTime eventEndDateTime = eventStartDateTime.addSecs(mEvents.at(i).duration());
70 if (eventStartDateTime >= groupStartDateTime.addSecs(timeSpan)) {
71 // a new group could be necessary
72 if (mGroups.last().mChildCount < minChildCount) {
73 // too few events in the group => no new group
74 // except a gap in time would occur that is longer than minTimeSpan
75 QDateTime prevEventStartDateTime = mEvents.at(i).start();
76 if (i > 0 && qMax(prevEventStartDateTime.addSecs(mEvents.at(i-1).duration()), groupEndDateTime).secsTo(eventStartDateTime) < minTimeSpan) {
77 timeSpan += minTimeSpan;
79 continue; // repeat with the same event
83 // a new group is necessary
84 mGroups.last().setTitle(mEvents);
85 groupStartDateTime = groupStartDateTime.addSecs(timeSpan);
86 groupEndDateTime = groupStartDateTime.addSecs(mEvents.at(i).duration());
87 mGroups << EventModel::Group("", i);
88 timeSpan = minTimeSpan;
91 // insert event into current group
92 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
93 mGroups.last().mChildCount += 1;
94 groupEndDateTime = qMax(eventEndDateTime, groupEndDateTime);
97 // the last group needs a title as well
98 mGroups.last().setTitle(mEvents);
103 void EventModel::createTrackGroups() {
110 int trackId = mEvents.first().trackId();
112 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), 0);
113 int nextTrackId = trackId;
115 for (int i=0; i<mEvents.count(); i++)
117 trackId = mEvents.at(i).trackId();
118 if (nextTrackId != trackId)
120 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
121 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), i);
122 nextTrackId = trackId;
124 // add parent-child relation
125 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
127 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
130 void EventModel::createRoomGroups()
138 int roomId = mEvents.first().roomId();
140 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), 0);
141 int nextRoomId = roomId;
143 QList<Event>::iterator event = mEvents.begin();
145 while (event != mEvents.end())
147 roomId = event->roomId();
148 if (nextRoomId != roomId)
150 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
151 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), i);
154 mParents[event->id()] = mGroups.count() - 1;
158 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
161 QVariant EventModel::data(const QModelIndex& index, int role) const
163 if (index.isValid() && role == Qt::DisplayRole)
165 if (index.internalId() == 0)
167 return mGroups.at(index.row()).mTitle;
171 return static_cast<Event*>(index.internalPointer())->id();
178 QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
180 // TODO: add checks for out of range rows
182 if (!parent.isValid())
184 return createIndex(row, column);
186 else if (parent.internalId() == 0)
188 const Group& group = mGroups.at(parent.row());
189 Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
190 return createIndex(row, column, reinterpret_cast<void*>(event));
194 return QModelIndex();
198 QModelIndex EventModel::parent(const QModelIndex & index) const
202 if (index.internalId() == 0)
204 return QModelIndex();
207 Event * event = static_cast<Event*>(index.internalPointer());
209 return createIndex(mParents[event->id()], 0);
212 return QModelIndex();
215 int EventModel::columnCount(const QModelIndex & parent) const
221 int EventModel::rowCount (const QModelIndex & parent) const
223 if (!parent.isValid())
225 return mGroups.count();
228 if (parent.internalId() == 0)
230 return mGroups.at(parent.row()).mChildCount;
236 void EventModel::clearModel()
246 void EventModel::loadEvents(const QDate &aDate, int aConferenceId) {
248 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start, duration");
253 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId) {
255 mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
260 int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId) {
263 mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start, duration");
265 catch( OrmException &e ){
266 qDebug() << "Event::getSearchResultByDate failed: " << e.text();
269 qDebug() << "Event::getSearchResultByDate failed";
274 return mEvents.count();
278 void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId) {
280 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start, duration");
285 void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId) {
287 mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
292 void EventModel::loadConflictEvents(int aEventId, int aConferenceId) {
294 mEvents = Event::conflictEvents(aEventId, aConferenceId);
299 void EventModel::updateModel(int aEventId)
301 for(int i=0; i<mEvents.count(); i++)
303 if(mEvents[i].id() == aEventId)
304 mEvents[i] = Event::getById(aEventId,Conference::activeConference());
307 // find the ModelIndex for given aEventId
308 for(int i=0; i<mGroups.count(); i++)
310 QModelIndex groupIndex = index(i,0,QModelIndex());
311 for(int j=0; j<mGroups[i].mChildCount; j++)
313 QModelIndex eventIndex = index(j,0,groupIndex);
314 if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
316 emit(dataChanged(groupIndex,groupIndex));
317 emit(dataChanged(eventIndex,eventIndex));