caching removed
[toast/confclerk.git] / src / mvc / eventmodel.cpp
1 #include "eventmodel.h"
2 #include <appsettings.h>
3 #include <conference.h>
4 #include <track.h>
5
6 const QString EventModel::COMMA_SEPARATOR = ", ";
7
8 EventModel::EventModel()
9 {
10     mEvents.clear();
11 }
12
13 void EventModel::createTimeGroups()
14 {
15     mGroups.clear();
16     mParents.clear();
17
18     if (mEvents.empty())
19     {
20         return;
21     }
22
23     const int timeSpan = 5400;
24
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);
29
30     for (int i=0; i<mEvents.count(); i++)
31     {
32         QTime eventTime = mEvents.at(i).start().time();
33
34         if (nextGroupTime <= eventTime)
35         {
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);
40         }
41
42         // add parent-child relation
43         mParents[mEvents.at(i).id()] = mGroups.count() - 1;
44     }
45
46     mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
47 }
48
49 void EventModel::createTrackGroups() {
50     mGroups.clear();
51     mParents.clear();
52     if (mEvents.empty())
53     {
54         return;
55     }
56     int trackId = mEvents.first().trackId();
57
58     mGroups << EventModel::Group(Track::retrieveTrackName(trackId), 0);
59     int nextTrackId = trackId;
60
61     for (int i=0; i<mEvents.count(); i++)
62     {
63         trackId = mEvents.at(i).trackId();
64         if (nextTrackId != trackId)
65         {
66             mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
67             mGroups << EventModel::Group(Track::retrieveTrackName(trackId), i);
68             nextTrackId = trackId;
69         }
70         // add parent-child relation
71         mParents[mEvents.at(i).id()] = mGroups.count() - 1;
72     }
73     mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
74 }
75
76 void EventModel::createTrackGroupsNew() {
77     mGroups.clear();
78     mParents.clear();
79     if (mEvents.empty())
80     {
81         return;
82     }
83     QList<Track> trackList = Track::getAll();
84     QList<Track>::iterator track = trackList.begin();
85     while (track != trackList.end())
86     {
87         QList<Event> eventList = Event::getByTrack(track->id());
88         QList<Event>::iterator event = eventList.begin();
89         while (event != eventList.end())
90         {
91             //TODO korinpa: pokracuj
92             event++;
93         }
94         track++;
95     }
96 }
97
98 QVariant EventModel::data(const QModelIndex& index, int role) const
99 {
100     if (index.isValid() && role == Qt::DisplayRole)
101     {
102         if (index.internalId() == 0)
103         {
104             return mGroups.at(index.row()).mTitle;
105         }
106         else //event data
107         {
108             return static_cast<Event*>(index.internalPointer())->id();
109         }
110     }
111
112     return QVariant();
113 }
114
115 QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
116 {
117     // TODO: add checks for out of range rows
118
119     if (!parent.isValid())
120     {
121         return createIndex(row, column, 0);
122     }
123     else if (parent.internalId() == 0)
124     {
125         const Group& group = mGroups.at(parent.row());
126         Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
127         return createIndex(row, column, reinterpret_cast<void*>(event));
128     }
129     else
130     {
131         return QModelIndex();
132     }
133 }
134
135 QModelIndex EventModel::parent(const QModelIndex & index) const
136 {
137     if (index.isValid())
138     {
139         if (index.internalId() == 0)
140         {
141             return QModelIndex();
142         }
143
144         Event * event = static_cast<Event*>(index.internalPointer());
145
146         return createIndex(mParents[event->id()], 0, 0);
147     }
148
149     return QModelIndex();
150 }
151
152 int EventModel::columnCount(const QModelIndex & parent) const
153 {
154     Q_UNUSED(parent);
155     return 1;
156 }
157
158 int EventModel::rowCount (const QModelIndex & parent) const
159 {
160     if (!parent.isValid())
161     {
162         return mGroups.count();
163     }
164
165     if (parent.internalId() == 0)
166     {
167         return mGroups.at(parent.row()).mChildCount;
168     }
169
170     return 0;
171 }
172
173 void EventModel::clearModel()
174 {
175     for(int i = 0;i < mGroups.count();i++){
176         QModelIndex idx = index(i, 0);
177         Group group = mGroups[i];
178         beginRemoveRows(idx, 0, group.mChildCount - 1);
179         /*bool ok =*/ removeRows(0, group.mChildCount, idx);
180         endRemoveRows();
181         //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok;
182     }
183     mEvents.clear();
184 }
185
186 void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
187 {
188     clearModel();
189     // check for existence of the conference in the DB
190     if(Conference::getAll().count())
191     {
192         qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
193         mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
194     }
195     createTimeGroups();
196 }
197
198 void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
199 {
200     clearModel();
201     // check for existence of the conference in the DB
202     if(Conference::getAll().count())
203     {
204         qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
205         mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
206     }
207     createTimeGroups();
208 }
209
210 int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId)
211 {
212     clearModel();
213     // check for existence of the conference in the DB
214     if(Conference::getAll().count())
215     {
216         qDebug() << "Loading search result Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
217         try{
218             mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
219         }
220         catch( OrmException &e  ){
221             qDebug() << "Event::getSearchResultByDate failed: " << e.text();
222         }
223         catch(...){
224             qDebug() << "Event::getSearchResultByDate failed";
225         }
226
227     }
228
229     createTimeGroups();
230
231     return mEvents.count();
232 }
233
234 void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId)
235 {
236     clearModel();
237     if(Conference::getAll().count())
238     {
239         qDebug() << "Loading Conference Data (by Track): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
240         mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start");
241     }
242     createTrackGroups();
243 }
244
245 void EventModel::loadNowEvents(int aConferenceId)
246 {
247     clearModel();
248     // check for existence of the conference in the DB
249     if(Conference::getAll().count())
250     {
251         qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] scheduled NOW";
252         mEvents = Event::nowEvents(aConferenceId, "start");
253     }
254     createTimeGroups();
255 }
256
257 void EventModel::updateModel(int aEventId)
258 {
259     for(int i=0; i<mEvents.count(); i++)
260     {
261         if(mEvents[i].id() == aEventId)
262             mEvents[i] = Event::getById(aEventId,AppSettings::confId());
263     }
264
265     // find the ModelIndex for given aEventId
266     for(int i=0; i<mGroups.count(); i++)
267     {
268         QModelIndex groupIndex = index(i,0,QModelIndex());
269         for(int j=0; j<mGroups[i].mChildCount; j++)
270         {
271             QModelIndex eventIndex = index(j,0,groupIndex);
272             if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
273             {
274                 emit(dataChanged(eventIndex,eventIndex));
275             }
276         }
277     }
278 }
279