-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonthheadermodel.cpp
More file actions
127 lines (106 loc) · 3.05 KB
/
Copy pathmonthheadermodel.cpp
File metadata and controls
127 lines (106 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "monthheadermodel.h"
MonthHeaderModel::MonthHeaderModel(int year, DataBase::SScore* scores)
: m_scores(scores), m_year(year), m_height(0), m_width(0)
{
const QString strYear = QStringLiteral("%1").arg(year, 4, 10, QLatin1Char('0'));
m_start = QDate::fromString(strYear + "0101", "yyyyMMdd");
m_end = QDate::fromString(strYear + "1231", "yyyyMMdd");
if (m_scores != NULL)
{
m_table.reserve(4);
m_width = _build(*m_scores);
m_height = static_cast<int>(m_table.size());
}
}
int MonthHeaderModel::scoreId(int row, int col) const
{
return item(row, col).id;
}
int MonthHeaderModel::span(int row, int col) const
{
return item(row, col).span;
}
QString MonthHeaderModel::data(int row, int col) const
{
return item(row, col).name;
}
const MonthHeaderModel::SHeaderItem& MonthHeaderModel::item(int row, int col) const
{
if (m_height == 0)
return m_nullItem;
if (row >= m_height)
row = m_height - 1;
if (m_width == 0)
return m_nullItem;
if (col >= m_width)
col = m_width - 1;
while(true)
{
const int s = static_cast<int>(m_table[row].size());
if (row < 0 || (s != 0 && col < s))
break;
row -= 1;
}
if (row < 0)
return m_nullItem;
return m_table[row][col];
}
int MonthHeaderModel::columnCount() const
{
return m_width;
}
int MonthHeaderModel::rowCount() const
{
return m_height;
}
int MonthHeaderModel::_build(const DataBase::SScore& root, int lvl)
{
if (static_cast<int>(m_table.size() - 1) < lvl)
{
std::vector<SHeaderItem> temp;
temp.reserve(10);
m_table.push_back(temp);
}
int span = 0;
for (std::list<DataBase::SScore>::const_iterator it = root.child.begin(); it != root.child.end(); ++it)
{
const DataBase::SScore& score = *it;
if (score.removed.isValid() && score.removed < m_start && score.removed > m_end)
continue;
if (lvl != 0)
{
while(true)
{
const size_t col = m_table[lvl].size();
if (m_table[lvl - 1].size() > col)
{
if (!m_table[lvl - 1][col].hasChilds)
{
m_table[lvl].push_back(m_table[lvl - 1][col]);
continue;
}
}
break;
}
}
int local_span = 1;
const bool hasChilds = score.child.size() > 0;
if (hasChilds)
local_span = _build(score, lvl + 1);
else
m_headers.push_back(score.name);
SHeaderItem item;
item.id = score.id;
item.name = score.name;
item.hasChilds = hasChilds;
item.hasParent = (score.parent_id != -1);
item.removed = score.removed;
item.lvl = lvl;
item.span = local_span;
item.flags = score.flags;
for (int i = 0; i < item.span; ++i)
m_table[lvl].push_back(item);
span += local_span;
}
return span;
}