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_displayMarginBeginning(0)
27 , m_displayMarginEnd(0)
28 , m_needsRelayout(false)
29 , m_delegateValidated(false)
30 , m_implicitHeightDirty(false)
32 connect(this, SIGNAL(widthChanged()), this, SLOT(relayout()));
33 connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
36QAbstractItemModel *AbstractDashView::model() const
38 return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
41void AbstractDashView::setModel(QAbstractItemModel *model)
43 if (model != this->model()) {
44 if (!m_delegateModel) {
45 createDelegateModel();
47 disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
49 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
50 connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
52 cleanupExistingItems();
54 Q_EMIT modelChanged();
59QQmlComponent *AbstractDashView::delegate() const
61 return m_delegateModel ? m_delegateModel->delegate() : nullptr;
64void AbstractDashView::setDelegate(QQmlComponent *delegate)
66 if (delegate != this->delegate()) {
67 if (!m_delegateModel) {
68 createDelegateModel();
71 cleanupExistingItems();
73 m_delegateModel->setDelegate(delegate);
75 Q_EMIT delegateChanged();
76 m_delegateValidated = false;
81qreal AbstractDashView::columnSpacing() const
83 return m_columnSpacing;
86void AbstractDashView::setColumnSpacing(qreal columnSpacing)
88 if (columnSpacing != m_columnSpacing) {
89 m_columnSpacing = columnSpacing;
90 Q_EMIT columnSpacingChanged();
92 if (isComponentComplete()) {
98qreal AbstractDashView::rowSpacing() const
103void AbstractDashView::setRowSpacing(qreal rowSpacing)
105 if (rowSpacing != m_rowSpacing) {
106 m_rowSpacing = rowSpacing;
107 Q_EMIT rowSpacingChanged();
109 if (isComponentComplete()) {
115qreal AbstractDashView::displayMarginBeginning() const
117 return m_displayMarginBeginning;
120void AbstractDashView::setDisplayMarginBeginning(qreal begin)
122 if (m_displayMarginBeginning == begin)
124 m_displayMarginBeginning = begin;
125 if (isComponentComplete()) {
128 emit displayMarginBeginningChanged();
131qreal AbstractDashView::displayMarginEnd() const
133 return m_displayMarginEnd;
136void AbstractDashView::setDisplayMarginEnd(qreal end)
138 if (m_displayMarginEnd == end)
140 m_displayMarginEnd = end;
141 if (isComponentComplete()) {
144 emit displayMarginEndChanged();
147void AbstractDashView::createDelegateModel()
149 m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
150 connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
151 if (isComponentComplete())
152 m_delegateModel->componentComplete();
155void AbstractDashView::refill()
157 if (!isComponentComplete() || height() < 0) {
161 const qreal from = -m_displayMarginBeginning;
162 const qreal to = height() + m_displayMarginEnd;
163 const qreal buffer = (to - from) * bufferRatio;
164 const qreal bufferFrom = from - buffer;
165 const qreal bufferTo = to + buffer;
167 bool added = addVisibleItems(from, to, false);
168 bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
169 added |= addVisibleItems(bufferFrom, bufferTo, true);
171 if (added || removed) {
172 m_implicitHeightDirty = true;
177bool AbstractDashView::addVisibleItems(qreal fillFromY, qreal fillToY, bool asynchronous)
182 if (m_delegateModel->count() == 0)
187 findBottomModelIndexToAdd(&modelIndex, &yPos);
188 bool changed = false;
189 while (modelIndex < m_delegateModel->count() && yPos <= fillToY) {
190 if (!createItem(modelIndex, asynchronous))
194 findBottomModelIndexToAdd(&modelIndex, &yPos);
197 findTopModelIndexToAdd(&modelIndex, &yPos);
198 while (modelIndex >= 0 && yPos > fillFromY) {
199 if (!createItem(modelIndex, asynchronous))
203 findTopModelIndexToAdd(&modelIndex, &yPos);
209QQuickItem *AbstractDashView::createItem(int modelIndex, bool asynchronous)
211 if (asynchronous && m_asyncRequestedIndex != -1)
214 m_asyncRequestedIndex = -1;
215 QObject* object = m_delegateModel->object(modelIndex, asynchronous);
216 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
219 m_delegateModel->release(object);
220 if (!m_delegateValidated) {
221 m_delegateValidated = true;
222 QObject* delegateObj = delegate();
223 qmlInfo(delegateObj ? delegateObj : this) << "Delegate must be of Item type";
226 m_asyncRequestedIndex = modelIndex;
230 addItemToView(modelIndex, item);
235void AbstractDashView::releaseItem(QQuickItem *item)
237 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
238 if (flags & QQmlDelegateModel::Destroyed) {
239 item->setParentItem(nullptr);
243void AbstractDashView::setImplicitHeightDirty()
245 m_implicitHeightDirty = true;
248void AbstractDashView::itemCreated(int modelIndex, QObject *object)
250 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
252 qWarning() << "AbstractDashView::itemCreated got a non item for index" << modelIndex;
255 item->setParentItem(this);
257 // We only need to call createItem if we are here because of an asynchronous generation
258 // otherwise we are in this slot because createItem is creating the item sync
259 // and thus we don't need to call createItem again, nor need to set m_implicitHeightDirty
260 // and call polish because the sync createItem was called from addVisibleItems that
261 // is called from refill that will already do those if an item was added
262 if (modelIndex == m_asyncRequestedIndex) {
263 createItem(modelIndex, false);
264 m_implicitHeightDirty = true;
269void AbstractDashView::onModelUpdated(const QQmlChangeSet &changeSet, bool reset)
272 cleanupExistingItems();
274 processModelRemoves(changeSet.removes());
280void AbstractDashView::relayout()
282 m_needsRelayout = true;
286void AbstractDashView::onHeightChanged()
291void AbstractDashView::updatePolish()
296 if (m_needsRelayout) {
298 m_needsRelayout = false;
299 m_implicitHeightDirty = true;
304 const qreal from = -m_displayMarginBeginning;
305 const qreal to = height() + m_displayMarginEnd;
306 updateItemCulling(from, to);
308 if (m_implicitHeightDirty) {
309 calculateImplicitHeight();
310 m_implicitHeightDirty = false;
314void AbstractDashView::componentComplete()
317 m_delegateModel->componentComplete();
319 QQuickItem::componentComplete();
321 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