Download cpp source code

 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
#include "GoldbachModel.h"
#include "GoldbachWorker.h"

GoldbachModel::GoldbachModel(QObject *parent)
    : QAbstractListModel(parent)
{
}

int GoldbachModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return this->fetchedRowCount;
}

QVariant GoldbachModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (index.row() >= this->results.size() || index.row() < 0)
        return QVariant();

    if (role == Qt::DisplayRole)
        return this->results[ index.row() ];

    return QVariant();
}

void GoldbachModel::calculate(long long number)
{
    this->beginResetModel();

    if ( this->worker )
        this->worker->deleteLater();

    this->worker = new GoldbachWorker{number, this->results, this};

    this->connect( this->worker, &GoldbachWorker::sumFound, this, &GoldbachModel::workerSumFound );
    this->connect( this->worker, &GoldbachWorker::calculationDone, this, &GoldbachModel::workerDone );
//    this->connect( this->worker, &GoldbachWorker::progressUpdated, this, &MainWindow::updateProgressBar );

    this->worker->start();

    this->endResetModel();
}

bool GoldbachModel::canFetchMore(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return this->fetchedRowCount < this->results.count();
}

void GoldbachModel::fetchMore(const QModelIndex &parent)
{
    Q_UNUSED(parent);

    int remainder = this->results.size() - this->fetchedRowCount;
    int itemsToFetch = qMin(100, remainder);

    if (itemsToFetch <= 0)
        return;

    beginInsertRows(QModelIndex(), this->fetchedRowCount, this->fetchedRowCount + itemsToFetch - 1);
    this->fetchedRowCount += itemsToFetch;
    endInsertRows();
}

void GoldbachModel::workerSumFound(const QString &sum)
{
    if ( this->fetchedRowCount <= 0 )
        this->fetchMore(QModelIndex());
}

void GoldbachModel::workerDone(long long sumCount)
{
    emit this->calculationDone(sumCount);
}