source: src/mvc/eventmodel.cpp @ 1735f55

qt5
Last change on this file since 1735f55 was 9d8946b, checked in by timkoma <timkoma@…>, 12 years ago

update for the search

  • Property mode set to 100644
File size: 6.2 KB
Line 
1#include "eventmodel.h"
2#include <conference.h>
3#include <track.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::createTrackGroups() {
49    mGroups.clear();
50    mParents.clear();
51    if (mEvents.empty())
52    {
53        return;
54    }
55    int trackId = mEvents.first().trackId();
56
57    mGroups << EventModel::Group(Track::getTrackName(trackId), 0);
58    int nextTrackId = trackId;
59
60    for (int i=0; i<mEvents.count(); i++)
61    {
62        trackId = mEvents.at(i).trackId();
63        if (nextTrackId != trackId)
64        {
65            mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex;
66            mGroups << EventModel::Group(Track::getTrackName(trackId), i);
67            nextTrackId = trackId;
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, "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
187int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId)
188{
189    clearModel();
190    // check for existence of the conference in the DB
191    if(Conference::getAll().count())
192    {
193        qDebug() << "Loading search result Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
194        try{
195            mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
196        }
197        catch( OrmException &e  ){
198            qDebug() << "Event::getSearchResultByDate failed: " << e.text();
199        }
200        catch(...){
201            qDebug() << "Event::getSearchResultByDate failed";
202        }
203
204    }
205
206    createTimeGroups();
207
208    return mEvents.count();
209}
210
211void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId)
212{
213    clearModel();
214    if(Conference::getAll().count())
215    {
216        qDebug() << "Loading Conference Data (by Track): [" << Conference::getById(aConferenceId).title() << "] " << aDate;
217        mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start");
218    }
219    createTrackGroups();
220}
221
222void EventModel::emitDataChangedSignal(const QModelIndex &aTopLeft, const QModelIndex &aBottomRight)
223{
224    emit(dataChanged(aTopLeft,aBottomRight));
225}
226
Note: See TracBrowser for help on using the repository browser.