~aacid/unity8/moreAsyncAudioCard

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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
/*
 * Copyright (C) 2013 Canonical, Ltd.
 *
 * 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; version 3.
 *
 * 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/>.
 */

/*
 * Some documentation on how this thing works:
 *
 * A flickable has two very important concepts that define the top and
 * height of the flickable area.
 * The top is returned in minYExtent()
 * The height is set using setContentHeight()
 * By changing those two values we can make the list grow up or down
 * as needed. e.g. if we are in the middle of the list
 * and something that is above the viewport grows, since we do not
 * want to change the viewport because of that we just adjust the
 * minYExtent so that the list grows up.
 *
 * The implementation on the list relies on the delegateModel doing
 * most of the instantiation work. You call createItem() when you
 * need to create an item asking for it async or not. If returns null
 * it means the item will be created async and the model will call the
 * itemCreated slot with the item.
 *
 * updatePolish is the central point of dispatch for the work of the
 * class. It is called by the scene graph just before drawing the class.
 * In it we:
 *  * Make sure all items are positioned correctly
 *  * Add/Remove items if needed
 *  * Update the content height if it was dirty
 *
 * m_visibleItems contains all the items we have created at the moment.
 * Actually not all of them are visible since it includes the ones
 * in the cache area we create asynchronously to help performance.
 * The first item in m_visibleItems has the m_firstVisibleIndex in
 * the model. If you actually want to know what is the first
 * item in the viewport you have to find the first non culled element
 * in m_visibleItems
 *
 * All the items (except the header) are childs of m_clipItem which
 * is a child of the contentItem() of the flickable (The contentItem()
 * is what actually 'moves' in a a flickable). This way
 * we can implement the clipping needed so we can have the header
 * shown in the middle of the list over the items without the items
 * leaking under the header in case it is transparent.
 *
 * The first item of m_visibleItems is the one that defines the
 * positions of all the rest of items (see updatePolish()) and
 * this is why sometimes we move it even if it's not the item
 * that has triggered the function (i.e. in itemGeometryChanged())
 *
 * m_visibleItems is a list of ListItem. Each ListItem
 * will contain a item and potentially a sectionItem. The sectionItem
 * is only there when the list is using sectionDelegate+sectionProperty
 * and this is the first item of the section. Each ListItem is vertically
 * layouted with the sectionItem first and then the item.
 *
 * For sectioning we also have a section item alone (m_topSectionItem)
 * that is used for the cases we need to show the sticky section item at
 * the top of the view.
 *
 * Each delegate item has a context property called heightToClip that is
 * used to communicate to the delegate implementation in case it has to
 * clip itself because of overlapping with the top sticky section item.
 * This is an implementation decision since it has been agreed it
 * is easier to implement the clipping in QML with this info than to
 * do it at the C++ level.
 *
 * Note that minYExtent and height are not always totally accurate, since
 * we don't have the items created we can't guess their heights
 * so we can only guarantee the values are correct when the first/last
 * items of the list are visible, otherwise we just live with good enough
 * values that make the list scrollable
 *
 * There are a few things that are not really implemented or tested properly
 * which we don't use at the moment like changing the model, changing
 * the section delegate, having a section delegate that changes its size, etc.
 * The known missing features are marked with TODOs along the code.
 */

#include "listviewwithpageheader.h"

#include <QCoreApplication>
#include <QDebug>
#include <qqmlinfo.h>
#include <qqmlengine.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-pedantic"
#include <private/qqmldelegatemodel_p.h>
#include <private/qqmlglobal_p.h>
#include <private/qquickitem_p.h>
#include <private/qquickanimation_p.h>
#pragma GCC diagnostic pop
// #include <private/qquickrectangle_p.h>

qreal ListViewWithPageHeader::ListItem::height() const
{
    return m_item->height() + (m_sectionItem ? m_sectionItem->height() : 0);
}

qreal ListViewWithPageHeader::ListItem::y() const
{
    return m_item->y() - (m_sectionItem ? m_sectionItem->height() : 0);
}

void ListViewWithPageHeader::ListItem::setY(qreal newY)
{
    if (m_sectionItem) {
        m_sectionItem->setY(newY);
        m_item->setY(newY + m_sectionItem->height());
    } else {
        m_item->setY(newY);
    }
}

bool ListViewWithPageHeader::ListItem::culled() const
{
    return QQuickItemPrivate::get(m_item)->culled;
}

void ListViewWithPageHeader::ListItem::setCulled(bool culled)
{
    QQuickItemPrivate::get(m_item)->setCulled(culled);
    if (m_sectionItem)
        QQuickItemPrivate::get(m_sectionItem)->setCulled(culled);
}

void ListViewWithPageHeader::ListItem::setSectionItem(QQuickItem *sectionItem)
{
    m_sectionItem = sectionItem;
}

ListViewWithPageHeader::ListViewWithPageHeader()
 : m_delegateModel(nullptr)
 , m_asyncRequestedIndex(-1)
 , m_delegateValidated(false)
 , m_firstVisibleIndex(-1)
 , m_minYExtent(0)
 , m_contentHeightDirty(false)
 , m_headerItem(nullptr)
 , m_previousContentY(0)
 , m_headerItemShownHeight(0)
 , m_sectionDelegate(nullptr)
 , m_topSectionItem(nullptr)
 , m_forceNoClip(false)
 , m_inLayout(false)
 , m_inContentHeightKeepHeaderShown(false)
 , m_cacheBuffer(0)
{
    m_clipItem = new QQuickItem(contentItem());
//     m_clipItem = new QQuickRectangle(contentItem());
//     ((QQuickRectangle*)m_clipItem)->setColor(Qt::gray);

    m_contentYAnimation = new QQuickNumberAnimation(this);
    m_contentYAnimation->setEasing(QEasingCurve::OutQuad);
    m_contentYAnimation->setProperty(QStringLiteral("contentY"));
    m_contentYAnimation->setDuration(200);
    m_contentYAnimation->setTargetObject(this);

    connect(this, &ListViewWithPageHeader::contentWidthChanged, this, &ListViewWithPageHeader::onContentWidthChanged);
    connect(this, &ListViewWithPageHeader::contentHeightChanged, this, &ListViewWithPageHeader::onContentHeightChanged);
    connect(this, &ListViewWithPageHeader::heightChanged, this, &ListViewWithPageHeader::onHeightChanged);
    connect(m_contentYAnimation, &QQuickNumberAnimation::runningChanged, this, &ListViewWithPageHeader::contentYAnimationRunningChanged);

    setFlickableDirection(VerticalFlick);
}

