source: src/mvc/eventmodel.cpp @ 84b9c07

qt5
Last change on this file since 84b9c07 was 27102d5, checked in by fortefr <fortefr@…>, 13 years ago

Temporal virtual_event change

  • Property mode set to 100644
File size: 4.9 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
45QVariant EventModel::data(const QModelIndex& index, int role) const
46{
47    if (index.isValid() && role == Qt::DisplayRole)
48    {
49        if (index.internalId() == 0)
50        {   //range of time data
51            //qDebug() << qVariantValue<QString>(mGroups.at(index.row()).mTitle);
52            return mGroups.at(index.row()).mTitle;
53        }
54        else //event data
55        {
56            //qDebug() << qVariantValue<QString>(static_cast<Event*>(index.internalPointer())->id());
57            //return static_cast<Event*>(index.internalPointer())->id();
58            //qDebug() << Event::getVirtualById(static_cast<Event*>(index.internalPointer())->id(), 1).title();// Id Conference is 1 by now
59            //return 1;
60            return static_cast<Event*>(index.internalPointer())->id();
61        }
62    }
63
64    return QVariant();
65}
66
67QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
68{
69    // TODO: add checks for out of range rows
70
71    if (!parent.isValid())
72    {
73        return createIndex(row, column, 0);
74    }
75    else if (parent.internalId() == 0)
76    {
77        const Group& group = mGroups.at(parent.row());
78        Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
79        return createIndex(row, column, reinterpret_cast<void*>(event));
80    }
81    else
82    {
83        return QModelIndex();
84    }
85}
86
87QModelIndex EventModel::parent(const QModelIndex & index) const
88{
89    if (index.isValid())
90    {
91        if (index.internalId() == 0)
92        {
93            return QModelIndex();
94        }
95
96        Event * event = static_cast<Event*>(index.internalPointer());
97
98        return createIndex(mParents[event->id()], 0, 0);
99    }
100
101    return QModelIndex();
102}
103
104int EventModel::columnCount(const QModelIndex & parent) const
105{
106    Q_UNUSED(parent);
107    return 1;
108}
109
110int EventModel::rowCount (const QModelIndex & parent) const
111{
112    if (!parent.isValid())
113    {
114        return mGroups.count();
115    }
116
117    if (parent.internalId() == 0)
118    {
119        return mGroups.at(parent.row()).mChildCount;
120    }
121
122    return 0;
123}
124
125void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
126{
127    for(int i=0; i<mGroups.count(); i++)
128    {
129        QModelIndex idx = index(i,0);
130        Group group = mGroups[i];
131        beginRemoveRows(idx,0,group.mChildCount-1);
132        bool ok = removeRows(0,group.mChildCount,idx);
133        endRemoveRows();
134        //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok;
135    }
136
137    mEvents.clear();
138
139    // check for existence of the conference in the DB
140    if(Conference::getAll().count())
141    {
142        qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
143        mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
144    }
145    createTimeGroups();
146}
147
148void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
149{
150    for(int i=0; i<mGroups.count(); i++)
151    {
152        QModelIndex idx = index(i,0);
153        Group group = mGroups[i];
154        beginRemoveRows(idx,0,group.mChildCount-1);
155        bool ok = removeRows(0,group.mChildCount,idx);
156        endRemoveRows();
157        //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok;
158    }
159
160    mEvents.clear();
161
162    // check for existence of the conference in the DB
163    if(Conference::getAll().count())
164    {
165        qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
166        mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
167    }
168    createTimeGroups();
169}
170
171void EventModel::emitDataChangedSignal(const QModelIndex &aTopLeft, const QModelIndex &aBottomRight)
172{
173    emit(dataChanged(aTopLeft,aBottomRight));
174}
175
Note: See TracBrowser for help on using the repository browser.