1 #include "eventmodel.h"
2 #include <conference.h>
4 EventModel::EventModel()
9 void EventModel::createTimeGroups()
19 const int timeSpan = 5400;
21 QTime startTime = mEvents.first().start().time();
22 mGroups << EventModel::Group(QString("%1 - %2").arg(startTime.toString("HH:mm"),
23 startTime.addSecs(timeSpan).toString("HH:mm")), 0);
24 QTime nextGroupTime = mEvents.first().start().time().addSecs(timeSpan);
26 for (int i=0; i<mEvents.count(); i++)
28 QTime eventTime = mEvents.at(i).start().time();
30 if (nextGroupTime <= eventTime)
32 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
33 mGroups << EventModel::Group(QString("%1 - %2").arg(nextGroupTime.toString("HH:mm"),
34 nextGroupTime.addSecs(timeSpan).toString("HH:mm")), i);
35 nextGroupTime = nextGroupTime.addSecs(timeSpan);
38 // add parent-child relation
39 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
42 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
45 void EventModel::createActivityGroups() {
52 int activityId = mEvents.first().activityId();
53 //TODO korinpa: get activity name
54 mGroups << EventModel::Group(QString("activity %1").arg(activityId), 0);
55 int nextActivityId = activityId;
57 for (int i=0; i<mEvents.count(); i++)
59 activityId = mEvents.at(i).activityId();
60 if (nextActivityId != activityId)
62 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
63 mGroups << EventModel::Group(QString("activity %1").arg(activityId), 0);
64 int nextActivityId = activityId;
66 // add parent-child relation
67 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
69 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
72 QVariant EventModel::data(const QModelIndex& index, int role) const
74 if (index.isValid() && role == Qt::DisplayRole)
76 if (index.internalId() == 0)
78 return mGroups.at(index.row()).mTitle;
82 return static_cast<Event*>(index.internalPointer())->id();
89 QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
91 // TODO: add checks for out of range rows
93 if (!parent.isValid())
95 return createIndex(row, column, 0);
97 else if (parent.internalId() == 0)
99 const Group& group = mGroups.at(parent.row());
100 Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
101 return createIndex(row, column, reinterpret_cast<void*>(event));
105 return QModelIndex();
109 QModelIndex EventModel::parent(const QModelIndex & index) const
113 if (index.internalId() == 0)
115 return QModelIndex();
118 Event * event = static_cast<Event*>(index.internalPointer());
120 return createIndex(mParents[event->id()], 0, 0);
123 return QModelIndex();
126 int EventModel::columnCount(const QModelIndex & parent) const
132 int EventModel::rowCount (const QModelIndex & parent) const
134 if (!parent.isValid())
136 return mGroups.count();
139 if (parent.internalId() == 0)
141 return mGroups.at(parent.row()).mChildCount;
147 void EventModel::clearModel()
149 for(int i = 0;i < mGroups.count();i++){
150 QModelIndex idx = index(i, 0);
151 Group group = mGroups[i];
152 beginRemoveRows(idx, 0, group.mChildCount - 1);
153 bool ok = removeRows(0, group.mChildCount, idx);
155 //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok;
160 void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
163 // check for existence of the conference in the DB
164 if(Conference::getAll().count())
166 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
167 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
172 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
175 // check for existence of the conference in the DB
176 if(Conference::getAll().count())
178 qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
179 mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
184 void EventModel::loadEventsByActivities(const QDate &aDate, int aConferenceId)
187 if(Conference::getAll().count())
189 qDebug() << "Loading Conference Data (by Activities): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
190 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
192 createActivityGroups();
195 void EventModel::emitDataChangedSignal(const QModelIndex &aTopLeft, const QModelIndex &aBottomRight)
197 emit(dataChanged(aTopLeft,aBottomRight));