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