ListViewWithPageHeader::~ListViewWithPageHeader()
{
}

QAbstractItemModel *ListViewWithPageHeader::model() const
{
    return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
}

void ListViewWithPageHeader::setModel(QAbstractItemModel *model)
{
    if (model != this->model()) {
        if (!m_delegateModel) {
            createDelegateModel();
        } else {
            disconnect(m_delegateModel, &QQmlDelegateModel::modelUpdated, this, &ListViewWithPageHeader::onModelUpdated);
        }
        m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
        connect(m_delegateModel, &QQmlDelegateModel::modelUpdated, this, &ListViewWithPageHeader::onModelUpdated);
        Q_EMIT modelChanged();
        polish();
        // TODO?
//         Q_EMIT contentHeightChanged();
//         Q_EMIT contentYChanged();
    }
}

QQmlComponent *ListViewWithPageHeader::delegate() const
{
    return m_delegateModel ? m_delegateModel->delegate() : nullptr;
}

void ListViewWithPageHeader::setDelegate(QQmlComponent *delegate)
{
    if (delegate != this->delegate()) {
        if (!m_delegateModel) {
            createDelegateModel();
        }

        // Cleanup the existing items
        Q_FOREACH(ListItem *item, m_visibleItems)
            releaseItem(item);
        m_visibleItems.clear();
        initializeValuesForEmptyList();

        m_delegateModel->setDelegate(delegate);

        Q_EMIT delegateChanged();
        m_delegateValidated = false;
        m_contentHeightDirty = true;
        polish();
    }
}

void ListViewWithPageHeader::initializeValuesForEmptyList()
{
    m_firstVisibleIndex = -1;
    adjustMinYExtent();
    setContentY(0);
    m_clipItem->setY(0);
    if (m_topSectionItem) {
        QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
    }
}

QQuickItem *ListViewWithPageHeader::header() const
{
    return m_headerItem;
}

void ListViewWithPageHeader::setHeader(QQuickItem *headerItem)
{
    if (m_headerItem != headerItem) {
        qreal oldHeaderHeight = 0;
        qreal oldHeaderY = 0;
        if (m_headerItem) {
            oldHeaderHeight = m_headerItem->height();
            oldHeaderY = m_headerItem->y();
            m_headerItem->setParentItem(nullptr);
        }
        m_headerItem = headerItem;
        if (m_headerItem) {
            m_headerItem->setParentItem(contentItem());
            m_headerItem->setZ(1);
            m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
            QQuickItemPrivate::get(m_headerItem)->addItemChangeListener(this, QQuickItemPrivate::ImplicitHeight);
        }
        qreal newHeaderHeight = m_headerItem ? m_headerItem->height() : 0;
        if (!m_visibleItems.isEmpty() && newHeaderHeight != oldHeaderHeight) {
            headerHeightChanged(newHeaderHeight, oldHeaderHeight, oldHeaderY);
            polish();
            m_contentHeightDirty = true;
        }
        Q_EMIT headerChanged();
    }
}

QQmlComponent *ListViewWithPageHeader::sectionDelegate() const
{
    return m_sectionDelegate;
}

void ListViewWithPageHeader::setSectionDelegate(QQmlComponent *delegate)
{
    if (delegate != m_sectionDelegate) {
        // TODO clean existing sections

        m_sectionDelegate = delegate;

        m_topSectionItem = getSectionItem(QString());
        m_topSectionItem->setZ(3);
        QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
        connect(m_topSectionItem, &QQuickItem::heightChanged, this, &ListViewWithPageHeader::stickyHeaderHeightChanged);

        // TODO create sections for existing items

        Q_EMIT sectionDelegateChanged();
        Q_EMIT stickyHeaderHeightChanged();
    }
}

QString ListViewWithPageHeader::sectionProperty() const
{
    return m_sectionProperty;
}

void ListViewWithPageHeader::setSectionProperty(const QString &property)
{
    if (property != m_sectionProperty) {
        m_sectionProperty = property;

        updateWatchedRoles();

        // TODO recreate sections

        Q_EMIT sectionPropertyChanged();
    }
}

bool ListViewWithPageHeader::forceNoClip() const
{
    return m_forceNoClip;
}

void ListViewWithPageHeader::setForceNoClip(bool noClip)
{
    if (noClip != m_forceNoClip) {
        m_forceNoClip = noClip;
        updateClipItem();
        Q_EMIT forceNoClipChanged();
    }
}

int ListViewWithPageHeader::stickyHeaderHeight() const
{
    return m_topSectionItem ? m_topSectionItem->height() : 0;
}

qreal ListViewWithPageHeader::headerItemShownHeight() const
{
    return m_headerItemShownHeight;
}

int ListViewWithPageHeader::cacheBuffer() const
{
    return m_cacheBuffer;
}

void ListViewWithPageHeader::setCacheBuffer(int cacheBuffer)
{
    if (cacheBuffer < 0) {
        qmlInfo(this) << "Cannot set a negative cache buffer";
        return;
    }

    if (cacheBuffer != m_cacheBuffer) {
        m_cacheBuffer = cacheBuffer;
        Q_EMIT cacheBufferChanged();
        polish();
    }
}

void ListViewWithPageHeader::positionAtBeginning()
{
    if (m_delegateModel->count() <= 0)
        return;

    qreal headerHeight = (m_headerItem ? m_headerItem->height() : 0);
    if (m_firstVisibleIndex != 0) {
        // TODO This could be optimized by trying to reuse the interesection
        // of items that may end up intersecting between the existing
        // m_visibleItems and the items we are creating in the next loop
        Q_FOREACH(ListItem *item, m_visibleItems)
            releaseItem(item);
        m_visibleItems.clear();
        m_firstVisibleIndex = -1;

        // Create the item 0, it will be already correctly positioned at createItem()
        m_clipItem->setY(0);
        ListItem *item = createItem(0, false);
        // Create the subsequent items
        int modelIndex = 1;
        qreal pos = item->y() + item->height();
        const qreal bufferTo = height() + m_cacheBuffer;
        while (modelIndex < m_delegateModel->count() && pos <= bufferTo) {
            if (!(item = createItem(modelIndex, false)))
                break;
            pos += item->height();
            ++modelIndex;
        }

        m_previousContentY = m_visibleItems.first()->y() - headerHeight;
    }
    setContentY(m_visibleItems.first()->y() + m_clipItem->y() - headerHeight);
    if (m_headerItem) {
        // TODO This should not be needed and the code that adjust the m_headerItem position
        // in viewportMoved() should be enough but in some cases we have not found a way to reproduce
        // yet the code of viewportMoved() fails so here we make sure that at least if we are calling
        // positionAtBeginning the header item will be correctly positioned
        m_headerItem->setY(-m_minYExtent);
    }
}

