From bd529f74d4ed4f93b750c25f933bdd77bd2ffd94 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Wed, 9 Jul 2014 11:08:32 +0200 Subject: [PATCH] QmlProfiler: make sure indices in SortedTimelineModel are correct In the rare case that a new item is not appended but inserted in the middle of already existing data, the end times were confused. On that occasion, also add a note explaining how the indices work, Change-Id: I587b8285cd5482a9ffb1592302b442192e5944b8 Reviewed-by: Kai Koehne --- src/plugins/qmlprofiler/sortedtimelinemodel.cpp | 4 ++++ src/plugins/qmlprofiler/sortedtimelinemodel.h | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/plugins/qmlprofiler/sortedtimelinemodel.cpp b/src/plugins/qmlprofiler/sortedtimelinemodel.cpp index 9453b636946..6a39b96d5b7 100644 --- a/src/plugins/qmlprofiler/sortedtimelinemodel.cpp +++ b/src/plugins/qmlprofiler/sortedtimelinemodel.cpp @@ -39,6 +39,10 @@ range. Mind that you can always make that happen by defining a range that spans the whole available time span. That, however, will make any code that uses firstStartTime() and lastEndTime() for selecting subsets of the model always select all of it. + + \note Indices returned from the various methods are only valid until a new range is inserted + before them. Inserting a new range before a given index moves the range pointed to by the + index by one. Incrementing the index by one will make it point to the item again. */ /*! diff --git a/src/plugins/qmlprofiler/sortedtimelinemodel.h b/src/plugins/qmlprofiler/sortedtimelinemodel.h index 883b3e30b26..3aac863c610 100644 --- a/src/plugins/qmlprofiler/sortedtimelinemodel.h +++ b/src/plugins/qmlprofiler/sortedtimelinemodel.h @@ -83,13 +83,18 @@ public: /* Doing insert-sort here is preferable as most of the time the times will actually be * presorted in the right way. So usually this will just result in appending. */ int index = insertSorted(ranges, Range(startTime, duration, item)); + if (index < ranges.size() - 1) + incrementStartIndices(index); insertSorted(endTimes, RangeEnd(index, startTime + duration)); return index; } inline int insertStart(qint64 startTime, const Data &item) { - return insertSorted(ranges, Range(startTime, 0, item)); + int index = insertSorted(ranges, Range(startTime, 0, item)); + if (index < ranges.size() - 1) + incrementStartIndices(index); + return index; } inline void insertEnd(int index, qint64 duration) @@ -154,6 +159,14 @@ public: } protected: + void incrementStartIndices(int index) + { + for (int i = 0; i < endTimes.size(); ++i) { + if (endTimes[i].startIndex >= index) + endTimes[i].startIndex++; + } + } + template static inline int insertSorted(QVector &container, const RangeDelimiter &item) {