From 9efb56e2d43161d952efb444d1f13d87bfdd45b5 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 30 Jun 2018 21:32:14 +0200 Subject: Dive pictures: don't update all images on drag&drop to profile Gracefully handle drag & drop to the profile, which changes the offset of the pictures. To do this, keep the pictures in the DivePictureModel and the ProfileWidget2 sorted by offset and re-arrange if needed to keep the list sorted. This needs some code reshuffling. Introduce a helper-function that moves ranges in arrays. Signed-off-by: Berthold Stoeger Signed-off-by: Dirk Hohndel --- core/qthelper.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'core') diff --git a/core/qthelper.h b/core/qthelper.h index a2decd527..e1517991c 100644 --- a/core/qthelper.h +++ b/core/qthelper.h @@ -88,6 +88,32 @@ QString getUserAgent(); #define TITLE_OR_TEXT(_t, _m) _t, _m #endif +// Move a range in a vector to a different position. +// The parameters are given according to the usual STL-semantics: +// v: a container with STL-like random access iterator via std::begin(...) +// rangeBegin: index of first element +// rangeEnd: index one *past* last element +// destination: index to element before which the range will be moved +// Owing to std::begin() magic, this function works with STL-like containers: +// QVector v{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; +// moveInVector(v, 1, 4, 6); +// as well as with C-style arrays: +// int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; +// moveInVector(array, 1, 4, 6); +// Both calls will have the following effect: +// Before: 0 1 2 3 4 5 6 7 8 9 +// After: 0 4 5 1 2 3 6 7 8 9 +// No sanitizing of the input arguments is performed. +template +void moveInVector(Vector &v, int rangeBegin, int rangeEnd, int destination) +{ + auto it = std::begin(v); + if (destination > rangeEnd) + std::rotate(it + rangeBegin, it + rangeEnd, it + destination); + else if (destination < rangeBegin) + std::rotate(it + destination, it + rangeBegin, it + rangeEnd); +} + #endif // 3) Functions visible to C and C++ -- cgit v1.2.3-70-g09d2