static inline bool uFuzzyCompare(qreal r1, qreal r2)
{
    return qFuzzyCompare(r1, r2) || (qFuzzyIsNull(r1) && qFuzzyIsNull(r2));
}

void ListViewWithPageHeader::showHeader()
{
    if (!m_headerItem)
        return;

    const auto to = qMax(-minYExtent(), contentY() - m_headerItem->height() + m_headerItemShownHeight);
    if (!uFuzzyCompare(to, contentY())) {
        const bool headerShownByItsOwn = contentY() < m_headerItem->y() + m_headerItem->height();
        if (headerShownByItsOwn && m_headerItemShownHeight == 0) {
            // We are not clipping since we are just at the top of the viewport
            // but because of the showHeader animation we will need to, so
            // enable the clipping without logically moving the items
            m_headerItemShownHeight = m_headerItem->y() + m_headerItem->height() - contentY();
            if (!m_visibleItems.isEmpty()) {
                updateClipItem();
                ListItem *firstItem = m_visibleItems.first();
                firstItem->setY(firstItem->y() - m_headerItemShownHeight);
                layout();
            }
            Q_EMIT headerItemShownHeightChanged();
        }
        m_contentYAnimation->setTo(to);
        contentYAnimationType = ContentYAnimationShowHeader;
        m_contentYAnimation->start();
    }
}

int ListViewWithPageHeader::firstCreatedIndex() const
{
    return m_firstVisibleIndex;
}

int ListViewWithPageHeader::createdItemCount() const
{
    return m_visibleItems.count();
}

QQuickItem *ListViewWithPageHeader::item(int modelIndex) const
{
    ListItem *item = itemAtIndex(modelIndex);
    if (item)
        return item->m_item;
    else
        return nullptr;
}

bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex)
{
    ListItem *listItem = itemAtIndex(modelIndex);
    if (listItem) {
        return maximizeVisibleArea(listItem, listItem->height());
    }

    return false;
}

bool ListViewWithPageHeader::maximizeVisibleArea(int modelIndex, int itemHeight)
{
    if (itemHeight < 0)
        return false;

    ListItem *listItem = itemAtIndex(modelIndex);
    if (listItem) {
        return maximizeVisibleArea(listItem, itemHeight + (listItem->sectionItem() ? listItem->sectionItem()->height() : 0));
    }

    return false;
}

bool ListViewWithPageHeader::maximizeVisibleArea(ListItem *listItem, int listItemHeight)
{
    if (listItem) {
        layout();
        const auto listItemY = m_clipItem->y() + listItem->y();
        if (listItemY > contentY() && listItemY + listItemHeight > contentY() + height()) {
            // we can scroll the list up to show more stuff
            const auto to = qMin(listItemY, listItemY + listItemHeight - height());
            m_contentYAnimation->setTo(to);
            contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
            m_contentYAnimation->start();
        } else if ((listItemY < contentY() && listItemY + listItemHeight < contentY() + height()) ||
                   (m_topSectionItem && !listItem->sectionItem() && listItemY - m_topSectionItem->height() < contentY() && listItemY + listItemHeight < contentY() + height()))
        {
            // we can scroll the list down to show more stuff
            auto realVisibleListItemY = listItemY;
            if (m_topSectionItem) {
                // If we are showing the top section sticky item and this item doesn't have a section
                // item we have to make sure to scroll it a bit more so that it is not underlapping
                // the top section sticky item
                bool topSectionShown = !QQuickItemPrivate::get(m_topSectionItem)->culled;
                if (topSectionShown && !listItem->sectionItem()) {
                    realVisibleListItemY -= m_topSectionItem->height();
                }
            }
            const auto to = qMax(realVisibleListItemY, listItemY + listItemHeight - height());
            m_contentYAnimation->setTo(to);
            contentYAnimationType = ContentYAnimationMaximizeVisibleArea;
            m_contentYAnimation->start();
        }
        return true;
    }
    return false;
}

qreal ListViewWithPageHeader::minYExtent() const
{
//     qDebug() << "ListViewWithPageHeader::minYExtent" << m_minYExtent;
    return m_minYExtent;
}

void ListViewWithPageHeader::componentComplete()
{
    if (m_delegateModel)
        m_delegateModel->componentComplete();

    QQuickFlickable::componentComplete();

    polish();
}

void ListViewWithPageHeader::viewportMoved(Qt::Orientations orient)
{
    // Check we are not being taken down and don't paint anything
    // TODO Check if we still need this in 5.2
    // For reproduction just inifnite loop testDash or testDashContent
    if (!QQmlEngine::contextForObject(this)->parentContext())
        return;

    QQuickFlickable::viewportMoved(orient);
//     qDebug() << "ListViewWithPageHeader::viewportMoved" << contentY();
    const qreal diff = m_previousContentY - contentY();
    adjustHeader(diff);
    m_previousContentY = contentY();
    layout();
    polish();
}

