-
Notifications
You must be signed in to change notification settings - Fork 0
/
treeproxymodel.cpp
191 lines (165 loc) · 5.87 KB
/
treeproxymodel.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "treeproxymodel.h"
#include <QDate>
TreeProxyModel::TreeProxyModel(QAbstractItemModel* sourceModel, QObject *parent) :
QAbstractProxyModel(parent)
{
setSourceModel(sourceModel);
}
void TreeProxyModel::setSourceModel(QAbstractItemModel* model)
{
if (sourceModel()) {
disconnect(sourceModel(), SIGNAL(modelReset()), this, SLOT(sourceReset()));
disconnect(sourceModel(), SIGNAL(rowsInserted(QModelIndex, int, int)),
this, SLOT(sourceRowsInserted(QModelIndex,int,int)));
disconnect(sourceModel(), SIGNAL(rowsRemoved(QModelIndex, int, int)),
this, SLOT(sourceRowsRemoved(QModelIndex,int,int)));
}
QAbstractProxyModel::setSourceModel(model);
if (model) {
connect(sourceModel(), SIGNAL(modelReset()), this, SLOT(sourceReset()));
connect(sourceModel(), SIGNAL(rowsInserted(QModelIndex, int, int)),
this, SLOT(sourceRowsInserted(QModelIndex,int,int)));
connect(sourceModel(), SIGNAL(rowsRemoved(QModelIndex, int, int)),
this, SLOT(sourceRowsRemoved(QModelIndex,int,int)));
}
reset();
}
QVariant TreeProxyModel::data(const QModelIndex &proxyIndex, int role) const
{
if ((role == Qt::EditRole || role == Qt::DisplayRole)) {
int start = proxyIndex.internalId();
if (start == 0) {
int offset = sourceDateRow(proxyIndex.row());
if (proxyIndex.column() == 1) {
QModelIndex idx = sourceModel()->index(offset, 1);
QDate date = idx.data().toDate();
return date.toString();
}
if (proxyIndex.column() == 2) {
double value = 0.0;
if (proxyIndex.isValid()) {
int startIdx = sourceDateRow(proxyIndex.row());
int endIdx = sourceDateRow(proxyIndex.row()+1);
for (int i=startIdx; i<endIdx; ++i) {
value += sourceModel()->index(i, 2).data().toDouble();
}
}
return " Summe: " + QString::number(value);
}
}
}
return QAbstractProxyModel::data(proxyIndex, role);
}
int TreeProxyModel::columnCount(const QModelIndex &parent) const
{
return sourceModel()->columnCount(mapToSource(parent));
}
int TreeProxyModel::rowCount(const QModelIndex &parent) const
{
if( parent.internalId() != 0 || parent.column() > 0 || !sourceModel())
return 0;
// Liefert die Anzahl der rows für die Tage
if (!parent.isValid()) {
if (!sourceRowCache.isEmpty())
return sourceRowCache.count();
QDate currentDate;
int rows = 0;
int sourceRows = sourceModel()->rowCount();
for (int i = 0; i < sourceRows; ++i) {
/*
The new model change the type automatic.
QString rowDateString = sourceModel()->index(i, 1).data().toString();
QDate rowDate = QDate::fromString(rowDateString);
*/
QDate rowDate = sourceModel()->index(i, 1).data().toDate();
if (rowDate != currentDate) {
sourceRowCache.append(i);
currentDate = rowDate;
++rows;
}
}
return rows;
}
// Liefert die Anzahl der rows für einen Tag
int start = sourceDateRow(parent.row());
int end = sourceDateRow(parent.row()+1);
return (end-start);
}
/** Liefert zu einem Datum die Start-Row im Source-Modell. Damit nicht immer
neu die Startposition bestimmt werden muss, werden sie im sourceRowCache
abgelegt.
*/
int TreeProxyModel::sourceDateRow(int row) const
{
if (row <= 0)
return 0;
if (sourceRowCache.isEmpty())
return rowCount(QModelIndex());
if (row >= sourceRowCache.count()) {
if (!sourceModel())
return 0;
return sourceModel()->rowCount();
}
return sourceRowCache.at(row);
}
/** Liefert den ModelIndex des SourceModels zum ModelIndex des ProxyModels
*/
QModelIndex TreeProxyModel::mapToSource(const QModelIndex &proxyIndex) const
{
int offset = proxyIndex.internalId();
if (offset == 0)
return QModelIndex();
int startRow = sourceDateRow(offset-1);
return sourceModel()->index(startRow+proxyIndex.row(), proxyIndex.column());
}
QModelIndex TreeProxyModel::index(int row, int column, const QModelIndex &parent) const
{
if (row < 0 || column < 0 || column >= columnCount(parent) || parent.column() > 0)
return QModelIndex();
if (!parent.isValid())
return createIndex(row, column, 0);
return createIndex(row, column, parent.row()+1);
}
QModelIndex TreeProxyModel::parent(const QModelIndex &index) const
{
int offset = index.internalId();
if (offset == 0 || !index.isValid())
return QModelIndex();
return createIndex(offset-1, 0, 0);
}
bool TreeProxyModel::hasChildren(const QModelIndex &parent) const
{
QModelIndex grandparent = parent.parent();
if (!grandparent.isValid())
return true;
return false;
}
QModelIndex TreeProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
{
if (!sourceIndex.isValid())
return QModelIndex();
if (sourceRowCache.isEmpty())
rowCount(QModelIndex());
QList<int>::iterator it;
it = qLowerBound(sourceRowCache.begin(), sourceRowCache.end(), sourceIndex.row());
if (*it != sourceIndex.row())
--it;
int dateRow = qMax(0, it - sourceRowCache.begin());
int row = sourceIndex.row() - sourceRowCache.at(dateRow);
return createIndex(row, sourceIndex.column(), dateRow + 1);
}
void TreeProxyModel::sourceReset()
{
sourceRowCache.clear();
reset();
}
void TreeProxyModel::sourceRowsInserted(const QModelIndex &parent, int start, int end)
{
sourceRowCache.clear();
reset();
}
void TreeProxyModel::sourceRowsRemoved(const QModelIndex &parent, int start, int end)
{
sourceRowCache.clear();
reset();
}