Removed unused nowEvent functions.
[toast/confclerk.git] / src / mvc / eventmodel.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
4  *
5  * This file is part of ConfClerk.
6  *
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)
10  * any later version.
11  *
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
15  * more details.
16  *
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/>.
19  */
20 #include "eventmodel.h"
21 #include <conference.h>
22 #include <track.h>
23 #include <room.h>
24
25 const QString EventModel::COMMA_SEPARATOR = ", ";
26
27 EventModel::EventModel()
28 { }
29
30
31 void EventModel::Group::setTitle(const QList<Event>& mEvents) {
32     QTime startTime = mEvents.at(mFirstEventIndex).start().time();
33     QTime endTime(0, 0);
34     for (int i = mFirstEventIndex; i != mFirstEventIndex + mChildCount; ++i) {
35         endTime = qMax(mEvents.at(i).start().time().addSecs(mEvents.at(i).duration()), endTime);
36     }
37     mTitle = QString("%1 - %2").arg(startTime.toString("HH:mm")).arg(endTime.toString("HH:mm"));
38 }
39
40
41 // We want to group the events into "time slots/time groups" that
42 // should start at full hours and have the duration of either
43 // one hour or (if less than 3 events are in one time slot)
44 // multiple of one hour.
45 void EventModel::createTimeGroups()
46 {
47     mGroups.clear();
48     mParents.clear();
49     if (mEvents.empty()) return;
50
51     const int minTimeSpan = 3600; // one hour
52     const int minChildCount = 3; // minimum number of events in one group
53
54     // Create the first time group. The events have to be sorted by start time at this point!
55     QTime groupStartTime(mEvents.first().start().time().hour(), 0);
56     QTime groupEndTime = groupStartTime.addSecs(mEvents.first().duration());
57     mGroups << EventModel::Group("", 0);
58     int timeSpan = minTimeSpan;
59
60     for (int i = 0; i != mEvents.count(); ++i) {
61         QTime eventStartTime = mEvents.at(i).start().time();
62         QTime eventEndTime = eventStartTime.addSecs(mEvents.at(i).duration());
63
64         if (eventStartTime >= groupStartTime.addSecs(timeSpan)) {
65             // a new group could be necessary
66             if (mGroups.last().mChildCount < minChildCount) {
67                 // too few events in the group => no new group
68                 // except a gap in time would occur that is longer than minTimeSpan
69                 if (i > 0 && qMax(mEvents.at(i-1).start().time().addSecs(mEvents.at(i-1).duration()), groupEndTime).secsTo(eventStartTime) < minTimeSpan) {
70                     timeSpan += minTimeSpan;
71                     --i;
72                     continue; // repeat with the same event
73                 }
74             }
75
76             // a new group is necessary
77             mGroups.last().setTitle(mEvents);
78             groupStartTime = groupStartTime.addSecs(timeSpan);
79             groupEndTime = groupStartTime.addSecs(mEvents.at(i).duration());
80             mGroups << EventModel::Group("", i);
81             timeSpan = minTimeSpan;
82         }
83
84         // insert event into current group
85         mParents[mEvents.at(i).id()] = mGroups.count() - 1;
86         mGroups.last().mChildCount += 1;
87         groupEndTime = qMax(eventEndTime, groupEndTime);
88     }
89
90     // the last group needs a title as well
91     mGroups.last().setTitle(mEvents);
92
93     reset();
94 }
95
96 void EventModel::createTrackGroups() {
97     mGroups.clear();
98     mParents.clear();
99     if (mEvents.empty())
100     {
101         return;
102     }
103     int trackId = mEvents.first().trackId();
104
105     mGroups << EventModel::Group(Track::retrieveTrackName(trackId), 0);
106     int nextTrackId = trackId;
107
108     for (int i=0; i<mEvents.count(); i++)
109     {
110         trackId = mEvents.at(i).trackId();
111         if (nextTrackId != trackId)
112         {
113             mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
114             mGroups << EventModel::Group(Track::retrieveTrackName(trackId), i);
115             nextTrackId = trackId;
116         }
117         // add parent-child relation
118         mParents[mEvents.at(i).id()] = mGroups.count() - 1;
119     }
120     mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
121 }
122
123 void EventModel::createRoomGroups()
124 {
125     mGroups.clear();
126     mParents.clear();
127     if (mEvents.empty())
128     {
129         return;
130     }
131     int roomId = mEvents.first().roomId();
132
133     mGroups << EventModel::Group(Room::retrieveRoomName(roomId), 0);
134     int nextRoomId = roomId;
135
136     QList<Event>::iterator event = mEvents.begin();
137     int i = 0;
138     while (event != mEvents.end())
139     {
140         roomId = event->roomId();
141         if (nextRoomId != roomId)
142         {
143             mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
144             mGroups << EventModel::Group(Room::retrieveRoomName(roomId), i);
145             nextRoomId = roomId;
146         }
147         mParents[event->id()] = mGroups.count() - 1;
148         event++;
149         i++;
150     }
151     mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
152 }
153
154 QVariant EventModel::data(const QModelIndex& index, int role) const
155 {
156     if (index.isValid() && role == Qt::DisplayRole)
157     {
158         if (index.internalId() == 0)
159         {
160             return mGroups.at(index.row()).mTitle;
161         }
162         else //event data
163         {
164             return static_cast<Event*>(index.internalPointer())->id();
165         }
166     }
167
168     return QVariant();
169 }
170
171 QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
172 {
173     // TODO: add checks for out of range rows
174
175     if (!parent.isValid())
176     {
177         return createIndex(row, column, 0);
178     }
179     else if (parent.internalId() == 0)
180     {
181         const Group& group = mGroups.at(parent.row());
182         Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
183         return createIndex(row, column, reinterpret_cast<void*>(event));
184     }
185     else
186     {
187         return QModelIndex();
188     }
189 }
190
191 QModelIndex EventModel::parent(const QModelIndex & index) const
192 {
193     if (index.isValid())
194     {
195         if (index.internalId() == 0)
196         {
197             return QModelIndex();
198         }
199
200         Event * event = static_cast<Event*>(index.internalPointer());
201
202         return createIndex(mParents[event->id()], 0, 0);
203     }
204
205     return QModelIndex();
206 }
207
208 int EventModel::columnCount(const QModelIndex & parent) const
209 {
210     Q_UNUSED(parent);
211     return 1;
212 }
213
214 int EventModel::rowCount (const QModelIndex & parent) const
215 {
216     if (!parent.isValid())
217     {
218         return mGroups.count();
219     }
220
221     if (parent.internalId() == 0)
222     {
223         return mGroups.at(parent.row()).mChildCount;
224     }
225
226     return 0;
227 }
228
229 void EventModel::clearModel()
230 {
231     mGroups.clear();
232     mEvents.clear();
233     mParents.clear();
234
235     reset();
236 }
237
238 void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
239 {
240     clearModel();
241     // check for existence of the conference in the DB
242     if(Conference::getAll().count())
243     {
244         mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
245     }
246     createTimeGroups();
247 }
248
249 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
250 {
251     clearModel();
252     // check for existence of the conference in the DB
253     if(Conference::getAll().count())
254     {
255         mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
256     }
257     createTimeGroups();
258 }
259
260 int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId)
261 {
262     clearModel();
263     // check for existence of the conference in the DB
264     if(Conference::getAll().count())
265     {
266         try{
267             mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
268         }
269         catch( OrmException &e  ){
270             qDebug() << "Event::getSearchResultByDate failed: " << e.text();
271         }
272         catch(...){
273             qDebug() << "Event::getSearchResultByDate failed";
274         }
275
276     }
277
278     createTimeGroups();
279
280     return mEvents.count();
281 }
282
283 void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId)
284 {
285     clearModel();
286     if (Conference::getAll().count())
287     {
288         mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start");
289     }
290     createTrackGroups();
291 }
292
293 void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId)
294 {
295     clearModel();
296     if (Conference::getAll().count())
297     {
298         mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
299     }
300     createRoomGroups();
301 }
302
303
304 void EventModel::loadConflictEvents(int aEventId, int aConferenceId) {
305     clearModel();
306     // check for existence of the conference in the DB
307     if(Conference::getAll().count())
308     {
309         mEvents = Event::conflictEvents(aEventId, aConferenceId);
310     }
311     createTimeGroups();
312 }
313
314 void EventModel::updateModel(int aEventId)
315 {
316     for(int i=0; i<mEvents.count(); i++)
317     {
318         if(mEvents[i].id() == aEventId)
319             mEvents[i] = Event::getById(aEventId,Conference::activeConference());
320     }
321
322     // find the ModelIndex for given aEventId
323     for(int i=0; i<mGroups.count(); i++)
324     {
325         QModelIndex groupIndex = index(i,0,QModelIndex());
326         for(int j=0; j<mGroups[i].mChildCount; j++)
327         {
328             QModelIndex eventIndex = index(j,0,groupIndex);
329             if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
330             {
331                 emit(dataChanged(groupIndex,groupIndex));
332                 emit(dataChanged(eventIndex,eventIndex));
333             }
334         }
335     }
336 }
337