on xenial help sign in
VerticalJournal improvements regarding model insertions and item height changes

insertions in the middle need to trigger a reset Item height changes need to trigger a relayout (LP: #1599754) Approved by: Michał Sawicz

commit7e0a3cbbcb123f9282ddb376e5661ee830db6b8c authorAlbert Astals Cid <albert.astals@canonical.com> dateWed 3 Aug 2016, 15:51 (10 years ago) parent1e1a30fd38d2e2
5 files changed+89−2
plugins/Dash/abstractdashview.cpp+13 plugins/Dash/abstractdashview.h+2−1 plugins/Dash/verticaljournal.cpp+9−1 plugins/Dash/verticaljournal.h+3 tests/plugins/Dash/verticaljournaltest.cpp+62
plugins/Dash/abstractdashview.cpp+13
@@ -16,6 +16,8 @@
1616
1717#include "abstractdashview.h"
1818
19+#include <private/qquickitem_p.h>
20+
1921AbstractDashView::AbstractDashView()
2022 : m_delegateModel(nullptr)
2123 , m_asyncRequestedIndex(-1)
@@ -249,6 +251,7 @@ QQuickItem *AbstractDashView::createItem(int modelIndex, bool asynchronous)
249251 }
250252 return nullptr;
251253 } else {
254+ QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
252255 addItemToView(modelIndex, item);
253256 return item;
254257 }
@@ -256,6 +259,7 @@ QQuickItem *AbstractDashView::createItem(int modelIndex, bool asynchronous)
256259
257260void AbstractDashView::releaseItem(QQuickItem *item)
258261{
262+ QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
259263 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
260264 if (flags & QQmlDelegateModel::Destroyed) {
261265 item->setParentItem(nullptr);
@@ -294,6 +298,15 @@ void AbstractDashView::onModelUpdated(const QQmlChangeSet &changeSet, bool reset
294298 cleanupExistingItems();
295299 } else {
296300 processModelRemoves(changeSet.removes());
301+
302+ // The current AbstractDashViews do not support insertions that are not at the end
303+ // so reset if that happens
304+ Q_FOREACH(const QQmlChangeSet::Change insert, changeSet.inserts()) {
305+ if (insert.index < m_delegateModel->count() - 1) {
306+ cleanupExistingItems();
307+ break;
308+ }
309+ }
297310 }
298311 polish();
299312}
plugins/Dash/abstractdashview.h+2−1
@@ -23,9 +23,10 @@ class QAbstractItemModel;
2323class QQmlComponent;
2424
2525#include <private/qqmldelegatemodel_p.h>
26+#include <private/qquickitemchangelistener_p.h>
2627#include <qqmlinfo.h>
2728
28−class AbstractDashView : public QQuickItem
29+class AbstractDashView : public QQuickItem, public QQuickItemChangeListener
2930{
3031 Q_OBJECT
3132
plugins/Dash/verticaljournal.cpp+9−1
@@ -96,7 +96,7 @@ void VerticalJournal::findTopModelIndexToAdd(int *modelIndex, qreal *yPos)
9696
9797 if (*modelIndex > 0) {
9898 Q_ASSERT(m_indexColumnMap.contains(*modelIndex));
99− while (m_indexColumnMap[*modelIndex] != columnToAddTo) {
99+ while (*modelIndex > 0 && m_indexColumnMap[*modelIndex] != columnToAddTo) {
100100 // We found out that we have to add to columnToAddTo
101101 // and thought that we had to add *modelIndex, but history tells
102102 // it is not correct, so find up from *modelIndex until we found the index
@@ -270,3 +270,11 @@ void VerticalJournal::processModelRemoves(const QVector<QQmlChangeSet::Change> &
270270 }
271271 }
272272}
273+
274+void VerticalJournal::itemGeometryChanged(QQuickItem * /*item*/, const QRectF &newGeometry, const QRectF &oldGeometry)
275+{
276+ const qreal heightDiff = newGeometry.height() - oldGeometry.height();
277+ if (heightDiff != 0) {
278+ relayout();
279+ }
280+}
plugins/Dash/verticaljournal.h+3
@@ -65,6 +65,9 @@ public:
6565 qreal columnWidth() const;
6666 void setColumnWidth(qreal columnWidth);
6767
68+protected:
69+ void itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry) override;
70+
6871Q_SIGNALS:
6972 void columnWidthChanged();
7073
tests/plugins/Dash/verticaljournaltest.cpp+62
@@ -72,6 +72,20 @@ public:
7272 endRemoveRows();
7373 }
7474
75+ void insertString(int index, const QString& string)
76+ {
77+ beginInsertRows(QModelIndex(), index, index);
78+ m_list.insert(index, string);
79+ endInsertRows();
80+ }
81+
82+ void changeString(int i, const QString& string)
83+ {
84+ m_list[i] = string;
85+ auto idx = index(i, 0);
86+ dataChanged(idx, idx);
87+ }
88+
7589private:
7690 QStringList m_list;
7791};
@@ -488,6 +502,54 @@ private Q_SLOTS:
488502 QTRY_COMPARE(vj->implicitHeight(), 0.);
489503 }
490504
505+ void testInsertItem()
506+ {
507+ // Remove a few items from the end so the verifyItem list is smaller
508+ for (int i = 0; i < 12; ++i)
509+ model->removeLast();
510+
511+ model->insertString(0, "200");
512+
513+ QTRY_COMPARE(vj->m_columnVisibleItems.count(), 3);
514+ QTRY_COMPARE(vj->m_columnVisibleItems[0].count(), 2);
515+ QTRY_COMPARE(vj->m_columnVisibleItems[1].count(), 4);
516+ QTRY_COMPARE(vj->m_columnVisibleItems[2].count(), 3);
517+ verifyItem(vj->m_columnVisibleItems[0][0], 0, 0, 0, true);
518+ verifyItem(vj->m_columnVisibleItems[1][0], 1, 160, 0, true);
519+ verifyItem(vj->m_columnVisibleItems[2][0], 2, 320, 0, true);
520+ verifyItem(vj->m_columnVisibleItems[2][1], 3, 320, 60, true);
521+ verifyItem(vj->m_columnVisibleItems[1][1], 4, 160, 110, true);
522+ verifyItem(vj->m_columnVisibleItems[1][2], 5, 160, 130, true);
523+ verifyItem(vj->m_columnVisibleItems[1][3], 6, 160, 180, true);
524+ verifyItem(vj->m_columnVisibleItems[2][2], 7, 320, 195, true);
525+ verifyItem(vj->m_columnVisibleItems[0][1], 8, 0, 210, true);
526+ QCOMPARE(vj->implicitHeight(), 395.);
527+ }
528+
529+ void testChangeItem()
530+ {
531+ // Remove a few items from the end so the verifyItem list is smaller
532+ for (int i = 0; i < 11; ++i)
533+ model->removeLast();
534+
535+ model->changeString(0, "200");
536+
537+ QTRY_COMPARE(vj->m_columnVisibleItems.count(), 3);
538+ QTRY_COMPARE(vj->m_columnVisibleItems[0].count(), 2);
539+ QTRY_COMPARE(vj->m_columnVisibleItems[1].count(), 5);
540+ QTRY_COMPARE(vj->m_columnVisibleItems[2].count(), 2);
541+ verifyItem(vj->m_columnVisibleItems[0][0], 0, 0, 0, true);
542+ verifyItem(vj->m_columnVisibleItems[1][0], 1, 160, 0, true);
543+ verifyItem(vj->m_columnVisibleItems[2][0], 2, 320, 0, true);
544+ verifyItem(vj->m_columnVisibleItems[1][1], 3, 160, 60, true);
545+ verifyItem(vj->m_columnVisibleItems[1][2], 4, 160, 80, true);
546+ verifyItem(vj->m_columnVisibleItems[1][3], 5, 160, 130, true);
547+ verifyItem(vj->m_columnVisibleItems[2][1], 6, 320, 135, true);
548+ verifyItem(vj->m_columnVisibleItems[0][1], 7, 0, 210, true);
549+ verifyItem(vj->m_columnVisibleItems[1][4], 8, 160, 210, true);
550+ QCOMPARE(vj->implicitHeight(), 370.);
551+ }
552+
491553private:
492554 QQuickView *view;
493555 VerticalJournal *vj;