[d0d0a66] | 1 | #include "eventmodel.h" |
---|
[c718a77] | 2 | #include <appsettings.h> |
---|
[69393c0] | 3 | #include <conference.h> |
---|
[4693fa6] | 4 | #include <track.h> |
---|
[d1fb9ee] | 5 | |
---|
[72cd3af] | 6 | const QString EventModel::COMMA_SEPARATOR = ", "; |
---|
| 7 | |
---|
[69393c0] | 8 | EventModel::EventModel() |
---|
[d0d0a66] | 9 | { |
---|
[969a840] | 10 | mEvents.clear(); |
---|
[d0d0a66] | 11 | } |
---|
| 12 | |
---|
| 13 | void 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 | |
---|
[a35aa83] | 34 | if (nextGroupTime <= eventTime) |
---|
[d0d0a66] | 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 | |
---|
[4693fa6] | 49 | void EventModel::createTrackGroups() { |
---|
[f6300c7] | 50 | mGroups.clear(); |
---|
| 51 | mParents.clear(); |
---|
| 52 | if (mEvents.empty()) |
---|
| 53 | { |
---|
| 54 | return; |
---|
| 55 | } |
---|
[4693fa6] | 56 | int trackId = mEvents.first().trackId(); |
---|
[9208bdb] | 57 | |
---|
[4693fa6] | 58 | mGroups << EventModel::Group(Track::getTrackName(trackId), 0); |
---|
| 59 | int nextTrackId = trackId; |
---|
[f6300c7] | 60 | |
---|
| 61 | for (int i=0; i<mEvents.count(); i++) |
---|
| 62 | { |
---|
[4693fa6] | 63 | trackId = mEvents.at(i).trackId(); |
---|
| 64 | if (nextTrackId != trackId) |
---|
[f6300c7] | 65 | { |
---|
| 66 | mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex; |
---|
[4693fa6] | 67 | mGroups << EventModel::Group(Track::getTrackName(trackId), i); |
---|
| 68 | nextTrackId = trackId; |
---|
[f6300c7] | 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 | |
---|
[d0d0a66] | 76 | QVariant EventModel::data(const QModelIndex& index, int role) const |
---|
| 77 | { |
---|
| 78 | if (index.isValid() && role == Qt::DisplayRole) |
---|
| 79 | { |
---|
| 80 | if (index.internalId() == 0) |
---|
[5842349] | 81 | { |
---|
[d0d0a66] | 82 | return mGroups.at(index.row()).mTitle; |
---|
| 83 | } |
---|
[27102d5] | 84 | else //event data |
---|
[d0d0a66] | 85 | { |
---|
| 86 | return static_cast<Event*>(index.internalPointer())->id(); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | return QVariant(); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | QModelIndex 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 | |
---|
| 113 | QModelIndex 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 | |
---|
| 130 | int EventModel::columnCount(const QModelIndex & parent) const |
---|
| 131 | { |
---|
| 132 | Q_UNUSED(parent); |
---|
| 133 | return 1; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | int 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 | } |
---|
[72f6fe4] | 150 | |
---|
[f6300c7] | 151 | void EventModel::clearModel() |
---|
[72f6fe4] | 152 | { |
---|
[f6300c7] | 153 | for(int i = 0;i < mGroups.count();i++){ |
---|
| 154 | QModelIndex idx = index(i, 0); |
---|
[8035cef] | 155 | Group group = mGroups[i]; |
---|
[f6300c7] | 156 | beginRemoveRows(idx, 0, group.mChildCount - 1); |
---|
[59c6cfe] | 157 | /*bool ok =*/ removeRows(0, group.mChildCount, idx); |
---|
[8035cef] | 158 | endRemoveRows(); |
---|
| 159 | //qDebug() << "removing " << group.mChildCount << " events from group:" << i << idx.data() << ":" << ok; |
---|
| 160 | } |
---|
[72f6fe4] | 161 | mEvents.clear(); |
---|
[f6300c7] | 162 | } |
---|
[69393c0] | 163 | |
---|
[f6300c7] | 164 | void EventModel::loadEvents(const QDate &aDate, int aConferenceId) |
---|
| 165 | { |
---|
| 166 | clearModel(); |
---|
[969a840] | 167 | // check for existence of the conference in the DB |
---|
[69393c0] | 168 | if(Conference::getAll().count()) |
---|
| 169 | { |
---|
[969a840] | 170 | qDebug() << "Loading Conference Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate; |
---|
[96fe8fb] | 171 | mEvents = Event::getByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start"); |
---|
[69393c0] | 172 | } |
---|
[72f6fe4] | 173 | createTimeGroups(); |
---|
| 174 | } |
---|
| 175 | |
---|
[6f39595] | 176 | void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId) |
---|
| 177 | { |
---|
[f6300c7] | 178 | clearModel(); |
---|
[6f39595] | 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 | |
---|
[9d8946b] | 188 | int EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId) |
---|
[e662750] | 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; |
---|
[9d8946b] | 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 | |
---|
[e662750] | 205 | } |
---|
[9d8946b] | 206 | |
---|
[e662750] | 207 | createTimeGroups(); |
---|
[9d8946b] | 208 | |
---|
| 209 | return mEvents.count(); |
---|
[e662750] | 210 | } |
---|
| 211 | |
---|
[4693fa6] | 212 | void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId) |
---|
[f6300c7] | 213 | { |
---|
| 214 | clearModel(); |
---|
| 215 | if(Conference::getAll().count()) |
---|
[fbc1646] | 216 | { |
---|
[4693fa6] | 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"); |
---|
[fbc1646] | 219 | } |
---|
[4693fa6] | 220 | createTrackGroups(); |
---|
[f6300c7] | 221 | } |
---|
| 222 | |
---|
[c718a77] | 223 | void EventModel::updateModel(int aEventId) |
---|
[67c59a7] | 224 | { |
---|
[c718a77] | 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 | } |
---|
[67c59a7] | 244 | } |
---|
| 245 | |
---|