From 5e7e9cac3500805650cc6b52e4e1503f7ec654a8 Mon Sep 17 00:00:00 2001 From: Pierre Lamot Date: Wed, 23 Mar 2022 17:19:48 +0100 Subject: [PATCH] qt: allow to move or remove range of items from the cache this would allow to move or update elements in "a priori" before getting the notification from the database --- modules/gui/qt/medialibrary/mlbasemodel.cpp | 21 +++++++++++ modules/gui/qt/medialibrary/mlbasemodel.hpp | 3 ++ modules/gui/qt/medialibrary/mllistcache.cpp | 42 +++++++++++++++++++++ modules/gui/qt/medialibrary/mllistcache.hpp | 13 +++++++ 4 files changed, 79 insertions(+) diff --git a/modules/gui/qt/medialibrary/mlbasemodel.cpp b/modules/gui/qt/medialibrary/mlbasemodel.cpp index bb5653dd5e..3380071121 100644 --- a/modules/gui/qt/medialibrary/mlbasemodel.cpp +++ b/modules/gui/qt/medialibrary/mlbasemodel.cpp @@ -574,6 +574,27 @@ void MLBaseModel::deleteItemInCache(const MLItemId& mlid) m_cache->deleteItem(mlid); } + +void MLBaseModel::moveRangeInCache(int first, int last, int to) +{ + if (!m_cache) + { + emit resetRequested(); + return; + } + m_cache->moveRange(first, last, to); +} + +void MLBaseModel::deleteRangeInCache(int first, int last) +{ + if (!m_cache) + { + emit resetRequested(); + return; + } + m_cache->deleteRange(first, last); +} + //------------------------------------------------------------------------------------------------- MLBaseModel::BaseLoader::BaseLoader(MLItemId parent, QString searchPattern, diff --git a/modules/gui/qt/medialibrary/mlbasemodel.hpp b/modules/gui/qt/medialibrary/mlbasemodel.hpp index daf6e697d8..a0dfab94de 100644 --- a/modules/gui/qt/medialibrary/mlbasemodel.hpp +++ b/modules/gui/qt/medialibrary/mlbasemodel.hpp @@ -123,6 +123,9 @@ protected: //this is only to reflect changes from the ML, it won't alter the database void deleteItemInCache(const MLItemId& mlid); + void moveRangeInCache(int first, int last, int to); + void deleteRangeInCache(int first, int last); + virtual void onVlcMlEvent( const MLEvent &event ); diff --git a/modules/gui/qt/medialibrary/mllistcache.cpp b/modules/gui/qt/medialibrary/mllistcache.cpp index 7ff6930d9c..4fd48cad79 100644 --- a/modules/gui/qt/medialibrary/mllistcache.cpp +++ b/modules/gui/qt/medialibrary/mllistcache.cpp @@ -151,6 +151,48 @@ int MLListCache::deleteItem(const MLItemId& mlid) return pos; } +void MLListCache::moveRange(int first, int last, int to) +{ + assert(first <= last); + if (first <= to && to <= last) + return; + + emit beginMoveRows(first, last, to); + auto it = m_cachedData->list.begin(); + //build a temporary list with the items in order + std::vector tmpList; + if (to < first) + { + std::move(it, it+to, std::back_inserter(tmpList)); + std::move(it+first, it+(last+1), std::back_inserter(tmpList)); + std::move(it+to, it+first, std::back_inserter(tmpList)); + std::move(it+(last+1), m_cachedData->list.end(), std::back_inserter(tmpList)); + } + else //last < to + { + std::move(it, it+first, std::back_inserter(tmpList)); + std::move(it+last+1, it+to, std::back_inserter(tmpList)); + std::move(it+first, it+(last+1), std::back_inserter(tmpList)); + std::move(it+to, m_cachedData->list.end(), std::back_inserter(tmpList)); + } + + m_cachedData->list = std::move(tmpList); + emit endMoveRows(); +} + +void MLListCache::deleteRange(int first, int last) +{ + assert(first <= last); + emit beginRemoveRows(first, last); + auto it = m_cachedData->list.begin(); + m_cachedData->list.erase(it+first, it+(last+1)); + size_t delta = m_cachedData->loadedCount - m_cachedData->list.size(); + m_cachedData->loadedCount -= delta; + m_cachedData->totalCount -= delta; + emit endRemoveRows(); + emit localSizeChanged(m_cachedData->totalCount); +} + ssize_t MLListCache::count() const { if (!m_cachedData) diff --git a/modules/gui/qt/medialibrary/mllistcache.hpp b/modules/gui/qt/medialibrary/mllistcache.hpp index d5f5a55e9a..56b1e61635 100644 --- a/modules/gui/qt/medialibrary/mllistcache.hpp +++ b/modules/gui/qt/medialibrary/mllistcache.hpp @@ -155,6 +155,19 @@ public: */ int deleteItem(const MLItemId& mlid); + + /** + * move items in the cache contained between @a first + * and @a last to the index @a to + */ + void moveRange(int first, int last, int to); + + /** + * remove items in the cache from the index @a first up to the + * index @a last + */ + void deleteRange(int first, int last); + /** * Return the number of items or `COUNT_UNINITIALIZED` *