1 #include "eventmodel.h"
2 #include <conference.h>
5 EventModel::EventModel()
10 void EventModel::createTimeGroups()
20 const int timeSpan = 5400;
22 QTime startTime = mEvents.first().start().time();
23 mGroups << EventModel::Group(QString("%1 - %2").arg(startTime.toString("HH:mm"),
24 startTime.addSecs(timeSpan).toString("HH:mm")), 0);
25 QTime nextGroupTime = mEvents.first().start().time().addSecs(timeSpan);
27 for (int i=0; i<mEvents.count(); i++)
29 QTime eventTime = mEvents.at(i).start().time();
31 if (nextGroupTime <= eventTime)
33 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
34 mGroups << EventModel::Group(QString("%1 - %2").arg(nextGroupTime.toString("HH:mm"),
35 nextGroupTime.addSecs(timeSpan).toString("HH:mm")), i);
36 nextGroupTime = nextGroupTime.addSecs(timeSpan);
39 // add parent-child relation
40 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
43 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
46 void EventModel::createActivityGroups() {
53 int activityId = mEvents.first().activityId();
55 mGroups << EventModel::Group(Activity::getActivityName(activityId), 0);
56 int nextActivityId = activityId;
58 for (int i=0; i<mEvents.count(); i++)
60 activityId = mEvents.at(i).activityId();
61 if (nextActivityId != activityId)
63 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
64 mGroups << EventModel::Group(Activity::getActivityName(activityId), i);
65 nextActivityId = activityId;
67 // add parent-child relation
68 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
70 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
73 QVariant EventModel::data(const QModelIndex& index, int role) const
75 if (index.isValid() && role == Qt::DisplayRole)
77 if (index.internalId() == 0)
79 return mGroups.at(index.row()).mTitle;
83 return static_cast<Event*>(index.internalPointer())->id();
90 QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
92 // TODO: add checks for out of range rows
94 if (!parent.isValid())
96 return createIndex(row, column, 0);
98 else if (parent.internalId() == 0)
100 const Group& group = mGroups.at(parent.row());
101 Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
102 return createIndex(row, column, reinterpret_cast<void*>(event));
106 return QModelIndex();
110 QModelIndex EventModel::parent(const QModelIndex & index) const
114 if (index.internalId() == 0)
116 return QModelIndex();
119 Event * event = static_cast<Event*>(index.internalPointer());
121 return createIndex(mParents[event->id()], 0, 0);
124 return QModelIndex();
127 int EventModel::columnCount(const QModelIndex & parent) const
133 int EventModel::rowCount (const QModelIndex & parent) const
135 if (!parent.isValid())
137 return mGroups.count();
140 if (parent.internalId() == 0)
142 return mGroups.at(parent.row()).mChildCount;
148 void EventModel::clearModel()
150 for(int i = 0;i < mGroups.count();i++){
151 QModelIndex idx = index(i, 0);
152 Group group = mGroups[i];
153 beginRemoveRows(idx, 0, group.mChildCount - 1);
154 /*bool ok =*/ removeRows(0, group.mChildCount, idx);
156 //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok;
161 void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
164 // check for existence of the conference in the DB
165 if(Conference::getAll().count())
167 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
168 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, Event::START);
173 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
176 // check for existence of the conference in the DB
177 if(Conference::getAll().count())
179 qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
180 mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
185 void EventModel::loadEventsByActivities(const QDate &aDate, int aConferenceId)
188 if(Conference::getAll().count())
190 qDebug() << "Loading Conference Data (by Activities): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
191 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, Event::XID_ACTIVITY);
193 createActivityGroups();
196 void EventModel::emitDataChangedSignal(const QModelIndex &aTopLeft, const QModelIndex &aBottomRight)
198 emit(dataChanged(aTopLeft,aBottomRight));