source: src/mvc/eventmodel.cpp @ 28d9e28

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

activities tab implemented - not finished yet

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