From 8425e198c1cbc45183f3ef5747da5e11e6a8f5b4 Mon Sep 17 00:00:00 2001
From: Ulf Hermann <ulf.hermann@theqtcompany.com>
Date: Thu, 9 Apr 2015 15:16:09 +0200
Subject: [PATCH] Timeline: Be more exact about model height changes.

Emit the signal every time the height changes, but not if it doesn't.

Change-Id: I3a3da737bc99ae99ac6d5690c55c21d94cf5b647
Reviewed-by: Joerg Bornemann <joerg.bornemann@theqtcompany.com>
---
 src/libs/timeline/timelinemodel.cpp           | 29 ++++++++++---------
 .../timelinemodel/tst_timelinemodel.cpp       |  9 ++++++
 2 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/src/libs/timeline/timelinemodel.cpp b/src/libs/timeline/timelinemodel.cpp
index 2b1d7d50cc1..5498077bfd8 100644
--- a/src/libs/timeline/timelinemodel.cpp
+++ b/src/libs/timeline/timelinemodel.cpp
@@ -121,8 +121,10 @@ void TimelineModel::setCollapsedRowCount(int rows)
     if (d->collapsedRowCount != rows) {
         d->collapsedRowCount = rows;
         emit collapsedRowCountChanged();
-        if (!d->expanded)
+        if (!d->expanded) {
             emit rowCountChanged();
+            emit heightChanged(); // collapsed rows have a fixed size
+        }
     }
 }
 
@@ -136,12 +138,16 @@ void TimelineModel::setExpandedRowCount(int rows)
 {
     Q_D(TimelineModel);
     if (d->expandedRowCount != rows) {
+        int prevHeight = height();
         if (d->rowOffsets.length() > rows)
             d->rowOffsets.resize(rows);
         d->expandedRowCount = rows;
         emit expandedRowCountChanged();
-        if (d->expanded)
+        if (d->expanded) {
             emit rowCountChanged();
+            if (height() != prevHeight)
+                emit heightChanged();
+        }
     }
 }
 
@@ -156,25 +162,16 @@ TimelineModel::TimelineModelPrivate::TimelineModelPrivate(int modelId, const QSt
 {
 }
 
-void TimelineModel::TimelineModelPrivate::init(TimelineModel *q)
-{
-    q_ptr = q;
-    connect(q,SIGNAL(expandedChanged()),q,SIGNAL(heightChanged()));
-    connect(q,SIGNAL(hiddenChanged()),q,SIGNAL(heightChanged()));
-    connect(q,SIGNAL(emptyChanged()),q,SIGNAL(heightChanged()));
-}
-
-
 TimelineModel::TimelineModel(TimelineModelPrivate &dd, QObject *parent) :
     QObject(parent), d_ptr(&dd)
 {
-    d_ptr->init(this);
+    d_ptr->q_ptr = this;
 }
 
 TimelineModel::TimelineModel(int modelId, const QString &displayName, QObject *parent) :
     QObject(parent), d_ptr(new TimelineModelPrivate(modelId, displayName))
 {
-    d_ptr->init(this);
+    d_ptr->q_ptr = this;
 }
 
 TimelineModel::~TimelineModel()
@@ -492,8 +489,11 @@ void TimelineModel::setExpanded(bool expanded)
 {
     Q_D(TimelineModel);
     if (expanded != d->expanded) {
+        int prevHeight = height();
         d->expanded = expanded;
         emit expandedChanged();
+        if (prevHeight != height())
+            emit heightChanged();
         if (d->collapsedRowCount != d->expandedRowCount)
             emit rowCountChanged();
     }
@@ -509,8 +509,11 @@ void TimelineModel::setHidden(bool hidden)
 {
     Q_D(TimelineModel);
     if (hidden != d->hidden) {
+        int prevHeight = height();
         d->hidden = hidden;
         emit hiddenChanged();
+        if (height() != prevHeight)
+            emit heightChanged();
     }
 }
 
diff --git a/tests/auto/timeline/timelinemodel/tst_timelinemodel.cpp b/tests/auto/timeline/timelinemodel/tst_timelinemodel.cpp
index 078bcbb04eb..0c8032ae7ad 100644
--- a/tests/auto/timeline/timelinemodel/tst_timelinemodel.cpp
+++ b/tests/auto/timeline/timelinemodel/tst_timelinemodel.cpp
@@ -227,14 +227,23 @@ void tst_TimelineModel::rowOffset()
 void tst_TimelineModel::height()
 {
     DummyModel dummy;
+    QSignalSpy spy(&dummy, SIGNAL(heightChanged()));
     QCOMPARE(dummy.height(), 0);
     dummy.loadData();
+    QCOMPARE(spy.count(), 1);
     QCOMPARE(dummy.height(), 2 * DefaultRowHeight);
     dummy.setExpanded(true);
+    QCOMPARE(spy.count(), 2);
     QCOMPARE(dummy.height(), 3 * DefaultRowHeight);
     dummy.setExpandedRowHeight(1, 80);
+    QCOMPARE(spy.count(), 3);
     QCOMPARE(dummy.height(), 2 * DefaultRowHeight + 80);
+    dummy.setHidden(true);
+    QCOMPARE(spy.count(), 4);
+    QCOMPARE(dummy.height(), 0);
     dummy.clear();
+    // When clearing the height can change several times in a row.
+    QVERIFY(spy.count() > 4);
     dummy.loadData();
     dummy.setExpanded(true);
     QCOMPARE(dummy.rowHeight(1), DefaultRowHeight); // Make sure the row height gets reset.
-- 
GitLab