1 #include "eventmodel.h"
2 #include <conference.h>
5 const QString EventModel::COMMA_SEPARATOR = ", ";
7 EventModel::EventModel()
12 void EventModel::createTimeGroups()
22 const int timeSpan = 5400;
24 QTime startTime = mEvents.first().start().time();
25 mGroups << EventModel::Group(QString("%1 - %2").arg(startTime.toString("HH:mm"),
26 startTime.addSecs(timeSpan).toString("HH:mm")), 0);
27 QTime nextGroupTime = mEvents.first().start().time().addSecs(timeSpan);
29 for (int i=0; i<mEvents.count(); i++)
31 QTime eventTime = mEvents.at(i).start().time();
33 if (nextGroupTime <= eventTime)
35 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
36 mGroups << EventModel::Group(QString("%1 - %2").arg(nextGroupTime.toString("HH:mm"),
37 nextGroupTime.addSecs(timeSpan).toString("HH:mm")), i);
38 nextGroupTime = nextGroupTime.addSecs(timeSpan);
41 // add parent-child relation
42 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
45 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
48 void EventModel::createActivityGroups() {
55 int activityId = mEvents.first().activityId();
57 mGroups << EventModel::Group(Activity::getActivityName(activityId), 0);
58 int nextActivityId = activityId;
60 for (int i=0; i<mEvents.count(); i++)
62 activityId = mEvents.at(i).activityId();
63 if (nextActivityId != activityId)
65 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
66 mGroups << EventModel::Group(Activity::getActivityName(activityId), i);
67 nextActivityId = activityId;
69 // add parent-child relation
70 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
72 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
75 QVariant EventModel::data(const QModelIndex& index, int role) const
77 if (index.isValid() && role == Qt::DisplayRole)
79 if (index.internalId() == 0)
81 return mGroups.at(index.row()).mTitle;
85 return static_cast<Event*>(index.internalPointer())->id();
92 QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
94 // TODO: add checks for out of range rows
96 if (!parent.isValid())
98 return createIndex(row, column, 0);
100 else if (parent.internalId() == 0)
102 const Group& group = mGroups.at(parent.row());
103 Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
104 return createIndex(row, column, reinterpret_cast<void*>(event));
108 return QModelIndex();
112 QModelIndex EventModel::parent(const QModelIndex & index) const
116 if (index.internalId() == 0)
118 return QModelIndex();
121 Event * event = static_cast<Event*>(index.internalPointer());
123 return createIndex(mParents[event->id()], 0, 0);
126 return QModelIndex();
129 int EventModel::columnCount(const QModelIndex & parent) const
135 int EventModel::rowCount (const QModelIndex & parent) const
137 if (!parent.isValid())
139 return mGroups.count();
142 if (parent.internalId() == 0)
144 return mGroups.at(parent.row()).mChildCount;
150 void EventModel::clearModel()
152 for(int i = 0;i < mGroups.count();i++){
153 QModelIndex idx = index(i, 0);
154 Group group = mGroups[i];
155 beginRemoveRows(idx, 0, group.mChildCount - 1);
156 /*bool ok =*/ removeRows(0, group.mChildCount, idx);
158 //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok;
163 void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
166 // check for existence of the conference in the DB
167 if(Conference::getAll().count())
169 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
170 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, Event::START);
175 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
178 // check for existence of the conference in the DB
179 if(Conference::getAll().count())
181 qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
182 mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
187 void EventModel::loadEventsByActivities(const QDate &aDate, int aConferenceId)
190 if(Conference::getAll().count())
192 qDebug() << "Loading Conference Data (by Activities): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
193 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, Event::XID_ACTIVITY + COMMA_SEPARATOR + Event::START);
195 createActivityGroups();
198 void EventModel::emitDataChangedSignal(const QModelIndex &aTopLeft, const QModelIndex &aBottomRight)
200 emit(dataChanged(aTopLeft,aBottomRight));