1-goldbach/{v3.0 → v3.1}/GoldbachModel.cpp RENAMED
@@ -1,77 +1,84 @@
1
  #include "GoldbachModel.h"
2
  #include "GoldbachWorker.h"
3
 
4
  GoldbachModel::GoldbachModel(QObject *parent)
5
  : QAbstractListModel(parent)
6
  {
7
  }
8
 
9
  int GoldbachModel::rowCount(const QModelIndex &parent) const
10
  {
11
  Q_UNUSED(parent);
12
  return this->fetchedRowCount;
13
  }
14
 
15
  QVariant GoldbachModel::data(const QModelIndex &index, int role) const
16
  {
17
  if (!index.isValid())
18
  return QVariant();
19
 
20
  if (index.row() >= this->results.size() || index.row() < 0)
21
  return QVariant();
22
 
23
  if (role == Qt::DisplayRole)
24
  return this->results[ index.row() ];
25
 
26
  return QVariant();
27
  }
28
 
29
  void GoldbachModel::calculate(long long number)
30
  {
31
  this->beginResetModel();
32
 
33
  if ( this->worker )
34
  this->worker->deleteLater();
35
 
36
  this->worker = new GoldbachWorker{number, this->results, this};
37
 
38
- this->connect( this->worker, &GoldbachWorker::sumFound, this, &GoldbachModel::workerSumFound );
39
  this->connect( this->worker, &GoldbachWorker::calculationDone, this, &GoldbachModel::workerDone );
40
- // this->connect( this->worker, &GoldbachWorker::progressUpdated, this, &MainWindow::updateProgressBar );
41
 
42
  this->worker->start();
43
 
44
  this->endResetModel();
45
  }
46
 
 
 
 
 
 
 
47
  bool GoldbachModel::canFetchMore(const QModelIndex &parent) const
48
  {
49
  Q_UNUSED(parent);
50
  return this->fetchedRowCount < this->results.count();
51
  }
52
 
53
  void GoldbachModel::fetchMore(const QModelIndex &parent)
54
  {
55
  Q_UNUSED(parent);
56
 
57
  int remainder = this->results.size() - this->fetchedRowCount;
58
  int itemsToFetch = qMin(100, remainder);
59
 
60
  if (itemsToFetch <= 0)
61
  return;
62
 
63
  beginInsertRows(QModelIndex(), this->fetchedRowCount, this->fetchedRowCount + itemsToFetch - 1);
64
  this->fetchedRowCount += itemsToFetch;
65
  endInsertRows();
66
  }
67
 
68
- void GoldbachModel::workerSumFound(const QString &sum)
69
  {
70
- if ( this->fetchedRowCount <= 0 )
71
- this->fetchMore(QModelIndex());
72
  }
73
 
74
- void GoldbachModel::workerDone(long long sumCount)
75
  {
76
- emit this->calculationDone(sumCount);
 
 
 
77
  }
1
  #include "GoldbachModel.h"
2
  #include "GoldbachWorker.h"
3
 
4
  GoldbachModel::GoldbachModel(QObject *parent)
5
  : QAbstractListModel(parent)
6
  {
7
  }
8
 
9
  int GoldbachModel::rowCount(const QModelIndex &parent) const
10
  {
11
  Q_UNUSED(parent);
12
  return this->fetchedRowCount;
13
  }
14
 
15
  QVariant GoldbachModel::data(const QModelIndex &index, int role) const
16
  {
17
  if (!index.isValid())
18
  return QVariant();
19
 
20
  if (index.row() >= this->results.size() || index.row() < 0)
21
  return QVariant();
22
 
23
  if (role == Qt::DisplayRole)
24
  return this->results[ index.row() ];
25
 
26
  return QVariant();
27
  }
28
 
29
  void GoldbachModel::calculate(long long number)
30
  {
31
  this->beginResetModel();
32
 
33
  if ( this->worker )
34
  this->worker->deleteLater();
35
 
36
  this->worker = new GoldbachWorker{number, this->results, this};
37
 
38
+ this->connect( this->worker, &GoldbachWorker::progressUpdated, this, &GoldbachModel::updateProgress );
39
  this->connect( this->worker, &GoldbachWorker::calculationDone, this, &GoldbachModel::workerDone );
 
40
 
41
  this->worker->start();
42
 
43
  this->endResetModel();
44
  }
45
 
46
+ void GoldbachModel::stop()
47
+ {
48
+ Q_ASSERT(this->worker);
49
+ this->worker->requestInterruption();
50
+ }
51
+
52
  bool GoldbachModel::canFetchMore(const QModelIndex &parent) const
53
  {
54
  Q_UNUSED(parent);
55
  return this->fetchedRowCount < this->results.count();
56
  }
57
 
58
  void GoldbachModel::fetchMore(const QModelIndex &parent)
59
  {
60
  Q_UNUSED(parent);
61
 
62
  int remainder = this->results.size() - this->fetchedRowCount;
63
  int itemsToFetch = qMin(100, remainder);
64
 
65
  if (itemsToFetch <= 0)
66
  return;
67
 
68
  beginInsertRows(QModelIndex(), this->fetchedRowCount, this->fetchedRowCount + itemsToFetch - 1);
69
  this->fetchedRowCount += itemsToFetch;
70
  endInsertRows();
71
  }
72
 
73
+ void GoldbachModel::workerDone(long long sumCount)
74
  {
75
+ emit this->calculationDone(sumCount);
 
76
  }
77
 
78
+ void GoldbachModel::updateProgress(int percent)
79
  {
80
+ emit this->progressUpdated(percent);
81
+
82
+ if ( this->fetchedRowCount <= 0 )
83
+ this->fetchMore(QModelIndex());
84
  }