void ListViewWithPageHeader::adjustHeader(qreal diff)
{
    const bool showHeaderAnimationRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationShowHeader;
    if (m_headerItem) {
        const auto oldHeaderItemShownHeight = m_headerItemShownHeight;
        if (uFuzzyCompare(contentY(), -m_minYExtent) || contentY() > -m_minYExtent) {
            m_headerItem->setHeight(m_headerItem->implicitHeight());
            // We are going down (but it's not because of the rebound at the end)
            // (but the header was not shown by it's own position)
            // or the header is partially shown and we are not doing a maximizeVisibleArea either
            const bool scrolledUp = m_previousContentY > contentY();
            const bool notRebounding = qRound(contentY() + height()) < qRound(contentHeight());
            const bool notShownByItsOwn = contentY() + diff >= m_headerItem->y() + m_headerItem->height();
            const bool maximizeVisibleAreaRunning = m_contentYAnimation->isRunning() && contentYAnimationType == ContentYAnimationMaximizeVisibleArea;

            if (!scrolledUp && (contentY() == -m_minYExtent || (m_headerItemShownHeight == 0 && m_previousContentY == m_headerItem->y()))) {
                m_headerItemShownHeight = 0;
                m_headerItem->setY(-m_minYExtent);
            } else if ((scrolledUp && notRebounding && notShownByItsOwn && !maximizeVisibleAreaRunning) || (m_headerItemShownHeight > 0) || m_inContentHeightKeepHeaderShown) {
                if (maximizeVisibleAreaRunning && diff > 0) {
                    // If we are maximizing and the header was shown, make sure we hide it
                    m_headerItemShownHeight -= diff;
                } else {
                    m_headerItemShownHeight += diff;
                }
                if (uFuzzyCompare(contentY(), -m_minYExtent)) {
                    m_headerItemShownHeight = 0;
                } else {
                    m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, m_headerItem->height());
                }
                if (m_headerItemShownHeight > 0) {
                    if (uFuzzyCompare(m_headerItem->height(), m_headerItemShownHeight)) {
                        m_headerItem->setY(contentY());
                        m_headerItemShownHeight = m_headerItem->height();
                    } else {
                        m_headerItem->setY(contentY() - m_headerItem->height() + m_headerItemShownHeight);
                    }
                } else {
                    m_headerItem->setY(-m_minYExtent);
                }
            }
            Q_EMIT headerItemShownHeightChanged();
        } else {
            // Stick the header item to the top when dragging down
            m_headerItem->setY(contentY());
            m_headerItem->setHeight(m_headerItem->implicitHeight() + (-m_minYExtent - contentY()));
        }
        // We will be changing the clip item, need to accomadate for it
        // otherwise we move the firstItem down/up twice (unless the
        // show header animation is running, where we want to keep the viewport stable)
        if (!showHeaderAnimationRunning) {
            diff += oldHeaderItemShownHeight - m_headerItemShownHeight;
        } else {
            diff = -diff;
        }
    }
    if (!m_visibleItems.isEmpty()) {
        updateClipItem();
        ListItem *firstItem = m_visibleItems.first();
        firstItem->setY(firstItem->y() + diff);
        if (showHeaderAnimationRunning) {
            adjustMinYExtent();
        }
    }
}

void ListViewWithPageHeader::createDelegateModel()
{
    m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
    connect(m_delegateModel, &QQmlDelegateModel::createdItem, this, &ListViewWithPageHeader::itemCreated);
    if (isComponentComplete())
        m_delegateModel->componentComplete();
    updateWatchedRoles();
}

void ListViewWithPageHeader::refill()
{
    if (m_inLayout) {
        return;
    }
    if (!isComponentComplete()) {
        return;
    }

    const qreal from = contentY();
    const qreal to = from + height();
    const qreal bufferFrom = from - m_cacheBuffer;
    const qreal bufferTo = to + m_cacheBuffer;

    bool added = addVisibleItems(from, to, false);
    bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
    added |= addVisibleItems(bufferFrom, bufferTo, true);

    if (added || removed) {
        m_contentHeightDirty = true;
    }
}

bool ListViewWithPageHeader::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
{
    if (!delegate())
        return false;

    if (m_delegateModel->count() == 0)
        return false;

    ListItem *item;
//     qDebug() << "ListViewWithPageHeader::addVisibleItems" << fillFrom << fillTo << asynchronous;

    int modelIndex = 0;
    qreal pos = 0;
    if (!m_visibleItems.isEmpty()) {
        modelIndex = m_firstVisibleIndex + m_visibleItems.count();
        item = m_visibleItems.last();
        pos = item->y() + item->height() + m_clipItem->y();
    }
    bool changed = false;
//     qDebug() << (modelIndex < m_delegateModel->count()) << pos << fillTo;
    while (modelIndex < m_delegateModel->count() && pos <= fillTo) {
//         qDebug() << "refill: append item" << modelIndex << "pos" << pos << "asynchronous" << asynchronous;
        if (!(item = createItem(modelIndex, asynchronous)))
            break;
        pos += item->height();
        ++modelIndex;
        changed = true;
    }

    modelIndex = 0;
    pos = 0;
    if (!m_visibleItems.isEmpty()) {
        modelIndex = m_firstVisibleIndex - 1;
        item = m_visibleItems.first();
        pos = item->y() + m_clipItem->y();
    }
    while (modelIndex >= 0 && pos > fillFrom) {
//         qDebug() << "refill: prepend item" << modelIndex << "pos" << pos << "fillFrom" << fillFrom << "asynchronous" << asynchronous;
        if (!(item = createItem(modelIndex, asynchronous)))
            break;
        pos -= item->height();
        --modelIndex;
        changed = true;
    }

    return changed;
}

void ListViewWithPageHeader::reallyReleaseItem(ListItem *listItem)
{
    QQuickItem *item = listItem->m_item;
    QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
    if (flags & QQmlDelegateModel::Destroyed) {
        item->setParentItem(nullptr);
    }
    listItem->sectionItem()->deleteLater();
    delete listItem;
}

void ListViewWithPageHeader::releaseItem(ListItem *listItem)
{
    QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(listItem->m_item);
    itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
    m_itemsToRelease << listItem;
}

void ListViewWithPageHeader::updateWatchedRoles()
{
    if (m_delegateModel) {
        QList<QByteArray> roles;
        if (!m_sectionProperty.isEmpty())
            roles << m_sectionProperty.toUtf8();
        m_delegateModel->setWatchedRoles(roles);
    }
}

QQuickItem *ListViewWithPageHeader::getSectionItem(int modelIndex, bool alreadyInserted)
{
    if (!m_sectionDelegate)
        return nullptr;

    const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);

    if (modelIndex > 0) {
        const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
        if (section == prevSection)
            return nullptr;
    }
    if (modelIndex + 1 < model()->rowCount() && !alreadyInserted) {
        // Already inserted items can't steal next section header
        const QString nextSection = m_delegateModel->stringValue(modelIndex + 1, m_sectionProperty);
        if (section == nextSection) {
            // Steal the section header
            ListItem *nextItem = itemAtIndex(modelIndex); // Not +1 since not yet inserted into m_visibleItems
            if (nextItem) {
                QQuickItem *sectionItem = nextItem->sectionItem();
                nextItem->setSectionItem(nullptr);
                return sectionItem;
            }
        }
    }

    return getSectionItem(section);
}

