source: src/mvc/eventmodel.cpp @ 979e5be

qt5
Last change on this file since 979e5be was d49254d, checked in by pavelpa <pavelpa@…>, 12 years ago

'conflict' dialog now contains list of events in conflict with given eventId

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