From 043460f1f42a0a47b4ab76cee43bef86eaeeaab6 Mon Sep 17 00:00:00 2001 From: Ahmed Sobhy <175150-ahmedsobhy@users.noreply.code.videolan.org> Date: Thu, 5 Feb 2026 18:52:40 +0200 Subject: [PATCH] qt: update getItemsForIndexes in NetworkDeviceModel to support asynchronous callbacks --- modules/gui/qt/network/networkdevicemodel.cpp | 79 +++++++++++-------- modules/gui/qt/network/networkdevicemodel.hpp | 5 +- 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/modules/gui/qt/network/networkdevicemodel.cpp b/modules/gui/qt/network/networkdevicemodel.cpp index cd753c12ff..d3379722d3 100644 --- a/modules/gui/qt/network/networkdevicemodel.cpp +++ b/modules/gui/qt/network/networkdevicemodel.cpp @@ -18,6 +18,8 @@ #include #include +#include +#include #include "maininterface/mainctx.hpp" @@ -599,48 +601,63 @@ bool NetworkDeviceModel::addAndPlay(const QModelIndexList& itemIdList) } /* Q_INVOKABLE */ -QVariantList NetworkDeviceModel::getItemsForIndexes(const QModelIndexList & indexes) const +void NetworkDeviceModel::getItemsForIndexes(const QModelIndexList & indexes, QJSValue callback) { - Q_D(const NetworkDeviceModel); - QVariantList items; - - bool allInCache = true; - for (const QModelIndex & modelIndex : indexes) - { - const NetworkDeviceItem* item = d->getItemForRow(modelIndex.row()); - if (!item) - { - allInCache = false; - break; - } + if (!callback.isCallable()) + return; - items.append(QVariant::fromValue(SharedInputItem(item->getInputItem().get(), true))); - } + getItemsForIndexes(indexes, [this, callback](const QVariantList& items) mutable { + auto engine = qjsEngine(this); + if (engine) + callback.call({engine->toScriptValue(items)}); + }); +} - if (allInCache) - return items; +void NetworkDeviceModel::getItemsForIndexes(const QModelIndexList & indexes, std::function callback) +{ + Q_D(NetworkDeviceModel); - // Sometimes if there are many items selected, some are not in cache so we need to rebuild the list - items.clear(); - std::vector modelData = d->getModelData(d->m_searchPattern); + if (!callback) + return; - auto sortFunc = d->getSortFunction(); - if (sortFunc) - std::sort(modelData.begin(), modelData.end(), sortFunc); + if (indexes.isEmpty()) + { + callback(QVariantList()); + return; + } + int maxIndex = 0; for (const QModelIndex & modelIndex : indexes) { - int index = modelIndex.row(); + if (modelIndex.row() > maxIndex) + maxIndex = modelIndex.row(); + } - if (index < 0 || index >= static_cast(modelData.size())) - continue; + // check if we have loaded all items we need + unsigned int loaded = getLoadedCount(); + unsigned int maximum = getMaximumCount(); + unsigned int needed = static_cast(maxIndex) + 1; - const NetworkDeviceItemPtr& item = modelData[index]; - if (!item) - continue; + if (loaded >= needed || loaded >= maximum) + { + QVariantList items; + for (const QModelIndex & modelIndex : indexes) + { + const NetworkDeviceItem* item = d->getItemForRow(modelIndex.row()); + if (!item) + continue; + + items.append(QVariant::fromValue(SharedInputItem(item->getInputItem().get(), true))); + } - items.append(QVariant::fromValue(SharedInputItem(item->getInputItem().get(), true))); + callback(items); + return; } - return items; + // item() internally calls refer() to trigger cache loading for all items till the max index + d->item(maxIndex); + + connect(this, &NetworkDeviceModel::dataChanged, this, [this, indexes, callback]() { + getItemsForIndexes(indexes, callback); + }, Qt::SingleShotConnection); } diff --git a/modules/gui/qt/network/networkdevicemodel.hpp b/modules/gui/qt/network/networkdevicemodel.hpp index 9a6c59f1c1..033d42e7b6 100644 --- a/modules/gui/qt/network/networkdevicemodel.hpp +++ b/modules/gui/qt/network/networkdevicemodel.hpp @@ -25,9 +25,11 @@ #include "networkbasemodel.hpp" #include +#include Q_MOC_INCLUDE("maininterface/mainctx.hpp") +class QJSValue; class MainCtx; class NetworkDeviceModelPrivate; @@ -83,7 +85,8 @@ public: Q_INVOKABLE bool addAndPlay(const QVariantList& itemIdList); Q_INVOKABLE bool addAndPlay(const QModelIndexList& itemIdList); - Q_INVOKABLE QVariantList getItemsForIndexes(const QModelIndexList & indexes) const; + Q_INVOKABLE void getItemsForIndexes(const QModelIndexList & indexes, QJSValue callback); + void getItemsForIndexes(const QModelIndexList & indexes, std::function callback); signals: void ctxChanged();