source: src/mvc/eventmodel.cpp @ 5b7fa79

Last change on this file since 5b7fa79 was 5b7fa79, checked in by Philipp Spitzer <philipp@…>, 10 months ago

Adjust shown event time when displayTimeShift is set.

  • Property mode set to 100644
File size: 9.5 KB
Line 
1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2017 Philipp Spitzer, gregor herrmann, Stefan Stahl
4 *
5 * This file is part of ConfClerk.
6 *
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)
10 * any later version.
11 *
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
15 * more details.
16 *
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/>.
19 */
20#include "eventmodel.h"
21#include "conference.h"
22#include "track.h"
23#include "room.h"
24
25#include <application.h>
26
27const QString EventModel::COMMA_SEPARATOR = ", ";
28
29EventModel::EventModel()
30{ }
31
32
33void 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);
38    }
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"));
43}
44
45
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.
50void EventModel::createTimeGroups()
51{
52    beginResetModel();
53
54    mGroups.clear();
55    mParents.clear();
56    if (mEvents.empty()) return;
57
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
60
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;
65
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());
69
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;
78                    --i;
79                    continue; // repeat with the same event
80                }
81            }
82
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;
89        }
90
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);
95    }
96
97    // the last group needs a title as well
98    mGroups.last().setTitle(mEvents);
99
100    endResetModel();
101}
102
103void EventModel::createTrackGroups() {
104    mGroups.clear();
105    mParents.clear();
106    if (mEvents.empty())
107    {
108        return;
109    }
110    int trackId = mEvents.first().trackId();
111
112    mGroups << EventModel::Group(Track::retrieveTrackName(trackId), 0);
113    int nextTrackId = trackId;
114
115    for (int i=0; i<mEvents.count(); i++)
116    {
117        trackId = mEvents.at(i).trackId();
118        if (nextTrackId != trackId)
119        {
120            mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
121            mGroups << EventModel::Group(Track::retrieveTrackName(trackId), i);
122            nextTrackId = trackId;
123        }
124        // add parent-child relation
125        mParents[mEvents.at(i).id()] = mGroups.count() - 1;
126    }
127    mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
128}
129
130void EventModel::createRoomGroups()
131{
132    mGroups.clear();
133    mParents.clear();
134    if (mEvents.empty())
135    {
136        return;
137    }
138    int roomId = mEvents.first().roomId();
139
140    mGroups << EventModel::Group(Room::retrieveRoomName(roomId), 0);
141    int nextRoomId = roomId;
142
143    QList<Event>::iterator event = mEvents.begin();
144    int i = 0;
145    while (event != mEvents.end())
146    {
147        roomId = event->roomId();
148        if (nextRoomId != roomId)
149        {
150            mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
151            mGroups << EventModel::Group(Room::retrieveRoomName(roomId), i);
152            nextRoomId = roomId;
153        }
154        mParents[event->id()] = mGroups.count() - 1;
155        event++;
156        i++;
157    }
158    mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
159}
160
161QVariant EventModel::data(const QModelIndex& index, int role) const
162{
163    if (index.isValid() && role == Qt::DisplayRole)
164    {
165        if (index.internalId() == 0)
166        {
167            return mGroups.at(index.row()).mTitle;
168        }
169        else //event data
170        {
171            return static_cast<Event*>(index.internalPointer())->id();
172        }
173    }
174
175    return QVariant();
176}
177
178QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
179{
180    // TODO: add checks for out of range rows
181
182    if (!parent.isValid())
183    {
184        return createIndex(row, column);
185    }
186    else if (parent.internalId() == 0)
187    {
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));
191    }
192    else
193    {
194        return QModelIndex();
195    }
196}
197
198QModelIndex EventModel::parent(const QModelIndex & index) const
199{
200    if (index.isValid())
201    {
202        if (index.internalId() == 0)
203        {
204            return QModelIndex();
205        }
206
207        Event * event = static_cast<Event*>(index.internalPointer());
208
209        return createIndex(mParents[event->id()], 0);
210    }
211
212    return QModelIndex();
213}
214
215int EventModel::columnCount(const QModelIndex & parent) const
216{
217    Q_UNUSED(parent);
218    return 1;
219}
220
221int EventModel::rowCount (const QModelIndex & parent) const
222{
223    if (!parent.isValid())
224    {
225        return mGroups.count();
226    }
227
228    if (parent.internalId() == 0)
229    {
230        return mGroups.at(parent.row()).mChildCount;
231    }
232
233    return 0;
234}
235
236void EventModel::clearModel()
237{
238    beginResetModel();
239    mGroups.clear();
240    mEvents.clear();
241    mParents.clear();
242    endResetModel();
243}
244
245
246void EventModel::loadEvents(const QDate &aDate, int aConferenceId) {
247    clearModel();
248    mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start, duration");
249    createTimeGroups();
250}
251
252
253void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId) {
254    clearModel();
255    mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
256    createTimeGroups();
257}
258
259
260int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId) {
261    clearModel();
262    try {
263        mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start, duration");
264    }
265    catch( OrmException &e  ){
266        qDebug() << "Event::getSearchResultByDate failed: " << e.text();
267    }
268    catch(...){
269        qDebug() << "Event::getSearchResultByDate failed";
270    }
271
272    createTimeGroups();
273
274    return mEvents.count();
275}
276
277
278void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId) {
279    clearModel();
280    mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start, duration");
281    createTrackGroups();
282}
283
284
285void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId) {
286    clearModel();
287    mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
288    createRoomGroups();
289}
290
291
292void EventModel::loadConflictEvents(int aEventId, int aConferenceId) {
293    clearModel();
294    mEvents = Event::conflictEvents(aEventId, aConferenceId);
295    createTimeGroups();
296}
297
298
299void EventModel::updateModel(int aEventId)
300{
301    for(int i=0; i<mEvents.count(); i++)
302    {
303        if(mEvents[i].id() == aEventId)
304            mEvents[i] = Event::getById(aEventId,Conference::activeConference());
305    }
306
307    // find the ModelIndex for given aEventId
308    for(int i=0; i<mGroups.count(); i++)
309    {
310        QModelIndex groupIndex = index(i,0,QModelIndex());
311        for(int j=0; j<mGroups[i].mChildCount; j++)
312        {
313            QModelIndex eventIndex = index(j,0,groupIndex);
314            if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
315            {
316                emit(dataChanged(groupIndex,groupIndex));
317                emit(dataChanged(eventIndex,eventIndex));
318            }
319        }
320    }
321}
322
Note: See TracBrowser for help on using the repository browser.