Unity 8
listviewwithpageheader.h
1 /*
2  * Copyright (C) 2013 Canonical, Ltd.
3  *
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.
7  *
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.
12  *
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/>.
15  */
16 
17 #ifndef LISTVIEWWITHPAGEHEADER_H
18 #define LISTVIEWWITHPAGEHEADER_H
19 
20 #include <private/qquickitemchangelistener_p.h>
21 #include <private/qquickflickable_p.h>
22 
23 class QAbstractItemModel;
24 class QQuickNumberAnimation;
25 class QQmlChangeSet;
26 class QQmlDelegateModel;
27 
28 
46 class ListViewWithPageHeader : public QQuickFlickable, public QQuickItemChangeListener
47 {
48  Q_OBJECT
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)
56  Q_PROPERTY(qreal headerItemShownHeight READ headerItemShownHeight NOTIFY headerItemShownHeightChanged)
57  Q_PROPERTY(int cacheBuffer READ cacheBuffer WRITE setCacheBuffer NOTIFY cacheBufferChanged)
58 
59  friend class ListViewWithPageHeaderTest;
60  friend class ListViewWithPageHeaderTestSection;
61  friend class ListViewWithPageHeaderTestExternalModel;
62 
63 public:
66 
67  QAbstractItemModel *model() const;
68  void setModel(QAbstractItemModel *model);
69 
70  QQmlComponent *delegate() const;
71  void setDelegate(QQmlComponent *delegate);
72 
73  QQuickItem *header() const;
74  void setHeader(QQuickItem *header);
75 
76  QQmlComponent *sectionDelegate() const;
77  void setSectionDelegate(QQmlComponent *delegate);
78 
79  QString sectionProperty() const;
80  void setSectionProperty(const QString &property);
81 
82  bool forceNoClip() const;
83  void setForceNoClip(bool noClip);
84 
85  int stickyHeaderHeight() const;
86  qreal headerItemShownHeight() const;
87 
88  int cacheBuffer() const;
89  void setCacheBuffer(int cacheBuffer);
90 
91  Q_INVOKABLE void positionAtBeginning();
92  Q_INVOKABLE void showHeader();
93  Q_INVOKABLE int firstCreatedIndex() const;
94  Q_INVOKABLE int createdItemCount() const;
95  Q_INVOKABLE QQuickItem *item(int modelIndex) const;
96 
97  // The index has to be created for this to try to do something
98  // Created items are those visible and the precached ones
99  // Returns if the item existed or not
100  Q_INVOKABLE bool maximizeVisibleArea(int modelIndex);
101  Q_INVOKABLE bool maximizeVisibleArea(int modelIndex, int itemHeight);
102 
103 Q_SIGNALS:
104  void modelChanged();
105  void delegateChanged();
106  void headerChanged();
107  void sectionDelegateChanged();
108  void sectionPropertyChanged();
109  void forceNoClipChanged();
110  void stickyHeaderHeightChanged();
111  void headerItemShownHeightChanged();
112  void cacheBufferChanged();
113 
114 protected:
115  void componentComplete() override;
116  void viewportMoved(Qt::Orientations orient) override;
117  qreal minYExtent() const override;
118  qreal maxYExtent() const override;
119  void itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry) override;
120  void itemImplicitHeightChanged(QQuickItem *item) override;
121  void updatePolish() override;
122 
123 private Q_SLOTS:
124  void itemCreated(int modelIndex, QObject *object);
125  void onContentHeightChanged();
126  void onContentWidthChanged();
127  void onHeightChanged();
128  void onModelUpdated(const QQmlChangeSet &changeSet, bool reset);
129  void contentYAnimationRunningChanged(bool running);
130 
131 private:
132  class ListItem
133  {
134  public:
135  qreal height() const;
136 
137  qreal y() const;
138  void setY(qreal newY);
139 
140  bool culled() const;
141  void setCulled(bool culled);
142 
143  QQuickItem *sectionItem() const { return m_sectionItem; }
144  void setSectionItem(QQuickItem *sectionItem);
145 
146  QQuickItem *m_item;
147  private:
148  QQuickItem *m_sectionItem;
149  };
150 
151  bool maximizeVisibleArea(ListItem *listItem, int listItemHeight);
152 
153  void createDelegateModel();
154 
155  void layout();
156  void refill();
157  bool addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous);
158  bool removeNonVisibleItems(qreal bufferFrom, qreal bufferTo);
159  ListItem *createItem(int modelIndex, bool asynchronous);
160 
161  void adjustHeader(qreal diff);
162  void adjustMinYExtent();
163  void updateClipItem();
164  void headerHeightChanged(qreal newHeaderHeight, qreal oldHeaderHeight, qreal oldHeaderY);
165  ListItem *itemAtIndex(int modelIndex) const; // Returns the item at modelIndex if has been created
166  void releaseItem(ListItem *item);
167  void reallyReleaseItem(ListItem *item);
168  void updateWatchedRoles();
169  QQuickItem *getSectionItem(int modelIndex, bool alreadyInserted);
170  QQuickItem *getSectionItem(const QString &sectionText, bool watchGeometry = true);
171  void updateSectionItem(int modelIndex);
172  void initializeValuesForEmptyList();
173 
174  QQmlDelegateModel *m_delegateModel;
175 
176  // Index we are waiting because we requested it asynchronously
177  int m_asyncRequestedIndex;
178 
179  // Used to only give a warning once if the delegate does not return objects
180  bool m_delegateValidated;
181 
182  // Visible indexes, [0] is m_firstValidIndex, [0+1] is m_firstValidIndex +1, ...
183  QList<ListItem *> m_visibleItems;
184  int m_firstVisibleIndex;
185 
186  qreal m_minYExtent;
187 
188  QQuickItem *m_clipItem;
189 
190  // If any of the heights has changed
191  // or new items have been added/removed
192  bool m_contentHeightDirty;
193 
194  QQuickItem *m_headerItem;
195  qreal m_previousContentY;
196  qreal m_previousHeaderImplicitHeight;
197  qreal m_headerItemShownHeight; // The height of header shown when the header is shown outside its topmost position
198  // i.e. it's being shown after dragging down in the middle of the list
199  enum ContentYAnimationType { ContentYAnimationShowHeader, ContentYAnimationMaximizeVisibleArea };
200  ContentYAnimationType contentYAnimationType;
201  QQuickNumberAnimation *m_contentYAnimation;
202 
203  QQmlComponent *m_sectionDelegate;
204  QString m_sectionProperty;
205  QQuickItem *m_topSectionItem;
206 
207  bool m_forceNoClip;
208  bool m_inLayout;
209  bool m_inContentHeightKeepHeaderShown;
210  int m_cacheBuffer;
211 
212  // Qt 5.0 doesn't like releasing the items just after itemCreated
213  // so we delay the releasing until the next updatePolish
214  QList<ListItem *> m_itemsToRelease;
215 };
216 
217 
218 #endif