Skip to content
Snippets Groups Projects
Commit a51880f1 authored by Pierre Lamot's avatar Pierre Lamot Committed by François Cartegnie
Browse files

qt: add getIndexFromId in MLBaseModel

This allows getting the position of an item in the model from its MLItemID
parent 7a21d7f5
No related branches found
No related tags found
No related merge requests found
......@@ -97,6 +97,36 @@ public:
return true;
}
//this will load data until id is in the cache, then returns its position
void getIndexFromIdImpl(MLItemId id, QJSValue resolve, QJSValue reject) const
{
Q_Q(const MLBaseModel);
auto jsEngine = qjsEngine(q);
int index;
MLItem* item = q->findInCache(id, &index);
if (item) {
resolve.call({index});
}
else
{
int count = q->getCount();
int limit = q->getLimit();
//item doesn't exists
if ((limit != 0 && count >= limit) || count == getMaximumCount())
reject.call(); //not present
else
{
//load data until we have our item in cache
QObject::connect(q, &MLBaseModel::countChanged, q, [this, id, resolve = resolve, reject = reject](){
getIndexFromIdImpl(id, resolve, reject);
}, Qt::SingleShotConnection);
if (m_cache)
m_cache->fetchMore();
}
}
}
QJSValue itemToJSValue(const MLItem* item) const {
Q_Q(const MLBaseModel);
if (!item)
......@@ -265,6 +295,16 @@ Q_INVOKABLE QJSValue MLBaseModel::getDataById(MLItemId id)
return p;
}
Q_INVOKABLE QJSValue MLBaseModel::getIndexFromId(MLItemId id)
{
Q_D(const MLBaseModel);
auto [p, resolve, reject] = d->makeJSPromise();
d->getIndexFromIdImpl(id, resolve, reject);
return p;
}
QVariant MLBaseModel::data(const QModelIndex &index, int role) const
{
Q_D(const MLBaseModel);
......
......@@ -75,6 +75,17 @@ public:
* @return a JS Promise with the content of the item
*/
Q_INVOKABLE QJSValue getDataById(MLItemId id);
/**
* @brief getIndexFromId allow getting the row of an item from its id,
* if the item is not in cache, data will be retreived until it is
* satisfied
* @param id of the item
* @return a JS Promise with the row index
*/
Q_INVOKABLE QJSValue getIndexFromId(MLItemId id);
public:
// properties functions
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment