File: threadedfetchmoremodel.cpp

package info (click to toggle)
qt6-declarative 6.9.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 310,088 kB
  • sloc: cpp: 779,045; javascript: 514,338; xml: 10,981; python: 2,806; ansic: 2,253; java: 954; sh: 262; makefile: 41; php: 27
file content (103 lines) | stat: -rw-r--r-- 3,089 bytes parent folder | download
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "threadedfetchmoremodel.h"

ThreadedFetchMoreModel::ThreadedFetchMoreModel()
{
    auto worker = new FetchWorker();
    connect(&m_workerThread, &QThread::finished, worker, &QObject::deleteLater);
    worker->moveToThread(&m_workerThread);
    connect(this, &ThreadedFetchMoreModel::fetchDataBlock, worker, &FetchWorker::fetchDataBlock);
    connect(worker, &FetchWorker::dataFetched, this, &ThreadedFetchMoreModel::dataReceived);
    connect(worker, &FetchWorker::noMoreToFetch, this, &ThreadedFetchMoreModel::noMoreToFetch);
    m_workerThread.start();
}

ThreadedFetchMoreModel::~ThreadedFetchMoreModel()
{
    m_workerThread.requestInterruption();
    m_workerThread.quit();
    m_workerThread.wait();
}

int ThreadedFetchMoreModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return m_dataList.size();
}

QVariant ThreadedFetchMoreModel::data(const QModelIndex &index, int role) const
{
    const auto item = m_dataList.at(index.row());
    switch (static_cast<Role>(role)) {
    case Role::Title:
        return item.title;
    case Role::Subtitle:
        return item.subtitle;
    case Role::Number:
        return item.number;
    case Role::LoadingElement:
        return m_fetchOngoing && index.row() == (rowCount() - 1);
    default:
        break;
    }
    return QVariant{};
}

void ThreadedFetchMoreModel::fetchMore(const QModelIndex &parent)
{
    Q_UNUSED(parent)
    if (!m_fetchOngoing) {
        addLoadingItem();
        emit fetchDataBlock();
    }
}

bool ThreadedFetchMoreModel::canFetchMore(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    if (m_fetchOngoing)
        return false;
    return m_hasUnfetchedItems;
}

void ThreadedFetchMoreModel::dataReceived(const QList<FetchWorker::DataBlock> &items)
{
    removeLoadingItem();
    beginInsertRows(QModelIndex{}, rowCount(), rowCount() + items.size() - 1);
    m_dataList.append(items);
    endInsertRows();
}

void ThreadedFetchMoreModel::noMoreToFetch()
{
    m_hasUnfetchedItems = false;
}

void ThreadedFetchMoreModel::addLoadingItem()
{
    beginInsertRows(QModelIndex{}, rowCount(), rowCount());
    m_fetchOngoing = true;
    m_dataList.append({{}, {}, 0});
    endInsertRows();
}

void ThreadedFetchMoreModel::removeLoadingItem()
{
    beginRemoveRows(QModelIndex{}, rowCount() - 1, rowCount() - 1);
    m_fetchOngoing = false;
    m_dataList.removeAt(rowCount() - 1);
    endRemoveRows();
}

QHash<int, QByteArray> ThreadedFetchMoreModel::roleNames() const
{
    static const QHash<int, QByteArray> names = {{static_cast<int>(Role::Title), "title"},
                                                 {static_cast<int>(Role::Subtitle), "subtitle"},
                                                 {static_cast<int>(Role::Number), "number"},
                                                 {static_cast<int>(Role::LoadingElement),
                                                  "isLoadingElement"}};
    return names;
}