2 * Copyright (C) 2013, 2014 Canonical, Ltd.
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.
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.
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/>.
17#include "abstractdashview.h"
19static const qreal bufferRatio = 0.5;
21AbstractDashView::AbstractDashView()
22 : m_delegateModel(nullptr)
23 , m_asyncRequestedIndex(-1)
26 , m_buffer(320) // Same value used in qquickitemview.cpp in Qt 5.4
27 , m_displayMarginBeginning(0)
28 , m_displayMarginEnd(0)
29 , m_needsRelayout(false)
30 , m_delegateValidated(false)
31 , m_implicitHeightDirty(false)
33 connect(this, SIGNAL(widthChanged()), this, SLOT(relayout()));
34 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
37QAbstractItemModel *AbstractDashView::model() const
39 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
42void AbstractDashView::setModel(QAbstractItemModel *model)
44 if (model != this->model()) {
45 if (!m_delegateModel) {
46 createDelegateModel();
48 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
50 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
51 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
53 cleanupExistingItems();
55 Q_EMIT modelChanged();
60QQmlComponent *AbstractDashView::delegate() const
62 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
65void AbstractDashView::setDelegate(QQmlComponent *delegate)
67 if (delegate != this->delegate()) {
68 if (!m_delegateModel) {
69 createDelegateModel();
72 cleanupExistingItems();
74 m_delegateModel->setDelegate(delegate);
76 Q_EMIT delegateChanged();
77 m_delegateValidated = false;
82qreal AbstractDashView::columnSpacing() const
84 return m_columnSpacing;
87void AbstractDashView::setColumnSpacing(qreal columnSpacing)
89 if (columnSpacing != m_columnSpacing) {
90 m_columnSpacing = columnSpacing;
91 Q_EMIT columnSpacingChanged();
93 if (isComponentComplete()) {
99qreal AbstractDashView::rowSpacing() const
104void AbstractDashView::setRowSpacing(qreal rowSpacing)
106 if (rowSpacing != m_rowSpacing) {
107 m_rowSpacing = rowSpacing;
108 Q_EMIT rowSpacingChanged();
110 if (isComponentComplete()) {
116qreal AbstractDashView::cacheBuffer() const
121void AbstractDashView::setCacheBuffer(qreal buffer)
124 qmlInfo(this) << "Cannot set a negative cache buffer";
128 if (m_buffer != buffer) {
130 if (isComponentComplete()) {
133 emit cacheBufferChanged();
137qreal AbstractDashView::displayMarginBeginning() const
139 return m_displayMarginBeginning;
142void AbstractDashView::setDisplayMarginBeginning(qreal begin)
144 if (m_displayMarginBeginning == begin)
146 m_displayMarginBeginning = begin;
147 if (isComponentComplete()) {
150 emit displayMarginBeginningChanged();
153qreal AbstractDashView::displayMarginEnd() const
155 return m_displayMarginEnd;
158void AbstractDashView::setDisplayMarginEnd(qreal end)
160 if (m_displayMarginEnd == end)
162 m_displayMarginEnd = end;
163 if (isComponentComplete()) {
166 emit displayMarginEndChanged();
169void AbstractDashView::createDelegateModel()
171 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
172 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
173 if (isComponentComplete())
174 m_delegateModel->componentComplete();
177void AbstractDashView::refill()
179 if (!isComponentComplete() || height() < 0) {
183 const qreal from = -m_displayMarginBeginning;
184 const qreal to = height() + m_displayMarginEnd;
185 const qreal bufferFrom = from - m_buffer;
186 const qreal bufferTo = to + m_buffer;
188 bool added = addVisibleItems(from, to, false);
189 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
190 added |= addVisibleItems(bufferFrom, bufferTo, true);
192 if (added || removed) {
193 m_implicitHeightDirty = true;
198bool AbstractDashView::addVisibleItems(qreal fillFromY, qreal fillToY, bool asynchronous)
200 if (fillToY <= fillFromY)
206 if (m_delegateModel->count() == 0)
211 findBottomModelIndexToAdd(&modelIndex, &yPos);
212 bool changed = false;
213 while (modelIndex < m_delegateModel->count() && yPos <= fillToY) {
214 if (!createItem(modelIndex, asynchronous))
218 findBottomModelIndexToAdd(&modelIndex, &yPos);
221 findTopModelIndexToAdd(&modelIndex, &yPos);
222 while (modelIndex >= 0 && yPos > fillFromY) {
223 if (!createItem(modelIndex, asynchronous))
227 findTopModelIndexToAdd(&modelIndex, &yPos);
233QQuickItem *AbstractDashView::createItem(int modelIndex, bool asynchronous)
235 if (asynchronous && m_asyncRequestedIndex != -1)
238 m_asyncRequestedIndex = -1;
239 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
240 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
243 m_delegateModel->release(object);
244 if (!m_delegateValidated) {
245 m_delegateValidated = true;
246 QObject* delegateObj = delegate();
247 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
250 m_asyncRequestedIndex = modelIndex;
254 addItemToView(modelIndex, item);
259void AbstractDashView::releaseItem(QQuickItem *item)
261 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
262 if (flags & QQmlDelegateModel::Destroyed) {
263 item->setParentItem(nullptr);
267void AbstractDashView::setImplicitHeightDirty()
269 m_implicitHeightDirty = true;
272void AbstractDashView::itemCreated(int modelIndex, QObject *object)
274 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
276 qWarning() << "AbstractDashView::itemCreated got a non item for index" << modelIndex;
279 item->setParentItem(this);
281 // We only need to call createItem if we are here because of an asynchronous generation
282 // otherwise we are in this slot because createItem is creating the item sync
283 // and thus we don't need to call createItem again, nor need to set m_implicitHeightDirty
284 // and call polish because the sync createItem was called from addVisibleItems that
285 // is called from refill that will already do those if an item was added
286 if (modelIndex == m_asyncRequestedIndex) {
287 createItem(modelIndex, false);
288 m_implicitHeightDirty = true;
293void AbstractDashView::onModelUpdated(const QQmlChangeSet &changeSet, bool reset)
296 cleanupExistingItems();
298 processModelRemoves(changeSet.removes());
304void AbstractDashView::relayout()
306 m_needsRelayout = true;
310void AbstractDashView::onHeightChanged()
315void AbstractDashView::updatePolish()
320 if (m_needsRelayout) {
322 m_needsRelayout = false;
323 m_implicitHeightDirty = true;
328 const qreal from = -m_displayMarginBeginning;
329 const qreal to = height() + m_displayMarginEnd;
330 updateItemCulling(from, to);
332 if (m_implicitHeightDirty) {
333 calculateImplicitHeight();
334 m_implicitHeightDirty = false;
338void AbstractDashView::componentComplete()
341 m_delegateModel->componentComplete();
343 QQuickItem::componentComplete();
345 m_needsRelayout = true;
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list