@@ -24,108 +24,9 @@
24static const qreal bufferRatio = 0.5;
24static const qreal bufferRatio = 0.5;
26OrganicGrid::OrganicGrid()
26OrganicGrid::OrganicGrid()
27− : m_delegateModel(nullptr)
28− , m_asyncRequestedIndex(-1)
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)
41− connect(this, SIGNAL(widthChanged()), this, SLOT(relayout()));
42− connect(this, SIGNAL(heightChanged()), this, SLOT(onHeightChanged()));
45−QAbstractItemModel *OrganicGrid::model() const
47− return m_delegateModel ? m_delegateModel->model().value<QAbstractItemModel *>() : nullptr;
50−void OrganicGrid::setModel(QAbstractItemModel *model)
52− if (model != this->model()) {
53− if (!m_delegateModel) {
54− createDelegateModel();
56−#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
57− disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
59− m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
60− connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
62− disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
64− m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
65− connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
68− cleanupExistingItems();
70− Q_EMIT modelChanged();
75−QQmlComponent *OrganicGrid::delegate() const
77− return m_delegateModel ? m_delegateModel->delegate() : nullptr;
80−void OrganicGrid::setDelegate(QQmlComponent *delegate)
82− if (delegate != this->delegate()) {
83− if (!m_delegateModel) {
84− createDelegateModel();
87− cleanupExistingItems();
89− m_delegateModel->setDelegate(delegate);
91− Q_EMIT delegateChanged();
92− m_delegateValidated = false;
97−qreal OrganicGrid::columnSpacing() const
99− return m_columnSpacing;
102−void OrganicGrid::setColumnSpacing(qreal columnSpacing)
104− if (columnSpacing != m_columnSpacing) {
105− m_columnSpacing = columnSpacing;
106− Q_EMIT columnSpacingChanged();
108− if (isComponentComplete()) {
114−qreal OrganicGrid::rowSpacing() const
116− return m_rowSpacing;
119−void OrganicGrid::setRowSpacing(qreal rowSpacing)
121− if (rowSpacing != m_rowSpacing) {
122− m_rowSpacing = rowSpacing;
123− Q_EMIT rowSpacingChanged();
125− if (isComponentComplete()) {
131QSizeF OrganicGrid::smallDelegateSize() const
32QSizeF OrganicGrid::smallDelegateSize() const
@@ -162,272 +63,6 @@ void OrganicGrid::setBigDelegateSize(const QSizeF &size)
165−qreal OrganicGrid::delegateCreationBegin() const
167− return m_delegateCreationBegin;
170−void OrganicGrid::setDelegateCreationBegin(qreal begin)
172− m_delegateCreationBeginValid = true;
173− if (m_delegateCreationBegin == begin)
175− m_delegateCreationBegin = begin;
176− if (isComponentComplete()) {
179− emit delegateCreationBeginChanged();
182−void OrganicGrid::resetDelegateCreationBegin()
184− m_delegateCreationBeginValid = false;
185− if (m_delegateCreationBegin == 0)
187− m_delegateCreationBegin = 0;
188− if (isComponentComplete()) {
191− emit delegateCreationBeginChanged();
194−qreal OrganicGrid::delegateCreationEnd() const
196− return m_delegateCreationEnd;
199−void OrganicGrid::setDelegateCreationEnd(qreal end)
201− m_delegateCreationEndValid = true;
202− if (m_delegateCreationEnd == end)
204− m_delegateCreationEnd = end;
205− if (isComponentComplete()) {
208− emit delegateCreationEndChanged();
211−void OrganicGrid::resetDelegateCreationEnd()
213− m_delegateCreationEndValid = false;
214− if (m_delegateCreationEnd == 0)
216− m_delegateCreationEnd = 0;
217− if (isComponentComplete()) {
220− emit delegateCreationEndChanged();
223−void OrganicGrid::createDelegateModel()
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*)));
229− m_delegateModel = new QQmlDelegateModel(qmlContext(this), this);
230− connect(m_delegateModel, SIGNAL(createdItem(int,QObject*)), this, SLOT(itemCreated(int,QObject*)));
232− if (isComponentComplete())
233− m_delegateModel->componentComplete();
236−void OrganicGrid::refill()
238− if (!isComponentComplete()) {
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;
249− bool added = addVisibleItems(from, to, false);
250− bool removed = removeNonVisibleItems(bufferFrom, bufferTo);
251− added |= addVisibleItems(bufferFrom, bufferTo, true);
253− if (added || removed) {
254− m_implicitHeightDirty = true;
259−bool OrganicGrid::addVisibleItems(qreal fillFromY, qreal fillToY, bool asynchronous)
264− if (m_delegateModel->count() == 0)
269− findBottomModelIndexToAdd(&modelIndex, &yPos);
270− bool changed = false;
271− while (modelIndex < m_delegateModel->count() && yPos <= fillToY) {
272− if (!createItem(modelIndex, asynchronous))
276− findBottomModelIndexToAdd(&modelIndex, &yPos);
279− findTopModelIndexToAdd(&modelIndex, &yPos);
280− while (modelIndex >= 0 && yPos > fillFromY) {
281− if (!createItem(modelIndex, asynchronous))
285− findTopModelIndexToAdd(&modelIndex, &yPos);
291−QQuickItem *OrganicGrid::createItem(int modelIndex, bool asynchronous)
293− if (asynchronous && m_asyncRequestedIndex != -1)
296− m_asyncRequestedIndex = -1;
297−#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
298− QQuickItem *item = m_delegateModel->item(modelIndex, asynchronous);
300− QObject* object = m_delegateModel->object(modelIndex, asynchronous);
301− QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
304−#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
305− m_asyncRequestedIndex = modelIndex;
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";
315− m_asyncRequestedIndex = modelIndex;
320− positionItem(modelIndex, item);
325−void OrganicGrid::releaseItem(QQuickItem *item)
327−#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
328− QQuickVisualModel::ReleaseFlags flags = m_delegateModel->release(item);
329− if (flags & QQuickVisualModel::Destroyed) {
331− QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
332− if (flags & QQmlDelegateModel::Destroyed) {
334− item->setParentItem(nullptr);
338−void OrganicGrid::setImplicitHeightDirty()
340− m_implicitHeightDirty = true;
343−#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
344−void OrganicGrid::itemCreated(int modelIndex, QQuickItem *item)
347−void OrganicGrid::itemCreated(int modelIndex, QObject *object)
349− QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
351− qWarning() << "OrganicGrid::itemCreated got a non item for index" << modelIndex;
355− item->setParentItem(this);
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;
369−#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
370−void OrganicGrid::onModelUpdated(const QQuickChangeSet &changeSet, bool reset)
372−void OrganicGrid::onModelUpdated(const QQmlChangeSet &changeSet, bool reset)
376− cleanupExistingItems();
378− processModelRemoves(changeSet.removes());
384−void OrganicGrid::relayout()
386− m_needsRelayout = true;
390−void OrganicGrid::onHeightChanged()
395−void OrganicGrid::updatePolish()
400− if (m_needsRelayout) {
402− m_needsRelayout = false;
403− m_implicitHeightDirty = true;
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);
413− if (m_implicitHeightDirty) {
414− calculateImplicitHeight();
415− m_implicitHeightDirty = false;
419−void OrganicGrid::componentComplete()
421− if (m_delegateModel)
422− m_delegateModel->componentComplete();
424− QQuickItem::componentComplete();
426− m_needsRelayout = true;
431QPointF OrganicGrid::positionForIndex(int modelIndex) const
66QPointF OrganicGrid::positionForIndex(int modelIndex) const
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();
565void OrganicGrid::doRelayout()
200void OrganicGrid::doRelayout()
@@ -587,7 +222,7 @@ void OrganicGrid::updateItemCulling(qreal visibleFromY, qreal visibleToY)
587void OrganicGrid::calculateImplicitHeight()
222void OrganicGrid::calculateImplicitHeight()
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);