source: src/mvc/eventmodel.cpp @ e27a3f5

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

Bump copyright years.

  • Property mode set to 100644
File size: 9.3 KB
RevLine 
[ca90cb1]1/*
2 * Copyright (C) 2010 Ixonos Plc.
[de5d0f1]3 * Copyright (C) 2011-2017 Philipp Spitzer, gregor herrmann, Stefan Stahl
[ca90cb1]4 *
[6df32f2]5 * This file is part of ConfClerk.
[ca90cb1]6 *
[6df32f2]7 * ConfClerk is free software: you can redistribute it and/or modify it
[ca90cb1]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 *
[6df32f2]12 * ConfClerk is distributed in the hope that it will be useful, but
[ca90cb1]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
[6df32f2]18 * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
[ca90cb1]19 */
[d0d0a66]20#include "eventmodel.h"
[7680536]21#include "conference.h"
22#include "track.h"
23#include "room.h"
[d1fb9ee]24
[72cd3af]25const QString EventModel::COMMA_SEPARATOR = ", ";
26
[69393c0]27EventModel::EventModel()
[04acaf9]28{ }
[d0d0a66]29
[12fe870]30
31void EventModel::Group::setTitle(const QList<Event>& mEvents) {
[41c4ceb]32    QDateTime startTime = mEvents.at(mFirstEventIndex).start();
33    QDateTime endTime(startTime);
[12fe870]34    for (int i = mFirstEventIndex; i != mFirstEventIndex + mChildCount; ++i) {
[41c4ceb]35        endTime = qMax(mEvents.at(i).start().addSecs(mEvents.at(i).duration()), endTime);
[12fe870]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.
[d0d0a66]45void EventModel::createTimeGroups()
46{
47    mGroups.clear();
48    mParents.clear();
[12fe870]49    if (mEvents.empty()) return;
[d0d0a66]50
[8f15208]51    const int minTimeSpan = 3600; // one hour // minimum duration of a group in seconds
[6bf226b]52    const int minChildCount = 3;  // minimum number of events in one group
[d0d0a66]53
[8f15208]54    QDateTime groupStartDateTime(mEvents.first().start().date(), QTime(mEvents.first().start().time().hour(), 0), mEvents.first().start().timeSpec());
[0b595d2]55    QDateTime groupEndDateTime = groupStartDateTime.addSecs(mEvents.first().duration());
[12fe870]56    mGroups << EventModel::Group("", 0);
57    int timeSpan = minTimeSpan;
[d0d0a66]58
[12fe870]59    for (int i = 0; i != mEvents.count(); ++i) {
[6bf226b]60        QDateTime eventStartDateTime = mEvents.at(i).start();
[0b595d2]61        QDateTime eventEndDateTime = eventStartDateTime.addSecs(mEvents.at(i).duration());
[d0d0a66]62
[0b595d2]63        if (eventStartDateTime >= groupStartDateTime.addSecs(timeSpan)) {
[12fe870]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
[6bf226b]68                QDateTime prevEventStartDateTime = mEvents.at(i).start();
[0b595d2]69                if (i > 0 && qMax(prevEventStartDateTime.addSecs(mEvents.at(i-1).duration()), groupEndDateTime).secsTo(eventStartDateTime) < minTimeSpan) {
[12fe870]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);
[0b595d2]78            groupStartDateTime = groupStartDateTime.addSecs(timeSpan);
79            groupEndDateTime = groupStartDateTime.addSecs(mEvents.at(i).duration());
[12fe870]80            mGroups << EventModel::Group("", i);
81            timeSpan = minTimeSpan;
[d0d0a66]82        }
83
[12fe870]84        // insert event into current group
[d0d0a66]85        mParents[mEvents.at(i).id()] = mGroups.count() - 1;
[12fe870]86        mGroups.last().mChildCount += 1;
[0b595d2]87        groupEndDateTime = qMax(eventEndDateTime, groupEndDateTime);
[d0d0a66]88    }
89
[12fe870]90    // the last group needs a title as well
91    mGroups.last().setTitle(mEvents);
[04acaf9]92
93    reset();
[d0d0a66]94}
95
[4693fa6]96void EventModel::createTrackGroups() {
[f6300c7]97    mGroups.clear();
98    mParents.clear();
99    if (mEvents.empty())
100    {
101        return;
102    }
[4693fa6]103    int trackId = mEvents.first().trackId();
[9208bdb]104
[005e2b7]105    mGroups << EventModel::Group(Track::retrieveTrackName(trackId), 0);
[4693fa6]106    int nextTrackId = trackId;
[f6300c7]107
108    for (int i=0; i<mEvents.count(); i++)
109    {
[4693fa6]110        trackId = mEvents.at(i).trackId();
111        if (nextTrackId != trackId)
[f6300c7]112        {
113            mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
[005e2b7]114            mGroups << EventModel::Group(Track::retrieveTrackName(trackId), i);
[4693fa6]115            nextTrackId = trackId;
[f6300c7]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
[7620de0]123void EventModel::createRoomGroups()
124{
[005e2b7]125    mGroups.clear();
126    mParents.clear();
127    if (mEvents.empty())
128    {
129        return;
130    }
[7620de0]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())
[005e2b7]139    {
[7620de0]140        roomId = event->roomId();
141        if (nextRoomId != roomId)
[005e2b7]142        {
[7620de0]143            mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
144            mGroups << EventModel::Group(Room::retrieveRoomName(roomId), i);
145            nextRoomId = roomId;
[005e2b7]146        }
[7620de0]147        mParents[event->id()] = mGroups.count() - 1;
148        event++;
149        i++;
[005e2b7]150    }
[7620de0]151    mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
[005e2b7]152}
153
[d0d0a66]154QVariant EventModel::data(const QModelIndex& index, int role) const
155{
156    if (index.isValid() && role == Qt::DisplayRole)
157    {
158        if (index.internalId() == 0)
[5842349]159        {
[d0d0a66]160            return mGroups.at(index.row()).mTitle;
161        }
[27102d5]162        else //event data
[d0d0a66]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}
[72f6fe4]228
[f6300c7]229void EventModel::clearModel()
[72f6fe4]230{
[04acaf9]231    mGroups.clear();
[72f6fe4]232    mEvents.clear();
[04acaf9]233    mParents.clear();
234
235    reset();
[f6300c7]236}
[69393c0]237
[41c4ceb]238
239void EventModel::loadEvents(const QDate &aDate, int aConferenceId) {
[f6300c7]240    clearModel();
[41c4ceb]241    mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start, duration");
[72f6fe4]242    createTimeGroups();
243}
244
[41c4ceb]245
246void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId) {
[f6300c7]247    clearModel();
[41c4ceb]248    mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
[6f39595]249    createTimeGroups();
250}
251
[9d8946b]252
[41c4ceb]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";
[e662750]263    }
[9d8946b]264
[e662750]265    createTimeGroups();
[9d8946b]266
267    return mEvents.count();
[e662750]268}
269
[41c4ceb]270
271void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId) {
[f6300c7]272    clearModel();
[41c4ceb]273    mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start, duration");
[4693fa6]274    createTrackGroups();
[f6300c7]275}
276
[41c4ceb]277
278void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId) {
[7620de0]279    clearModel();
[41c4ceb]280    mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
[7620de0]281    createRoomGroups();
282}
283
[b8a3ad1]284
[066b41f]285void EventModel::loadConflictEvents(int aEventId, int aConferenceId) {
[d49254d]286    clearModel();
[41c4ceb]287    mEvents = Event::conflictEvents(aEventId, aConferenceId);
[d49254d]288    createTimeGroups();
289}
290
[41c4ceb]291
[c718a77]292void EventModel::updateModel(int aEventId)
[67c59a7]293{
[c718a77]294    for(int i=0; i<mEvents.count(); i++)
295    {
296        if(mEvents[i].id() == aEventId)
[0bb39f5]297            mEvents[i] = Event::getById(aEventId,Conference::activeConference());
[c718a77]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            {
[7b70507]309                emit(dataChanged(groupIndex,groupIndex));
[c718a77]310                emit(dataChanged(eventIndex,eventIndex));
311            }
312        }
313    }
[67c59a7]314}
315
Note: See TracBrowser for help on using the repository browser.