source: src/mvc/eventmodel.cpp @ 9208bdb

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

support for view activities with their names added

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