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