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
This commit is contained in:
Pierre Lamot 2022-03-23 17:19:48 +01:00 committed by Jean-Baptiste Kempf
parent ed433cc7c1
commit 5e7e9cac35
4 changed files with 79 additions and 0 deletions

View File

@ -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,

View File

@ -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 );

View File

@ -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<ItemType> 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)

View File

@ -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`
*