#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->countResults() || index.row() < 0)
return QVariant();
if (role == Qt::DisplayRole)
return findValue( index.row() );
return QVariant();
}
const QString &GoldbachModel::findValue(int index) const
{
int row = 0;
while ( index >= this->results[row].count() )
index -= this->results[row++].count();
return this->results[row][index];
}
int GoldbachModel::countResults() const
{
int resultCount = 0;
for ( auto vector : this->results )
resultCount += vector.count();
return resultCount;
}
void GoldbachModel::calculate(long long number)
{
this->beginResetModel();
for ( GoldbachWorker* worker : this->workers )
worker->deleteLater();
this->workers.clear();
int workerCount = qMin( QThread::idealThreadCount(), static_cast<int>(number - 2));
this->results.clear();
this->results.resize( workerCount );
for ( int current = 0; current < workerCount; ++current )
{
//this->worker = new GoldbachWorker{number, this->results, this};
GoldbachWorker* worker = new GoldbachWorker{number, current, workerCount, this->results[current], this};
this->workers.append( worker );
this->connect( worker, &GoldbachWorker::progressUpdated, this, &GoldbachModel::updateProgress );
this->connect( worker, &GoldbachWorker::calculationDone, this, &GoldbachModel::workerDone );
worker->start();
}
this->endResetModel();
}
void GoldbachModel::stop()
{
Q_ASSERT(this->workers.count() > 0);
for ( GoldbachWorker* worker : this->workers )
worker->requestInterruption();
}
bool GoldbachModel::canFetchMore(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return this->fetchedRowCount < this->countResults();
}
void GoldbachModel::fetchMore(const QModelIndex &parent)
{
Q_UNUSED(parent);
int remainder = this->countResults() - 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::workerDone(long long sumCount)
{
emit this->calculationDone(sumCount);
}
void GoldbachModel::updateProgress(int percent)
{
emit this->progressUpdated(percent);
if ( this->fetchedRowCount <= 0 )
this->fetchMore(QModelIndex());
}