on xenial help sign in
VerticalJournal improvements regarding model insertions and item height changes
7e0a3cbAlbert Astals Cid
plugins/Dash/abstractdashview.cpp+13
@@ -16,6 +16,8 @@
16
16
17#include "abstractdashview.h"
17#include "abstractdashview.h"
18
18
19+#include <private/qquickitem_p.h>
20+
19AbstractDashView::AbstractDashView()
21AbstractDashView::AbstractDashView()
20 : m_delegateModel(nullptr)
22 : m_delegateModel(nullptr)
21 , m_asyncRequestedIndex(-1)
23 , m_asyncRequestedIndex(-1)
@@ -249,6 +251,7 @@ QQuickItem *AbstractDashView::createItem(int modelIndex, bool asynchronous)
249 }
251 }
250 return nullptr;
252 return nullptr;
251 } else {
253 } else {
254+ QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Geometry);
252 addItemToView(modelIndex, item);
255 addItemToView(modelIndex, item);
253 return item;
256 return item;
254 }
257 }
@@ -256,6 +259,7 @@ QQuickItem *AbstractDashView::createItem(int modelIndex, bool asynchronous)
256
259
257void AbstractDashView::releaseItem(QQuickItem *item)
260void AbstractDashView::releaseItem(QQuickItem *item)
258{
261{
262+ QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
259 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
263 QQmlDelegateModel::ReleaseFlags flags = m_delegateModel->release(item);
260 if (flags & QQmlDelegateModel::Destroyed) {
264 if (flags & QQmlDelegateModel::Destroyed) {
261 item->setParentItem(nullptr);
265 item->setParentItem(nullptr);
@@ -294,6 +298,15 @@ void AbstractDashView::onModelUpdated(const QQmlChangeSet &changeSet, bool reset
294 cleanupExistingItems();
298 cleanupExistingItems();
295 } else {
299 } else {
296 processModelRemoves(changeSet.removes());
300 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+ }
297 }
310 }
298 polish();
311 polish();
299}
312}
plugins/Dash/abstractdashview.h+2−1
@@ -23,9 +23,10 @@ class QAbstractItemModel;
23class QQmlComponent;
23class QQmlComponent;
24
24
25#include <private/qqmldelegatemodel_p.h>
25#include <private/qqmldelegatemodel_p.h>
26+#include <private/qquickitemchangelistener_p.h>
26#include <qqmlinfo.h>
27#include <qqmlinfo.h>
27
28
28−class AbstractDashView : public QQuickItem
29+class AbstractDashView : public QQuickItem, public QQuickItemChangeListener
29{
30{
30 Q_OBJECT
31 Q_OBJECT
31
32
plugins/Dash/verticaljournal.cpp+9−1
@@ -96,7 +96,7 @@ void VerticalJournal::findTopModelIndexToAdd(int *modelIndex, qreal *yPos)
96
96
97 if (*modelIndex > 0) {
97 if (*modelIndex > 0) {
98 Q_ASSERT(m_indexColumnMap.contains(*modelIndex));
98 Q_ASSERT(m_indexColumnMap.contains(*modelIndex));
99− while (m_indexColumnMap[*modelIndex] != columnToAddTo) {
99+ while (*modelIndex > 0 && m_indexColumnMap[*modelIndex] != columnToAddTo) {
100 // We found out that we have to add to columnToAddTo
100 // We found out that we have to add to columnToAddTo
101 // and thought that we had to add *modelIndex, but history tells
101 // and thought that we had to add *modelIndex, but history tells
102 // it is not correct, so find up from *modelIndex until we found the index
102 // 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> &
270 }
270 }
271 }
271 }
272}
272}
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:
65 qreal columnWidth() const;
65 qreal columnWidth() const;
66 void setColumnWidth(qreal columnWidth);
66 void setColumnWidth(qreal columnWidth);
67
67
68+protected:
69+ void itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry) override;
70+
68Q_SIGNALS:
71Q_SIGNALS:
69 void columnWidthChanged();
72 void columnWidthChanged();
70
73
tests/plugins/Dash/verticaljournaltest.cpp+62
@@ -72,6 +72,20 @@ public:
72 endRemoveRows();
72 endRemoveRows();
73 }
73 }
74
74
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+
75private:
89private:
76 QStringList m_list;
90 QStringList m_list;
77};
91};
@@ -488,6 +502,54 @@ private Q_SLOTS:
488 QTRY_COMPARE(vj->implicitHeight(), 0.);
502 QTRY_COMPARE(vj->implicitHeight(), 0.);
489 }
503 }
490
504
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+
491private:
553private:
492 QQuickView *view;
554 QQuickView *view;
493 VerticalJournal *vj;
555 VerticalJournal *vj;