on xenial help sign in
Update m_minYExtent more often and make it have more "accurate" values
commit24ffcc6b53764d6c7c50bda2a2fec345e3d8b377 authorAlbert Astals <albert.astals@canonical.com> dateTue 18 Jun 2013, 17:04 (13 years ago) parentda029e1
2 files changed+69−36
plugins/ListViewWithPageHeader/listviewwithpageheader.cpp+31−4 tests/plugins/ListViewWithPageHeader/listviewwithpageheadertest.cpp+38−32
plugins/ListViewWithPageHeader/listviewwithpageheader.cpp+31−4
@@ -70,6 +70,12 @@
7070 * that is used for the cases we need to show the sticky section item at
7171 * the top of the view.
7272 *
73+ * Note that minYExtent and height are not always totally accurate, since
74+ * we don't have the items created we can't guess their heights
75+ * so we can only guarantee the values are correct when the first/last
76+ * items of the list are visible, otherwise we just live with good enough
77+ * values that make the list scrollable
78+ *
7379 * There are a few things that are not really implemented or tested properly
7480 * which we don't use at the moment like changing the model, changing
7581 * the section delegate, having a section delegate that changes its size, etc.
@@ -191,6 +197,7 @@ void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
191197 releaseItem(item);
192198 m_visibleItems.clear();
193199 m_firstVisibleIndex = -1;
200+ adjustMinYExtent();
194201 setContentY(0);
195202 m_clipItem->setY(0);
196203
@@ -568,6 +575,7 @@ bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal buffe
568575 bool foundVisible = false;
569576 int i = 0;
570577 int removedItems = 0;
578+ const auto oldFirstVisibleIndex = m_firstVisibleIndex;
571579 while (i < m_visibleItems.count()) {
572580 ListItem *item = m_visibleItems[i];
573581 const qreal pos = item->y() + m_clipItem->y();
@@ -587,6 +595,9 @@ bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal buffe
587595 ++i;
588596 }
589597 }
598+ if (m_firstVisibleIndex != oldFirstVisibleIndex) {
599+ adjustMinYExtent();
600+ }
590601
591602 return changed;
592603}
@@ -682,6 +693,8 @@ void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bo
682693{
683694 // TODO Do something with reset
684695// qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
696+ const auto oldFirstVisibleIndex = m_firstVisibleIndex;
697+
685698 foreach(const QQuickChangeSet::Remove &remove, changeSet.removes()) {
686699// qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove;
687700 if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
@@ -791,6 +804,10 @@ void ListViewWithPageHeader::onModelUpdated(const QQuickChangeSet &changeSet, bo
791804 }
792805 }
793806
807+ if (m_firstVisibleIndex != oldFirstVisibleIndex) {
808+ adjustMinYExtent();
809+ }
810+
794811 layout();
795812 polish();
796813 m_contentHeightDirty = true;
@@ -845,8 +862,18 @@ void ListViewWithPageHeader::adjustMinYExtent()
845862 if (m_visibleItems.isEmpty()) {
846863 m_minYExtent = 0;
847864 } else {
848− m_minYExtent = -m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0);
849− qDebug() << "adjustMinYExtent" << m_minYExtent << m_visibleItems.first()->y() << m_clipItem->y();
865+ qreal nonCreatedHeight = 0;
866+ if (m_firstVisibleIndex != 0) {
867+ // Calculate the average height of items to estimate the position of the list start
868+ const int visibleItems = m_visibleItems.count();
869+ qreal visibleItemsHeight = 0;
870+ foreach(ListItem *item, m_visibleItems) {
871+ visibleItemsHeight += item->height();
872+ }
873+ nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
874+// qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
875+ }
876+ m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0);
850877 }
851878}
852879
@@ -867,7 +894,7 @@ void ListViewWithPageHeader::layout()
867894
868895 qreal pos = m_visibleItems.first()->y();
869896
870−// qDebug() << "ListViewWithPageHeader::updatePolish Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
897+// qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
871898 int firstReallyVisibleItem = -1;
872899 int modelIndex = m_firstVisibleIndex;
873900 foreach(ListItem *item, m_visibleItems) {
@@ -906,7 +933,7 @@ void ListViewWithPageHeader::layout()
906933 } else {
907934 context->setContextProperty(QLatin1String("heightToClip"), QVariant::fromValue<int>(0));
908935 }
909−// qDebug() << "ListViewWithPageHeader::updatePolish" << item->m_item;
936+// qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
910937 pos += item->height();
911938 ++modelIndex;
912939 }
tests/plugins/ListViewWithPageHeader/listviewwithpageheadertest.cpp+38−32
@@ -216,7 +216,13 @@ private Q_SLOTS:
216216 verifyItem(1, -120, 350., false);
217217 verifyItem(2, 230, 350., false);
218218 verifyItem(3, 580, 350., true);
219− QCOMPARE(lvwph->m_minYExtent, 0.);
219+ // Just here as first check against m_minYExtent when m_firstVisibleIndex is not 0
220+ // We as humans know that m_minYExtent will be 0 but since the first delegate is not there anymore
221+ // we have to estimate its size, the average item height is 312.5 which is 162.5 more than the
222+ // "real" size of 150 and that's why the m_minYExtent has that "peculiar" value
223+ // It's fine since what it means is that we could scroll more up than the original position
224+ // but we will recalculate m_minYExtent when the item 0 is created and set it correctly
225+ QCOMPARE(lvwph->m_minYExtent, 162.5);
220226 QCOMPARE(lvwph->m_clipItem->y(), 520.);
221227 QCOMPARE(lvwph->m_clipItem->clip(), false);
222228 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -336,7 +342,7 @@ private Q_SLOTS:
336342 verifyItem(1, -120, 350., false);
337343 verifyItem(2, 230, 350., false);
338344 verifyItem(3, 580, 350., true);
339− QCOMPARE(lvwph->m_minYExtent, 0.);
345+ QCOMPARE(lvwph->m_minYExtent, 162.5);
340346 QCOMPARE(lvwph->m_clipItem->y(), 520.);
341347 QCOMPARE(lvwph->m_clipItem->clip(), false);
342348 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -468,7 +474,7 @@ private Q_SLOTS:
468474 verifyItem(1, -120, 350., false);
469475 verifyItem(2, 230, 350., false);
470476 verifyItem(3, 580, 350., true);
471− QCOMPARE(lvwph->m_minYExtent, 0.);
477+ QCOMPARE(lvwph->m_minYExtent, 162.5);
472478 QCOMPARE(lvwph->m_clipItem->y(), 520.);
473479 QCOMPARE(lvwph->m_clipItem->clip(), false);
474480 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -484,7 +490,7 @@ private Q_SLOTS:
484490 verifyItem(1, -120, 350., false);
485491 verifyItem(2, 230, 350., false);
486492 verifyItem(3, 580, 350., true);
487− QCOMPARE(lvwph->m_minYExtent, 0.);
493+ QCOMPARE(lvwph->m_minYExtent, 162.5);
488494 QCOMPARE(lvwph->m_clipItem->y(), 520.);
489495 QCOMPARE(lvwph->m_clipItem->clip(), false);
490496 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -516,7 +522,7 @@ private Q_SLOTS:
516522 verifyItem(0, -508., 350., true);
517523 verifyItem(1, -158, 350., false);
518524 verifyItem(2, 192, 350., false);
519− QCOMPARE(lvwph->m_minYExtent, 0.);
525+ QCOMPARE(lvwph->m_minYExtent, 350.);
520526 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
521527 QCOMPARE(lvwph->m_clipItem->clip(), false);
522528 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -530,7 +536,7 @@ private Q_SLOTS:
530536 verifyItem(0, -508., 350., true);
531537 verifyItem(1, -158, 350., false);
532538 verifyItem(2, 192, 350., false);
533− QCOMPARE(lvwph->m_minYExtent, 0.);
539+ QCOMPARE(lvwph->m_minYExtent, 350.);
534540 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
535541 QCOMPARE(lvwph->m_clipItem->clip(), true);
536542 QCOMPARE(lvwph->m_headerItem->y(), 1208.);
@@ -544,10 +550,10 @@ private Q_SLOTS:
544550 verifyItem(0, -508., 350., true);
545551 verifyItem(1, -158, 350., false);
546552 verifyItem(2, 192, 350., false);
547− QCOMPARE(lvwph->m_minYExtent, 0.);
553+ QCOMPARE(lvwph->m_minYExtent, 350.);
548554 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
549555 QCOMPARE(lvwph->m_clipItem->clip(), false);
550− QCOMPARE(lvwph->m_headerItem->y(), 0.);
556+ QCOMPARE(lvwph->m_headerItem->y(), -350.);
551557 QCOMPARE(lvwph->m_headerItem->height(), 50.);
552558 QCOMPARE(lvwph->contentY(), 1258.);
553559 QCOMPARE(lvwph->m_headerItemShownHeight, 0.);
@@ -558,7 +564,7 @@ private Q_SLOTS:
558564 verifyItem(0, -508., 350., true);
559565 verifyItem(1, -158, 350., false);
560566 verifyItem(2, 192, 350., false);
561− QCOMPARE(lvwph->m_minYExtent, 0.);
567+ QCOMPARE(lvwph->m_minYExtent, 350.);
562568 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
563569 QCOMPARE(lvwph->m_clipItem->clip(), true);
564570 QCOMPARE(lvwph->m_headerItem->y(), 1208.);
@@ -575,7 +581,7 @@ private Q_SLOTS:
575581 verifyItem(0, -508., 350., true);
576582 verifyItem(1, -158, 350., false);
577583 verifyItem(2, 192, 350., false);
578− QCOMPARE(lvwph->m_minYExtent, 0.);
584+ QCOMPARE(lvwph->m_minYExtent, 350.);
579585 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
580586 QCOMPARE(lvwph->m_clipItem->clip(), false);
581587 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -643,7 +649,7 @@ private Q_SLOTS:
643649 verifyItem(0, -508., 350., true);
644650 verifyItem(1, -158, 350., false);
645651 verifyItem(2, 192, 350., false);
646− QCOMPARE(lvwph->m_minYExtent, 0.);
652+ QCOMPARE(lvwph->m_minYExtent, 350.);
647653 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
648654 QCOMPARE(lvwph->m_clipItem->clip(), false);
649655 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -658,7 +664,7 @@ private Q_SLOTS:
658664 verifyItem(0, -508., 350., true);
659665 verifyItem(1, -158, 350., false);
660666 verifyItem(2, 192, 350., false);
661− QCOMPARE(lvwph->m_minYExtent, -650.);
667+ QCOMPARE(lvwph->m_minYExtent, 400.);
662668 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
663669 QCOMPARE(lvwph->m_clipItem->clip(), false);
664670 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -673,7 +679,7 @@ private Q_SLOTS:
673679 verifyItem(0, -508., 350., true);
674680 verifyItem(1, -158, 350., false);
675681 verifyItem(2, 192, 350., false);
676− QCOMPARE(lvwph->m_minYExtent, -650.);
682+ QCOMPARE(lvwph->m_minYExtent, 400.);
677683 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
678684 QCOMPARE(lvwph->m_clipItem->clip(), true);
679685 QCOMPARE(lvwph->m_headerItem->y(), 1158.);
@@ -707,7 +713,7 @@ private Q_SLOTS:
707713 verifyItem(0, -508., 350., true);
708714 verifyItem(1, -158, 350., false);
709715 verifyItem(2, 192, 350., false);
710− QCOMPARE(lvwph->m_minYExtent, 0.);
716+ QCOMPARE(lvwph->m_minYExtent, 350.);
711717 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
712718 QCOMPARE(lvwph->m_clipItem->clip(), false);
713719 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -722,7 +728,7 @@ private Q_SLOTS:
722728 verifyItem(0, -508., 350., true);
723729 verifyItem(1, -158, 350., false);
724730 verifyItem(2, 192, 350., false);
725− QCOMPARE(lvwph->m_minYExtent, 0.);
731+ QCOMPARE(lvwph->m_minYExtent, 350.);
726732 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
727733 QCOMPARE(lvwph->m_clipItem->clip(), true);
728734 QCOMPARE(lvwph->m_headerItem->y(), 1208.);
@@ -737,7 +743,7 @@ private Q_SLOTS:
737743 verifyItem(0, -508., 350., true);
738744 verifyItem(1, -158, 350., false);
739745 verifyItem(2, 192, 350., false);
740− QCOMPARE(lvwph->m_minYExtent, -700.);
746+ QCOMPARE(lvwph->m_minYExtent, 350.);
741747 QCOMPARE(lvwph->m_clipItem->y(), 1308.);
742748 QCOMPARE(lvwph->m_clipItem->clip(), true);
743749 QCOMPARE(lvwph->m_headerItem->y(), 1208.);
@@ -773,7 +779,7 @@ private Q_SLOTS:
773779 verifyItem(0, -508., 350., true);
774780 verifyItem(1, -158, 350., false);
775781 verifyItem(2, 192, 350., false);
776− QCOMPARE(lvwph->m_minYExtent, -650.);
782+ QCOMPARE(lvwph->m_minYExtent, 400.);
777783 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
778784 QCOMPARE(lvwph->m_clipItem->clip(), true);
779785 QCOMPARE(lvwph->m_headerItem->y(), 1158.);
@@ -788,7 +794,7 @@ private Q_SLOTS:
788794 verifyItem(0, -508., 350., true);
789795 verifyItem(1, -158, 350., false);
790796 verifyItem(2, 192, 350., false);
791− QCOMPARE(lvwph->m_minYExtent, -670.);
797+ QCOMPARE(lvwph->m_minYExtent, 380.);
792798 QCOMPARE(lvwph->m_clipItem->y(), 1228.);
793799 QCOMPARE(lvwph->m_clipItem->clip(), false);
794800 QCOMPARE(lvwph->m_headerItem->y(), 1158.);
@@ -842,7 +848,7 @@ private Q_SLOTS:
842848 verifyItem(0, -508., 350., true);
843849 verifyItem(1, -158, 350., false);
844850 verifyItem(2, 192, 350., false);
845− QCOMPARE(lvwph->m_minYExtent, -750.);
851+ QCOMPARE(lvwph->m_minYExtent, 300.);
846852 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
847853 QCOMPARE(lvwph->m_clipItem->clip(), false);
848854 QCOMPARE(lvwph->m_headerItem, (QQuickItem*)nullptr);
@@ -876,7 +882,7 @@ private Q_SLOTS:
876882 verifyItem(0, -508., 350., true);
877883 verifyItem(1, -158, 350., false);
878884 verifyItem(2, 192, 350., false);
879− QCOMPARE(lvwph->m_minYExtent, -720.);
885+ QCOMPARE(lvwph->m_minYExtent, 330.);
880886 QCOMPARE(lvwph->m_clipItem->y(), 1228.);
881887 QCOMPARE(lvwph->m_clipItem->clip(), false);
882888 QCOMPARE(lvwph->m_headerItem, (QQuickItem*)nullptr);
@@ -968,7 +974,7 @@ private Q_SLOTS:
968974 verifyItem(1, 50, 350., false);
969975 verifyItem(2, 400, 350., false);
970976 verifyItem(3, 750, 350., true);
971− QCOMPARE(lvwph->m_minYExtent, 0.);
977+ QCOMPARE(lvwph->m_minYExtent, 350.);
972978 QCOMPARE(lvwph->m_clipItem->y(), 700.);
973979 QCOMPARE(lvwph->m_clipItem->clip(), false);
974980 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -985,7 +991,7 @@ private Q_SLOTS:
985991 verifyItem(1, 50, 350., false);
986992 verifyItem(2, 400, 350., false);
987993 verifyItem(3, 750, 350., true);
988− QCOMPARE(lvwph->m_minYExtent, 0.);
994+ QCOMPARE(lvwph->m_minYExtent, 1050.);
989995 QCOMPARE(lvwph->m_clipItem->y(), 700.);
990996 QCOMPARE(lvwph->m_clipItem->clip(), false);
991997 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -1041,7 +1047,7 @@ private Q_SLOTS:
10411047 verifyItem(1, -120, 350., false);
10421048 verifyItem(2, 230, 350., false);
10431049 verifyItem(3, 580, 350., true);
1044− QCOMPARE(lvwph->m_minYExtent, 0.);
1050+ QCOMPARE(lvwph->m_minYExtent, 162.5);
10451051 QCOMPARE(lvwph->m_clipItem->y(), 520.);
10461052 QCOMPARE(lvwph->m_clipItem->clip(), false);
10471053 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -1058,7 +1064,7 @@ private Q_SLOTS:
10581064 verifyItem(1, -120, 350., false);
10591065 verifyItem(2, 230, 350., false);
10601066 verifyItem(3, 580, 350., true);
1061− QCOMPARE(lvwph->m_minYExtent, 0.);
1067+ QCOMPARE(lvwph->m_minYExtent, 787.5);
10621068 QCOMPARE(lvwph->m_clipItem->y(), 520.);
10631069 QCOMPARE(lvwph->m_clipItem->clip(), false);
10641070 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -1096,7 +1102,7 @@ private Q_SLOTS:
10961102 verifyItem(2, 25, 350., false);
10971103 verifyItem(3, 375, 350., false);
10981104 verifyItem(4, 725, 350., true);
1099− QCOMPARE(lvwph->m_minYExtent, -50.);
1105+ QCOMPARE(lvwph->m_minYExtent, 490.);
11001106 QCOMPARE(lvwph->m_clipItem->y(), 375.);
11011107 QCOMPARE(lvwph->m_clipItem->clip(), false);
11021108 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -1138,7 +1144,7 @@ private Q_SLOTS:
11381144 verifyItem(2, 192, 350., false);
11391145 verifyItem(3, 542, 125., true);
11401146 verifyItem(4, 667, 100., true);
1141− QCOMPARE(lvwph->m_minYExtent, 0.);
1147+ QCOMPARE(lvwph->m_minYExtent, 350.);
11421148 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
11431149 QCOMPARE(lvwph->m_clipItem->clip(), false);
11441150 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -1155,7 +1161,7 @@ private Q_SLOTS:
11551161 verifyItem(1, -33, 350., false);
11561162 verifyItem(2, 317, 125., false);
11571163 verifyItem(3, 442, 100., false);
1158− QCOMPARE(lvwph->m_minYExtent, 0.);
1164+ QCOMPARE(lvwph->m_minYExtent, -125.);
11591165 QCOMPARE(lvwph->m_clipItem->y(), 1483.);
11601166 QCOMPARE(lvwph->m_clipItem->clip(), false);
11611167 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -1205,7 +1211,7 @@ private Q_SLOTS:
12051211 verifyItem(0, -508., 350., true);
12061212 verifyItem(1, -158, 350., false);
12071213 verifyItem(2, 192, 350., false);
1208− QCOMPARE(lvwph->m_minYExtent, 0.);
1214+ QCOMPARE(lvwph->m_minYExtent, 3850.);
12091215 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
12101216 QCOMPARE(lvwph->m_clipItem->clip(), false);
12111217 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -1268,7 +1274,7 @@ private Q_SLOTS:
12681274 verifyItem(0, -508., 350., true);
12691275 verifyItem(1, -158, 350., false);
12701276 verifyItem(2, 192, 350., false);
1271− QCOMPARE(lvwph->m_minYExtent, 0.);
1277+ QCOMPARE(lvwph->m_minYExtent, -350.);
12721278 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
12731279 QCOMPARE(lvwph->m_clipItem->clip(), false);
12741280 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -1288,7 +1294,7 @@ private Q_SLOTS:
12881294 verifyItem(0, -358., 200., true);
12891295 verifyItem(1, -158, 350., false);
12901296 verifyItem(2, 192, 350., false);
1291− QCOMPARE(lvwph->m_minYExtent, -150.);
1297+ QCOMPARE(lvwph->m_minYExtent, 150.);
12921298 QCOMPARE(lvwph->m_clipItem->y(), 558.);
12931299 QCOMPARE(lvwph->m_clipItem->clip(), false);
12941300 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -1307,7 +1313,7 @@ private Q_SLOTS:
13071313 verifyItem(1, -120, 350., false);
13081314 verifyItem(2, 230, 350., false);
13091315 verifyItem(3, 580, 350., true);
1310− QCOMPARE(lvwph->m_minYExtent, 0.);
1316+ QCOMPARE(lvwph->m_minYExtent, 162.5);
13111317 QCOMPARE(lvwph->m_clipItem->y(), 520.);
13121318 QCOMPARE(lvwph->m_clipItem->clip(), false);
13131319 QCOMPARE(lvwph->m_headerItem->y(), 0.);
@@ -1381,7 +1387,7 @@ private Q_SLOTS:
13811387 verifyItem(0, -508., 350., true);
13821388 verifyItem(1, -158, 350., false);
13831389 verifyItem(2, 192, 350., false);
1384− QCOMPARE(lvwph->m_minYExtent, 0.);
1390+ QCOMPARE(lvwph->m_minYExtent, 350.);
13851391 QCOMPARE(lvwph->m_clipItem->y(), 1258.);
13861392 QCOMPARE(lvwph->m_clipItem->clip(), false);
13871393 QCOMPARE(lvwph->m_headerItem->y(), 0.);