-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazylistview.cpp
201 lines (164 loc) · 5.48 KB
/
lazylistview.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
192
193
194
195
196
197
198
199
200
201
/*
* LazyListView
* Copyright (C) 2015 Romeo Calota <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lazylistview.h"
#include <private/qquickflickable_p.h>
#include <private/qquickanchors_p.h>
#include <private/qquickanchors_p_p.h>
#include "itempool.h"
#include <QDebug>
#define CACHE_SIZE 20
QQuickFlickable::FlickableDirection convertFromOrientation(Qt::Orientation orientation);
Qt::Orientation convertToOrientation(QQuickFlickable::FlickableDirection direction);
LazyListView::LazyListView() :
m_flickable(new QQuickFlickable(this)),
m_cacheSize(CACHE_SIZE),
m_itemPool(new ItemPool()),
m_model(NULL)
{
// Anchor the QQuickFlickable to this item
QQuickAnchors * flickable_anchor = qvariant_cast<QQuickAnchors *>(m_flickable->property("anchors"));
flickable_anchor->setFill(this);
// Set the default orientation to vertical
m_flickable->setFlickableDirection(QQuickFlickable::VerticalFlick);
}
LazyListView::~LazyListView()
{
}
int LazyListView::getOrientation() const
{
return convertToOrientation(m_flickable->flickableDirection());
}
void LazyListView::setOrientation(int orientation)
{
QQuickFlickable::FlickableDirection direction = convertFromOrientation((Qt::Orientation)orientation);
if (direction != m_flickable->flickableDirection())
{
m_flickable->setFlickableDirection(direction);
// When orientation changes make sure to rearange all the visible items
rearangeListItems();
emit orientationChanged();
}
}
QQmlComponent *LazyListView::getDelegate() const
{
// Why would anyone do this??
return m_delegate;
}
void LazyListView::setDelegate(QQmlComponent *delegate)
{
m_delegate = delegate;
createListItems(delegate);
rearangeListItems();
}
QObject *LazyListView::getModel() const
{
return m_model;
}
void LazyListView::setModel(QObject *model)
{
if (model != m_model)
{
m_model = model;
qDebug() << "model:" << m_model;
emit modelChanged();
}
}
void LazyListView::createListItems(QQmlComponent *component)
{
// Clear previously cached items
m_itemPool->clear();
m_visibleItems.clear();
// Create m_cacheSize number of items from the delegate
// TODO: figure out how to use roleNames() ??
for (int i = 0; i < m_cacheSize; ++i)
{
// Create a new item within the item pool
// This might be exposing a bit too much info
// of something that is supposed to be internal info...
// you can do better then this...
m_itemPool->newItem(component);
QQuickItem *item = m_itemPool->getItem();
qDebug() << "Item at" << i << "is" << item;
item->setParent(m_flickable->contentItem());
item->setParentItem(m_flickable->contentItem());
m_visibleItems.append(item);
}
}
void LazyListView::rearangeListItems()
{
// Reset flickable to default values
m_flickable->setContentHeight(0);
m_flickable->setContentWidth(0);
int bufferSize = m_visibleItems.size();
for (int i = 0; i < bufferSize; ++i)
{
QQuickItem *item = m_visibleItems.at(i);
// Reset item position
item->setX(0);
item->setY(0);
// Based on orientation set the correct position for each item
if (m_flickable->flickableDirection() == QQuickFlickable::VerticalFlick)
{
// Stack them under each other
item->setY(i * item->height());
}
else
{
// Place them next to each other
item->setX(i * item->width());
}
}
// Set the proper content size based on orientation
if (m_flickable->flickableDirection() == QQuickFlickable::VerticalFlick)
{
m_flickable->setContentHeight(bufferSize * m_visibleItems.at(0)->height()); // What is no items are visible
}
else
{
m_flickable->setContentWidth(bufferSize * m_visibleItems.at(0)->width()); // What if no items are visible
}
}
/*
* Helper functions to convert between QQuickFlickable::FlickableDirection and Qt::Orientation
*/
inline QQuickFlickable::FlickableDirection convertFromOrientation(Qt::Orientation orientation)
{
switch (orientation)
{
case Qt::Vertical:
return QQuickFlickable::VerticalFlick;
case Qt::Horizontal:
return QQuickFlickable::HorizontalFlick;
}
return QQuickFlickable::VerticalFlick;
}
inline Qt::Orientation convertToOrientation(QQuickFlickable::FlickableDirection direction)
{
switch (direction)
{
default:
return Qt::Vertical;
case QQuickFlickable::AutoFlickDirection:
case QQuickFlickable::HorizontalAndVerticalFlick:
return Qt::Vertical;
case QQuickFlickable::VerticalFlick:
return Qt::Vertical;
case QQuickFlickable::HorizontalFlick:
return Qt::Horizontal;
}
return Qt::Vertical;
}