2
* Copyright (C) 2013-2015 Canonical, Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#ifndef VERTICALVIEW_H
18
#define VERTICALVIEW_H
20
#include <private/qquickitemchangelistener_p.h>
21
#include <private/qquickflickable_p.h>
23
class QAbstractItemModel;
25
class QQmlDelegateModel;
29
Note for users of this class
31
VerticalView already loads delegates async when appropiate so if
32
your delegate uses a Loader you should not enable the asynchronous feature since
33
that will need to introduce sizing problems
35
With the double async it may happen what while we are scrolling down
36
we reach to a point where given the size of the just created delegate with loader not yet loaded (which will be very close to 0)
37
we are already "at the end" of the list, but then a few milliseconds later the loader finishes loading and we could
38
have kept scrolling. This is specially visible at the end of the list where you realize
39
that scrolling ended a bit before the end of the list but the speed of the flicking was good
42
By not having the second async we get a better sizing when the delegate is created and things work better
45
class VerticalView : public QQuickFlickable, public QQuickItemChangeListener
48
Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged)
49
Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
50
Q_PROPERTY(int cacheBuffer READ cacheBuffer WRITE setCacheBuffer NOTIFY cacheBufferChanged)
51
Q_PROPERTY(qreal spacing READ spacing WRITE setSpacing NOTIFY spacingChanged)
52
Q_PROPERTY(int count READ count NOTIFY countChanged)
53
Q_PROPERTY(int currentPageIndex READ currentPageIndex NOTIFY currentPageIndexChanged)
54
Q_PROPERTY(QQuickItem *currentPageItem READ currentPageItem NOTIFY currentPageItemChanged)
60
QAbstractItemModel *model() const;
61
void setModel(QAbstractItemModel *model);
63
QQmlComponent *delegate() const;
64
void setDelegate(QQmlComponent *delegate);
66
int cacheBuffer() const;
67
void setCacheBuffer(int cacheBuffer);
69
qreal spacing() const;
70
void setSpacing(qreal spacing);
74
int currentPageIndex() const;
75
QQuickItem *currentPageItem() const;
77
Q_INVOKABLE void positionAtBeginning();
78
Q_INVOKABLE void positionAtIndex(int index);
79
Q_INVOKABLE void positionAtEnd();
81
Q_INVOKABLE QQuickItem *itemAt(int modelIndex) const;
85
void delegateChanged();
86
void cacheBufferChanged();
87
void spacingChanged();
89
void currentPageIndexChanged();
90
void currentPageItemChanged();
93
void componentComplete() override;
94
void viewportMoved(Qt::Orientations orient) override;
95
qreal minYExtent() const override;
96
void itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry) override;
97
void updatePolish() override;
100
void _q_itemCreated(int modelIndex, QObject *object);
101
void _q_heightChanged();
102
void _q_modelUpdated(const QQmlChangeSet &changeSet, bool reset);
103
void _q_updateCurrentPageIndex();
109
qreal height() const;
112
void setY(qreal newY);
115
void setCulled(bool culled);
120
void createDelegateModel();
124
bool addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous);
125
bool removeNonVisibleItems(qreal bufferFrom, qreal bufferTo);
126
ListItem *createItem(int modelIndex, bool asynchronous);
128
void adjustMinYExtent();
129
ListItem *itemAtIndex(int modelIndex) const; // Returns the item at modelIndex if has been created
130
void releaseItem(ListItem *item);
131
void reallyReleaseItem(ListItem *item);
132
void updateWatchedRoles();
134
QQmlDelegateModel *m_delegateModel;
136
// Index we are waiting because we requested it asynchronously
137
int m_asyncRequestedIndex;
139
// Used to only give a warning once if the delegate does not return objects
140
bool m_delegateValidated;
142
// Visible indexes, [0] is m_firstValidIndex, [0+1] is m_firstValidIndex +1, ...
143
QList<ListItem *> m_visibleItems;
144
int m_firstVisibleIndex;
146
int m_currentPageIndex;
150
// If any of the heights has changed
151
// or new items have been added/removed
152
bool m_contentHeightDirty;
154
qreal m_previousContentY;
161
// Qt 5.0 doesn't like releasing the items just after itemCreated
162
// so we delay the releasing until the next updatePolish
163
QList<ListItem *> m_itemsToRelease;
167
#endif // VERTICALVIEW_H