on xenial help sign in
Implement external cache buffer setting
4559c4aAlbert Astals
plugins/Dash/listviewwithpageheader.cpp+19−7
@@ -104,8 +104,6 @@
104#pragma GCC diagnostic pop
104#pragma GCC diagnostic pop
105// #include <private/qquickrectangle_p.h>
105// #include <private/qquickrectangle_p.h>
106
106
107−static const qreal bufferRatio = 0.5;
108−
109qreal ListViewWithPageHeader::ListItem::height() const
107qreal ListViewWithPageHeader::ListItem::height() const
110{
108{
111 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
109 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
@@ -158,6 +156,7 @@ ListViewWithPageHeader::ListViewWithPageHeader()
158 , m_forceNoClip(false)
156 , m_forceNoClip(false)
159 , m_inLayout(false)
157 , m_inLayout(false)
160 , m_inContentHeightKeepHeaderShown(false)
158 , m_inContentHeightKeepHeaderShown(false)
159+ , m_cacheBuffer(0)
161{
160{
162 m_clipItem = new QQuickItem(contentItem());
161 m_clipItem = new QQuickItem(contentItem());
163// m_clipItem = new QQuickRectangle(contentItem());
162// m_clipItem = new QQuickRectangle(contentItem());
@@ -335,6 +334,20 @@ qreal ListViewWithPageHeader::headerItemShownHeight() const
335 return m_headerItemShownHeight;
334 return m_headerItemShownHeight;
336}
335}
337
336
337+qreal ListViewWithPageHeader::cacheBuffer() const
338+{
339+ return m_cacheBuffer;
340+}
341+
342+void ListViewWithPageHeader::setCacheBuffer(qreal cacheBuffer)
343+{
344+ if (cacheBuffer != m_cacheBuffer) {
345+ m_cacheBuffer = cacheBuffer;
346+ Q_EMIT cacheBufferChanged();
347+ polish();
348+ }
349+}
350+
338void ListViewWithPageHeader::positionAtBeginning()
351void ListViewWithPageHeader::positionAtBeginning()
339{
352{
340 if (m_delegateModel->count() <= 0)
353 if (m_delegateModel->count() <= 0)
@@ -356,8 +369,7 @@ void ListViewWithPageHeader::positionAtBeginning()
356 // Create the subsequent items
369 // Create the subsequent items
357 int modelIndex = 1;
370 int modelIndex = 1;
358 qreal pos = item->y() + item->height();
371 qreal pos = item->y() + item->height();
359− const qreal buffer = height() * bufferRatio;
360− const qreal bufferTo = height() + buffer;
372+ const qreal bufferTo = height() + m_cacheBuffer;
361 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
373 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
362 if (!(item = createItem(modelIndex, false)))
374 if (!(item = createItem(modelIndex, false)))
363 break;
375 break;
@@ -593,11 +605,11 @@ void ListViewWithPageHeader::refill()
593 return;
605 return;
594 }
606 }
595
607
596− const qreal buffer = height() * bufferRatio;
597 const qreal from = contentY();
608 const qreal from = contentY();
598 const qreal to = from + height();
609 const qreal to = from + height();
599− const qreal bufferFrom = from - buffer;
610+ const qreal bufferFrom = from - m_cacheBuffer;
600− const qreal bufferTo = to + buffer;
611+ const qreal bufferTo = to + m_cacheBuffer;
612+// qDebug() << from << to << bufferFrom << bufferTo << m_cacheBuffer;
601
613
602 bool added = addVisibleItems(from, to, false);
614 bool added = addVisibleItems(from, to, false);
603 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
615 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
plugins/Dash/listviewwithpageheader.h+6
@@ -54,6 +54,7 @@ class ListViewWithPageHeader : public QQuickFlickable, public QQuickItemChangeLi
54 Q_PROPERTY(bool forceNoClip READ forceNoClip WRITE setForceNoClip NOTIFY forceNoClipChanged)
54 Q_PROPERTY(bool forceNoClip READ forceNoClip WRITE setForceNoClip NOTIFY forceNoClipChanged)
55 Q_PROPERTY(int stickyHeaderHeight READ stickyHeaderHeight NOTIFY stickyHeaderHeightChanged)
55 Q_PROPERTY(int stickyHeaderHeight READ stickyHeaderHeight NOTIFY stickyHeaderHeightChanged)
56 Q_PROPERTY(qreal headerItemShownHeight READ headerItemShownHeight NOTIFY headerItemShownHeightChanged)
56 Q_PROPERTY(qreal headerItemShownHeight READ headerItemShownHeight NOTIFY headerItemShownHeightChanged)
57+ Q_PROPERTY(int cacheBuffer READ cacheBuffer WRITE setCacheBuffer NOTIFY cacheBufferChanged)
57
58
58 friend class ListViewWithPageHeaderTest;
59 friend class ListViewWithPageHeaderTest;
59 friend class ListViewWithPageHeaderTestSection;
60 friend class ListViewWithPageHeaderTestSection;
@@ -84,6 +85,9 @@ public:
84 int stickyHeaderHeight() const;
85 int stickyHeaderHeight() const;
85 qreal headerItemShownHeight() const;
86 qreal headerItemShownHeight() const;
86
87
88+ qreal cacheBuffer() const;
89+ void setCacheBuffer(qreal cacheBuffer);
90+
87 Q_INVOKABLE void positionAtBeginning();
91 Q_INVOKABLE void positionAtBeginning();
88 Q_INVOKABLE void showHeader();
92 Q_INVOKABLE void showHeader();
89 Q_INVOKABLE QQuickItem *item(int modelIndex) const;
93 Q_INVOKABLE QQuickItem *item(int modelIndex) const;
@@ -103,6 +107,7 @@ Q_SIGNALS:
103 void forceNoClipChanged();
107 void forceNoClipChanged();
104 void stickyHeaderHeightChanged();
108 void stickyHeaderHeightChanged();
105 void headerItemShownHeightChanged();
109 void headerItemShownHeightChanged();
110+ void cacheBufferChanged();
106
111
107protected:
112protected:
108 void componentComplete() override;
113 void componentComplete() override;
@@ -197,6 +202,7 @@ private:
197 bool m_forceNoClip;
202 bool m_forceNoClip;
198 bool m_inLayout;
203 bool m_inLayout;
199 bool m_inContentHeightKeepHeaderShown;
204 bool m_inContentHeightKeepHeaderShown;
205+ qreal m_cacheBuffer;
200
206
201 // Qt 5.0 doesn't like releasing the items just after itemCreated
207 // Qt 5.0 doesn't like releasing the items just after itemCreated
202 // so we delay the releasing until the next updatePolish
208 // so we delay the releasing until the next updatePolish
tests/plugins/Dash/listviewwithpageheadertest.cpp+39
@@ -28,6 +28,8 @@
28#include <private/qquickitem_p.h>
28#include <private/qquickitem_p.h>
29#pragma GCC diagnostic pop
29#pragma GCC diagnostic pop
30
30
31+#include <limits>
32+
31// TODO Think on how doing a test for lost items
33// TODO Think on how doing a test for lost items
32// particullary making sure that lost items are culled
34// particullary making sure that lost items are culled
33// and then removed in the next updatePolish cycle
35// and then removed in the next updatePolish cycle
@@ -1899,6 +1901,43 @@ private Q_SLOTS:
1899 QTRY_COMPARE(lvwph->m_firstVisibleIndex, 0);
1901 QTRY_COMPARE(lvwph->m_firstVisibleIndex, 0);
1900 }
1902 }
1901
1903
1904+ void testNoCacheBuffer()
1905+ {
1906+ lvwph->setCacheBuffer(0);
1907+ QTRY_COMPARE(lvwph->m_visibleItems.count(), 3);
1908+ QCOMPARE(lvwph->m_firstVisibleIndex, 0);
1909+ verifyItem(0, 50., 150., false);
1910+ verifyItem(1, 200., 200., false);
1911+ verifyItem(2, 400., 350., false);
1912+ QCOMPARE(lvwph->m_minYExtent, 0.);
1913+ QCOMPARE(lvwph->m_clipItem->y(), 0.);
1914+ QCOMPARE(lvwph->m_clipItem->clip(), false);
1915+ QCOMPARE(lvwph->m_headerItem->y(), 0.);
1916+ QCOMPARE(lvwph->m_headerItem->height(), 50.);
1917+ QCOMPARE(lvwph->contentY(), 0.);
1918+ QCOMPARE(lvwph->m_headerItemShownHeight, 0.);
1919+ }
1920+
1921+ void testAllCacheBuffer()
1922+ {
1923+ lvwph->setCacheBuffer(std::numeric_limits<qreal>::max());
1924+ QTRY_COMPARE(lvwph->m_visibleItems.count(), 6);
1925+ QCOMPARE(lvwph->m_firstVisibleIndex, 0);
1926+ verifyItem(0, 50., 150., false);
1927+ verifyItem(1, 200., 200., false);
1928+ verifyItem(2, 400., 350., false);
1929+ verifyItem(3, 750., 350., true);
1930+ verifyItem(4, 1100., 350., true);
1931+ verifyItem(5, 1450., 350., true);
1932+ QCOMPARE(lvwph->m_minYExtent, 0.);
1933+ QCOMPARE(lvwph->m_clipItem->y(), 0.);
1934+ QCOMPARE(lvwph->m_clipItem->clip(), false);
1935+ QCOMPARE(lvwph->m_headerItem->y(), 0.);
1936+ QCOMPARE(lvwph->m_headerItem->height(), 50.);
1937+ QCOMPARE(lvwph->contentY(), 0.);
1938+ QCOMPARE(lvwph->m_headerItemShownHeight, 0.);
1939+ }
1940+
1902private:
1941private:
1903 QQuickView *view;
1942 QQuickView *view;
1904 ListViewWithPageHeader *lvwph;
1943 ListViewWithPageHeader *lvwph;
tests/plugins/Dash/listviewwithpageheadertest.qml+1
@@ -60,6 +60,7 @@ Rectangle {
60 anchors.top: parent.top
60 anchors.top: parent.top
61 anchors.bottom: parent.bottom
61 anchors.bottom: parent.bottom
62 model: model
62 model: model
63+ cacheBuffer: height * 0.5
63 delegate: Rectangle {
64 delegate: Rectangle {
64 property bool timerDone: false
65 property bool timerDone: false
65 width: parent.width - 20
66 width: parent.width - 20
tests/plugins/Dash/listviewwithpageheadertestsection.qml+1
@@ -60,6 +60,7 @@ Rectangle {
60 anchors.top: parent.top
60 anchors.top: parent.top
61 anchors.bottom: parent.bottom
61 anchors.bottom: parent.bottom
62 model: model
62 model: model
63+ cacheBuffer: height * 0.5
63 delegate: Rectangle {
64 delegate: Rectangle {
64 property bool timerDone: false
65 property bool timerDone: false
65 width: parent.width - 20
66 width: parent.width - 20
tests/plugins/Dash/listviewwithpageheadertestsectionexternalmodel.qml+1
@@ -28,6 +28,7 @@ Rectangle {
28 anchors.top: parent.top
28 anchors.top: parent.top
29 anchors.bottom: parent.bottom
29 anchors.bottom: parent.bottom
30 model: model
30 model: model
31+ cacheBuffer: height * 0.5
31 delegate: Rectangle {
32 delegate: Rectangle {
32 property bool timerDone: false
33 property bool timerDone: false
33 width: parent.width - 20
34 width: parent.width - 20