source: src/mvc/eventmodel.cpp @ de5d0f1

qt5
Last change on this file since de5d0f1 was de5d0f1, checked in by gregor herrmann <gregoa@…>, 5 years ago

Bump copyright years.

  • Property mode set to 100644
File size: 9.3 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
25const QString EventModel::COMMA_SEPARATOR = ", ";
26
27EventModel::EventModel()
28{ }
29
30
31void 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);
36    }
37    mTitle = QString("%1 - %2").arg(startTime.toString("HH:mm")).arg(endTime.toString("HH:mm"));
38}
39
40
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.
45void EventModel::createTimeGroups()
46{
47    mGroups.clear();
48    mParents.clear();
49    if (mEvents.empty()) return;
50
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
53
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;
58
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());
62
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;
71                    --i;
72                    continue; // repeat with the same event
73                }
74            }
75
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;
82        }
83
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);
88    }
89
90    // the last group needs a title as well
91    mGroups.last().setTitle(mEvents);
92
93    reset();
94}
95
96void EventModel::createTrackGroups() {
97    mGroups.clear();
98    mParents.clear();
99    if (mEvents.empty())
100    {
101        return;
102    }
103    int trackId = mEvents.first().trackId();
104
105    mGroups << EventModel::Group(Track::retrieveTrackName(trackId), 0);
106    int nextTrackId = trackId;
107
108    for (int i=0; i<mEvents.count(); i++)
109    {
110        trackId = mEvents.at(i).trackId();
111        if (nextTrackId != trackId)
112        {
113            mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
114            mGroups << EventModel::Group(Track::retrieveTrackName(trackId), i);
115            nextTrackId = trackId;
116        }
117        // add parent-child relation
118        mParents[mEvents.at(i).id()] = mGroups.count() - 1;
119    }
120    mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
121}
122
123void EventModel::createRoomGroups()
124{
125    mGroups.clear();
126    mParents.clear();
127    if (mEvents.empty())
128    {
129        return;
130    }
131    int roomId = mEvents.first().roomId();
132
133    mGroups << EventModel::Group(Room::retrieveRoomName(roomId), 0);
134    int nextRoomId = roomId;
135
136    QList<Event>::iterator event = mEvents.begin();
137    int i = 0;
138    while (event != mEvents.end())
139    {
140        roomId = event->roomId();
141        if (nextRoomId != roomId)
142        {
143            mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
144            mGroups << EventModel::Group(Room::retrieveRoomName(roomId), i);
145            nextRoomId = roomId;
146        }
147        mParents[event->id()] = mGroups.count() - 1;
148        event++;
149        i++;
150    }
151    mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
152}
153
154QVariant EventModel::data(const QModelIndex& index, int role) const
155{
156    if (index.isValid() && role == Qt::DisplayRole)
157    {
158        if (index.internalId() == 0)
159        {
160            return mGroups.at(index.row()).mTitle;
161        }
162        else //event data
163        {
164            return static_cast<Event*>(index.internalPointer())->id();
165        }
166    }
167
168    return QVariant();
169}
170
171QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
172{
173    // TODO: add checks for out of range rows
174
175    if (!parent.isValid())
176    {
177        return createIndex(row, column, 0);
178    }
179    else if (parent.internalId() == 0)
180    {
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));
184    }
185    else
186    {
187        return QModelIndex();
188    }
189}
190
191QModelIndex EventModel::parent(const QModelIndex & index) const
192{
193    if (index.isValid())
194    {
195        if (index.internalId() == 0)
196        {
197            return QModelIndex();
198        }
199
200        Event * event = static_cast<Event*>(index.internalPointer());
201
202        return createIndex(mParents[event->id()], 0, 0);
203    }
204
205    return QModelIndex();
206}
207
208int EventModel::columnCount(const QModelIndex & parent) const
209{
210    Q_UNUSED(parent);
211    return 1;
212}
213
214int EventModel::rowCount (const QModelIndex & parent) const
215{
216    if (!parent.isValid())
217    {
218        return mGroups.count();
219    }
220
221    if (parent.internalId() == 0)
222    {
223        return mGroups.at(parent.row()).mChildCount;
224    }
225
226    return 0;
227}
228
229void EventModel::clearModel()
230{
231    mGroups.clear();
232    mEvents.clear();
233    mParents.clear();
234
235    reset();
236}
237
238
239void EventModel::loadEvents(const QDate &aDate, int aConferenceId) {
240    clearModel();
241    mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start, duration");
242    createTimeGroups();
243}
244
245
246void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId) {
247    clearModel();
248    mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
249    createTimeGroups();
250}
251
252
253int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId) {
254    clearModel();
255    try {
256        mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start, duration");
257    }
258    catch( OrmException &e  ){
259        qDebug() << "Event::getSearchResultByDate failed: " << e.text();
260    }
261    catch(...){
262        qDebug() << "Event::getSearchResultByDate failed";
263    }
264
265    createTimeGroups();
266
267    return mEvents.count();
268}
269
270
271void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId) {
272    clearModel();
273    mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start, duration");
274    createTrackGroups();
275}
276
277
278void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId) {
279    clearModel();
280    mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
281    createRoomGroups();
282}
283
284
285void EventModel::loadConflictEvents(int aEventId, int aConferenceId) {
286    clearModel();
287    mEvents = Event::conflictEvents(aEventId, aConferenceId);
288    createTimeGroups();
289}
290
291
292void EventModel::updateModel(int aEventId)
293{
294    for(int i=0; i<mEvents.count(); i++)
295    {
296        if(mEvents[i].id() == aEventId)
297            mEvents[i] = Event::getById(aEventId,Conference::activeConference());
298    }
299
300    // find the ModelIndex for given aEventId
301    for(int i=0; i<mGroups.count(); i++)
302    {
303        QModelIndex groupIndex = index(i,0,QModelIndex());
304        for(int j=0; j<mGroups[i].mChildCount; j++)
305        {
306            QModelIndex eventIndex = index(j,0,groupIndex);
307            if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
308            {
309                emit(dataChanged(groupIndex,groupIndex));
310                emit(dataChanged(eventIndex,eventIndex));
311            }
312        }
313    }
314}
315
Note: See TracBrowser for help on using the repository browser.