From bf60d29e99a7d201850766d9195a052599dfd018 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Thu, 28 Jun 2018 20:11:39 +0200 Subject: [PATCH] Dive pictures: adjust rowDDEnd and rowDDStart on picture deletion In DivePictureModel, rowDDEnd and rowDDStart specify the range of pictures in the profile plot. Obviously, these have to be adjusted when pictures are deleted. Signed-off-by: Berthold Stoeger --- qt-models/divepicturemodel.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/qt-models/divepicturemodel.cpp b/qt-models/divepicturemodel.cpp index 8c66f5974..541f88027 100644 --- a/qt-models/divepicturemodel.cpp +++ b/qt-models/divepicturemodel.cpp @@ -138,6 +138,14 @@ static bool removePictureFromSelectedDive(const char *fileUrl) return false; } +// Calculate how many items of a range are before the given index +static int rangeBefore(int rangeFrom, int rangeTo, int index) +{ + if (rangeTo <= rangeFrom) + return 0; + return std::min(rangeTo, index) - std::min(rangeFrom, index); +} + void DivePictureModel::removePictures(const QVector &fileUrls) { bool removed = false; @@ -163,6 +171,12 @@ void DivePictureModel::removePictures(const QVector &fileUrls) beginRemoveRows(QModelIndex(), i, j - 1); pictures.erase(pictures.begin() + i, pictures.begin() + j); endRemoveRows(); + + // After removing pictures, we have to adjust rowDDStart and rowDDEnd. + // Calculate the part of the range that is before rowDDStart and rowDDEnd, + // respectively and subtract accordingly. + rowDDStart -= rangeBefore(i, j, rowDDStart); + rowDDEnd -= rangeBefore(i, j, rowDDEnd); } }