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 QVariant EventModel::data(const QModelIndex& index, int role) const
47 if (index.isValid() && role == Qt::DisplayRole)
49 if (index.internalId() == 0)
51 return mGroups.at(index.row()).mTitle;
55 return static_cast<Event*>(index.internalPointer())->id();
62 QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
64 // TODO: add checks for out of range rows
66 if (!parent.isValid())
68 return createIndex(row, column, 0);
70 else if (parent.internalId() == 0)
72 const Group& group = mGroups.at(parent.row());
73 Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
74 return createIndex(row, column, reinterpret_cast<void*>(event));
82 QModelIndex EventModel::parent(const QModelIndex & index) const
86 if (index.internalId() == 0)
91 Event * event = static_cast<Event*>(index.internalPointer());
93 return createIndex(mParents[event->id()], 0, 0);
99 int EventModel::columnCount(const QModelIndex & parent) const
105 int EventModel::rowCount (const QModelIndex & parent) const
107 if (!parent.isValid())
109 return mGroups.count();
112 if (parent.internalId() == 0)
114 return mGroups.at(parent.row()).mChildCount;
120 void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
122 for(int i=0; i<mGroups.count(); i++)
124 QModelIndex idx = index(i,0);
125 Group group = mGroups[i];
126 beginRemoveRows(idx,0,group.mChildCount-1);
127 bool ok = removeRows(0,group.mChildCount,idx);
129 //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok;
134 // check for existence of the conference in the DB
135 if(Conference::getAll().count())
137 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
138 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
143 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
145 for(int i=0; i<mGroups.count(); i++)
147 QModelIndex idx = index(i,0);
148 Group group = mGroups[i];
149 beginRemoveRows(idx,0,group.mChildCount-1);
150 bool ok = removeRows(0,group.mChildCount,idx);
152 //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok;
157 // check for existence of the conference in the DB
158 if(Conference::getAll().count())
160 qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
161 mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
166 void EventModel::emitDataChangedSignal(const QModelIndex &aTopLeft, const QModelIndex &aBottomRight)
168 emit(dataChanged(aTopLeft,aBottomRight));