2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
5 * This file is part of ConfClerk.
7 * ConfClerk is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 2 of the License, or (at your option)
12 * ConfClerk is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * ConfClerk. If not, see <http://www.gnu.org/licenses/>.
20 #include "eventmodel.h"
21 #include <conference.h>
25 const QString EventModel::COMMA_SEPARATOR = ", ";
27 EventModel::EventModel()
30 void EventModel::createTimeGroups()
40 const int timeSpan = 5400;
42 QTime startTime = mEvents.first().start().time();
43 mGroups << EventModel::Group(QString("%1 - %2").arg(startTime.toString("HH:mm"),
44 startTime.addSecs(timeSpan).toString("HH:mm")), 0);
45 QTime nextGroupTime = mEvents.first().start().time().addSecs(timeSpan);
47 for (int i=0; i<mEvents.count(); i++)
49 QTime eventTime = mEvents.at(i).start().time();
51 if (nextGroupTime <= eventTime)
53 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
54 mGroups << EventModel::Group(QString("%1 - %2").arg(nextGroupTime.toString("HH:mm"),
55 nextGroupTime.addSecs(timeSpan).toString("HH:mm")), i);
56 nextGroupTime = nextGroupTime.addSecs(timeSpan);
59 // add parent-child relation
60 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
63 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
68 void EventModel::createTrackGroups() {
75 int trackId = mEvents.first().trackId();
77 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), 0);
78 int nextTrackId = trackId;
80 for (int i=0; i<mEvents.count(); i++)
82 trackId = mEvents.at(i).trackId();
83 if (nextTrackId != trackId)
85 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
86 mGroups << EventModel::Group(Track::retrieveTrackName(trackId), i);
87 nextTrackId = trackId;
89 // add parent-child relation
90 mParents[mEvents.at(i).id()] = mGroups.count() - 1;
92 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
95 void EventModel::createRoomGroups()
103 int roomId = mEvents.first().roomId();
105 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), 0);
106 int nextRoomId = roomId;
108 QList<Event>::iterator event = mEvents.begin();
110 while (event != mEvents.end())
112 roomId = event->roomId();
113 if (nextRoomId != roomId)
115 mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
116 mGroups << EventModel::Group(Room::retrieveRoomName(roomId), i);
119 mParents[event->id()] = mGroups.count() - 1;
123 mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
126 QVariant EventModel::data(const QModelIndex& index, int role) const
128 if (index.isValid() && role == Qt::DisplayRole)
130 if (index.internalId() == 0)
132 return mGroups.at(index.row()).mTitle;
136 return static_cast<Event*>(index.internalPointer())->id();
143 QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
145 // TODO: add checks for out of range rows
147 if (!parent.isValid())
149 return createIndex(row, column, 0);
151 else if (parent.internalId() == 0)
153 const Group& group = mGroups.at(parent.row());
154 Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
155 return createIndex(row, column, reinterpret_cast<void*>(event));
159 return QModelIndex();
163 QModelIndex EventModel::parent(const QModelIndex & index) const
167 if (index.internalId() == 0)
169 return QModelIndex();
172 Event * event = static_cast<Event*>(index.internalPointer());
174 return createIndex(mParents[event->id()], 0, 0);
177 return QModelIndex();
180 int EventModel::columnCount(const QModelIndex & parent) const
186 int EventModel::rowCount (const QModelIndex & parent) const
188 if (!parent.isValid())
190 return mGroups.count();
193 if (parent.internalId() == 0)
195 return mGroups.at(parent.row()).mChildCount;
201 void EventModel::clearModel()
210 void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
213 // check for existence of the conference in the DB
214 if(Conference::getAll().count())
216 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
221 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
224 // check for existence of the conference in the DB
225 if(Conference::getAll().count())
227 mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
232 int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId)
235 // check for existence of the conference in the DB
236 if(Conference::getAll().count())
239 mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
241 catch( OrmException &e ){
242 qDebug() << "Event::getSearchResultByDate failed: " << e.text();
245 qDebug() << "Event::getSearchResultByDate failed";
252 return mEvents.count();
255 void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId)
258 if (Conference::getAll().count())
260 mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start");
265 void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId)
268 if (Conference::getAll().count())
270 mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
275 void EventModel::loadNowEvents(int aConferenceId)
278 // check for existence of the conference in the DB
279 if(Conference::getAll().count())
281 mEvents = Event::nowEvents(aConferenceId, "start");
286 void EventModel::loadConflictEvents(int aEventId, int aConferenceId)
289 // check for existence of the conference in the DB
290 if(Conference::getAll().count())
292 mEvents = Event::conflictEvents(aEventId, aConferenceId);
297 void EventModel::updateModel(int aEventId)
299 for(int i=0; i<mEvents.count(); i++)
301 if(mEvents[i].id() == aEventId)
302 mEvents[i] = Event::getById(aEventId,Conference::activeConference());
305 // find the ModelIndex for given aEventId
306 for(int i=0; i<mGroups.count(); i++)
308 QModelIndex groupIndex = index(i,0,QModelIndex());
309 for(int j=0; j<mGroups[i].mChildCount; j++)
311 QModelIndex eventIndex = index(j,0,groupIndex);
312 if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
314 emit(dataChanged(groupIndex,groupIndex));
315 emit(dataChanged(eventIndex,eventIndex));