source: src/mvc/eventmodel.cpp @ 7620de0

qt5
Last change on this file since 7620de0 was 7620de0, checked in by korrco <korrco@…>, 12 years ago

room view added - need to test it

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