1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "threadedlistmodel.h"
ThreadedListModel::ThreadedListModel()
{
m_idList = m_storage.idList();
connect(&m_storage, &DataStorage::dataUpdated, this, &ThreadedListModel::dataUpdated);
}
int ThreadedListModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_idList.size();
}
QVariant ThreadedListModel::data(const QModelIndex &index, int role) const
{
const auto id = m_idList.at(index.row());
const auto &item = m_storage.item(id);
if (item.isValid()) {
switch (static_cast<Role>(role)) {
case Role::Album:
return item.album();
case Role::AlbumArt:
return item.albumArtFile();
case Role::Artist:
return item.artist();
case Role::Song:
return item.song();
case Role::LoadingElement:
return false;
case Role::LoadingText:
return QString();
default:
break;
}
} else {
/* Item not fetched yet, pass "loading" item. */
switch (static_cast<Role>(role)) {
case Role::Album:
case Role::AlbumArt:
case Role::Artist:
case Role::Song:
return QString{};
case Role::LoadingElement:
return true;
case Role::LoadingText:
return m_storage.currentlyFetchedId() == id ? QString{} : tr("Waiting...");
default:
break;
}
}
return QVariant{};
}
void ThreadedListModel::dataUpdated(int id)
{
const QModelIndex changedIndex = index(m_idList.indexOf(id));
emit dataChanged(changedIndex, changedIndex);
}
QHash<int, QByteArray> ThreadedListModel::roleNames() const
{
static const QHash<int, QByteArray> roles{{static_cast<int>(Role::Song), "song"},
{static_cast<int>(Role::Artist), "artist"},
{static_cast<int>(Role::Album), "album"},
{static_cast<int>(Role::AlbumArt), "albumArt"},
{static_cast<int>(Role::LoadingText), "loadingText"},
{static_cast<int>(Role::LoadingElement),
"isLoadingElement"}};
return roles;
}
|