on xenial help sign in
Implement external cache buffer setting
commit4559c4ae472eddb851edc89dd5ae8a49c901c3f1 authorAlbert Astals <albert.astals@canonical.com> dateTue 26 Aug 2014, 10:41 (12 years ago) parent770382b
6 files changed+67−7
plugins/Dash/listviewwithpageheader.cpp+19−7 plugins/Dash/listviewwithpageheader.h+6 tests/plugins/Dash/listviewwithpageheadertest.cpp+39 tests/plugins/Dash/listviewwithpageheadertest.qml+1 tests/plugins/Dash/listviewwithpageheadertestsection.qml+1 tests/plugins/Dash/listviewwithpageheadertestsectionexternalmodel.qml+1
plugins/Dash/listviewwithpageheader.cpp+19−7
@@ -104,8 +104,6 @@
104104#pragma GCC diagnostic pop
105105// #include <private/qquickrectangle_p.h>
106106
107−static const qreal bufferRatio = 0.5;
108−
109107qreal ListViewWithPageHeader::ListItem::height() const
110108{
111109 return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
@@ -158,6 +156,7 @@ ListViewWithPageHeader::ListViewWithPageHeader()
158156 , m_forceNoClip(false)
159157 , m_inLayout(false)
160158 , m_inContentHeightKeepHeaderShown(false)
159+ , m_cacheBuffer(0)
161160{
162161 m_clipItem = new QQuickItem(contentItem());
163162// m_clipItem = new QQuickRectangle(contentItem());
@@ -335,6 +334,20 @@ qreal ListViewWithPageHeader::headerItemShownHeight() const
335334 return m_headerItemShownHeight;
336335}
337336
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+
338351void ListViewWithPageHeader::positionAtBeginning()
339352{
340353 if (m_delegateModel->count() <= 0)
@@ -356,8 +369,7 @@ void ListViewWithPageHeader::positionAtBeginning()
356369 // Create the subsequent items
357370 int modelIndex = 1;
358371 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;
361373 while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
362374 if (!(item = createItem(modelIndex, false)))
363375 break;
@@ -593,11 +605,11 @@ void ListViewWithPageHeader::refill()
593605 return;
594606 }
595607
596− const qreal buffer = height() * bufferRatio;
597608 const qreal from = contentY();
598609 const qreal to = from + height();
599− const qreal bufferFrom = from - buffer;
600− const qreal bufferTo = to + buffer;
610+ const qreal bufferFrom = from - m_cacheBuffer;
611+ const qreal bufferTo = to + m_cacheBuffer;
612+// qDebug() << from << to << bufferFrom << bufferTo << m_cacheBuffer;
601613
602614 bool added = addVisibleItems(from, to, false);
603615 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
plugins/Dash/listviewwithpageheader.h+6
@@ -54,6 +54,7 @@ class ListViewWithPageHeader : public QQuickFlickable, public QQuickItemChangeLi
5454 Q_PROPERTY(bool forceNoClip READ forceNoClip WRITE setForceNoClip NOTIFY forceNoClipChanged)
5555 Q_PROPERTY(int stickyHeaderHeight READ stickyHeaderHeight NOTIFY stickyHeaderHeightChanged)
5656 Q_PROPERTY(qreal headerItemShownHeight READ headerItemShownHeight NOTIFY headerItemShownHeightChanged)
57+ Q_PROPERTY(int cacheBuffer READ cacheBuffer WRITE setCacheBuffer NOTIFY cacheBufferChanged)
5758
5859 friend class ListViewWithPageHeaderTest;
5960 friend class ListViewWithPageHeaderTestSection;
@@ -84,6 +85,9 @@ public:
8485 int stickyHeaderHeight() const;
8586 qreal headerItemShownHeight() const;
8687
88+ qreal cacheBuffer() const;
89+ void setCacheBuffer(qreal cacheBuffer);
90+
8791 Q_INVOKABLE void positionAtBeginning();
8892 Q_INVOKABLE void showHeader();
8993 Q_INVOKABLE QQuickItem *item(int modelIndex) const;
@@ -103,6 +107,7 @@ Q_SIGNALS:
103107 void forceNoClipChanged();
104108 void stickyHeaderHeightChanged();
105109 void headerItemShownHeightChanged();
110+ void cacheBufferChanged();
106111
107112protected:
108113 void componentComplete() override;
@@ -197,6 +202,7 @@ private:
197202 bool m_forceNoClip;
198203 bool m_inLayout;
199204 bool m_inContentHeightKeepHeaderShown;
205+ qreal m_cacheBuffer;
200206
201207 // Qt 5.0 doesn't like releasing the items just after itemCreated
202208 // so we delay the releasing until the next updatePolish
tests/plugins/Dash/listviewwithpageheadertest.cpp+39
@@ -28,6 +28,8 @@
2828#include <private/qquickitem_p.h>
2929#pragma GCC diagnostic pop
3030
31+#include <limits>
32+
3133// TODO Think on how doing a test for lost items
3234// particullary making sure that lost items are culled
3335// and then removed in the next updatePolish cycle
@@ -1899,6 +1901,43 @@ private Q_SLOTS:
18991901 QTRY_COMPARE(lvwph->m_firstVisibleIndex, 0);
19001902 }
19011903
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+
19021941private:
19031942 QQuickView *view;
19041943 ListViewWithPageHeader *lvwph;
tests/plugins/Dash/listviewwithpageheadertest.qml+1
@@ -60,6 +60,7 @@ Rectangle {
6060 anchors.top: parent.top
6161 anchors.bottom: parent.bottom
6262 model: model
63+ cacheBuffer: height * 0.5
6364 delegate: Rectangle {
6465 property bool timerDone: false
6566 width: parent.width - 20
tests/plugins/Dash/listviewwithpageheadertestsection.qml+1
@@ -60,6 +60,7 @@ Rectangle {
6060 anchors.top: parent.top
6161 anchors.bottom: parent.bottom
6262 model: model
63+ cacheBuffer: height * 0.5
6364 delegate: Rectangle {
6465 property bool timerDone: false
6566 width: parent.width - 20
tests/plugins/Dash/listviewwithpageheadertestsectionexternalmodel.qml+1
@@ -28,6 +28,7 @@ Rectangle {
2828 anchors.top: parent.top
2929 anchors.bottom: parent.bottom
3030 model: model
31+ cacheBuffer: height * 0.5
3132 delegate: Rectangle {
3233 property bool timerDone: false
3334 width: parent.width - 20