QQuickItem *ListViewWithPageHeader::getSectionItem(const QString &sectionText)
{
    QQuickItem *sectionItem = nullptr;

    QQmlContext *creationContext = m_sectionDelegate->creationContext();
    QQmlContext *context = new QQmlContext(creationContext ? creationContext : qmlContext(this));
    context->setContextProperty(QStringLiteral("section"), sectionText);
    context->setContextProperty(QStringLiteral("delegateIndex"), -1);
    QObject *nobj = m_sectionDelegate->beginCreate(context);
    if (nobj) {
        QQml_setParent_noEvent(context, nobj);
        sectionItem = qobject_cast<QQuickItem *>(nobj);
        if (!sectionItem) {
            delete nobj;
        } else {
            sectionItem->setZ(2);
            QQml_setParent_noEvent(sectionItem, m_clipItem);
            sectionItem->setParentItem(m_clipItem);
        }
    } else {
        delete context;
    }
    m_sectionDelegate->completeCreate();

    // TODO attach to sectionItem so we can accomodate to it changing its height

    return sectionItem;
}

void ListViewWithPageHeader::updateSectionItem(int modelIndex)
{
    ListItem *item = itemAtIndex(modelIndex);
    if (item) {
        const QString sectionText = m_delegateModel->stringValue(modelIndex, m_sectionProperty);

        bool needSectionHeader = true;
        // if it is the same section as the previous item need to drop the section
        if (modelIndex > 0) {
            const QString prevSection = m_delegateModel->stringValue(modelIndex - 1, m_sectionProperty);
            if (sectionText == prevSection) {
                needSectionHeader = false;
            }
        }

        if (needSectionHeader) {
            if (!item->sectionItem()) {
                item->setSectionItem(getSectionItem(sectionText));
            } else {
                QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
                context->setContextProperty(QStringLiteral("section"), sectionText);
            }
        } else {
            if (item->sectionItem()) {
                item->sectionItem()->deleteLater();
                item->setSectionItem(nullptr);
            }
        }
    }
}

bool ListViewWithPageHeader::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
{
//     qDebug() << "ListViewWithPageHeader::removeNonVisibleItems" << bufferFrom << bufferTo;
    // Do not remove items if we are overshooting up or down, since we'll come back
    // to the "stable" position and delete/create items without any reason
    if (contentY() < -m_minYExtent) {
        return false;
    } else if (contentY() + height() > contentHeight()) {
        return false;
    }
    bool changed = false;

    bool foundVisible = false;
    int i = 0;
    int removedItems = 0;
    const auto oldFirstVisibleIndex = m_firstVisibleIndex;
    while (i < m_visibleItems.count()) {
        ListItem *item = m_visibleItems[i];
        const qreal pos = item->y() + m_clipItem->y();
//         qDebug() << i << pos << (pos + item->height()) << bufferFrom << bufferTo;
        if (pos + item->height() < bufferFrom || pos > bufferTo) {
//             qDebug() << "Releasing" << i << (pos + item->height() < bufferFrom) << pos + item->height() << bufferFrom << (pos > bufferTo) << pos << bufferTo;
            releaseItem(item);
            m_visibleItems.removeAt(i);
            changed = true;
            ++removedItems;
        } else {
            if (!foundVisible) {
                foundVisible = true;
                const int itemIndex = m_firstVisibleIndex + removedItems + i;
                m_firstVisibleIndex = itemIndex;
            }
            ++i;
        }
    }
    if (!foundVisible) {
        initializeValuesForEmptyList();
    }
    if (m_firstVisibleIndex != oldFirstVisibleIndex) {
        adjustMinYExtent();
    }

    return changed;
}

ListViewWithPageHeader::ListItem *ListViewWithPageHeader::createItem(int modelIndex, bool asynchronous)
{
//     qDebug() << "CREATE ITEM" << modelIndex;
    if (asynchronous && m_asyncRequestedIndex != -1)
        return nullptr;

    m_asyncRequestedIndex = -1;
    QObject* object = m_delegateModel->object(modelIndex, asynchronous);
    QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
    if (!item) {
        if (object) {
            m_delegateModel->release(object);
            if (!m_delegateValidated) {
                m_delegateValidated = true;
                QObject* delegateObj = delegate();
                qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
            }
        } else {
            m_asyncRequestedIndex = modelIndex;
        }
        return 0;
    } else {
//         qDebug() << "ListViewWithPageHeader::createItem::We have the item" << modelIndex << item;
        ListItem *listItem = new ListItem;
        listItem->m_item = item;
        listItem->setSectionItem(getSectionItem(modelIndex, false /*Not yet inserted into m_visibleItems*/));
        QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
        ListItem *prevItem = itemAtIndex(modelIndex - 1);
        bool lostItem = false; // Is an item that we requested async but because of model changes
                               // it is no longer attached to any of the existing items (has no prev nor next item)
                               // nor is the first item
        if (prevItem) {
            listItem->setY(prevItem->y() + prevItem->height());
        } else {
            ListItem *currItem = itemAtIndex(modelIndex);
            if (currItem) {
                // There's something already in m_visibleItems at out index, meaning this is an insert, so attach to its top
                listItem->setY(currItem->y() - listItem->height());
            } else {
                ListItem *nextItem = itemAtIndex(modelIndex + 1);
                if (nextItem) {
                    listItem->setY(nextItem->y() - listItem->height());
                } else if (modelIndex == 0) {
                    listItem->setY(-m_clipItem->y() + (m_headerItem ? m_headerItem->height() : 0));
                } else if (!m_visibleItems.isEmpty()) {
                    lostItem = true;
                }
            }
        }
        if (lostItem) {
            listItem->setCulled(true);
            releaseItem(listItem);
            listItem = nullptr;
        } else {
            listItem->setCulled(listItem->y() + listItem->height() + m_clipItem->y() <= contentY() || listItem->y() + m_clipItem->y() >= contentY() + height());
            if (m_visibleItems.isEmpty()) {
                m_visibleItems << listItem;
            } else {
                m_visibleItems.insert(modelIndex - m_firstVisibleIndex, listItem);
            }
            if (m_firstVisibleIndex < 0 || modelIndex < m_firstVisibleIndex) {
                m_firstVisibleIndex = modelIndex;
                polish();
            }
            if (listItem->sectionItem()) {
                QQmlContext *context = QQmlEngine::contextForObject(listItem->sectionItem())->parentContext();
                context->setContextProperty(QStringLiteral("delegateIndex"), modelIndex);
            }
            adjustMinYExtent();
            m_contentHeightDirty = true;
        }
        return listItem;
    }
}

