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