1 #include "eventmodel.h"
2 #include <appsettings.h>
3 #include <conference.h>
7 const QString EventModel::COMMA_SEPARATOR = ", ";
9 EventModel::EventModel()
14 void EventModel::createTimeGroups()
24 const int timeSpan = 5400;
26 QTime startTime = mEvents.first().start().time();
27 mGroups << EventModel::Group(QString("%1 - %2").arg(startTime.toString("HH:mm"),
28 startTime.addSecs(timeSpan).toString("HH:mm")), 0);
29 QTime nextGroupTime = mEvents.first().start().time().addSecs(timeSpan);
31 for (int i=0; i<mEvents.count(); i++)
33 QTime eventTime = mEvents.at(i).start().time();
35 if (nextGroupTime <= eventTime)
37 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
38 mGroups << EventModel::Group(QString("%1 - %2").arg(nextGroupTime.toString("HH:mm"),
39 nextGroupTime.addSecs(timeSpan).toString("HH:mm")), i);
40 nextGroupTime = nextGroupTime.addSecs(timeSpan);
43 // add parent-child relation
44 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
47 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
50 void EventModel::createTrackGroups() {
57 int trackId = mEvents.first().trackId();
59 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), 0);
60 int nextTrackId = trackId;
62 for (int i=0; i<mEvents.count(); i++)
64 trackId = mEvents.at(i).trackId();
65 if (nextTrackId != trackId)
67 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
68 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), i);
69 nextTrackId = trackId;
71 // add parent-child relation
72 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
74 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
77 void EventModel::createRoomGroups()
85 int roomId = mEvents.first().roomId();
87 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), 0);
88 int nextRoomId = roomId;
90 QList<Event>::iterator event = mEvents.begin();
92 while (event != mEvents.end())
94 roomId = event->roomId();
95 if (nextRoomId != roomId)
97 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
98 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), i);
101 mParents[event->id()] = mGroups.count() - 1;
105 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
108 QVariant EventModel::data(const QModelIndex& index, int role) const
110 if (index.isValid() && role == Qt::DisplayRole)
112 if (index.internalId() == 0)
114 return mGroups.at(index.row()).mTitle;
118 return static_cast<Event*>(index.internalPointer())->id();
125 QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
127 // TODO: add checks for out of range rows
129 if (!parent.isValid())
131 return createIndex(row, column, 0);
133 else if (parent.internalId() == 0)
135 const Group& group = mGroups.at(parent.row());
136 Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
137 return createIndex(row, column, reinterpret_cast<void*>(event));
141 return QModelIndex();
145 QModelIndex EventModel::parent(const QModelIndex & index) const
149 if (index.internalId() == 0)
151 return QModelIndex();
154 Event * event = static_cast<Event*>(index.internalPointer());
156 return createIndex(mParents[event->id()], 0, 0);
159 return QModelIndex();
162 int EventModel::columnCount(const QModelIndex & parent) const
168 int EventModel::rowCount (const QModelIndex & parent) const
170 if (!parent.isValid())
172 return mGroups.count();
175 if (parent.internalId() == 0)
177 return mGroups.at(parent.row()).mChildCount;
183 void EventModel::clearModel()
185 for(int i = 0;i < mGroups.count();i++){
186 QModelIndex idx = index(i, 0);
187 Group group = mGroups[i];
188 beginRemoveRows(idx, 0, group.mChildCount - 1);
189 /*bool ok =*/ removeRows(0, group.mChildCount, idx);
191 //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok;
196 void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
199 // check for existence of the conference in the DB
200 if(Conference::getAll().count())
202 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
203 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
208 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
211 // check for existence of the conference in the DB
212 if(Conference::getAll().count())
214 qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
215 mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
220 int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId)
223 // check for existence of the conference in the DB
224 if(Conference::getAll().count())
226 qDebug() << "Loading search result Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
228 mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
230 catch( OrmException &e ){
231 qDebug() << "Event::getSearchResultByDate failed: " << e.text();
234 qDebug() << "Event::getSearchResultByDate failed";
241 return mEvents.count();
244 void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId)
247 if (Conference::getAll().count())
249 qDebug() << "Loading Conference Data (by Track): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
250 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start");
255 void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId)
258 if (Conference::getAll().count())
260 qDebug() << "Loading Conference Data (by Room): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
261 mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
266 void EventModel::loadNowEvents(int aConferenceId)
269 // check for existence of the conference in the DB
270 if(Conference::getAll().count())
272 qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] scheduled NOW";
273 mEvents = Event::nowEvents(aConferenceId, "start");
278 void EventModel::updateModel(int aEventId)
280 for(int i=0; i<mEvents.count(); i++)
282 if(mEvents[i].id() == aEventId)
283 mEvents[i] = Event::getById(aEventId,AppSettings::confId());
286 // find the ModelIndex for given aEventId
287 for(int i=0; i<mGroups.count(); i++)
289 QModelIndex groupIndex = index(i,0,QModelIndex());
290 for(int j=0; j<mGroups[i].mChildCount; j++)
292 QModelIndex eventIndex = index(j,0,groupIndex);
293 if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
295 emit(dataChanged(groupIndex,groupIndex));
296 emit(dataChanged(eventIndex,eventIndex));