X-Git-Url: https://git.toastfreeware.priv.at/toast/confclerk.git/blobdiff_plain/4693fa6dfc3d959f0bd7decd5facae07a261784a..12fe870b1285b501da13e5ba626537e3a78a3339:/src/mvc/eventmodel.cpp?ds=sidebyside diff --git a/src/mvc/eventmodel.cpp b/src/mvc/eventmodel.cpp index c877f02..280868f 100644 --- a/src/mvc/eventmodel.cpp +++ b/src/mvc/eventmodel.cpp @@ -1,46 +1,96 @@ +/* + * Copyright (C) 2010 Ixonos Plc. + * Copyright (C) 2011 Philipp Spitzer, gregor herrmann + * + * This file is part of ConfClerk. + * + * ConfClerk is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 2 of the License, or (at your option) + * any later version. + * + * ConfClerk is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * ConfClerk. If not, see . + */ #include "eventmodel.h" #include #include +#include + +const QString EventModel::COMMA_SEPARATOR = ", "; EventModel::EventModel() -{ - mEvents.clear(); +{ } + + +void EventModel::Group::setTitle(const QList& mEvents) { + QTime startTime = mEvents.at(mFirstEventIndex).start().time(); + QTime endTime(0, 0); + for (int i = mFirstEventIndex; i != mFirstEventIndex + mChildCount; ++i) { + endTime = qMax(mEvents.at(i).start().time().addSecs(mEvents.at(i).duration()), endTime); + } + mTitle = QString("%1 - %2").arg(startTime.toString("HH:mm")).arg(endTime.toString("HH:mm")); } + +// We want to group the events into "time slots/time groups" that +// should start at full hours and have the duration of either +// one hour or (if less than 3 events are in one time slot) +// multiple of one hour. void EventModel::createTimeGroups() { mGroups.clear(); mParents.clear(); + if (mEvents.empty()) return; - if (mEvents.empty()) - { - return; - } + const int minTimeSpan = 3600; // one hour + const int minChildCount = 3; // minimum number of events in one group - const int timeSpan = 5400; + // Create the first time group. The events have to be sorted by start time at this point! + QTime groupStartTime(mEvents.first().start().time().hour(), 0); + QTime groupEndTime = groupStartTime.addSecs(mEvents.first().duration()); + mGroups << EventModel::Group("", 0); + int timeSpan = minTimeSpan; - QTime startTime = mEvents.first().start().time(); - mGroups << EventModel::Group(QString("%1 - %2").arg(startTime.toString("HH:mm"), - startTime.addSecs(timeSpan).toString("HH:mm")), 0); - QTime nextGroupTime = mEvents.first().start().time().addSecs(timeSpan); + for (int i = 0; i != mEvents.count(); ++i) { + QTime eventStartTime = mEvents.at(i).start().time(); + QTime eventEndTime = eventStartTime.addSecs(mEvents.at(i).duration()); - for (int i=0; i= groupStartTime.addSecs(timeSpan)) { + // a new group could be necessary + if (mGroups.last().mChildCount < minChildCount) { + // too few events in the group => no new group + // except a gap in time would occur that is longer than minTimeSpan + if (i > 0 && qMax(mEvents.at(i-1).start().time().addSecs(mEvents.at(i-1).duration()), groupEndTime).secsTo(eventStartTime) < minTimeSpan) { + timeSpan += minTimeSpan; + --i; + continue; // repeat with the same event + } + } - if (nextGroupTime <= eventTime) - { - mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex; - mGroups << EventModel::Group(QString("%1 - %2").arg(nextGroupTime.toString("HH:mm"), - nextGroupTime.addSecs(timeSpan).toString("HH:mm")), i); - nextGroupTime = nextGroupTime.addSecs(timeSpan); + // a new group is necessary + mGroups.last().setTitle(mEvents); + groupStartTime = groupStartTime.addSecs(timeSpan); + groupEndTime = groupStartTime.addSecs(mEvents.at(i).duration()); + mGroups << EventModel::Group("", i); + timeSpan = minTimeSpan; } - // add parent-child relation + // insert event into current group mParents[mEvents.at(i).id()] = mGroups.count() - 1; + mGroups.last().mChildCount += 1; + groupEndTime = qMax(eventEndTime, groupEndTime); } - mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex; + // the last group needs a title as well + mGroups.last().setTitle(mEvents); + + reset(); } void EventModel::createTrackGroups() { @@ -52,7 +102,7 @@ void EventModel::createTrackGroups() { } int trackId = mEvents.first().trackId(); - mGroups << EventModel::Group(Track::getTrackName(trackId), 0); + mGroups << EventModel::Group(Track::retrieveTrackName(trackId), 0); int nextTrackId = trackId; for (int i=0; i::iterator event = mEvents.begin(); + int i = 0; + while (event != mEvents.end()) + { + roomId = event->roomId(); + if (nextRoomId != roomId) + { + mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex; + mGroups << EventModel::Group(Room::retrieveRoomName(roomId), i); + nextRoomId = roomId; + } + mParents[event->id()] = mGroups.count() - 1; + event++; + i++; + } + mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex; +} + QVariant EventModel::data(const QModelIndex& index, int role) const { if (index.isValid() && role == Qt::DisplayRole) @@ -147,15 +228,11 @@ int EventModel::rowCount (const QModelIndex & parent) const void EventModel::clearModel() { - for(int i = 0;i < mGroups.count();i++){ - QModelIndex idx = index(i, 0); - Group group = mGroups[i]; - beginRemoveRows(idx, 0, group.mChildCount - 1); - /*bool ok =*/ removeRows(0, group.mChildCount, idx); - endRemoveRows(); - //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok; - } + mGroups.clear(); mEvents.clear(); + mParents.clear(); + + reset(); } void EventModel::loadEvents(const QDate &aDate, int aConferenceId) @@ -164,7 +241,6 @@ void EventModel::loadEvents(const QDate &aDate, int aConferenceId) // check for existence of the conference in the DB if(Conference::getAll().count()) { - qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate; mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start"); } createTimeGroups(); @@ -176,25 +252,97 @@ void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId) // check for existence of the conference in the DB if(Conference::getAll().count()) { - qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate; mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId); } createTimeGroups(); } -void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId) +int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId) { clearModel(); + // check for existence of the conference in the DB if(Conference::getAll().count()) { - qDebug() << "Loading Conference Data (by Track): [" << Conference::getById(aConferenceId).title() << "] " << aDate; + try{ + mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start"); + } + catch( OrmException &e ){ + qDebug() << "Event::getSearchResultByDate failed: " << e.text(); + } + catch(...){ + qDebug() << "Event::getSearchResultByDate failed"; + } + + } + + createTimeGroups(); + + return mEvents.count(); +} + +void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId) +{ + clearModel(); + if (Conference::getAll().count()) + { mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start"); } createTrackGroups(); } -void EventModel::emitDataChangedSignal(const QModelIndex &aTopLeft, const QModelIndex &aBottomRight) +void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId) { - emit(dataChanged(aTopLeft,aBottomRight)); + clearModel(); + if (Conference::getAll().count()) + { + mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId); + } + createRoomGroups(); +} + +void EventModel::loadNowEvents(int aConferenceId) +{ + clearModel(); + // check for existence of the conference in the DB + if(Conference::getAll().count()) + { + mEvents = Event::nowEvents(aConferenceId, "start"); + } + createTimeGroups(); +} + +void EventModel::loadConflictEvents(int aEventId, int aConferenceId) +{ + clearModel(); + // check for existence of the conference in the DB + if(Conference::getAll().count()) + { + mEvents = Event::conflictEvents(aEventId, aConferenceId); + } + createTimeGroups(); +} + +void EventModel::updateModel(int aEventId) +{ + for(int i=0; i(eventIndex.internalPointer())->id() == aEventId) + { + emit(dataChanged(groupIndex,groupIndex)); + emit(dataChanged(eventIndex,eventIndex)); + } + } + } }