void ListViewWithPageHeader::itemCreated(int modelIndex, QObject *object)
{
    QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
    if (!item) {
        qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
        return;
    }
//     qDebug() << "ListViewWithPageHeader::itemCreated" << modelIndex << item;
    // Check we are not being taken down and don't paint anything
    // TODO Check if we still need this in 5.2
    // For reproduction just inifnite loop testDash or testDashContent
    if (!QQmlEngine::contextForObject(this)->parentContext())
        return;

    item->setParentItem(m_clipItem);
    QQmlContext *context = QQmlEngine::contextForObject(item)->parentContext();
    context->setContextProperty(QStringLiteral("ListViewWithPageHeader"), this);
    context->setContextProperty(QStringLiteral("heightToClip"), QVariant::fromValue<int>(0));
    if (modelIndex == m_asyncRequestedIndex) {
        createItem(modelIndex, false);
        refill();
    }
}

void ListViewWithPageHeader::updateClipItem()
{
    m_clipItem->setHeight(height() - m_headerItemShownHeight);
    m_clipItem->setY(contentY() + m_headerItemShownHeight);
    m_clipItem->setClip(!m_forceNoClip && m_headerItemShownHeight > 0);
}

void ListViewWithPageHeader::onContentHeightChanged()
{
    updateClipItem();
}

void ListViewWithPageHeader::onContentWidthChanged()
{
    m_clipItem->setWidth(contentItem()->width());
}

void ListViewWithPageHeader::onHeightChanged()
{
    polish();
}


void ListViewWithPageHeader::onModelUpdated(const QQmlChangeSet &changeSet, bool /*reset*/)
{
    // TODO Do something with reset
//     qDebug() << "ListViewWithPageHeader::onModelUpdated" << changeSet << reset;
    const auto oldFirstVisibleIndex = m_firstVisibleIndex;

    Q_FOREACH(const QQmlChangeSet::Change remove, changeSet.removes()) {
//         qDebug() << "ListViewWithPageHeader::onModelUpdated Remove" << remove.index << remove.count;
        if (remove.index + remove.count > m_firstVisibleIndex && remove.index < m_firstVisibleIndex + m_visibleItems.count()) {
            const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
            // If all the items we are removing are either not created or culled
            // we have to grow down to avoid viewport changing
            bool growDown = true;
            for (int i = 0; growDown && i < remove.count; ++i) {
                const int modelIndex = remove.index + i;
                ListItem *item = itemAtIndex(modelIndex);
                if (item && !item->culled()) {
                    growDown = false;
                }
            }
            for (int i = remove.count - 1; i >= 0; --i) {
                const int visibleIndex = remove.index + i - m_firstVisibleIndex;
                if (visibleIndex >= 0 && visibleIndex < m_visibleItems.count()) {
                    ListItem *item = m_visibleItems[visibleIndex];
                    // Pass the section item down if needed
                    if (item->sectionItem() && visibleIndex + 1 < m_visibleItems.count()) {
                        ListItem *nextItem = m_visibleItems[visibleIndex + 1];
                        if (!nextItem->sectionItem()) {
                            nextItem->setSectionItem(item->sectionItem());
                            item->setSectionItem(nullptr);
                        }
                    }
                    releaseItem(item);
                    m_visibleItems.removeAt(visibleIndex);
                }
            }
            if (growDown) {
                adjustMinYExtent();
            } else if (remove.index <= m_firstVisibleIndex && !m_visibleItems.isEmpty()) {
                m_visibleItems.first()->setY(oldFirstValidIndexPos);
            }
            if (m_visibleItems.isEmpty()) {
                m_firstVisibleIndex = -1;
            } else {
                m_firstVisibleIndex -= qMax(0, m_firstVisibleIndex - remove.index);
            }
        } else if (remove.index + remove.count <= m_firstVisibleIndex) {
            m_firstVisibleIndex -= remove.count;
        }
        for (int i = remove.count - 1; i >= 0; --i) {
            const int modelIndex = remove.index + i;
            if (modelIndex == m_asyncRequestedIndex) {
                m_asyncRequestedIndex = -1;
            } else if (modelIndex < m_asyncRequestedIndex) {
                m_asyncRequestedIndex--;
            }
        }
    }

    Q_FOREACH(const QQmlChangeSet::Change insert, changeSet.inserts()) {
//         qDebug() << "ListViewWithPageHeader::onModelUpdated Insert" << insert.index << insert.count;
        const bool insertingInValidIndexes = insert.index > m_firstVisibleIndex && insert.index < m_firstVisibleIndex + m_visibleItems.count();
        const bool firstItemWithViewOnTop = insert.index == 0 && m_firstVisibleIndex == 0 && m_visibleItems.first()->y() + m_clipItem->y() > contentY();
        if (insertingInValidIndexes || firstItemWithViewOnTop)
        {
            // If the items we are adding won't be really visible
            // we grow up instead of down to not change the viewport
            bool growUp = false;
            if (!firstItemWithViewOnTop) {
                for (int i = 0; i < m_visibleItems.count(); ++i) {
                    if (!m_visibleItems[i]->culled()) {
                        if (insert.index <= m_firstVisibleIndex + i) {
                            growUp = true;
                        }
                        break;
                    }
                }
            }

            const qreal oldFirstValidIndexPos = m_visibleItems.first()->y();
            for (int i = insert.count - 1; i >= 0; --i) {
                const int modelIndex = insert.index + i;
                ListItem *item = createItem(modelIndex, false);
                if (growUp) {
                    ListItem *firstItem = m_visibleItems.first();
                    firstItem->setY(firstItem->y() - item->height());
                }
                // Adding an item may break a "same section" chain, so check
                // if we need adding a new section item
                if (m_sectionDelegate) {
                    ListItem *nextItem = itemAtIndex(modelIndex + 1);
                    if (nextItem && !nextItem->sectionItem()) {
                        nextItem->setSectionItem(getSectionItem(modelIndex + 1, true /* alredy inserted into m_visibleItems*/));
                        if (growUp && nextItem->sectionItem()) {
                            ListItem *firstItem = m_visibleItems.first();
                            firstItem->setY(firstItem->y() - nextItem->sectionItem()->height());
                        }
                    }
                }
            }
            if (firstItemWithViewOnTop) {
                ListItem *firstItem = m_visibleItems.first();
                firstItem->setY(oldFirstValidIndexPos);
            }
            adjustMinYExtent();
        } else if (insert.index <= m_firstVisibleIndex) {
            m_firstVisibleIndex += insert.count;
        }

        for (int i = insert.count - 1; i >= 0; --i) {
            const int modelIndex = insert.index + i;
            if (modelIndex <= m_asyncRequestedIndex) {
                m_asyncRequestedIndex++;
            }
        }
    }

    Q_FOREACH(const QQmlChangeSet::Change change, changeSet.changes()) {
        for (int i = change.start(); i < change.end(); ++i) {
            updateSectionItem(i);
        }
        // Also update the section header for the next item after the change since it may be influenced
        updateSectionItem(change.end());
    }

    if (m_firstVisibleIndex != oldFirstVisibleIndex) {
        if (m_visibleItems.isEmpty()) {
            initializeValuesForEmptyList();
        } else {
            adjustMinYExtent();
        }
    }

    for (int i = 0; i < m_visibleItems.count(); ++i) {
        ListItem *item = m_visibleItems[i];
        if (item->sectionItem()) {
            QQmlContext *context = QQmlEngine::contextForObject(item->sectionItem())->parentContext();
            context->setContextProperty(QStringLiteral("delegateIndex"), m_firstVisibleIndex + i);
        }
    }

    layout();
    polish();
    m_contentHeightDirty = true;
}

void ListViewWithPageHeader::contentYAnimationRunningChanged(bool running)
{
    setInteractive(!running);
    if (!running) {
        m_contentHeightDirty = true;
        polish();
    }
}

void ListViewWithPageHeader::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
{
    const qreal heightDiff = newGeometry.height() - oldGeometry.height();
    if (heightDiff != 0) {
        if (!m_inContentHeightKeepHeaderShown && oldGeometry.y() + oldGeometry.height() + m_clipItem->y() <= contentY() && !m_visibleItems.isEmpty()) {
            ListItem *firstItem = m_visibleItems.first();
            firstItem->setY(firstItem->y() - heightDiff);
            adjustMinYExtent();
            layout();
        }
        refill();
        adjustMinYExtent();
        polish();
        m_contentHeightDirty = true;
    }
}

void ListViewWithPageHeader::itemImplicitHeightChanged(QQuickItem *item)
{
    if (item == m_headerItem) {
        const qreal diff = m_headerItem->implicitHeight() - m_previousHeaderImplicitHeight;
        if (diff != 0) {
            adjustHeader(diff);
            m_previousHeaderImplicitHeight = m_headerItem->implicitHeight();
            layout();
            polish();
            m_contentHeightDirty = true;
        }
    }
}

void ListViewWithPageHeader::headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY)
{
    const qreal heightDiff = newHeaderHeight - oldHeaderHeight;
    if (m_headerItemShownHeight > 0) {
        // If the header is shown because of the clip
        // Change its size
        m_headerItemShownHeight += heightDiff;
        m_headerItemShownHeight = qBound(static_cast<qreal>(0.), m_headerItemShownHeight, newHeaderHeight);
        updateClipItem();
        adjustMinYExtent();
        Q_EMIT headerItemShownHeightChanged();
    } else {
        if (oldHeaderY + oldHeaderHeight > contentY()) {
            // If the header is shown because its position
            // Change its size
            ListItem *firstItem = m_visibleItems.first();
            firstItem->setY(firstItem->y() + heightDiff);
            layout();
        } else {
            // If the header is not on screen, just change the start of the list
            // so the viewport is not changed
            adjustMinYExtent();
        }
    }
}


void ListViewWithPageHeader::adjustMinYExtent()
{
    if (m_visibleItems.isEmpty() || contentHeight() < height()) {
        m_minYExtent = 0;
    } else {
        qreal nonCreatedHeight = 0;
        if (m_firstVisibleIndex != 0) {
            // Calculate the average height of items to estimate the position of the list start
            const int visibleItems = m_visibleItems.count();
            qreal visibleItemsHeight = 0;
            Q_FOREACH(ListItem *item, m_visibleItems) {
                visibleItemsHeight += item->height();
            }
            nonCreatedHeight = m_firstVisibleIndex * visibleItemsHeight / visibleItems;
//             qDebug() << m_firstVisibleIndex << visibleItemsHeight << visibleItems << nonCreatedHeight;
        }
        const qreal headerHeight = (m_headerItem ? m_headerItem->implicitHeight() : 0);
        m_minYExtent = nonCreatedHeight - m_visibleItems.first()->y() - m_clipItem->y() + headerHeight;
        if (m_minYExtent != 0 && qFuzzyIsNull(m_minYExtent)) {
            m_minYExtent = 0;
            m_visibleItems.first()->setY(nonCreatedHeight - m_clipItem->y() + headerHeight);
        }
    }
}

ListViewWithPageHeader::ListItem *ListViewWithPageHeader::itemAtIndex(int modelIndex) const
{
    const int visibleIndexedModelIndex = modelIndex - m_firstVisibleIndex;
    if (visibleIndexedModelIndex >= 0 && visibleIndexedModelIndex < m_visibleItems.count())
        return m_visibleItems[visibleIndexedModelIndex];

    return nullptr;
}

