source: src/mvc/eventmodel.cpp @ d336730

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

EventModel? signaling changed

  • if some of the data (favourite,alarm) has changed on the event, signal 'eventHasChanged' is emitted
  • all treeViews (eg. DayView?, FavsView?, TracksView?, ...) have to listen on this signal

Only favouritiesView is 'reset' when current tab is changed in mainWindow

  • 'cause time groupings have to be recreated, since favs may have changed
  • Property mode set to 100644
File size: 6.7 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::updateModel(int aEventId)
224{
225    for(int i=0; i<mEvents.count(); i++)
226    {
227        if(mEvents[i].id() == aEventId)
228            mEvents[i] = Event::getById(aEventId,AppSettings::confId());
229    }
230
231    // find the ModelIndex for given aEventId
232    for(int i=0; i<mGroups.count(); i++)
233    {
234        QModelIndex groupIndex = index(i,0,QModelIndex());
235        for(int j=0; j<mGroups[i].mChildCount; j++)
236        {
237            QModelIndex eventIndex = index(j,0,groupIndex);
238            if(static_cast<Event*>(eventIndex.internalPointer())->id() == aEventId)
239            {
240                emit(dataChanged(eventIndex,eventIndex));
241            }
242        }
243    }
244}
245
Note: See TracBrowser for help on using the repository browser.