2 * Copyright (C) 2013 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 LISTVIEWWITHPAGEHEADER_H
18#define LISTVIEWWITHPAGEHEADER_H
20#include <private/qquickitemchangelistener_p.h>
21#include <private/qquickflickable_p.h>
23class QAbstractItemModel;
24class QQuickNumberAnimation;
26class QQmlDelegateModel;
30 Note for users of this class
32 ListViewWithPageHeader already loads delegates async when appropiate so if
33 your delegate uses a Loader you should not enable the asynchronous feature since
34 that will need to introduce sizing problems
36 With the double async it may happen what while we are scrolling down
37 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)
38 we are already "at the end" of the list, but then a few milliseconds later the loader finishes loading and we could
39 have kept scrolling. This is specially visible at the end of the list where you realize
40 that scrolling ended a bit before the end of the list but the speed of the flicking was good
43 By not having the second async we get a better sizing when the delegate is created and things work better
46class ListViewWithPageHeader : public QQuickFlickable, public QQuickItemChangeListener
49 Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged)
50 Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
51 Q_PROPERTY(QQuickItem *pageHeader READ header WRITE setHeader NOTIFY headerChanged)
52 Q_PROPERTY(QQmlComponent *sectionDelegate READ sectionDelegate WRITE setSectionDelegate NOTIFY sectionDelegateChanged)
53 Q_PROPERTY(QString sectionProperty READ sectionProperty WRITE setSectionProperty NOTIFY sectionPropertyChanged)
54 Q_PROPERTY(bool forceNoClip READ forceNoClip WRITE setForceNoClip NOTIFY forceNoClipChanged)
55 Q_PROPERTY(int stickyHeaderHeight READ stickyHeaderHeight NOTIFY stickyHeaderHeightChanged)
57 friend class ListViewWithPageHeaderTest;
58 friend class ListViewWithPageHeaderTestSection;
59 friend class ListViewWithPageHeaderTestExternalModel;
62 ListViewWithPageHeader();
63 ~ListViewWithPageHeader();
65 QAbstractItemModel *model() const;
66 void setModel(QAbstractItemModel *model);
68 QQmlComponent *delegate() const;
69 void setDelegate(QQmlComponent *delegate);
71 QQuickItem *header() const;
72 void setHeader(QQuickItem *header);
74 QQmlComponent *sectionDelegate() const;
75 void setSectionDelegate(QQmlComponent *delegate);
77 QString sectionProperty() const;
78 void setSectionProperty(const QString &property);
80 bool forceNoClip() const;
81 void setForceNoClip(bool noClip);
83 int stickyHeaderHeight() const;
85 Q_INVOKABLE void positionAtBeginning();
86 Q_INVOKABLE void showHeader();
87 Q_INVOKABLE QQuickItem *item(int modelIndex) const;
89 // The index has to be created for this to try to do something
90 // Created items are those visible and the precached ones
91 // Returns if the item existed or not
92 Q_INVOKABLE bool maximizeVisibleArea(int modelIndex);
93 Q_INVOKABLE bool maximizeVisibleArea(int modelIndex, int itemHeight);
97 void delegateChanged();
99 void sectionDelegateChanged();
100 void sectionPropertyChanged();
101 void forceNoClipChanged();
102 void stickyHeaderHeightChanged();
105 void componentComplete() override;
106 void viewportMoved(Qt::Orientations orient) override;
107 qreal minYExtent() const override;
108 void itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry) override;
109 void itemImplicitHeightChanged(QQuickItem *item) override;
110 void updatePolish() override;
113 void itemCreated(int modelIndex, QObject *object);
114 void onContentHeightChanged();
115 void onContentWidthChanged();
116 void onHeightChanged();
117 void onModelUpdated(const QQmlChangeSet &changeSet, bool reset);
118 void onShowHeaderAnimationFinished();
124 qreal height() const;
127 void setY(qreal newY);
130 void setCulled(bool culled);
132 QQuickItem *sectionItem() const { return m_sectionItem; }
133 void setSectionItem(QQuickItem *sectionItem);
137 QQuickItem *m_sectionItem;
140 bool maximizeVisibleArea(ListItem *listItem, int listItemHeight);
142 void createDelegateModel();
146 bool addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous);
147 bool removeNonVisibleItems(qreal bufferFrom, qreal bufferTo);
148 ListItem *createItem(int modelIndex, bool asynchronous);
150 void adjustHeader(qreal diff);
151 void adjustMinYExtent();
152 void updateClipItem();
153 void headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY);
154 ListItem *itemAtIndex(int modelIndex) const; // Returns the item at modelIndex if has been created
155 void releaseItem(ListItem *item);
156 void reallyReleaseItem(ListItem *item);
157 void updateWatchedRoles();
158 QQuickItem *getSectionItem(int modelIndex, bool alreadyInserted);
159 QQuickItem *getSectionItem(const QString §ionText);
161 QQmlDelegateModel *m_delegateModel;
163 // Index we are waiting because we requested it asynchronously
164 int m_asyncRequestedIndex;
166 // Used to only give a warning once if the delegate does not return objects
167 bool m_delegateValidated;
169 // Visible indexes, [0] is m_firstValidIndex, [0+1] is m_firstValidIndex +1, ...
170 QList<ListItem *> m_visibleItems;
171 int m_firstVisibleIndex;
175 QQuickItem *m_clipItem;
177 // If any of the heights has changed
178 // or new items have been added/removed
179 bool m_contentHeightDirty;
181 QQuickItem *m_headerItem;
182 qreal m_previousContentY;
183 qreal m_previousHeaderImplicitHeight;
184 qreal m_headerItemShownHeight; // The height of header shown when the header is shown outside its topmost position
185 // i.e. it's being shown after dragging down in the middle of the list
186 enum ContentYAnimationType { ContentYAnimationShowHeader, ContentYAnimationMaximizeVisibleArea };
187 ContentYAnimationType contentYAnimationType;
188 QQuickNumberAnimation *m_contentYAnimation;
190 QQmlComponent *m_sectionDelegate;
191 QString m_sectionProperty;
192 QQuickItem *m_topSectionItem;
196 bool m_inContentHeightKeepHeaderShown;
198 // Qt 5.0 doesn't like releasing the items just after itemCreated
199 // so we delay the releasing until the next updatePolish
200 QList<ListItem *> m_itemsToRelease;
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list