void ListViewWithPageHeader::layout()
{
    if (m_inLayout)
        return;

    m_inLayout = true;
    if (!m_visibleItems.isEmpty()) {
        const qreal visibleFrom = contentY() - m_clipItem->y() + m_headerItemShownHeight;
        const qreal visibleTo = contentY() + height() - m_clipItem->y();

        qreal pos = m_visibleItems.first()->y();

//         qDebug() << "ListViewWithPageHeader::layout Updating positions and heights. contentY" << contentY() << "minYExtent" << minYExtent();
        int firstReallyVisibleItem = -1;
        int modelIndex = m_firstVisibleIndex;
        Q_FOREACH(ListItem *item, m_visibleItems) {
            const bool cull = pos + item->height() <= visibleFrom || pos >= visibleTo;
            item->setCulled(cull);
            item->setY(pos);
            if (!cull && firstReallyVisibleItem == -1) {
                firstReallyVisibleItem = modelIndex;
                if (m_topSectionItem) {
                    // Positing the top section sticky item is a two step process
                    // First we set it either we cull it (because it doesn't need to be sticked to the top)
                    // or stick it to the top
                    // Then after the loop we'll make sure that if there's another section just below it
                    // pushed the sticky section up to make it disappear
                    const qreal topSectionStickPos = m_headerItemShownHeight + contentY() - m_clipItem->y();
                    bool showStickySectionItem;
                    // We need to show the "top section sticky item" when the position at the "top" of the
                    // viewport is bigger than the start of the position of the first visible item
                    // i.e. the first visible item starts before the viewport, or when the first
                    // visible item starts just at the viewport start and it does not have its own section item
                    if (topSectionStickPos > pos) {
                        showStickySectionItem = true;
                    } else if (topSectionStickPos == pos) {
                        showStickySectionItem = !item->sectionItem();
                    } else {
                        showStickySectionItem = false;
                    }
                    if (!showStickySectionItem) {
                        QQuickItemPrivate::get(m_topSectionItem)->setCulled(true);
                        if (item->sectionItem()) {
                            // This seems it should happen since why would we cull the top section
                            // if the first visible item has no section header? This only happens briefly
                            // when increasing the height of a list that is at the bottom, the m_topSectionItem
                            // gets shown shortly in the next polish call
                            QQuickItemPrivate::get(item->sectionItem())->setCulled(false);
                        }
                    } else {
                        // Update the top sticky section header
                        const QString section = m_delegateModel->stringValue(modelIndex, m_sectionProperty);
                        QQmlContext *context = QQmlEngine::contextForObject(m_topSectionItem)->parentContext();
                        context->setContextProperty(QStringLiteral("section"), section);

                        QQuickItemPrivate::get(m_topSectionItem)->setCulled(false);
                        m_topSectionItem->setY(topSectionStickPos);
                        int delegateIndex = modelIndex;
                        // Look for the first index with this section text
                        while (delegateIndex > 0) {
                            const QString prevSection = m_delegateModel->stringValue(delegateIndex - 1, m_sectionProperty);
                            if (prevSection != section)
                                break;
                            delegateIndex--;
                        }
                        context->setContextProperty(QStringLiteral("delegateIndex"), delegateIndex);
                        if (item->sectionItem()) {
                            QQuickItemPrivate::get(item->sectionItem())->setCulled(true);
                        }
                    }
                }
            }
            QQmlContext *context = QQmlEngine::contextForObject(item->m_item)->parentContext();
            const qreal clipFrom = visibleFrom + (!item->sectionItem() && m_topSectionItem && !QQuickItemPrivate::get(m_topSectionItem)->culled ? m_topSectionItem->height() : 0);
            if (!cull && pos < clipFrom) {
                context->setContextProperty(QStringLiteral("heightToClip"), clipFrom - pos);
            } else {
                context->setContextProperty(QStringLiteral("heightToClip"), QVariant::fromValue<int>(0));
            }
//             qDebug() << "ListViewWithPageHeader::layout" << item->m_item;
            pos += item->height();
            ++modelIndex;
        }

        // Second step of section sticky item positioning
        // Look at the next section header, check if it's pushing up the sticky one
        if (m_topSectionItem) {
            if (firstReallyVisibleItem >= 0) {
                for (int i = firstReallyVisibleItem - m_firstVisibleIndex + 1; i < m_visibleItems.count(); ++i) {
                    ListItem *item = m_visibleItems[i];
                    if (item->sectionItem()) {
                        if (m_topSectionItem->y() + m_topSectionItem->height() > item->y()) {
                            m_topSectionItem->setY(item->y() - m_topSectionItem->height());
                        }
                        break;
                    }
                }
            }
        }
    }
    m_inLayout = false;
}

void ListViewWithPageHeader::updatePolish()
{
    // Check we are not being taken down and don't paint anything
    // TODO Check if we still need this in 5.2
    // For reproduction just inifnite loop testDash or testDashContent
    if (!QQmlEngine::contextForObject(this)->parentContext())
        return;

    Q_FOREACH(ListItem *item, m_itemsToRelease)
        reallyReleaseItem(item);
    m_itemsToRelease.clear();

    if (!model())
        return;

    layout();

    refill();

    if (m_contentHeightDirty) {
        qreal contentHeight;
        if (m_visibleItems.isEmpty()) {
            contentHeight = m_headerItem ? m_headerItem->height() : 0;
        } else {
            const int modelCount = model()->rowCount();
            const int visibleItems = m_visibleItems.count();
            const int lastValidIndex = m_firstVisibleIndex + visibleItems - 1;
            qreal nonCreatedHeight = 0;
            if (lastValidIndex != modelCount - 1) {
                const int visibleItems = m_visibleItems.count();
                qreal visibleItemsHeight = 0;
                Q_FOREACH(ListItem *item, m_visibleItems) {
                    visibleItemsHeight += item->height();
                }
                const int unknownSizes = modelCount - (m_firstVisibleIndex + visibleItems);
                nonCreatedHeight = unknownSizes * visibleItemsHeight / visibleItems;
            }
            ListItem *item = m_visibleItems.last();
            contentHeight = nonCreatedHeight + item->y() + item->height() + m_clipItem->y();
            if (m_firstVisibleIndex != 0) {
                // Make sure that if we are shrinking we tell the view we still fit
                m_minYExtent = qMax(m_minYExtent, -(contentHeight - height()));
            }
        }

        m_contentHeightDirty = false;
        adjustMinYExtent();
        if (contentHeight < height()) {
            // need this since in the previous call to adjustMinYExtent contentHeight is not set yet
            m_minYExtent = 0;
        }
        m_inContentHeightKeepHeaderShown = m_headerItem && m_headerItem->y() == contentY();
        setContentHeight(contentHeight);
        m_inContentHeightKeepHeaderShown = false;
    }
}

#include "moc_listviewwithpageheader.cpp"