source: src/mvc/eventmodel.cpp @ 8bdf2d3

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

Removed unnecessary debug output and code.

  • Property mode set to 100644
File size: 9.3 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    // qDebug() << __PRETTY_FUNCTION__ << this << mEvents.count();
204    mGroups.clear();
205    mEvents.clear();
206    mParents.clear();
207
208    reset();
209}
210
211void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
212{
213    clearModel();
214    // check for existence of the conference in the DB
215    if(Conference::getAll().count())
216    {
217        qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
218        mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
219    }
220    createTimeGroups();
221}
222
223void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
224{
225    clearModel();
226    // check for existence of the conference in the DB
227    if(Conference::getAll().count())
228    {
229        qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
230        mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
231    }
232    createTimeGroups();
233}
234
235int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId)
236{
237    clearModel();
238    // check for existence of the conference in the DB
239    if(Conference::getAll().count())
240    {
241        // qDebug() << "Loading search result Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
242        try{
243            mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
244        }
245        catch( OrmException &e  ){
246            qDebug() << "Event::getSearchResultByDate failed: " << e.text();
247        }
248        catch(...){
249            qDebug() << "Event::getSearchResultByDate failed";
250        }
251
252    }
253
254    createTimeGroups();
255
256    return mEvents.count();
257}
258
259void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId)
260{
261    clearModel();
262    if (Conference::getAll().count())
263    {
264        qDebug() << "Loading Conference Data (by Track): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
265        mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start");
266    }
267    createTrackGroups();
268}
269
270void EventModel::loadEventsByRoom(const QDate &aDate, int aConferenceId)
271{
272    clearModel();
273    if (Conference::getAll().count())
274    {
275        qDebug() << "Loading Conference Data (by Room): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
276        mEvents = Event::getByDateAndRoom(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
277    }
278    createRoomGroups();
279}
280
281void EventModel::loadNowEvents(int aConferenceId)
282{
283    clearModel();
284    // check for existence of the conference in the DB
285    if(Conference::getAll().count())
286    {
287        qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] scheduled NOW";
288        mEvents = Event::nowEvents(aConferenceId, "start");
289    }
290    createTimeGroups();
291}
292
293void EventModel::loadConflictEvents(int aEventId, int aConferenceId)
294{
295    clearModel();
296    // check for existence of the conference in the DB
297    if(Conference::getAll().count())
298    {
299        qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] in conflict with " << aEventId;
300        mEvents = Event::conflictEvents(aEventId, aConferenceId);
301    }
302    createTimeGroups();
303}
304
305void EventModel::updateModel(int aEventId)
306{
307    for(int i=0; i<mEvents.count(); i++)
308    {
309        if(mEvents[i].id() == aEventId)
310            mEvents[i] = Event::getById(aEventId,Conference::activeConference());
311    }
312
313    // find the ModelIndex for given aEventId
314    for(int i=0; i<mGroups.count(); i++)
315    {
316        QModelIndex groupIndex = index(i,0,QModelIndex());
317        for(int j=0; j<mGroups[i].mChildCount; j++)
318        {
319            QModelIndex eventIndex = index(j,0,groupIndex);
320            if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
321            {
322                emit(dataChanged(groupIndex,groupIndex));
323                emit(dataChanged(eventIndex,eventIndex));
324            }
325        }
326    }
327}
328
Note: See TracBrowser for help on using the repository browser.