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 <kai.koehne@digia.com>
This commit is contained in:
Ulf Hermann
2014-07-09 11:08:32 +02:00
parent d79026e3fb
commit bd529f74d4
2 changed files with 18 additions and 1 deletions

View File

@@ -39,6 +39,10 @@
range. Mind that you can always make that happen by defining a range that spans the whole 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 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. 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.
*/ */
/*! /*!

View File

@@ -83,13 +83,18 @@ public:
/* Doing insert-sort here is preferable as most of the time the times will actually be /* 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. */ * presorted in the right way. So usually this will just result in appending. */
int index = insertSorted(ranges, Range(startTime, duration, item)); int index = insertSorted(ranges, Range(startTime, duration, item));
if (index < ranges.size() - 1)
incrementStartIndices(index);
insertSorted(endTimes, RangeEnd(index, startTime + duration)); insertSorted(endTimes, RangeEnd(index, startTime + duration));
return index; return index;
} }
inline int insertStart(qint64 startTime, const Data &item) 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) inline void insertEnd(int index, qint64 duration)
@@ -154,6 +159,14 @@ public:
} }
protected: protected:
void incrementStartIndices(int index)
{
for (int i = 0; i < endTimes.size(); ++i) {
if (endTimes[i].startIndex >= index)
endTimes[i].startIndex++;
}
}
template<typename RangeDelimiter> template<typename RangeDelimiter>
static inline int insertSorted(QVector<RangeDelimiter> &container, const RangeDelimiter &item) static inline int insertSorted(QVector<RangeDelimiter> &container, const RangeDelimiter &item)
{ {