1 | #include "eventmodel.h" |
---|
2 | #include <conference.h> |
---|
3 | #include <track.h> |
---|
4 | |
---|
5 | const QString EventModel::COMMA_SEPARATOR = ", "; |
---|
6 | |
---|
7 | EventModel::EventModel() |
---|
8 | { |
---|
9 | mEvents.clear(); |
---|
10 | } |
---|
11 | |
---|
12 | void 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 | |
---|
48 | void 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 | |
---|
75 | QVariant 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 | |
---|
92 | QModelIndex 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 | |
---|
112 | QModelIndex 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 | |
---|
129 | int EventModel::columnCount(const QModelIndex & parent) const |
---|
130 | { |
---|
131 | Q_UNUSED(parent); |
---|
132 | return 1; |
---|
133 | } |
---|
134 | |
---|
135 | int 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 | |
---|
150 | void 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 | |
---|
163 | void 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, Event::START); |
---|
171 | } |
---|
172 | createTimeGroups(); |
---|
173 | } |
---|
174 | |
---|
175 | void 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 | |
---|
187 | void 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 | mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start"); |
---|
195 | } |
---|
196 | createTimeGroups(); |
---|
197 | } |
---|
198 | |
---|
199 | void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId) |
---|
200 | { |
---|
201 | clearModel(); |
---|
202 | if(Conference::getAll().count()) |
---|
203 | { |
---|
204 | qDebug() << "Loading Conference Data (by Track): [" << Conference::getById(aConferenceId).title() << "] " << aDate; |
---|
205 | mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "xid_track, start"); |
---|
206 | } |
---|
207 | createTrackGroups(); |
---|
208 | } |
---|
209 | |
---|
210 | void EventModel::emitDataChangedSignal(const QModelIndex &aTopLeft, const QModelIndex &aBottomRight) |
---|
211 | { |
---|
212 | emit(dataChanged(aTopLeft,aBottomRight)); |
---|
213 | } |
---|
214 | |
---|