on xenial help sign in
Make organicgrid and journals share code
789c672Albert Astals
plugins/DashViews/CMakeLists.txt+1−1
@@ -24,7 +24,7 @@ remove_definitions(-DQT_NO_KEYWORDS)
24set(QMLPLUGIN_SRC
24set(QMLPLUGIN_SRC
25 plugin.cpp
25 plugin.cpp
26 listviewwithpageheader.cpp
26 listviewwithpageheader.cpp
27− abstractjournal.cpp
27+ abstractdashview.cpp
28 verticaljournal.cpp
28 verticaljournal.cpp
29 horizontaljournal.cpp
29 horizontaljournal.cpp
30 organicgrid.cpp
30 organicgrid.cpp
plugins/DashViews/abstractjournal.cpp → plugins/DashViews/abstractdashview.cpprenamed+32−32
@@ -1,5 +1,5 @@
1/*
1/*
2− * Copyright (C) 2013 Canonical, Ltd.
2+ * Copyright (C) 2013, 2014 Canonical, Ltd.
3 *
3 *
4 * This program is free software; you can redistribute it and/or modify
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
5 * it under the terms of the GNU General Public License as published by
@@ -14,11 +14,11 @@
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
15 */
16
16
17−#include "abstractjournal.h"
17+#include "abstractdashview.h"
18
18
19static const qreal bufferRatio = 0.5;
19static const qreal bufferRatio = 0.5;
20
20
21−AbstractJournal::AbstractJournal()
21+AbstractDashView::AbstractDashView()
22 : m_delegateModel(nullptr)
22 : m_delegateModel(nullptr)
23 , m_asyncRequestedIndex(-1)
23 , m_asyncRequestedIndex(-1)
24 , m_columnSpacing(0)
24 , m_columnSpacing(0)
@@ -35,12 +35,12 @@ AbstractJournal::AbstractJournal()
35 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
35 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
36}
36}
37
37
38−QAbstractItemModel *AbstractJournal::model() const
38+QAbstractItemModel *AbstractDashView::model() const
39{
39{
40 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
40 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
41}
41}
42
42
43−void AbstractJournal::setModel(QAbstractItemModel *model)
43+void AbstractDashView::setModel(QAbstractItemModel *model)
44{
44{
45 if (model != this->model()) {
45 if (model != this->model()) {
46 if (!m_delegateModel) {
46 if (!m_delegateModel) {
@@ -65,12 +65,12 @@ void AbstractJournal::setModel(QAbstractItemModel *model)
65 }
65 }
66}
66}
67
67
68−QQmlComponent *AbstractJournal::delegate() const
68+QQmlComponent *AbstractDashView::delegate() const
69{
69{
70 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
70 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
71}
71}
72
72
73−void AbstractJournal::setDelegate(QQmlComponent *delegate)
73+void AbstractDashView::setDelegate(QQmlComponent *delegate)
74{
74{
75 if (delegate != this->delegate()) {
75 if (delegate != this->delegate()) {
76 if (!m_delegateModel) {
76 if (!m_delegateModel) {
@@ -87,12 +87,12 @@ void AbstractJournal::setDelegate(QQmlComponent *delegate)
87 }
87 }
88}
88}
89
89
90−qreal AbstractJournal::columnSpacing() const
90+qreal AbstractDashView::columnSpacing() const
91{
91{
92 return m_columnSpacing;
92 return m_columnSpacing;
93}
93}
94
94
95−void AbstractJournal::setColumnSpacing(qreal columnSpacing)
95+void AbstractDashView::setColumnSpacing(qreal columnSpacing)
96{
96{
97 if (columnSpacing != m_columnSpacing) {
97 if (columnSpacing != m_columnSpacing) {
98 m_columnSpacing = columnSpacing;
98 m_columnSpacing = columnSpacing;
@@ -104,12 +104,12 @@ void AbstractJournal::setColumnSpacing(qreal columnSpacing)
104 }
104 }
105}
105}
106
106
107−qreal AbstractJournal::rowSpacing() const
107+qreal AbstractDashView::rowSpacing() const
108{
108{
109 return m_rowSpacing;
109 return m_rowSpacing;
110}
110}
111
111
112−void AbstractJournal::setRowSpacing(qreal rowSpacing)
112+void AbstractDashView::setRowSpacing(qreal rowSpacing)
113{
113{
114 if (rowSpacing != m_rowSpacing) {
114 if (rowSpacing != m_rowSpacing) {
115 m_rowSpacing = rowSpacing;
115 m_rowSpacing = rowSpacing;
@@ -121,12 +121,12 @@ void AbstractJournal::setRowSpacing(qreal rowSpacing)
121 }
121 }
122}
122}
123
123
124−qreal AbstractJournal::delegateCreationBegin() const
124+qreal AbstractDashView::delegateCreationBegin() const
125{
125{
126 return m_delegateCreationBegin;
126 return m_delegateCreationBegin;
127}
127}
128
128
129−void AbstractJournal::setDelegateCreationBegin(qreal begin)
129+void AbstractDashView::setDelegateCreationBegin(qreal begin)
130{
130{
131 m_delegateCreationBeginValid = true;
131 m_delegateCreationBeginValid = true;
132 if (m_delegateCreationBegin == begin)
132 if (m_delegateCreationBegin == begin)
@@ -138,7 +138,7 @@ void AbstractJournal::setDelegateCreationBegin(qreal begin)
138 emit delegateCreationBeginChanged();
138 emit delegateCreationBeginChanged();
139}
139}
140
140
141−void AbstractJournal::resetDelegateCreationBegin()
141+void AbstractDashView::resetDelegateCreationBegin()
142{
142{
143 m_delegateCreationBeginValid = false;
143 m_delegateCreationBeginValid = false;
144 if (m_delegateCreationBegin == 0)
144 if (m_delegateCreationBegin == 0)
@@ -150,12 +150,12 @@ void AbstractJournal::resetDelegateCreationBegin()
150 emit delegateCreationBeginChanged();
150 emit delegateCreationBeginChanged();
151}
151}
152
152
153−qreal AbstractJournal::delegateCreationEnd() const
153+qreal AbstractDashView::delegateCreationEnd() const
154{
154{
155 return m_delegateCreationEnd;
155 return m_delegateCreationEnd;
156}
156}
157
157
158−void AbstractJournal::setDelegateCreationEnd(qreal end)
158+void AbstractDashView::setDelegateCreationEnd(qreal end)
159{
159{
160 m_delegateCreationEndValid = true;
160 m_delegateCreationEndValid = true;
161 if (m_delegateCreationEnd == end)
161 if (m_delegateCreationEnd == end)
@@ -167,7 +167,7 @@ void AbstractJournal::setDelegateCreationEnd(qreal end)
167 emit delegateCreationEndChanged();
167 emit delegateCreationEndChanged();
168}
168}
169
169
170−void AbstractJournal::resetDelegateCreationEnd()
170+void AbstractDashView::resetDelegateCreationEnd()
171{
171{
172 m_delegateCreationEndValid = false;
172 m_delegateCreationEndValid = false;
173 if (m_delegateCreationEnd == 0)
173 if (m_delegateCreationEnd == 0)
@@ -179,7 +179,7 @@ void AbstractJournal::resetDelegateCreationEnd()
179 emit delegateCreationEndChanged();
179 emit delegateCreationEndChanged();
180}
180}
181
181
182−void AbstractJournal::createDelegateModel()
182+void AbstractDashView::createDelegateModel()
183{
183{
184#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
184#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
185 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
185 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
@@ -192,7 +192,7 @@ void AbstractJournal::createDelegateModel()
192 m_delegateModel->componentComplete();
192 m_delegateModel->componentComplete();
193}
193}
194
194
195−void AbstractJournal::refill()
195+void AbstractDashView::refill()
196{
196{
197 if (!isComponentComplete()) {
197 if (!isComponentComplete()) {
198 return;
198 return;
@@ -215,7 +215,7 @@ void AbstractJournal::refill()
215 }
215 }
216}
216}
217
217
218−bool AbstractJournal::addVisibleItems(qreal fillFromY, qreal fillToY, bool asynchronous)
218+bool AbstractDashView::addVisibleItems(qreal fillFromY, qreal fillToY, bool asynchronous)
219{
219{
220 if (!delegate())
220 if (!delegate())
221 return false;
221 return false;
@@ -247,7 +247,7 @@ bool AbstractJournal::addVisibleItems(qreal fillFromY, qreal fillToY, bool async
247 return changed;
247 return changed;
248}
248}
249
249
250−QQuickItem *AbstractJournal::createItem(int modelIndex, bool asynchronous)
250+QQuickItem *AbstractDashView::createItem(int modelIndex, bool asynchronous)
251{
251{
252 if (asynchronous && m_asyncRequestedIndex != -1)
252 if (asynchronous && m_asyncRequestedIndex != -1)
253 return nullptr;
253 return nullptr;
@@ -281,7 +281,7 @@ QQuickItem *AbstractJournal::createItem(int modelIndex, bool asynchronous)
281 }
281 }
282}
282}
283
283
284−void AbstractJournal::releaseItem(QQuickItem *item)
284+void AbstractDashView::releaseItem(QQuickItem *item)
285{
285{
286#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
286#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
287 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
287 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
@@ -294,20 +294,20 @@ void AbstractJournal::releaseItem(QQuickItem *item)
294 }
294 }
295}
295}
296
296
297−void AbstractJournal::setImplicitHeightDirty()
297+void AbstractDashView::setImplicitHeightDirty()
298{
298{
299 m_implicitHeightDirty = true;
299 m_implicitHeightDirty = true;
300}
300}
301
301
302#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
302#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
303−void AbstractJournal::itemCreated(int modelIndex, QQuickItem *item)
303+void AbstractDashView::itemCreated(int modelIndex, QQuickItem *item)
304{
304{
305#else
305#else
306−void AbstractJournal::itemCreated(int modelIndex, QObject *object)
306+void AbstractDashView::itemCreated(int modelIndex, QObject *object)
307{
307{
308 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
308 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
309 if (!item) {
309 if (!item) {
310− qWarning() << "AbstractJournal::itemCreated got a non item for index" << modelIndex;
310+ qWarning() << "AbstractDashView::itemCreated got a non item for index" << modelIndex;
311 return;
311 return;
312 }
312 }
313#endif
313#endif
@@ -326,9 +326,9 @@ void AbstractJournal::itemCreated(int modelIndex, QObject *object)
326}
326}
327
327
328#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
328#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
329−void AbstractJournal::onModelUpdated(const QQuickChangeSet &changeSet, bool reset)
329+void AbstractDashView::onModelUpdated(const QQuickChangeSet &changeSet, bool reset)
330#else
330#else
331−void AbstractJournal::onModelUpdated(const QQmlChangeSet &changeSet, bool reset)
331+void AbstractDashView::onModelUpdated(const QQmlChangeSet &changeSet, bool reset)
332#endif
332#endif
333{
333{
334 if (reset) {
334 if (reset) {
@@ -340,18 +340,18 @@ void AbstractJournal::onModelUpdated(const QQmlChangeSet &changeSet, bool reset)
340}
340}
341
341
342
342
343−void AbstractJournal::relayout()
343+void AbstractDashView::relayout()
344{
344{
345 m_needsRelayout = true;
345 m_needsRelayout = true;
346 polish();
346 polish();
347}
347}
348
348
349−void AbstractJournal::onHeightChanged()
349+void AbstractDashView::onHeightChanged()
350{
350{
351 polish();
351 polish();
352}
352}
353
353
354−void AbstractJournal::updatePolish()
354+void AbstractDashView::updatePolish()
355{
355{
356 if (!model())
356 if (!model())
357 return;
357 return;
@@ -375,7 +375,7 @@ void AbstractJournal::updatePolish()
375 }
375 }
376}
376}
377
377
378−void AbstractJournal::componentComplete()
378+void AbstractDashView::componentComplete()
379{
379{
380 if (m_delegateModel)
380 if (m_delegateModel)
381 m_delegateModel->componentComplete();
381 m_delegateModel->componentComplete();
plugins/DashViews/abstractjournal.h → plugins/DashViews/abstractdashview.hrenamed+6−5
@@ -1,5 +1,5 @@
1/*
1/*
2− * Copyright (C) 2013 Canonical, Ltd.
2+ * Copyright (C) 2013, 2014 Canonical, Ltd.
3 *
3 *
4 * This program is free software; you can redistribute it and/or modify
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
5 * it under the terms of the GNU General Public License as published by
@@ -14,8 +14,8 @@
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
15 */
16
16
17−#ifndef ABSTRACTJOURNAL_H
17+#ifndef ABSTRACTDASHVIEW_H
18−#define ABSTRACTJOURNAL_H
18+#define ABSTRACTDASHVIEW_H
19
19
20#include <QQuickItem>
20#include <QQuickItem>
21
21
@@ -31,7 +31,7 @@ class QQmlComponent;
31#endif
31#endif
32#pragma GCC diagnostic pop
32#pragma GCC diagnostic pop
33
33
34−class AbstractJournal : public QQuickItem
34+class AbstractDashView : public QQuickItem
35{
35{
36 Q_OBJECT
36 Q_OBJECT
37
37
@@ -50,9 +50,10 @@ class AbstractJournal : public QQuickItem
50
50
51friend class VerticalJournalTest;
51friend class VerticalJournalTest;
52friend class HorizontalJournalTest;
52friend class HorizontalJournalTest;
53+friend class OrganicGridTest;
53
54
54public:
55public:
55− AbstractJournal();
56+ AbstractDashView();
56
57
57 QAbstractItemModel *model() const;
58 QAbstractItemModel *model() const;
58 void setModel(QAbstractItemModel *model);
59 void setModel(QAbstractItemModel *model);
plugins/DashViews/horizontaljournal.h+2−2
@@ -17,7 +17,7 @@
17#ifndef HORIZONTALJOURNAL_H
17#ifndef HORIZONTALJOURNAL_H
18#define HORIZONTALJOURNAL_H
18#define HORIZONTALJOURNAL_H
19
19
20−#include "abstractjournal.h"
20+#include "abstractdashview.h"
21
21
22 /** A horizontal journal is a view that creates delegates
22 /** A horizontal journal is a view that creates delegates
23 * based on a model and layouts them one after the other
23 * based on a model and layouts them one after the other
@@ -26,7 +26,7 @@
26 *
26 *
27 * All delegates are forced to rowHeight if they don't have it.
27 * All delegates are forced to rowHeight if they don't have it.
28 */
28 */
29− class HorizontalJournal : public AbstractJournal
29+ class HorizontalJournal : public AbstractDashView
30{
30{
31 Q_OBJECT
31 Q_OBJECT
32
32
plugins/DashViews/organicgrid.cpp+3−368
@@ -24,108 +24,9 @@
24static const qreal bufferRatio = 0.5;
24static const qreal bufferRatio = 0.5;
25
25
26OrganicGrid::OrganicGrid()
26OrganicGrid::OrganicGrid()
27− : m_delegateModel(nullptr)
28− , m_asyncRequestedIndex(-1)
29− , m_columnSpacing(0)
30− , m_rowSpacing(0)
31− , m_delegateCreationBegin(0)
32− , m_delegateCreationEnd(0)
33− , m_delegateCreationBeginValid(false)
34− , m_delegateCreationEndValid(false)
35− , m_needsRelayout(false)
36− , m_delegateValidated(false)
37− , m_implicitHeightDirty(false)
38− , m_firstVisibleIndex(-1)
27+ : m_firstVisibleIndex(-1)
39 , m_numberOfModulesPerRow(-1)
28 , m_numberOfModulesPerRow(-1)
40{
29{
41− connect(this, SIGNAL(widthChanged()), this, SLOT(relayout()));
42− connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
43−}
44−
45−QAbstractItemModel *OrganicGrid::model() const
46−{
47− return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
48−}
49−
50−void OrganicGrid::setModel(QAbstractItemModel *model)
51−{
52− if (model != this->model()) {
53− if (!m_delegateModel) {
54− createDelegateModel();
55− } else {
56−#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
57− disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
58− }
59− m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
60− connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
61−#else
62− disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
63− }
64− m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
65− connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
66−#endif
67−
68− cleanupExistingItems();
69−
70− Q_EMIT modelChanged();
71− polish();
72− }
73−}
74−
75−QQmlComponent *OrganicGrid::delegate() const
76−{
77− return m_delegateModel ? m_delegateModel->delegate() : nullptr;
78−}
79−
80−void OrganicGrid::setDelegate(QQmlComponent *delegate)
81−{
82− if (delegate != this->delegate()) {
83− if (!m_delegateModel) {
84− createDelegateModel();
85− }
86−
87− cleanupExistingItems();
88−
89− m_delegateModel->setDelegate(delegate);
90−
91− Q_EMIT delegateChanged();
92− m_delegateValidated = false;
93− polish();
94− }
95−}
96−
97−qreal OrganicGrid::columnSpacing() const
98−{
99− return m_columnSpacing;
100−}
101−
102−void OrganicGrid::setColumnSpacing(qreal columnSpacing)
103−{
104− if (columnSpacing != m_columnSpacing) {
105− m_columnSpacing = columnSpacing;
106− Q_EMIT columnSpacingChanged();
107−
108− if (isComponentComplete()) {
109− relayout();
110− }
111− }
112−}
113−
114−qreal OrganicGrid::rowSpacing() const
115−{
116− return m_rowSpacing;
117−}
118−
119−void OrganicGrid::setRowSpacing(qreal rowSpacing)
120−{
121− if (rowSpacing != m_rowSpacing) {
122− m_rowSpacing = rowSpacing;
123− Q_EMIT rowSpacingChanged();
124−
125− if (isComponentComplete()) {
126− relayout();
127− }
128− }
129}
30}
130
31
131QSizeF OrganicGrid::smallDelegateSize() const
32QSizeF OrganicGrid::smallDelegateSize() const
@@ -162,272 +63,6 @@ void OrganicGrid::setBigDelegateSize(const QSizeF &size)
162 }
63 }
163}
64}
164
65
165−qreal OrganicGrid::delegateCreationBegin() const
166−{
167− return m_delegateCreationBegin;
168−}
169−
170−void OrganicGrid::setDelegateCreationBegin(qreal begin)
171−{
172− m_delegateCreationBeginValid = true;
173− if (m_delegateCreationBegin == begin)
174− return;
175− m_delegateCreationBegin = begin;
176− if (isComponentComplete()) {
177− polish();
178− }
179− emit delegateCreationBeginChanged();
180−}
181−
182−void OrganicGrid::resetDelegateCreationBegin()
183−{
184− m_delegateCreationBeginValid = false;
185− if (m_delegateCreationBegin == 0)
186− return;
187− m_delegateCreationBegin = 0;
188− if (isComponentComplete()) {
189− polish();
190− }
191− emit delegateCreationBeginChanged();
192−}
193−
194−qreal OrganicGrid::delegateCreationEnd() const
195−{
196− return m_delegateCreationEnd;
197−}
198−
199−void OrganicGrid::setDelegateCreationEnd(qreal end)
200−{
201− m_delegateCreationEndValid = true;
202− if (m_delegateCreationEnd == end)
203− return;
204− m_delegateCreationEnd = end;
205− if (isComponentComplete()) {
206− polish();
207− }
208− emit delegateCreationEndChanged();
209−}
210−
211−void OrganicGrid::resetDelegateCreationEnd()
212−{
213− m_delegateCreationEndValid = false;
214− if (m_delegateCreationEnd == 0)
215− return;
216− m_delegateCreationEnd = 0;
217− if (isComponentComplete()) {
218− polish();
219− }
220− emit delegateCreationEndChanged();
221−}
222−
223−void OrganicGrid::createDelegateModel()
224−{
225−#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
226− m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
227− connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
228−#else
229− m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
230− connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
231−#endif
232− if (isComponentComplete())
233− m_delegateModel->componentComplete();
234−}
235−
236−void OrganicGrid::refill()
237−{
238− if (!isComponentComplete()) {
239− return;
240− }
241−
242− const bool delegateRangesValid = m_delegateCreationBeginValid && m_delegateCreationEndValid;
243− const qreal from = delegateRangesValid ? m_delegateCreationBegin : 0;
244− const qreal to = delegateRangesValid ? m_delegateCreationEnd : from + height();
245− const qreal buffer = (to - from) * bufferRatio;
246− const qreal bufferFrom = from - buffer;
247− const qreal bufferTo = to + buffer;
248−
249− bool added = addVisibleItems(from, to, false);
250− bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
251− added |= addVisibleItems(bufferFrom, bufferTo, true);
252−
253− if (added || removed) {
254− m_implicitHeightDirty = true;
255− polish();
256− }
257−}
258−
259−bool OrganicGrid::addVisibleItems(qreal fillFromY, qreal fillToY, bool asynchronous)
260−{
261− if (!delegate())
262− return false;
263−
264− if (m_delegateModel->count() == 0)
265− return false;
266−
267− int modelIndex;
268− qreal yPos;
269− findBottomModelIndexToAdd(&modelIndex, &yPos);
270− bool changed = false;
271− while (modelIndex < m_delegateModel->count() && yPos <= fillToY) {
272− if (!createItem(modelIndex, asynchronous))
273− break;
274−
275− changed = true;
276− findBottomModelIndexToAdd(&modelIndex, &yPos);
277− }
278−
279− findTopModelIndexToAdd(&modelIndex, &yPos);
280− while (modelIndex >= 0 && yPos > fillFromY) {
281− if (!createItem(modelIndex, asynchronous))
282− break;
283−
284− changed = true;
285− findTopModelIndexToAdd(&modelIndex, &yPos);
286− }
287−
288− return changed;
289−}
290−
291−QQuickItem *OrganicGrid::createItem(int modelIndex, bool asynchronous)
292−{
293− if (asynchronous && m_asyncRequestedIndex != -1)
294− return nullptr;
295−
296− m_asyncRequestedIndex = -1;
297−#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
298− QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
299−#else
300− QObject* object = m_delegateModel->object(modelIndex, asynchronous);
301− QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
302−#endif
303− if (!item) {
304−#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
305− m_asyncRequestedIndex = modelIndex;
306−#else
307− if (object) {
308− m_delegateModel->release(object);
309− if (!m_delegateValidated) {
310− m_delegateValidated = true;
311− QObject* delegateObj = delegate();
312− qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
313− }
314− } else {
315− m_asyncRequestedIndex = modelIndex;
316− }
317−#endif
318− return nullptr;
319− } else {
320− positionItem(modelIndex, item);
321− return item;
322− }
323−}
324−
325−void OrganicGrid::releaseItem(QQuickItem *item)
326−{
327−#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
328− QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
329− if (flags & QQuickVisualModel::Destroyed) {
330−#else
331− QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
332− if (flags & QQmlDelegateModel::Destroyed) {
333−#endif
334− item->setParentItem(nullptr);
335− }
336−}
337−
338−void OrganicGrid::setImplicitHeightDirty()
339−{
340− m_implicitHeightDirty = true;
341−}
342−
343−#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
344−void OrganicGrid::itemCreated(int modelIndex, QQuickItem *item)
345−{
346−#else
347−void OrganicGrid::itemCreated(int modelIndex, QObject *object)
348−{
349− QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
350− if (!item) {
351− qWarning() << "OrganicGrid::itemCreated got a non item for index" << modelIndex;
352− return;
353− }
354−#endif
355− item->setParentItem(this);
356−
357− // We only need to call createItem if we are here because of an asynchronous generation
358− // otherwise we are in this slot because createItem is creating the item sync
359− // and thus we don't need to call createItem again, nor need to set m_implicitHeightDirty
360− // and call polish because the sync createItem was called from addVisibleItems that
361− // is called from refill that will already do those if an item was added
362− if (modelIndex == m_asyncRequestedIndex) {
363− createItem(modelIndex, false);
364− m_implicitHeightDirty = true;
365− polish();
366− }
367−}
368−
369−#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
370−void OrganicGrid::onModelUpdated(const QQuickChangeSet &changeSet, bool reset)
371−#else
372−void OrganicGrid::onModelUpdated(const QQmlChangeSet &changeSet, bool reset)
373−#endif
374−{
375− if (reset) {
376− cleanupExistingItems();
377− } else {
378− processModelRemoves(changeSet.removes());
379− }
380− polish();
381−}
382−
383−
384−void OrganicGrid::relayout()
385−{
386− m_needsRelayout = true;
387− polish();
388−}
389−
390−void OrganicGrid::onHeightChanged()
391−{
392− polish();
393−}
394−
395−void OrganicGrid::updatePolish()
396−{
397− if (!model())
398− return;
399−
400− if (m_needsRelayout) {
401− doRelayout();
402− m_needsRelayout = false;
403− m_implicitHeightDirty = true;
404− }
405−
406− refill();
407−
408− const bool delegateRangesValid = m_delegateCreationBeginValid && m_delegateCreationEndValid;
409− const qreal from = delegateRangesValid ? m_delegateCreationBegin : 0;
410− const qreal to = delegateRangesValid ? m_delegateCreationEnd : from + height();
411− updateItemCulling(from, to);
412−
413− if (m_implicitHeightDirty) {
414− calculateImplicitHeight();
415− m_implicitHeightDirty = false;
416− }
417−}
418−
419−void OrganicGrid::componentComplete()
420−{
421− if (m_delegateModel)
422− m_delegateModel->componentComplete();
423−
424− QQuickItem::componentComplete();
425−
426− m_needsRelayout = true;
427−
428− polish();
429−}
430−
431QPointF OrganicGrid::positionForIndex(int modelIndex) const
66QPointF OrganicGrid::positionForIndex(int modelIndex) const
432{
67{
433 const qreal moduleHeight = m_smallDelegateSize.height() + rowSpacing() + m_bigDelegateSize.height();
68 const qreal moduleHeight = m_smallDelegateSize.height() + rowSpacing() + m_bigDelegateSize.height();
@@ -559,7 +194,7 @@ void OrganicGrid::cleanupExistingItems()
559 releaseItem(item);
194 releaseItem(item);
560 m_visibleItems.clear();
195 m_visibleItems.clear();
561 m_firstVisibleIndex = -1;
196 m_firstVisibleIndex = -1;
562− m_implicitHeightDirty = true;
197+ setImplicitHeightDirty();
563}
198}
564
199
565void OrganicGrid::doRelayout()
200void OrganicGrid::doRelayout()
@@ -587,7 +222,7 @@ void OrganicGrid::updateItemCulling(qreal visibleFromY, qreal visibleToY)
587void OrganicGrid::calculateImplicitHeight()
222void OrganicGrid::calculateImplicitHeight()
588{
223{
589 const qreal moduleHeight = m_smallDelegateSize.height() + rowSpacing() + m_bigDelegateSize.height();
224 const qreal moduleHeight = m_smallDelegateSize.height() + rowSpacing() + m_bigDelegateSize.height();
590− const int itemCount = !m_delegateModel ? 0 : m_delegateModel->count();
225+ const int itemCount = !model() ? 0 : model()->rowCount();
591 const int fullRows = floor(itemCount / (m_numberOfModulesPerRow * 6));
226 const int fullRows = floor(itemCount / (m_numberOfModulesPerRow * 6));
592 const qreal fullRowsHeight = fullRows == 0 ? 0 : fullRows * moduleHeight + rowSpacing() * (fullRows - 1);
227 const qreal fullRowsHeight = fullRows == 0 ? 0 : fullRows * moduleHeight + rowSpacing() * (fullRows - 1);
593
228
plugins/DashViews/organicgrid.h+12−117
@@ -17,7 +17,7 @@
17#ifndef ORGANICGRID_H
17#ifndef ORGANICGRID_H
18#define ORGANICGRID_H
18#define ORGANICGRID_H
19
19
20−#include <QQuickItem>
20+#include "abstractdashview.h"
21
21
22class QAbstractItemModel;
22class QAbstractItemModel;
23class QQmlComponent;
23class QQmlComponent;
@@ -31,150 +31,45 @@ class QQmlComponent;
31#endif
31#endif
32#pragma GCC diagnostic pop
32#pragma GCC diagnostic pop
33
33
34−class OrganicGrid : public QQuickItem
34+class OrganicGrid : public AbstractDashView
35{
35{
36 Q_OBJECT
36 Q_OBJECT
37
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(QSizeF smallDelegateSize READ smallDelegateSize WRITE setSmallDelegateSize NOTIFY smallDelegateSizeChanged)
38 Q_PROPERTY(QSizeF smallDelegateSize READ smallDelegateSize WRITE setSmallDelegateSize NOTIFY smallDelegateSizeChanged)
43 Q_PROPERTY(QSizeF bigDelegateSize READ bigDelegateSize WRITE setBigDelegateSize NOTIFY bigDelegateSizeChanged)
39 Q_PROPERTY(QSizeF bigDelegateSize READ bigDelegateSize WRITE setBigDelegateSize NOTIFY bigDelegateSizeChanged)
44− Q_PROPERTY(qreal delegateCreationBegin READ delegateCreationBegin
45− WRITE setDelegateCreationBegin
46− NOTIFY delegateCreationBeginChanged
47− RESET resetDelegateCreationBegin)
48− Q_PROPERTY(qreal delegateCreationEnd READ delegateCreationEnd
49− WRITE setDelegateCreationEnd
50− NOTIFY delegateCreationEndChanged
51− RESET resetDelegateCreationEnd)
52
40
53friend class OrganicGridTest;
41friend class OrganicGridTest;
54
42
55public:
43public:
56 OrganicGrid();
44 OrganicGrid();
57
45
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− // TODO esto es mejor que lo pongamos en OrganicGrid que sea una subclase de la clase DashView que aun no existe
71− // que comparta la ostia con abstractjournal
72 QSizeF smallDelegateSize() const;
46 QSizeF smallDelegateSize() const;
73 void setSmallDelegateSize(const QSizeF &size);
47 void setSmallDelegateSize(const QSizeF &size);
74
48
75 QSizeF bigDelegateSize() const;
49 QSizeF bigDelegateSize() const;
76 void setBigDelegateSize(const QSizeF &size);
50 void setBigDelegateSize(const QSizeF &size);
77− // END TODO
78−
79− qreal delegateCreationBegin() const;
80− void setDelegateCreationBegin(qreal);
81− void resetDelegateCreationBegin();
82−
83− qreal delegateCreationEnd() const;
84− void setDelegateCreationEnd(qreal);
85− void resetDelegateCreationEnd();
86
51
87Q_SIGNALS:
52Q_SIGNALS:
88− void modelChanged();
89− void delegateChanged();
90− void columnSpacingChanged();
91− void rowSpacingChanged();
92 void smallDelegateSizeChanged();
53 void smallDelegateSizeChanged();
93 void bigDelegateSizeChanged();
54 void bigDelegateSizeChanged();
94− void delegateCreationBeginChanged();
95− void delegateCreationEndChanged();
96−
97−protected Q_SLOTS:
98− void relayout();
99−
100−protected:
101− void updatePolish() override;
102− void componentComplete() override;
103−
104− void releaseItem(QQuickItem *item);
105− void setImplicitHeightDirty();
106−
107−private Q_SLOTS:
108−#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
109− void itemCreated(int modelIndex, QQuickItem *item);
110− void onModelUpdated(const QQuickChangeSet &changeSet, bool reset);
111−#else
112− void itemCreated(int modelIndex, QObject *object);
113− void onModelUpdated(const QQmlChangeSet &changeSet, bool reset);
114−#endif
115− void onHeightChanged();
116−
117private:
55private:
118− void createDelegateModel();
119− void refill();
120− bool addVisibleItems(qreal fillFromY, qreal fillToY, bool asynchronous);
121− QQuickItem *createItem(int modelIndex, bool asynchronous);
122−
123− // TODO esto es mejor que lo pongamos en OrganicGrid que sea una subclase de la clase DashView que aun no existe
124− // que comparta la ostia con abstractjournal
125−// virtual void findBottomModelIndexToAdd(int *modelIndex, qreal *yPos) = 0;
126−// virtual void findTopModelIndexToAdd(int *modelIndex, qreal *yPos) = 0;
127−// virtual void positionItem(int modelIndex, QQuickItem *item) = 0;
128−// virtual bool removeNonVisibleItems(qreal bufferFromY, qreal bufferToY) = 0;
129−// virtual void cleanupExistingItems() = 0;
130−// virtual void doRelayout() = 0;
131−// virtual void updateItemCulling(qreal visibleFromY, qreal visibleToY) = 0;
132−// virtual void calculateImplicitHeight() = 0;
133−// #if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
134−// virtual void processModelRemoves(const QVector<QQuickChangeSet::Remove> &removes) = 0;
135−// #else
136−// virtual void processModelRemoves(const QVector<QQmlChangeSet::Remove> &removes) = 0;
137−// #endif
138−
139 QPointF positionForIndex(int modelIndex) const;
56 QPointF positionForIndex(int modelIndex) const;
140 QSizeF sizeForIndex(int modelIndex) const;
57 QSizeF sizeForIndex(int modelIndex) const;
141
58
142− void findBottomModelIndexToAdd(int *modelIndex, qreal *yPos);
59+ void findBottomModelIndexToAdd(int *modelIndex, qreal *yPos) override;
143− void findTopModelIndexToAdd(int *modelIndex, qreal *yPos);
60+ void findTopModelIndexToAdd(int *modelIndex, qreal *yPos) override;
144− void positionItem(int modelIndex, QQuickItem *item);
61+ void positionItem(int modelIndex, QQuickItem *item) override;
145− bool removeNonVisibleItems(qreal bufferFromY, qreal bufferToY);
62+ bool removeNonVisibleItems(qreal bufferFromY, qreal bufferToY) override;
146− void cleanupExistingItems();
63+ void cleanupExistingItems() override;
147− void doRelayout();
64+ void doRelayout() override;
148− void updateItemCulling(qreal visibleFromY, qreal visibleToY);
65+ void updateItemCulling(qreal visibleFromY, qreal visibleToY) override;
149− void calculateImplicitHeight();
66+ void calculateImplicitHeight() override;
150#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
67#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
151− void processModelRemoves(const QVector<QQuickChangeSet::Remove> &removes);
68+ void processModelRemoves(const QVector<QQuickChangeSet::Remove> &removes) override;
152#else
69#else
153− void processModelRemoves(const QVector<QQmlChangeSet::Remove> &removes);
70+ void processModelRemoves(const QVector<QQmlChangeSet::Remove> &removes) override;
154#endif
71#endif
155−
156−
157−#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
158− QQuickVisualDataModel *m_delegateModel;
159−#else
160− QQmlDelegateModel *m_delegateModel;
161−#endif
162−
163− // Index we are waiting because we requested it asynchronously
164− int m_asyncRequestedIndex;
165
72
166− int m_columnSpacing;
167− int m_rowSpacing;
168− qreal m_delegateCreationBegin;
169− qreal m_delegateCreationEnd;
170− bool m_delegateCreationBeginValid;
171− bool m_delegateCreationEndValid;
172− bool m_needsRelayout;
173− bool m_delegateValidated;
174− bool m_implicitHeightDirty;
175−
176− // TODO esto es mejor que lo pongamos en OrganicGrid que sea una subclase de la clase DashView que aun no existe
177− // que comparta la ostia con abstractjournal
178 QSizeF m_smallDelegateSize;
73 QSizeF m_smallDelegateSize;
179 QSizeF m_bigDelegateSize;
74 QSizeF m_bigDelegateSize;
180 int m_firstVisibleIndex;
75 int m_firstVisibleIndex;
plugins/DashViews/verticaljournal.h+2−2
@@ -17,7 +17,7 @@
17#ifndef VERTICALJOURNAL_H
17#ifndef VERTICALJOURNAL_H
18#define VERTICALJOURNAL_H
18#define VERTICALJOURNAL_H
19
19
20−#include "abstractjournal.h"
20+#include "abstractdashview.h"
21
21
22 /** A vertical journal is a view that creates delegates
22 /** A vertical journal is a view that creates delegates
23 * based on a model and layouts them in columns following
23 * based on a model and layouts them in columns following
@@ -51,7 +51,7 @@
51 *
51 *
52 */
52 */
53
53
54−class VerticalJournal : public AbstractJournal
54+class VerticalJournal : public AbstractDashView
55{
55{
56 Q_OBJECT
56 Q_OBJECT
57
57
tests/plugins/DashViews/CMakeLists.txt+2−2
@@ -49,7 +49,7 @@ macro(add_dashview_try_test FILENAME)
49 add_executable(${FILENAME}testExec
49 add_executable(${FILENAME}testExec
50 ${FILENAME}test.cpp
50 ${FILENAME}test.cpp
51 ${CMAKE_CURRENT_SOURCE_DIR}/../../../plugins/DashViews/${FILENAME}.cpp
51 ${CMAKE_CURRENT_SOURCE_DIR}/../../../plugins/DashViews/${FILENAME}.cpp
52− ${CMAKE_CURRENT_SOURCE_DIR}/../../../plugins/DashViews/abstractjournal.cpp
52+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../plugins/DashViews/abstractdashview.cpp
53 )
53 )
54 qt5_use_modules(${FILENAME}testExec Test Core Qml)
54 qt5_use_modules(${FILENAME}testExec Test Core Qml)
55 target_link_libraries(${FILENAME}testExec ${Qt5Gui_LIBRARIES} ${Qt5Quick_LIBRARIES})
55 target_link_libraries(${FILENAME}testExec ${Qt5Gui_LIBRARIES} ${Qt5Quick_LIBRARIES})
@@ -63,7 +63,7 @@ macro(add_dashview_try_test FILENAME)
63 add_executable(${FILENAME}tryExec
63 add_executable(${FILENAME}tryExec
64 ${FILENAME}try.cpp
64 ${FILENAME}try.cpp
65 ${CMAKE_CURRENT_SOURCE_DIR}/../../../plugins/DashViews/${FILENAME}.cpp
65 ${CMAKE_CURRENT_SOURCE_DIR}/../../../plugins/DashViews/${FILENAME}.cpp
66− ${CMAKE_CURRENT_SOURCE_DIR}/../../../plugins/DashViews/abstractjournal.cpp
66+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../plugins/DashViews/abstractdashview.cpp
67 )
67 )
68 qt5_use_modules(${FILENAME}tryExec Test Core Qml)
68 qt5_use_modules(${FILENAME}tryExec Test Core Qml)
69 target_link_libraries(${FILENAME}tryExec ${Qt5Gui_LIBRARIES} ${Qt5Quick_LIBRARIES})
69 target_link_libraries(${FILENAME}tryExec ${Qt5Gui_LIBRARIES} ${Qt5Quick_LIBRARIES})