source: src/mvc/eventmodel.cpp @ b8a3ad1

qt5
Last change on this file since b8a3ad1 was b8a3ad1, checked in by pavelpa <pavelpa@…>, 12 years ago

implemented NOW tab

  • Property mode set to 100644
File size: 7.1 KB
Line 
1#include "eventmodel.h"
2#include <appsettings.h>
3#include <conference.h>
4#include <track.h>
5
6const QString EventModel::COMMA_SEPARATOR = ", ";
7
8EventModel::EventModel()
9{
10    mEvents.clear();
11}
12
13void EventModel::createTimeGroups()
14{
15    mGroups.clear();
16    mParents.clear();
17
18    if (mEvents.empty())
19    {
20        return;
21    }
22
23    const int timeSpan = 5400;
24
25    QTime startTime = mEvents.first().start().time();
26    mGroups << EventModel::Group(QString("%1 - %2").arg(startTime.toString("HH:mm"),
27        startTime.addSecs(timeSpan).toString("HH:mm")), 0);
28    QTime nextGroupTime = mEvents.first().start().time().addSecs(timeSpan);
29
30    for (int i=0; i<mEvents.count(); i++)
31    {
32        QTime eventTime = mEvents.at(i).start().time();
33
34        if (nextGroupTime <= eventTime)
35        {
36            mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
37            mGroups << EventModel::Group(QString("%1 - %2").arg(nextGroupTime.toString("HH:mm"),
38                nextGroupTime.addSecs(timeSpan).toString("HH:mm")), i);
39            nextGroupTime = nextGroupTime.addSecs(timeSpan);
40        }
41
42        // add parent-child relation
43        mParents[mEvents.at(i).id()] = mGroups.count() - 1;
44    }
45
46    mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
47}
48
49void EventModel::createTrackGroups() {
50    mGroups.clear();
51    mParents.clear();
52    if (mEvents.empty())
53    {
54        return;
55    }
56    int trackId = mEvents.first().trackId();
57
58    mGroups << EventModel::Group(Track::getTrackName(trackId), 0);
59    int nextTrackId = trackId;
60
61    for (int i=0; i<mEvents.count(); i++)
62    {
63        trackId = mEvents.at(i).trackId();
64        if (nextTrackId != trackId)
65        {
66            mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
67            mGroups << EventModel::Group(Track::getTrackName(trackId), i);
68            nextTrackId = trackId;
69        }
70        // add parent-child relation
71        mParents[mEvents.at(i).id()] = mGroups.count() - 1;
72    }
73    mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex;
74}
75
76QVariant EventModel::data(const QModelIndex& index, int role) const
77{
78    if (index.isValid() && role == Qt::DisplayRole)
79    {
80        if (index.internalId() == 0)
81        {
82            return mGroups.at(index.row()).mTitle;
83        }
84        else //event data
85        {
86            return static_cast<Event*>(index.internalPointer())->id();
87        }
88    }
89
90    return QVariant();
91}
92
93QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const
94{
95    // TODO: add checks for out of range rows
96
97    if (!parent.isValid())
98    {
99        return createIndex(row, column, 0);
100    }
101    else if (parent.internalId() == 0)
102    {
103        const Group& group = mGroups.at(parent.row());
104        Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex));
105        return createIndex(row, column, reinterpret_cast<void*>(event));
106    }
107    else
108    {
109        return QModelIndex();
110    }
111}
112
113QModelIndex EventModel::parent(const QModelIndex & index) const
114{
115    if (index.isValid())
116    {
117        if (index.internalId() == 0)
118        {
119            return QModelIndex();
120        }
121
122        Event * event = static_cast<Event*>(index.internalPointer());
123
124        return createIndex(mParents[event->id()], 0, 0);
125    }
126
127    return QModelIndex();
128}
129
130int EventModel::columnCount(const QModelIndex & parent) const
131{
132    Q_UNUSED(parent);
133    return 1;
134}
135
136int EventModel::rowCount (const QModelIndex & parent) const
137{
138    if (!parent.isValid())
139    {
140        return mGroups.count();
141    }
142
143    if (parent.internalId() == 0)
144    {
145        return mGroups.at(parent.row()).mChildCount;
146    }
147
148    return 0;
149}
150
151void EventModel::clearModel()
152{
153    for(int i = 0;i < mGroups.count();i++){
154        QModelIndex idx = index(i, 0);
155        Group group = mGroups[i];
156        beginRemoveRows(idx, 0, group.mChildCount - 1);
157        /*bool ok =*/ removeRows(0, group.mChildCount, idx);
158        endRemoveRows();
159        //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok;
160    }
161    mEvents.clear();
162}
163
164void EventModel::loadEvents(const QDate &aDate, int aConferenceId)
165{
166    clearModel();
167    // check for existence of the conference in the DB
168    if(Conference::getAll().count())
169    {
170        qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
171        mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
172    }
173    createTimeGroups();
174}
175
176void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
177{
178    clearModel();
179    // check for existence of the conference in the DB
180    if(Conference::getAll().count())
181    {
182        qDebug() << "Loading FAV Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
183        mEvents = Event::getFavByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId);
184    }
185    createTimeGroups();
186}
187
188int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId)
189{
190    clearModel();
191    // check for existence of the conference in the DB
192    if(Conference::getAll().count())
193    {
194        qDebug() << "Loading search result Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
195        try{
196            mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
197        }
198        catch( OrmException &e  ){
199            qDebug() << "Event::getSearchResultByDate failed: " << e.text();
200        }
201        catch(...){
202            qDebug() << "Event::getSearchResultByDate failed";
203        }
204
205    }
206
207    createTimeGroups();
208
209    return mEvents.count();
210}
211
212void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId)
213{
214    clearModel();
215    if(Conference::getAll().count())
216    {
217        qDebug() << "Loading Conference Data (by Track): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
218        mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start");
219    }
220    createTrackGroups();
221}
222
223void EventModel::loadNowEvents(int aConferenceId)
224{
225    clearModel();
226    // check for existence of the conference in the DB
227    if(Conference::getAll().count())
228    {
229        qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] scheduled NOW";
230        mEvents = Event::nowEvents(aConferenceId, "start");
231    }
232    createTimeGroups();
233}
234
235void EventModel::updateModel(int aEventId)
236{
237    for(int i=0; i<mEvents.count(); i++)
238    {
239        if(mEvents[i].id() == aEventId)
240            mEvents[i] = Event::getById(aEventId,AppSettings::confId());
241    }
242
243    // find the ModelIndex for given aEventId
244    for(int i=0; i<mGroups.count(); i++)
245    {
246        QModelIndex groupIndex = index(i,0,QModelIndex());
247        for(int j=0; j<mGroups[i].mChildCount; j++)
248        {
249            QModelIndex eventIndex = index(j,0,groupIndex);
250            if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
251            {
252                emit(dataChanged(eventIndex,eventIndex));
253            }
254        }
255    }
256}
257
Note: See TracBrowser for help on using the repository browser.