-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistview.cpp
More file actions
90 lines (77 loc) · 3.07 KB
/
listview.cpp
File metadata and controls
90 lines (77 loc) · 3.07 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
#include "listview.h"
ListView::ListView(Item *parent, float x, float y, float width, float hieght): Flickable (parent, x, y, width, hieght)
{
mContentItem = add<Item>();
}
void ListView::setAdaptor(ListAdaptor *a) {
mAdaptor = a;
for (int i=0; i<a->count(); ++i) {
auto item = a->itemAt(i);
mContentItem->addChild(item);
performLayout();
//printf("%d %f %f\n", item->position(), mContentItem->hieght(), hieght());
if (mContentItem->children().back()->contains(0, hieght())) {
//mContentItem->hieght = a->count() * (mContentItem->hieght()/i);
break;
}
}
}
void ListView::onScrolled() {
float currentY = -mContentItem->y();
std::vector<Item *> toBeRemoved;
for (auto child : mContentItem->children()) {
if(!child->contains(0, currentY) && child->y() < currentY-100) {
toBeRemoved.push_back(child);
} else if(!child->contains(0, currentY + hieght() + 100) && child->y() > (currentY + hieght()+100)) {
toBeRemoved.push_back(child);
}
}
//printf("-> ListView::scrollEvent %f %d\n", currentY, toBeRemoved.size());
for (auto child : toBeRemoved) {
mContentItem->removeChild(child);
}
if(mContentItem->childCount() == 0) {
int pos = int(currentY + hieght()/2.0f)/mAdaptor->hieghtForitemAt(0);
auto item = mAdaptor->itemAt(pos);
item->y = pos*60;
mContentItem->addChild(0, item);
}
while(!mContentItem->children().front()->contains(0, currentY-100) && mContentItem->children().front()->y() > currentY-100) {
auto first = static_cast<ListItem*>(mContentItem->children().front());
if (first->position() > 0) {
auto item = mAdaptor->itemAt(first->position() - 1);
item->y = first->y() - item->hieght() - spacing();
mContentItem->addChild(0, item);
} else {
break;
}
}
while(!mContentItem->children().back()->contains(0, currentY + hieght() + 100) && mContentItem->children().back()->y() < (currentY + hieght()+100)) {
auto last = static_cast<ListItem*>(mContentItem->children().back());
if(last->position() < mAdaptor->count()-1) {
auto item = mAdaptor->itemAt(last->position() + 1);
mContentItem->addChild(item);
item->y = last->y() + last->hieght() + spacing();
} else {
break;
}
}
}
void ListView::performLayout() {
float yOffset = mContentItem->children().front()->y() + padding();
float maxWidth = 0;
for (auto child : mContentItem->children()) {
if (child->visible()) {
child->x = padding();
child->y = yOffset;
yOffset += child->hieght() + spacing();
if (child->width() > maxWidth)
maxWidth = child->width();
}
}
mContentItem->hieght = std::max(yOffset - spacing() + padding(), mAdaptor->count()*mAdaptor->hieghtForitemAt(0));
mContentItem->width = maxWidth + 2*padding();
}
void ListView::draw(NVGcontext *ctx) {
Flickable::draw(ctx);
}