Unity 8
abstractdashview.h
1 /*
2  * Copyright (C) 2013, 2014 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 ABSTRACTDASHVIEW_H
18 #define ABSTRACTDASHVIEW_H
19 
20 #include <QQuickItem>
21 
22 class QAbstractItemModel;
23 class QQmlComponent;
24 
25 #include <private/qqmldelegatemodel_p.h>
26 #include <qqmlinfo.h>
27 
28 class AbstractDashView : public QQuickItem
29 {
30  Q_OBJECT
31 
32  Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged)
33  Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
34  Q_PROPERTY(qreal columnSpacing READ columnSpacing WRITE setColumnSpacing NOTIFY columnSpacingChanged)
35  Q_PROPERTY(qreal rowSpacing READ rowSpacing WRITE setRowSpacing NOTIFY rowSpacingChanged)
36  Q_PROPERTY(int cacheBuffer READ cacheBuffer WRITE setCacheBuffer NOTIFY cacheBufferChanged)
37  Q_PROPERTY(qreal displayMarginBeginning READ displayMarginBeginning
38  WRITE setDisplayMarginBeginning
39  NOTIFY displayMarginBeginningChanged)
40  Q_PROPERTY(qreal displayMarginEnd READ displayMarginEnd
41  WRITE setDisplayMarginEnd
42  NOTIFY displayMarginEndChanged)
43 
44 friend class VerticalJournalTest;
45 friend class HorizontalJournalTest;
46 friend class OrganicGridTest;
47 
48 public:
49  AbstractDashView();
50 
51  QAbstractItemModel *model() const;
52  void setModel(QAbstractItemModel *model);
53 
54  QQmlComponent *delegate() const;
55  void setDelegate(QQmlComponent *delegate);
56 
57  qreal columnSpacing() const;
58  void setColumnSpacing(qreal columnSpacing);
59 
60  qreal rowSpacing() const;
61  void setRowSpacing(qreal rowSpacing);
62 
63  int cacheBuffer() const;
64  void setCacheBuffer(int);
65 
66  qreal displayMarginBeginning() const;
67  void setDisplayMarginBeginning(qreal);
68 
69  qreal displayMarginEnd() const;
70  void setDisplayMarginEnd(qreal);
71 
72 Q_SIGNALS:
73  void modelChanged();
74  void delegateChanged();
75  void columnSpacingChanged();
76  void rowSpacingChanged();
77  void cacheBufferChanged();
78  void displayMarginBeginningChanged();
79  void displayMarginEndChanged();
80 
81 protected Q_SLOTS:
82  void relayout();
83 
84 protected:
85  void updatePolish() override;
86  void componentComplete() override;
87 
88  void releaseItem(QQuickItem *item);
89  void setImplicitHeightDirty();
90 
91 private Q_SLOTS:
92  void itemCreated(int modelIndex, QObject *object);
93  void onModelUpdated(const QQmlChangeSet &changeSet, bool reset);
94  void onHeightChanged();
95 
96 private:
97  void createDelegateModel();
98  void refill();
99  bool addVisibleItems(qreal fillFromY, qreal fillToY, bool asynchronous);
100  QQuickItem *createItem(int modelIndex, bool asynchronous);
101 
102  virtual void findBottomModelIndexToAdd(int *modelIndex, qreal *yPos) = 0;
103  virtual void findTopModelIndexToAdd(int *modelIndex, qreal *yPos) = 0;
104  virtual void addItemToView(int modelIndex, QQuickItem *item) = 0;
105  virtual bool removeNonVisibleItems(qreal bufferFromY, qreal bufferToY) = 0;
106  virtual void cleanupExistingItems() = 0;
107  virtual void doRelayout() = 0;
108  virtual void updateItemCulling(qreal visibleFromY, qreal visibleToY) = 0;
109  virtual void calculateImplicitHeight() = 0;
110  virtual void processModelRemoves(const QVector<QQmlChangeSet::Change> &removes) = 0;
111 
112  QQmlDelegateModel *m_delegateModel;
113 
114  // Index we are waiting because we requested it asynchronously
115  int m_asyncRequestedIndex;
116 
117  int m_columnSpacing;
118  int m_rowSpacing;
119  int m_buffer;
120  qreal m_displayMarginBeginning;
121  qreal m_displayMarginEnd;
122  bool m_needsRelayout;
123  bool m_delegateValidated;
124  bool m_implicitHeightDirty;
125 };
126 
127 #endif