source: src/mvc/eventmodel.cpp @ cf385cd

qt5
Last change on this file since cf385cd was cf385cd, checked in by Philipp Spitzer <philipp@…>, 11 years ago

Removed many of the qDebug() output lines (see ticket #10).

  • Property mode set to 100644
File size: 8.4 KB
Line 
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
25const QString EventModel::COMMA_SEPARATOR = ", ";
26
27EventModel::EventModel()
28{ }
29
30void EventModel::createTimeGroups()
31{
32    mGroups.clear();
33    mParents.clear();
34
35    if (mEvents.empty())
36    {
37        return;
38    }
39
40    const int timeSpan = 5400;
41
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);
46
47    for (int i=0; i<mEvents.count(); i++)
48    {
49        QTime eventTime = mEvents.at(i).start().time();
50
51        if (nextGroupTime <= eventTime)
52        {
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);
57        }
58
59        // add parent-child relation
60        mParents[mEvents.at(i).id()] = mGroups.count() - 1;
61    }
62
63    mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
64
65    reset();
66}
67
68void EventModel::createTrackGroups() {
69    mGroups.clear();
70    mParents.clear();
71    if (mEvents.empty())
72    {
73        return;
74    }
75    int trackId = mEvents.first().trackId();
76
77    mGroups << EventModel::Group(Track::retrieveTrackName(trackId), 0);
78    int nextTrackId = trackId;
79
80    for (int i=0; i<mEvents.count(); i++)
81    {
82        trackId = mEvents.at(i).trackId();
83        if (nextTrackId != trackId)
84        {
85            mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
86            mGroups << EventModel::Group(Track::retrieveTrackName(trackId), i);
87            nextTrackId = trackId;
88        }
89        // add parent-child relation
90        mParents[mEvents.at(i).id()] = mGroups.count() - 1;
91    }
92    mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
93}
94
95void EventModel::createRoomGroups()
96{
97    mGroups.clear();
98    mParents.clear();
99    if (mEvents.empty())
100    {
101        return;
102    }
103    int roomId = mEvents.first().roomId();
104
105    mGroups << EventModel::Group(Room::retrieveRoomName(roomId), 0);
106    int nextRoomId = roomId;
107
108    QList<Event>::iterator event = mEvents.begin();
109    int i = 0;
110    while (event != mEvents.end())
111    {
112        roomId = event->roomId();
113        if (nextRoomId != roomId)
114        {
115            mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
116            mGroups << EventModel::Group(Room::retrieveRoomName(roomId), i);
117            nextRoomId = roomId;
118        }
119        mParents[event->id()] = mGroups.count() - 1;
120        event++;
121        i++;
122    }
123    mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
124}
125
126QVariant EventModel::data(const QModelIndex& index, int role) const
127{
128    if (index.isValid() && role == Qt::DisplayRole)
129    {
130        if (index.internalId() == 0)
131        {
132            return mGroups.at(index.row()).mTitle;
133        }
134        else //event data
135        {
136            return static_cast<Event*>(index.internalPointer())->id();
137        }
138    }
139
140    return QVariant();
141}
142
143QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
144{
145    // TODO: add checks for out of range rows
146
147    if (!parent.isValid())
148    {
149        return createIndex(row, column, 0);
150    }
151    else if (parent.internalId() == 0)
152    {
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));
156    }
157    else
158    {
159        return QModelIndex();
160    }
161}
162
163QModelIndex EventModel::parent(const QModelIndex & index) const
164{
165    if (index.isValid())
166    {
167        if (index.internalId() == 0)
168        {
169            return QModelIndex();
170        }
171
172        Event * event = static_cast<Event*>(index.internalPointer());
173
174        return createIndex(mParents[event->id()], 0, 0);
175    }
176
177    return QModelIndex();
178}
179
180int EventModel::columnCount(const QModelIndex & parent) const
181{
182    Q_UNUSED(parent);
183    return 1;
184}
185
186int EventModel::rowCount (const QModelIndex & parent) const
187{
188    if (!parent.isValid())
189    {
190        return mGroups.count();
191    }
192
193    if (parent.internalId() == 0)
194    {
195        return mGroups.at(parent.row()).mChildCount;
196    }
197
198    return 0;
199}
200
201void EventModel::clearModel()
202{
203    mGroups.clear();
204    mEvents.clear();
205    mParents.clear();
206
207    reset();
208}
209
210void EventModel::loadEvents(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        mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
217    }
218    createTimeGroups();
219}
220
221void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
222{
223    clearModel();
224    // check for existence of the conference in the DB
225    if(Conference::getAll().count())
226    {
227        mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
228    }
229    createTimeGroups();
230}
231
232int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId)
233{
234    clearModel();
235    // check for existence of the conference in the DB
236    if(Conference::getAll().count())
237    {
238        try{
239            mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
240        }
241        catch( OrmException &e  ){
242            qDebug() << "Event::getSearchResultByDate failed: " << e.text();
243        }
244        catch(...){
245            qDebug() << "Event::getSearchResultByDate failed";
246        }
247
248    }
249
250    createTimeGroups();
251
252    return mEvents.count();
253}
254
255void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId)
256{
257    clearModel();
258    if (Conference::getAll().count())
259    {
260        mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start");
261    }
262    createTrackGroups();
263}
264
265void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId)
266{
267    clearModel();
268    if (Conference::getAll().count())
269    {
270        mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
271    }
272    createRoomGroups();
273}
274
275void EventModel::loadNowEvents(int aConferenceId)
276{
277    clearModel();
278    // check for existence of the conference in the DB
279    if(Conference::getAll().count())
280    {
281        mEvents = Event::nowEvents(aConferenceId, "start");
282    }
283    createTimeGroups();
284}
285
286void EventModel::loadConflictEvents(int aEventId, int aConferenceId)
287{
288    clearModel();
289    // check for existence of the conference in the DB
290    if(Conference::getAll().count())
291    {
292        mEvents = Event::conflictEvents(aEventId, aConferenceId);
293    }
294    createTimeGroups();
295}
296
297void EventModel::updateModel(int aEventId)
298{
299    for(int i=0; i<mEvents.count(); i++)
300    {
301        if(mEvents[i].id() == aEventId)
302            mEvents[i] = Event::getById(aEventId,Conference::activeConference());
303    }
304
305    // find the ModelIndex for given aEventId
306    for(int i=0; i<mGroups.count(); i++)
307    {
308        QModelIndex groupIndex = index(i,0,QModelIndex());
309        for(int j=0; j<mGroups[i].mChildCount; j++)
310        {
311            QModelIndex eventIndex = index(j,0,groupIndex);
312            if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
313            {
314                emit(dataChanged(groupIndex,groupIndex));
315                emit(dataChanged(eventIndex,eventIndex));
316            }
317        }
318    }
319}
320
Note: See TracBrowser for help on using the repository browser.