1 | #include "eventmodel.h" |
---|
2 | #include <conference.h> |
---|
3 | |
---|
4 | EventModel::EventModel() |
---|
5 | { |
---|
6 | |
---|
7 | loadEvents(); |
---|
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 | QVariant EventModel::data(const QModelIndex& index, int role) const |
---|
47 | { |
---|
48 | if (index.isValid() && role == Qt::DisplayRole) |
---|
49 | { |
---|
50 | if (index.internalId() == 0) |
---|
51 | { |
---|
52 | return mGroups.at(index.row()).mTitle; |
---|
53 | } |
---|
54 | else |
---|
55 | { |
---|
56 | return static_cast<Event*>(index.internalPointer())->id(); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | return QVariant(); |
---|
61 | } |
---|
62 | |
---|
63 | QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const |
---|
64 | { |
---|
65 | // TODO: add checks for out of range rows |
---|
66 | |
---|
67 | if (!parent.isValid()) |
---|
68 | { |
---|
69 | return createIndex(row, column, 0); |
---|
70 | } |
---|
71 | else if (parent.internalId() == 0) |
---|
72 | { |
---|
73 | const Group& group = mGroups.at(parent.row()); |
---|
74 | Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex)); |
---|
75 | return createIndex(row, column, reinterpret_cast<void*>(event)); |
---|
76 | } |
---|
77 | else |
---|
78 | { |
---|
79 | return QModelIndex(); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | QModelIndex EventModel::parent(const QModelIndex & index) const |
---|
84 | { |
---|
85 | if (index.isValid()) |
---|
86 | { |
---|
87 | if (index.internalId() == 0) |
---|
88 | { |
---|
89 | return QModelIndex(); |
---|
90 | } |
---|
91 | |
---|
92 | Event * event = static_cast<Event*>(index.internalPointer()); |
---|
93 | |
---|
94 | return createIndex(mParents[event->id()], 0, 0); |
---|
95 | } |
---|
96 | |
---|
97 | return QModelIndex(); |
---|
98 | } |
---|
99 | |
---|
100 | int EventModel::columnCount(const QModelIndex & parent) const |
---|
101 | { |
---|
102 | Q_UNUSED(parent); |
---|
103 | return 1; |
---|
104 | } |
---|
105 | |
---|
106 | int EventModel::rowCount (const QModelIndex & parent) const |
---|
107 | { |
---|
108 | if (!parent.isValid()) |
---|
109 | { |
---|
110 | return mGroups.count(); |
---|
111 | } |
---|
112 | |
---|
113 | if (parent.internalId() == 0) |
---|
114 | { |
---|
115 | return mGroups.at(parent.row()).mChildCount; |
---|
116 | } |
---|
117 | |
---|
118 | return 0; |
---|
119 | } |
---|
120 | |
---|
121 | void EventModel::loadEvents() |
---|
122 | { |
---|
123 | mEvents.clear(); |
---|
124 | |
---|
125 | mConfId = 1; // current conference selected: we have only one DB so far |
---|
126 | // check for existence of conference in the DB |
---|
127 | if(Conference::getAll().count()) |
---|
128 | { |
---|
129 | mCurrentDate = Conference::getById(mConfId).start(); |
---|
130 | qDebug() << "Loading Conference Data: [" << Conference::getById(mConfId).title() << "] " << mCurrentDate; |
---|
131 | mEvents = Event::getByDate(QDate(mCurrentDate.year(), mCurrentDate.month(), mCurrentDate.day()), mConfId); |
---|
132 | } |
---|
133 | mEvents = Event::getByDate(QDate(mCurrentDate.year(), mCurrentDate.month(), mCurrentDate.day()), mConfId); |
---|
134 | createTimeGroups(); |
---|
135 | } |
---|
136 | |
---|