on xenial help sign in
Make organicgrid and journals share code
commit789c6720ac2c2ae1e47525b0a6d7c3041d9a738d authorAlbert Astals <albert.astals@canonical.com> dateTue 14 Jan 2014, 13:51 (13 years ago) parent295355b
8 files changed+60−529
plugins/DashViews/CMakeLists.txt+1−1 plugins/DashViews/abstractjournal.cpp → plugins/DashViews/abstractdashview.cpp+32−32 plugins/DashViews/abstractjournal.h → plugins/DashViews/abstractdashview.h+6−5 plugins/DashViews/horizontaljournal.h+2−2 plugins/DashViews/organicgrid.cpp+3−368 plugins/DashViews/organicgrid.h+12−117 plugins/DashViews/verticaljournal.h+2−2 tests/plugins/DashViews/CMakeLists.txt+2−2
plugins/DashViews/CMakeLists.txt+1−1
@@ -24,7 +24,7 @@ remove_definitions(-DQT_NO_KEYWORDS)
2424set(QMLPLUGIN_SRC
2525 plugin.cpp
2626 listviewwithpageheader.cpp
27− abstractjournal.cpp
27+ abstractdashview.cpp
2828 verticaljournal.cpp
2929 horizontaljournal.cpp
3030 organicgrid.cpp
plugins/DashViews/abstractjournal.cpp → plugins/DashViews/abstractdashview.cpprenamed+32−32
@@ -1,5 +1,5 @@
11/*
2− * Copyright (C) 2013 Canonical, Ltd.
2+ * Copyright (C) 2013, 2014 Canonical, Ltd.
33 *
44 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
@@ -14,11 +14,11 @@
1414 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1515 */
1616
17−#include "abstractjournal.h"
17+#include "abstractdashview.h"
1818
1919static const qreal bufferRatio = 0.5;
2020
21−AbstractJournal::AbstractJournal()
21+AbstractDashView::AbstractDashView()
2222 : m_delegateModel(nullptr)
2323 , m_asyncRequestedIndex(-1)
2424 , m_columnSpacing(0)
@@ -35,12 +35,12 @@ AbstractJournal::AbstractJournal()
3535 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
3636}
3737
38−QAbstractItemModel *AbstractJournal::model() const
38+QAbstractItemModel *AbstractDashView::model() const
3939{
4040 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
4141}
4242
43−void AbstractJournal::setModel(QAbstractItemModel *model)
43+void AbstractDashView::setModel(QAbstractItemModel *model)
4444{
4545 if (model != this->model()) {
4646 if (!m_delegateModel) {
@@ -65,12 +65,12 @@ void AbstractJournal::setModel(QAbstractItemModel *model)
6565 }
6666}
6767
68−QQmlComponent *AbstractJournal::delegate() const
68+QQmlComponent *AbstractDashView::delegate() const
6969{
7070 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
7171}
7272
73−void AbstractJournal::setDelegate(QQmlComponent *delegate)
73+void AbstractDashView::setDelegate(QQmlComponent *delegate)
7474{
7575 if (delegate != this->delegate()) {
7676 if (!m_delegateModel) {
@@ -87,12 +87,12 @@ void AbstractJournal::setDelegate(QQmlComponent *delegate)
8787 }
8888}
8989
90−qreal AbstractJournal::columnSpacing() const
90+qreal AbstractDashView::columnSpacing() const
9191{
9292 return m_columnSpacing;
9393}
9494
95−void AbstractJournal::setColumnSpacing(qreal columnSpacing)
95+void AbstractDashView::setColumnSpacing(qreal columnSpacing)
9696{
9797 if (columnSpacing != m_columnSpacing) {
9898 m_columnSpacing = columnSpacing;
@@ -104,12 +104,12 @@ void AbstractJournal::setColumnSpacing(qreal columnSpacing)
104104 }
105105}
106106
107−qreal AbstractJournal::rowSpacing() const
107+qreal AbstractDashView::rowSpacing() const
108108{
109109 return m_rowSpacing;
110110}
111111
112−void AbstractJournal::setRowSpacing(qreal rowSpacing)
112+void AbstractDashView::setRowSpacing(qreal rowSpacing)
113113{
114114 if (rowSpacing != m_rowSpacing) {
115115 m_rowSpacing = rowSpacing;
@@ -121,12 +121,12 @@ void AbstractJournal::setRowSpacing(qreal rowSpacing)
121121 }
122122}
123123
124−qreal AbstractJournal::delegateCreationBegin() const
124+qreal AbstractDashView::delegateCreationBegin() const
125125{
126126 return m_delegateCreationBegin;
127127}
128128
129−void AbstractJournal::setDelegateCreationBegin(qreal begin)
129+void AbstractDashView::setDelegateCreationBegin(qreal begin)
130130{
131131 m_delegateCreationBeginValid = true;
132132 if (m_delegateCreationBegin == begin)
@@ -138,7 +138,7 @@ void AbstractJournal::setDelegateCreationBegin(qreal begin)
138138 emit delegateCreationBeginChanged();
139139}
140140
141−void AbstractJournal::resetDelegateCreationBegin()
141+void AbstractDashView::resetDelegateCreationBegin()
142142{
143143 m_delegateCreationBeginValid = false;
144144 if (m_delegateCreationBegin == 0)
@@ -150,12 +150,12 @@ void AbstractJournal::resetDelegateCreationBegin()
150150 emit delegateCreationBeginChanged();
151151}
152152
153−qreal AbstractJournal::delegateCreationEnd() const
153+qreal AbstractDashView::delegateCreationEnd() const
154154{
155155 return m_delegateCreationEnd;
156156}
157157
158−void AbstractJournal::setDelegateCreationEnd(qreal end)
158+void AbstractDashView::setDelegateCreationEnd(qreal end)
159159{
160160 m_delegateCreationEndValid = true;
161161 if (m_delegateCreationEnd == end)
@@ -167,7 +167,7 @@ void AbstractJournal::setDelegateCreationEnd(qreal end)
167167 emit delegateCreationEndChanged();
168168}
169169
170−void AbstractJournal::resetDelegateCreationEnd()
170+void AbstractDashView::resetDelegateCreationEnd()
171171{
172172 m_delegateCreationEndValid = false;
173173 if (m_delegateCreationEnd == 0)
@@ -179,7 +179,7 @@ void AbstractJournal::resetDelegateCreationEnd()
179179 emit delegateCreationEndChanged();
180180}
181181
182−void AbstractJournal::createDelegateModel()
182+void AbstractDashView::createDelegateModel()
183183{
184184#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
185185 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
@@ -192,7 +192,7 @@ void AbstractJournal::createDelegateModel()
192192 m_delegateModel->componentComplete();
193193}
194194
195−void AbstractJournal::refill()
195+void AbstractDashView::refill()
196196{
197197 if (!isComponentComplete()) {
198198 return;
@@ -215,7 +215,7 @@ void AbstractJournal::refill()
215215 }
216216}
217217
218−bool AbstractJournal::addVisibleItems(qreal fillFromY, qreal fillToY, bool asynchronous)
218+bool AbstractDashView::addVisibleItems(qreal fillFromY, qreal fillToY, bool asynchronous)
219219{
220220 if (!delegate())
221221 return false;
@@ -247,7 +247,7 @@ bool AbstractJournal::addVisibleItems(qreal fillFromY, qreal fillToY, bool async
247247 return changed;
248248}
249249
250−QQuickItem *AbstractJournal::createItem(int modelIndex, bool asynchronous)
250+QQuickItem *AbstractDashView::createItem(int modelIndex, bool asynchronous)
251251{
252252 if (asynchronous && m_asyncRequestedIndex != -1)
253253 return nullptr;
@@ -281,7 +281,7 @@ QQuickItem *AbstractJournal::createItem(int modelIndex, bool asynchronous)
281281 }
282282}
283283
284−void AbstractJournal::releaseItem(QQuickItem *item)
284+void AbstractDashView::releaseItem(QQuickItem *item)
285285{
286286#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
287287 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
@@ -294,20 +294,20 @@ void AbstractJournal::releaseItem(QQuickItem *item)
294294 }
295295}
296296
297−void AbstractJournal::setImplicitHeightDirty()
297+void AbstractDashView::setImplicitHeightDirty()
298298{
299299 m_implicitHeightDirty = true;
300300}
301301
302302#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)
304304{
305305#else
306−void AbstractJournal::itemCreated(int modelIndex, QObject *object)
306+void AbstractDashView::itemCreated(int modelIndex, QObject *object)
307307{
308308 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
309309 if (!item) {
310− qWarning() << "AbstractJournal::itemCreated got a non item for index" << modelIndex;
310+ qWarning() << "AbstractDashView::itemCreated got a non item for index" << modelIndex;
311311 return;
312312 }
313313#endif
@@ -326,9 +326,9 @@ void AbstractJournal::itemCreated(int modelIndex, QObject *object)
326326}
327327
328328#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)
330330#else
331−void AbstractJournal::onModelUpdated(const QQmlChangeSet &changeSet, bool reset)
331+void AbstractDashView::onModelUpdated(const QQmlChangeSet &changeSet, bool reset)
332332#endif
333333{
334334 if (reset) {
@@ -340,18 +340,18 @@ void AbstractJournal::onModelUpdated(const QQmlChangeSet &changeSet, bool reset)
340340}
341341
342342
343−void AbstractJournal::relayout()
343+void AbstractDashView::relayout()
344344{
345345 m_needsRelayout = true;
346346 polish();
347347}
348348
349−void AbstractJournal::onHeightChanged()
349+void AbstractDashView::onHeightChanged()
350350{
351351 polish();
352352}
353353
354−void AbstractJournal::updatePolish()
354+void AbstractDashView::updatePolish()
355355{
356356 if (!model())
357357 return;
@@ -375,7 +375,7 @@ void AbstractJournal::updatePolish()
375375 }
376376}
377377
378−void AbstractJournal::componentComplete()
378+void AbstractDashView::componentComplete()
379379{
380380 if (m_delegateModel)
381381 m_delegateModel->componentComplete();
plugins/DashViews/abstractjournal.h → plugins/DashViews/abstractdashview.hrenamed+6−5
@@ -1,5 +1,5 @@
11/*
2− * Copyright (C) 2013 Canonical, Ltd.
2+ * Copyright (C) 2013, 2014 Canonical, Ltd.
33 *
44 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
@@ -14,8 +14,8 @@
1414 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1515 */
1616
17−#ifndef ABSTRACTJOURNAL_H
18−#define ABSTRACTJOURNAL_H
17+#ifndef ABSTRACTDASHVIEW_H
18+#define ABSTRACTDASHVIEW_H
1919
2020#include <QQuickItem>
2121
@@ -31,7 +31,7 @@ class QQmlComponent;
3131#endif
3232#pragma GCC diagnostic pop
3333
34−class AbstractJournal : public QQuickItem
34+class AbstractDashView : public QQuickItem
3535{
3636 Q_OBJECT
3737
@@ -50,9 +50,10 @@ class AbstractJournal : public QQuickItem
5050
5151friend class VerticalJournalTest;
5252friend class HorizontalJournalTest;
53+friend class OrganicGridTest;
5354
5455public:
55− AbstractJournal();
56+ AbstractDashView();
5657
5758 QAbstractItemModel *model() const;
5859 void setModel(QAbstractItemModel *model);
plugins/DashViews/horizontaljournal.h+2−2
@@ -17,7 +17,7 @@
1717#ifndef HORIZONTALJOURNAL_H
1818#define HORIZONTALJOURNAL_H
1919
20−#include "abstractjournal.h"
20+#include "abstractdashview.h"
2121
2222 /** A horizontal journal is a view that creates delegates
2323 * based on a model and layouts them one after the other
@@ -26,7 +26,7 @@
2626 *
2727 * All delegates are forced to rowHeight if they don't have it.
2828 */
29− class HorizontalJournal : public AbstractJournal
29+ class HorizontalJournal : public AbstractDashView
3030{
3131 Q_OBJECT
3232
plugins/DashViews/organicgrid.cpp+3−368
@@ -24,108 +24,9 @@
2424static const qreal bufferRatio = 0.5;
2525
2626OrganicGrid::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)
3928 , m_numberOfModulesPerRow(-1)
4029{
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− }
12930}
13031
13132QSizeF OrganicGrid::smallDelegateSize() const
@@ -162,272 +63,6 @@ void OrganicGrid::setBigDelegateSize(const QSizeF &size)
16263 }
16364}
16465
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−
43166QPointF OrganicGrid::positionForIndex(int modelIndex) const
43267{
43368 const qreal moduleHeight = m_smallDelegateSize.height() + rowSpacing() + m_bigDelegateSize.height();
@@ -559,7 +194,7 @@ void OrganicGrid::cleanupExistingItems()
559194 releaseItem(item);
560195 m_visibleItems.clear();
561196 m_firstVisibleIndex = -1;
562− m_implicitHeightDirty = true;
197+ setImplicitHeightDirty();
563198}
564199
565200void OrganicGrid::doRelayout()
@@ -587,7 +222,7 @@ void OrganicGrid::updateItemCulling(qreal visibleFromY, qreal visibleToY)
587222void OrganicGrid::calculateImplicitHeight()
588223{
589224 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();
591226 const int fullRows = floor(itemCount / (m_numberOfModulesPerRow * 6));
592227 const qreal fullRowsHeight = fullRows == 0 ? 0 : fullRows * moduleHeight + rowSpacing() * (fullRows - 1);
593228
plugins/DashViews/organicgrid.h+12−117
@@ -17,7 +17,7 @@
1717#ifndef ORGANICGRID_H
1818#define ORGANICGRID_H
1919
20−#include <QQuickItem>
20+#include "abstractdashview.h"
2121
2222class QAbstractItemModel;
2323class QQmlComponent;
@@ -31,150 +31,45 @@ class QQmlComponent;
3131#endif
3232#pragma GCC diagnostic pop
3333
34−class OrganicGrid : public QQuickItem
34+class OrganicGrid : public AbstractDashView
3535{
3636 Q_OBJECT
3737
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)
4238 Q_PROPERTY(QSizeF smallDelegateSize READ smallDelegateSize WRITE setSmallDelegateSize NOTIFY smallDelegateSizeChanged)
4339 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)
5240
5341friend class OrganicGridTest;
5442
5543public:
5644 OrganicGrid();
5745
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
7246 QSizeF smallDelegateSize() const;
7347 void setSmallDelegateSize(const QSizeF &size);
7448
7549 QSizeF bigDelegateSize() const;
7650 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();
8651
8752Q_SIGNALS:
88− void modelChanged();
89− void delegateChanged();
90− void columnSpacingChanged();
91− void rowSpacingChanged();
9253 void smallDelegateSizeChanged();
9354 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−
11755private:
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−
13956 QPointF positionForIndex(int modelIndex) const;
14057 QSizeF sizeForIndex(int modelIndex) const;
14158
142− void findBottomModelIndexToAdd(int *modelIndex, qreal *yPos);
143− void findTopModelIndexToAdd(int *modelIndex, qreal *yPos);
144− void positionItem(int modelIndex, QQuickItem *item);
145− bool removeNonVisibleItems(qreal bufferFromY, qreal bufferToY);
146− void cleanupExistingItems();
147− void doRelayout();
148− void updateItemCulling(qreal visibleFromY, qreal visibleToY);
149− void calculateImplicitHeight();
59+ void findBottomModelIndexToAdd(int *modelIndex, qreal *yPos) override;
60+ void findTopModelIndexToAdd(int *modelIndex, qreal *yPos) override;
61+ void positionItem(int modelIndex, QQuickItem *item) override;
62+ bool removeNonVisibleItems(qreal bufferFromY, qreal bufferToY) override;
63+ void cleanupExistingItems() override;
64+ void doRelayout() override;
65+ void updateItemCulling(qreal visibleFromY, qreal visibleToY) override;
66+ void calculateImplicitHeight() override;
15067#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;
15269#else
153− void processModelRemoves(const QVector<QQmlChangeSet::Remove> &removes);
70+ void processModelRemoves(const QVector<QQmlChangeSet::Remove> &removes) override;
15471#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;
16572
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
17873 QSizeF m_smallDelegateSize;
17974 QSizeF m_bigDelegateSize;
18075 int m_firstVisibleIndex;
plugins/DashViews/verticaljournal.h+2−2
@@ -17,7 +17,7 @@
1717#ifndef VERTICALJOURNAL_H
1818#define VERTICALJOURNAL_H
1919
20−#include "abstractjournal.h"
20+#include "abstractdashview.h"
2121
2222 /** A vertical journal is a view that creates delegates
2323 * based on a model and layouts them in columns following
@@ -51,7 +51,7 @@
5151 *
5252 */
5353
54−class VerticalJournal : public AbstractJournal
54+class VerticalJournal : public AbstractDashView
5555{
5656 Q_OBJECT
5757
tests/plugins/DashViews/CMakeLists.txt+2−2
@@ -49,7 +49,7 @@ macro(add_dashview_try_test FILENAME)
4949 add_executable(${FILENAME}testExec
5050 ${FILENAME}test.cpp
5151 ${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
5353 )
5454 qt5_use_modules(${FILENAME}testExec Test Core Qml)
5555 target_link_libraries(${FILENAME}testExec ${Qt5Gui_LIBRARIES} ${Qt5Quick_LIBRARIES})
@@ -63,7 +63,7 @@ macro(add_dashview_try_test FILENAME)
6363 add_executable(${FILENAME}tryExec
6464 ${FILENAME}try.cpp
6565 ${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
6767 )
6868 qt5_use_modules(${FILENAME}tryExec Test Core Qml)
6969 target_link_libraries(${FILENAME}tryExec ${Qt5Gui_LIBRARIES} ${Qt5Quick_LIBRARIES})