on 37a3326 help sign in
unity8/plugins/DashViews/abstractjournal.cpp
381 lines 10 kB history raw
1/*
2 * Copyright (C) 2013 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 */
17#include "abstractjournal.h"
19static const qreal bufferRatio = 0.5;
21AbstractJournal::AbstractJournal()
22 : m_delegateModel(nullptr)
23 , m_asyncRequestedIndex(-1)
24 , m_columnSpacing(0)
25 , m_rowSpacing(0)
26 , m_delegateCreationBegin(0)
27 , m_delegateCreationEnd(0)
28 , m_delegateCreationBeginValid(false)
29 , m_delegateCreationEndValid(false)
30 , m_needsRelayout(false)
31 , m_delegateValidated(false)
32 , m_implicitHeightDirty(false)
34 connect(this, SIGNAL(widthChanged()), this, SLOT(relayout()));
35 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
38QAbstractItemModel *AbstractJournal::model() const
40 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
43void AbstractJournal::setModel(QAbstractItemModel *model)
45 if (model != this->model()) {
46 if (!m_delegateModel) {
47 createDelegateModel();
48 } else {
49#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
50 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
51 }
52 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
53 connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
54#else
55 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
56 }
57 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
58 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
59#endif
61 cleanupExistingItems();
63 Q_EMIT modelChanged();
64 polish();
65 }
68QQmlComponent *AbstractJournal::delegate() const
70 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
73void AbstractJournal::setDelegate(QQmlComponent *delegate)
75 if (delegate != this->delegate()) {
76 if (!m_delegateModel) {
77 createDelegateModel();
78 }
80 cleanupExistingItems();
82 m_delegateModel->setDelegate(delegate);
84 Q_EMIT delegateChanged();
85 m_delegateValidated = false;
86 polish();
87 }
90qreal AbstractJournal::columnSpacing() const
92 return m_columnSpacing;
95void AbstractJournal::setColumnSpacing(qreal columnSpacing)
97 if (columnSpacing != m_columnSpacing) {
98 m_columnSpacing = columnSpacing;
99 Q_EMIT columnSpacingChanged();
101 if (isComponentComplete()) {
102 relayout();
103 }
104 }
107qreal AbstractJournal::rowSpacing() const
109 return m_rowSpacing;
112void AbstractJournal::setRowSpacing(qreal rowSpacing)
114 if (rowSpacing != m_rowSpacing) {
115 m_rowSpacing = rowSpacing;
116 Q_EMIT rowSpacingChanged();
118 if (isComponentComplete()) {
119 relayout();
120 }
121 }
124qreal AbstractJournal::delegateCreationBegin() const
126 return m_delegateCreationBegin;
129void AbstractJournal::setDelegateCreationBegin(qreal begin)
131 m_delegateCreationBeginValid = true;
132 if (m_delegateCreationBegin == begin)
133 return;
134 m_delegateCreationBegin = begin;
135 if (isComponentComplete()) {
136 polish();
137 }
138 emit delegateCreationBeginChanged();
141void AbstractJournal::resetDelegateCreationBegin()
143 m_delegateCreationBeginValid = false;
144 if (m_delegateCreationBegin == 0)
145 return;
146 m_delegateCreationBegin = 0;
147 if (isComponentComplete()) {
148 polish();
149 }
150 emit delegateCreationBeginChanged();
153qreal AbstractJournal::delegateCreationEnd() const
155 return m_delegateCreationEnd;
158void AbstractJournal::setDelegateCreationEnd(qreal end)
160 m_delegateCreationEndValid = true;
161 if (m_delegateCreationEnd == end)
162 return;
163 m_delegateCreationEnd = end;
164 if (isComponentComplete()) {
165 polish();
166 }
167 emit delegateCreationEndChanged();
170void AbstractJournal::resetDelegateCreationEnd()
172 m_delegateCreationEndValid = false;
173 if (m_delegateCreationEnd == 0)
174 return;
175 m_delegateCreationEnd = 0;
176 if (isComponentComplete()) {
177 polish();
178 }
179 emit delegateCreationEndChanged();
182void AbstractJournal::createDelegateModel()
184#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
185 m_delegateModel = new QQuickVisualDataModel(qmlContext(this), this);
186 connect(m_delegateModel, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(itemCreated(int,QQuickItem*)));
187#else
188 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
189 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
190#endif
191 if (isComponentComplete())
192 m_delegateModel->componentComplete();
195void AbstractJournal::refill()
197 if (!isComponentComplete()) {
198 return;
199 }
201 const bool delegateRangesValid = m_delegateCreationBeginValid && m_delegateCreationEndValid;
202 const qreal from = delegateRangesValid ? m_delegateCreationBegin : 0;
203 const qreal to = delegateRangesValid ? m_delegateCreationEnd : from + height();
204 const qreal buffer = (to - from) * bufferRatio;
205 const qreal bufferFrom = from - buffer;
206 const qreal bufferTo = to + buffer;
208 bool added = addVisibleItems(from, to, false);
209 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
210 added |= addVisibleItems(bufferFrom, bufferTo, true);
212 if (added || removed) {
213 m_implicitHeightDirty = true;
214 }
217bool AbstractJournal::addVisibleItems(qreal fillFrom, qreal fillTo, bool asynchronous)
219 if (!delegate())
220 return false;
222 if (m_delegateModel->count() == 0)
223 return false;
225 int modelIndex;
226 qreal yPos;
227 findBottomModelIndexToAdd(&modelIndex, &yPos);
228 bool changed = false;
229 while (modelIndex < m_delegateModel->count() && yPos <= fillTo) {
230 if (!createItem(modelIndex, asynchronous))
231 break;
233 changed = true;
234 findBottomModelIndexToAdd(&modelIndex, &yPos);
235 }
237 findTopModelIndexToAdd(&modelIndex, &yPos);
238 while (modelIndex >= 0 && yPos > fillFrom) {
239 if (!createItem(modelIndex, asynchronous))
240 break;
242 changed = true;
243 findTopModelIndexToAdd(&modelIndex, &yPos);
244 }
246 return changed;
249QQuickItem *AbstractJournal::createItem(int modelIndex, bool asynchronous)
251 if (asynchronous && m_asyncRequestedIndex != -1)
252 return nullptr;
254 m_asyncRequestedIndex = -1;
255#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
256 QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
257#else
258 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
259 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
260#endif
261 if (!item) {
262#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
263 m_asyncRequestedIndex = modelIndex;
264#else
265 if (object) {
266 m_delegateModel->release(object);
267 if (!m_delegateValidated) {
268 m_delegateValidated = true;
269 QObject* delegateObj = delegate();
270 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
271 }
272 } else {
273 m_asyncRequestedIndex = modelIndex;
274 }
275#endif
276 return nullptr;
277 } else {
278 positionItem(modelIndex, item);
279 return item;
280 }
283void AbstractJournal::releaseItem(QQuickItem *item)
285#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
286 QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
287 if (flags & QQuickVisualModel::Destroyed) {
288#else
289 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
290 if (flags & QQmlDelegateModel::Destroyed) {
291#endif
292 item->setParentItem(nullptr);
293 }
296void AbstractJournal::setImplicitHeightDirty()
298 m_implicitHeightDirty = true;
301#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
302void AbstractJournal::itemCreated(int modelIndex, QQuickItem *item)
304#else
305void AbstractJournal::itemCreated(int modelIndex, QObject *object)
307 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
308 if (!item) {
309 qWarning() << "AbstractJournal::itemCreated got a non item for index" << modelIndex;
310 return;
311 }
312#endif
313 item->setParentItem(this);
314 if (modelIndex == m_asyncRequestedIndex) {
315 createItem(modelIndex, false);
316 m_implicitHeightDirty = true;
317 polish();
318 }
321#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
322void AbstractJournal::onModelUpdated(const QQuickChangeSet &changeSet, bool reset)
323#else
324void AbstractJournal::onModelUpdated(const QQmlChangeSet &changeSet, bool reset)
325#endif
327 if (reset) {
328 cleanupExistingItems();
329 } else {
330 processModelRemoves(changeSet.removes());
331 }
332 polish();
336void AbstractJournal::relayout()
338 m_needsRelayout = true;
339 polish();
342void AbstractJournal::onHeightChanged()
344 polish();
347void AbstractJournal::updatePolish()
349 if (!model())
350 return;
352 if (m_needsRelayout) {
353 doRelayout();
354 m_needsRelayout = false;
355 m_implicitHeightDirty = true;
356 }
358 refill();
360 const bool delegateRangesValid = m_delegateCreationBeginValid && m_delegateCreationEndValid;
361 const qreal from = delegateRangesValid ? m_delegateCreationBegin : 0;
362 const qreal to = delegateRangesValid ? m_delegateCreationEnd : from + height();
363 updateItemCulling(from, to);
365 if (m_implicitHeightDirty) {
366 calculateImplicitHeight();
367 m_implicitHeightDirty = false;
368 }
371void AbstractJournal::componentComplete()
373 if (m_delegateModel)
374 m_delegateModel->componentComplete();
376 QQuickItem::componentComplete();
378 m_needsRelayout = true;
380 polish();