1 | #include "eventmodel.h" |
---|
2 | |
---|
3 | EventModel::EventModel() : |
---|
4 | mEvents(Event::getByDate(QDate(2009, 2, 7), 1)) |
---|
5 | { |
---|
6 | createTimeGroups(); |
---|
7 | } |
---|
8 | |
---|
9 | void EventModel::createTimeGroups() |
---|
10 | { |
---|
11 | mGroups.clear(); |
---|
12 | mParents.clear(); |
---|
13 | |
---|
14 | if (mEvents.empty()) |
---|
15 | { |
---|
16 | return; |
---|
17 | } |
---|
18 | |
---|
19 | const int timeSpan = 5400; |
---|
20 | |
---|
21 | QTime startTime = mEvents.first().start().time(); |
---|
22 | mGroups << EventModel::Group(QString("%1 - %2").arg(startTime.toString("HH:mm"), |
---|
23 | startTime.addSecs(timeSpan).toString("HH:mm")), 0); |
---|
24 | QTime nextGroupTime = mEvents.first().start().time().addSecs(timeSpan); |
---|
25 | |
---|
26 | for (int i=0; i<mEvents.count(); i++) |
---|
27 | { |
---|
28 | QTime eventTime = mEvents.at(i).start().time(); |
---|
29 | |
---|
30 | if (nextGroupTime < eventTime) |
---|
31 | { |
---|
32 | mGroups.last().mChildCount = i - mGroups.last().mFirstEventIndex; |
---|
33 | mGroups << EventModel::Group(QString("%1 - %2").arg(nextGroupTime.toString("HH:mm"), |
---|
34 | nextGroupTime.addSecs(timeSpan).toString("HH:mm")), i); |
---|
35 | nextGroupTime = nextGroupTime.addSecs(timeSpan); |
---|
36 | } |
---|
37 | |
---|
38 | // add parent-child relation |
---|
39 | mParents[mEvents.at(i).id()] = mGroups.count() - 1; |
---|
40 | } |
---|
41 | |
---|
42 | mGroups.last().mChildCount = mEvents.count() - mGroups.last().mFirstEventIndex; |
---|
43 | } |
---|
44 | |
---|
45 | QVariant EventModel::data(const QModelIndex& index, int role) const |
---|
46 | { |
---|
47 | if (index.isValid() && role == Qt::DisplayRole) |
---|
48 | { |
---|
49 | if (index.internalId() == 0) |
---|
50 | { |
---|
51 | return mGroups.at(index.row()).mTitle; |
---|
52 | } |
---|
53 | else |
---|
54 | { |
---|
55 | return static_cast<Event*>(index.internalPointer())->id(); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | return QVariant(); |
---|
60 | } |
---|
61 | |
---|
62 | QModelIndex EventModel::index(int row, int column, const QModelIndex& parent) const |
---|
63 | { |
---|
64 | // TODO: add checks for out of range rows |
---|
65 | |
---|
66 | if (!parent.isValid()) |
---|
67 | { |
---|
68 | return createIndex(row, column, 0); |
---|
69 | } |
---|
70 | else if (parent.internalId() == 0) |
---|
71 | { |
---|
72 | const Group& group = mGroups.at(parent.row()); |
---|
73 | Event* event = const_cast<Event*>(&mEvents.at(row + group.mFirstEventIndex)); |
---|
74 | return createIndex(row, column, reinterpret_cast<void*>(event)); |
---|
75 | } |
---|
76 | else |
---|
77 | { |
---|
78 | return QModelIndex(); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | QModelIndex EventModel::parent(const QModelIndex & index) const |
---|
83 | { |
---|
84 | if (index.isValid()) |
---|
85 | { |
---|
86 | if (index.internalId() == 0) |
---|
87 | { |
---|
88 | return QModelIndex(); |
---|
89 | } |
---|
90 | |
---|
91 | Event * event = static_cast<Event*>(index.internalPointer()); |
---|
92 | |
---|
93 | return createIndex(mParents[event->id()], 0, 0); |
---|
94 | } |
---|
95 | |
---|
96 | return QModelIndex(); |
---|
97 | } |
---|
98 | |
---|
99 | int EventModel::columnCount(const QModelIndex & parent) const |
---|
100 | { |
---|
101 | Q_UNUSED(parent); |
---|
102 | return 1; |
---|
103 | } |
---|
104 | |
---|
105 | int EventModel::rowCount (const QModelIndex & parent) const |
---|
106 | { |
---|
107 | if (!parent.isValid()) |
---|
108 | { |
---|
109 | return mGroups.count(); |
---|
110 | } |
---|
111 | |
---|
112 | if (parent.internalId() == 0) |
---|
113 | { |
---|
114 | return mGroups.at(parent.row()).mChildCount; |
---|
115 | } |
---|
116 | |
---|
117 | return 0; |
---|
118 | } |
---|