From 12fe870b1285b501da13e5ba626537e3a78a3339 Mon Sep 17 00:00:00 2001 From: Philipp Spitzer Date: Tue, 23 Aug 2011 22:07:45 +0000 Subject: [PATCH 1/1] Rewrote code to group events together with gregoa. Closes bug #22. --- src/mvc/eventmodel.cpp | 76 +++++++++++++++++++++++++++++------------- src/mvc/eventmodel.h | 4 ++- 2 files changed, 55 insertions(+), 25 deletions(-) diff --git a/src/mvc/eventmodel.cpp b/src/mvc/eventmodel.cpp index 074c51a..280868f 100644 --- a/src/mvc/eventmodel.cpp +++ b/src/mvc/eventmodel.cpp @@ -27,40 +27,68 @@ const QString EventModel::COMMA_SEPARATOR = ", "; EventModel::EventModel() { } -void EventModel::createTimeGroups() -{ - mGroups.clear(); - mParents.clear(); - if (mEvents.empty()) - { - return; +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")); +} - const int timeSpan = 5400; - - 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= 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(); } diff --git a/src/mvc/eventmodel.h b/src/mvc/eventmodel.h index adf633c..49d5dc7 100644 --- a/src/mvc/eventmodel.h +++ b/src/mvc/eventmodel.h @@ -58,6 +58,8 @@ private: QString mTitle; int mFirstEventIndex; int mChildCount; + + void setTitle(const QList& mEvents); }; private: @@ -72,7 +74,7 @@ public slots: private: QList mEvents; QList mGroups; - QHash mParents; + QHash mParents; ///< eventId, groupId }; #endif // EVENTMODEL_H -- 2.39.5