Unity 8
 All Classes Functions Properties
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 #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
27 #include <private/qquickvisualdatamodel_p.h>
28 #else
29 #include <private/qqmldelegatemodel_p.h>
30 #include <qqmlinfo.h>
31 #endif
32 #pragma GCC diagnostic pop
33 
34 class AbstractDashView : public QQuickItem
35 {
36  Q_OBJECT
37 
38  Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged)
39  Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
40  Q_PROPERTY(qreal columnSpacing READ columnSpacing WRITE setColumnSpacing NOTIFY columnSpacingChanged)
41  Q_PROPERTY(qreal rowSpacing READ rowSpacing WRITE setRowSpacing NOTIFY rowSpacingChanged)
42  Q_PROPERTY(qreal delegateCreationBegin READ delegateCreationBegin
43  WRITE setDelegateCreationBegin
44  NOTIFY delegateCreationBeginChanged
45  RESET resetDelegateCreationBegin)
46  Q_PROPERTY(qreal delegateCreationEnd READ delegateCreationEnd
47  WRITE setDelegateCreationEnd
48  NOTIFY delegateCreationEndChanged
49  RESET resetDelegateCreationEnd)
50 
51 friend class VerticalJournalTest;
52 friend class HorizontalJournalTest;
53 friend class OrganicGridTest;
54 
55 public:
56  AbstractDashView();
57 
58  QAbstractItemModel *model() const;
59  void setModel(QAbstractItemModel *model);
60 
61  QQmlComponent *delegate() const;
62  void setDelegate(QQmlComponent *delegate);
63 
64  qreal columnSpacing() const;
65  void setColumnSpacing(qreal columnSpacing);
66 
67  qreal rowSpacing() const;
68  void setRowSpacing(qreal rowSpacing);
69 
70  qreal delegateCreationBegin() const;
71  void setDelegateCreationBegin(qreal);
72  void resetDelegateCreationBegin();
73 
74  qreal delegateCreationEnd() const;
75  void setDelegateCreationEnd(qreal);
76  void resetDelegateCreationEnd();
77 
78 Q_SIGNALS:
79  void modelChanged();
80  void delegateChanged();
81  void columnSpacingChanged();
82  void rowSpacingChanged();
83  void delegateCreationBeginChanged();
84  void delegateCreationEndChanged();
85 
86 protected Q_SLOTS:
87  void relayout();
88 
89 protected:
90  void updatePolish() override;
91  void componentComplete() override;
92 
93  void releaseItem(QQuickItem *item);
94  void setImplicitHeightDirty();
95 
96 private Q_SLOTS:
97 #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
98  void itemCreated(int modelIndex, QQuickItem *item);
99  void onModelUpdated(const QQuickChangeSet &changeSet, bool reset);
100 #else
101  void itemCreated(int modelIndex, QObject *object);
102  void onModelUpdated(const QQmlChangeSet &changeSet, bool reset);
103 #endif
104  void onHeightChanged();
105 
106 private:
107  void createDelegateModel();
108  void refill();
109  bool addVisibleItems(qreal fillFromY, qreal fillToY, bool asynchronous);
110  QQuickItem *createItem(int modelIndex, bool asynchronous);
111 
112  virtual void findBottomModelIndexToAdd(int *modelIndex, qreal *yPos) = 0;
113  virtual void findTopModelIndexToAdd(int *modelIndex, qreal *yPos) = 0;
114  virtual void addItemToView(int modelIndex, QQuickItem *item) = 0;
115  virtual bool removeNonVisibleItems(qreal bufferFromY, qreal bufferToY) = 0;
116  virtual void cleanupExistingItems() = 0;
117  virtual void doRelayout() = 0;
118  virtual void updateItemCulling(qreal visibleFromY, qreal visibleToY) = 0;
119  virtual void calculateImplicitHeight() = 0;
120 #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
121  virtual void processModelRemoves(const QVector<QQuickChangeSet::Remove> &removes) = 0;
122 #else
123  virtual void processModelRemoves(const QVector<QQmlChangeSet::Remove> &removes) = 0;
124 #endif
125 
126 #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
127  QQuickVisualDataModel *m_delegateModel;
128 #else
129  QQmlDelegateModel *m_delegateModel;
130 #endif
131 
132  // Index we are waiting because we requested it asynchronously
133  int m_asyncRequestedIndex;
134 
135  int m_columnSpacing;
136  int m_rowSpacing;
137  qreal m_delegateCreationBegin;
138  qreal m_delegateCreationEnd;
139  bool m_delegateCreationBeginValid;
140  bool m_delegateCreationEndValid;
141  bool m_needsRelayout;
142  bool m_delegateValidated;
143  bool m_implicitHeightDirty;
144 };
145 
146 #endif