source: src/mvc/eventmodel.cpp @ a5c1179

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

activities viewed ordered by activity id and start time

  • Property mode set to 100644
File size: 5.6 KB
Line 
1#include "eventmodel.h"
2#include <conference.h>
3#include <activity.h>
4
5const QString EventModel::COMMA_SEPARATOR = ", ";
6
7EventModel::EventModel()
8{
9    mEvents.clear();
10}
11
12void EventModel::createTimeGroups()
13{
14    mGroups.clear();
15    mParents.clear();
16
17    if (mEvents.empty())
18    {
19        return;
20    }
21
22    const int timeSpan = 5400;
23
24    QTime startTime = mEvents.first().start().time();
25    mGroups << EventModel::Group(QString("%1 - %2").arg(startTime.toString("HH:mm"),
26        startTime.addSecs(timeSpan).toString("HH:mm")), 0);
27    QTime nextGroupTime = mEvents.first().start().time().addSecs(timeSpan);
28
29    for (int i=0; i<mEvents.count(); i++)
30    {
31        QTime eventTime = mEvents.at(i).start().time();
32
33        if (nextGroupTime <= eventTime)
34        {
35            mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
36            mGroups << EventModel::Group(QString("%1 - %2").arg(nextGroupTime.toString("HH:mm"),
37                nextGroupTime.addSecs(timeSpan).toString("HH:mm")), i);
38            nextGroupTime = nextGroupTime.addSecs(timeSpan);
39        }
40
41        // add parent-child relation
42        mParents[mEvents.at(i).id()] = mGroups.count() - 1;
43    }
44
45    mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
46}
47
48void EventModel::createActivityGroups() {
49    mGroups.clear();
50    mParents.clear();
51    if (mEvents.empty())
52    {
53        return;
54    }
55    int activityId = mEvents.first().activityId();
56
57    mGroups << EventModel::Group(Activity::getActivityName(activityId), 0);
58    int nextActivityId = activityId;
59
60    for (int i=0; i<mEvents.count(); i++)
61    {
62        activityId = mEvents.at(i).activityId();
63        if (nextActivityId != activityId)
64        {
65            mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
66            mGroups << EventModel::Group(Activity::getActivityName(activityId), i);
67            nextActivityId = activityId;
68        }
69        // add parent-child relation
70        mParents[mEvents.at(i).id()] = mGroups.count() - 1;
71    }
72    mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
73}
74
75QVariant EventModel::data(const QModelIndex& index, int role) const
76{
77    if (index.isValid() && role == Qt::DisplayRole)
78    {
79        if (index.internalId() == 0)
80        {
81            return mGroups.at(index.row()).mTitle;
82        }
83        else //event data
84        {
85            return static_cast<Event*>(index.internalPointer())->id();
86        }
87    }
88
89    return QVariant();
90}
91
92QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
93{
94    // TODO: add checks for out of range rows
95
96    if (!parent.isValid())
97    {
98        return createIndex(row, column, 0);
99    }
100    else if (parent.internalId() == 0)
101    {
102        const Group& group = mGroups.at(parent.row());
103        Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
104        return createIndex(row, column, reinterpret_cast<void*>(event));
105    }
106    else
107    {
108        return QModelIndex();
109    }
110}
111
112QModelIndex EventModel::parent(const QModelIndex & index) const
113{
114    if (index.isValid())
115    {
116        if (index.internalId() == 0)
117        {
118            return QModelIndex();
119        }
120
121        Event * event = static_cast<Event*>(index.internalPointer());
122
123        return createIndex(mParents[event->id()], 0, 0);
124    }
125
126    return QModelIndex();
127}
128
129int EventModel::columnCount(const QModelIndex & parent) const
130{
131    Q_UNUSED(parent);
132    return 1;
133}
134
135int EventModel::rowCount (const QModelIndex & parent) const
136{
137    if (!parent.isValid())
138    {
139        return mGroups.count();
140    }
141
142    if (parent.internalId() == 0)
143    {
144        return mGroups.at(parent.row()).mChildCount;
145    }
146
147    return 0;
148}
149
150void EventModel::clearModel()
151{
152    for(int i = 0;i < mGroups.count();i++){
153        QModelIndex idx = index(i, 0);
154        Group group = mGroups[i];
155        beginRemoveRows(idx, 0, group.mChildCount - 1);
156        /*bool ok =*/ removeRows(0, group.mChildCount, idx);
157        endRemoveRows();
158        //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok;
159    }
160    mEvents.clear();
161}
162
163void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
164{
165    clearModel();
166    // check for existence of the conference in the DB
167    if(Conference::getAll().count())
168    {
169        qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
170        mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, Event::START);
171    }
172    createTimeGroups();
173}
174
175void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
176{
177    clearModel();
178    // check for existence of the conference in the DB
179    if(Conference::getAll().count())
180    {
181        qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
182        mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
183    }
184    createTimeGroups();
185}
186
187void EventModel::loadEventsByActivities(const QDate &aDate, int aConferenceId)
188{
189    clearModel();
190    if(Conference::getAll().count())
191    {
192        qDebug() << "Loading Conference Data (by Activities): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
193        mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, Event::XID_ACTIVITY + COMMA_SEPARATOR + Event::START);
194    }
195    createActivityGroups();
196}
197
198void EventModel::emitDataChangedSignal(const QModelIndex &aTopLeft, const QModelIndex &aBottomRight)
199{
200    emit(dataChanged(aTopLeft,aBottomRight));
201}
202
Note: See TracBrowser for help on using the repository browser.