1 #include "eventmodel.h"
2 #include <conference.h>
6 const QString EventModel::COMMA_SEPARATOR = ", ";
8 EventModel::EventModel()
13 void EventModel::createTimeGroups()
23 const int timeSpan = 5400;
25 QTime startTime = mEvents.first().start().time();
26 mGroups << EventModel::Group(QString("%1 - %2").arg(startTime.toString("HH:mm"),
27 startTime.addSecs(timeSpan).toString("HH:mm")), 0);
28 QTime nextGroupTime = mEvents.first().start().time().addSecs(timeSpan);
30 for (int i=0; i<mEvents.count(); i++)
32 QTime eventTime = mEvents.at(i).start().time();
34 if (nextGroupTime <= eventTime)
36 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
37 mGroups << EventModel::Group(QString("%1 - %2").arg(nextGroupTime.toString("HH:mm"),
38 nextGroupTime.addSecs(timeSpan).toString("HH:mm")), i);
39 nextGroupTime = nextGroupTime.addSecs(timeSpan);
42 // add parent-child relation
43 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
46 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
49 void EventModel::createTrackGroups() {
56 int trackId = mEvents.first().trackId();
58 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), 0);
59 int nextTrackId = trackId;
61 for (int i=0; i<mEvents.count(); i++)
63 trackId = mEvents.at(i).trackId();
64 if (nextTrackId != trackId)
66 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
67 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), i);
68 nextTrackId = trackId;
70 // add parent-child relation
71 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
73 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
76 void EventModel::createRoomGroups()
84 int roomId = mEvents.first().roomId();
86 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), 0);
87 int nextRoomId = roomId;
89 QList<Event>::iterator event = mEvents.begin();
91 while (event != mEvents.end())
93 roomId = event->roomId();
94 if (nextRoomId != roomId)
96 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
97 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), i);
100 mParents[event->id()] = mGroups.count() - 1;
104 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
107 QVariant EventModel::data(const QModelIndex& index, int role) const
109 if (index.isValid() && role == Qt::DisplayRole)
111 if (index.internalId() == 0)
113 return mGroups.at(index.row()).mTitle;
117 return static_cast<Event*>(index.internalPointer())->id();
124 QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
126 // TODO: add checks for out of range rows
128 if (!parent.isValid())
130 return createIndex(row, column, 0);
132 else if (parent.internalId() == 0)
134 const Group& group = mGroups.at(parent.row());
135 Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
136 return createIndex(row, column, reinterpret_cast<void*>(event));
140 return QModelIndex();
144 QModelIndex EventModel::parent(const QModelIndex & index) const
148 if (index.internalId() == 0)
150 return QModelIndex();
153 Event * event = static_cast<Event*>(index.internalPointer());
155 return createIndex(mParents[event->id()], 0, 0);
158 return QModelIndex();
161 int EventModel::columnCount(const QModelIndex & parent) const
167 int EventModel::rowCount (const QModelIndex & parent) const
169 if (!parent.isValid())
171 return mGroups.count();
174 if (parent.internalId() == 0)
176 return mGroups.at(parent.row()).mChildCount;
182 void EventModel::clearModel()
184 for(int i = 0;i < mGroups.count();i++){
185 QModelIndex idx = index(i, 0);
186 Group group = mGroups[i];
187 beginRemoveRows(idx, 0, group.mChildCount - 1);
188 /*bool ok =*/ removeRows(0, group.mChildCount, idx);
190 //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok;
195 void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
198 // check for existence of the conference in the DB
199 if(Conference::getAll().count())
201 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
202 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
207 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
210 // check for existence of the conference in the DB
211 if(Conference::getAll().count())
213 qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
214 mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
219 int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId)
222 // check for existence of the conference in the DB
223 if(Conference::getAll().count())
225 qDebug() << "Loading search result Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
227 mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
229 catch( OrmException &e ){
230 qDebug() << "Event::getSearchResultByDate failed: " << e.text();
233 qDebug() << "Event::getSearchResultByDate failed";
240 return mEvents.count();
243 void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId)
246 if (Conference::getAll().count())
248 qDebug() << "Loading Conference Data (by Track): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
249 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start");
254 void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId)
257 if (Conference::getAll().count())
259 qDebug() << "Loading Conference Data (by Room): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
260 mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
265 void EventModel::loadNowEvents(int aConferenceId)
268 // check for existence of the conference in the DB
269 if(Conference::getAll().count())
271 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] scheduled NOW";
272 mEvents = Event::nowEvents(aConferenceId, "start");
277 void EventModel::updateModel(int aEventId)
279 for(int i=0; i<mEvents.count(); i++)
281 if(mEvents[i].id() == aEventId)
282 mEvents[i] = Event::getById(aEventId,Conference::activeConference());
285 // find the ModelIndex for given aEventId
286 for(int i=0; i<mGroups.count(); i++)
288 QModelIndex groupIndex = index(i,0,QModelIndex());
289 for(int j=0; j<mGroups[i].mChildCount; j++)
291 QModelIndex eventIndex = index(j,0,groupIndex);
292 if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
294 emit(dataChanged(groupIndex,groupIndex));
295 emit(dataChanged(eventIndex,eventIndex));