1-goldbach/{v2.1 → v3.0}/MainWindow.cpp RENAMED
@@ -1,86 +1,82 @@
1
  #include <QProgressBar>
2
 
3
- #include "GoldbachWorker.h"
4
  #include "MainWindow.h"
5
  #include "ui_MainWindow.h"
6
 
7
  MainWindow::MainWindow(QWidget *parent)
8
  : QMainWindow(parent)
9
  , ui(new Ui::MainWindow)
10
  {
11
  ui->setupUi(this);
12
 
13
  this->progressBar = new QProgressBar();
14
  this->ui->statusBar->addPermanentWidget(progressBar);
15
  this->ui->statusBar->showMessage(tr("Ready"));
16
  }
17
 
18
  MainWindow::~MainWindow()
19
  {
20
  delete ui;
21
  }
22
 
23
  #include <iostream>
24
  void MainWindow::on_lineEditNumber_textEdited(const QString &arg1)
25
  {
26
  // std::cout << qPrintable(arg1) << std::endl;
27
  bool enable = arg1.trimmed().length() > 0;
28
  this->ui->pushButtonCalculate->setEnabled(enable);
29
  }
30
 
31
  void MainWindow::on_pushButtonStop_clicked()
32
  {
33
- this->worker->requestInterruption();
34
  }
35
 
36
  void MainWindow::on_pushButtonCalculate_clicked()
37
  {
38
  bool isValid = true;
39
  const QString& text = this->ui->lineEditNumber->text();
40
 
41
  long long int number = text.toLongLong(&isValid);
42
 
43
  if ( isValid )
44
  {
45
- this->ui->plainTextEditResults->clear();
 
 
 
 
 
 
46
  this->progressBar->reset();
47
 
48
  this->ui->pushButtonCalculate->setEnabled(false);
49
  this->ui->pushButtonStop->setEnabled(true);
50
  ui->statusBar->showMessage( tr("Calculating...") );
51
 
52
  this->time.start();
53
- // todo: fix the memory leak
54
- this->worker = new GoldbachWorker{number};
55
 
56
- this->connect( this->worker, &GoldbachWorker::sumFound, this, &MainWindow::appendResult );
57
- this->connect( this->worker, &GoldbachWorker::calculationDone, this, &MainWindow::calculationDone );
58
- this->connect( this->worker, &GoldbachWorker::progressUpdated, this, &MainWindow::updateProgressBar );
59
 
60
- //long long sumCount = this->calculate(number);
61
- this->worker->start();
62
  }
63
  else
64
  {
65
  ui->statusBar->showMessage( tr("Invalid number: %1").arg(text) );
66
  }
67
  }
68
 
69
- void MainWindow::appendResult(const QString& result)
70
- {
71
- this->ui->plainTextEditResults->appendPlainText(result);
72
- }
73
-
74
  void MainWindow::calculationDone(long long sumCount)
75
  {
76
  double seconds = this->time.elapsed() / 1000.0;
77
  ui->statusBar->showMessage( tr("%1 sums found in %2 seconds").arg(sumCount).arg(seconds) );
78
 
79
  this->ui->pushButtonCalculate->setEnabled(true);
80
  this->ui->pushButtonStop->setEnabled(false);
81
  }
82
 
83
  void MainWindow::updateProgressBar(int percent)
84
  {
85
  this->progressBar->setValue(percent);
86
  }
1
  #include <QProgressBar>
2
 
3
+ #include "GoldbachModel.h"
4
  #include "MainWindow.h"
5
  #include "ui_MainWindow.h"
6
 
7
  MainWindow::MainWindow(QWidget *parent)
8
  : QMainWindow(parent)
9
  , ui(new Ui::MainWindow)
10
  {
11
  ui->setupUi(this);
12
 
13
  this->progressBar = new QProgressBar();
14
  this->ui->statusBar->addPermanentWidget(progressBar);
15
  this->ui->statusBar->showMessage(tr("Ready"));
16
  }
17
 
18
  MainWindow::~MainWindow()
19
  {
20
  delete ui;
21
  }
22
 
23
  #include <iostream>
24
  void MainWindow::on_lineEditNumber_textEdited(const QString &arg1)
25
  {
26
  // std::cout << qPrintable(arg1) << std::endl;
27
  bool enable = arg1.trimmed().length() > 0;
28
  this->ui->pushButtonCalculate->setEnabled(enable);
29
  }
30
 
31
  void MainWindow::on_pushButtonStop_clicked()
32
  {
33
+ // this->worker->requestInterruption();
34
  }
35
 
36
  void MainWindow::on_pushButtonCalculate_clicked()
37
  {
38
  bool isValid = true;
39
  const QString& text = this->ui->lineEditNumber->text();
40
 
41
  long long int number = text.toLongLong(&isValid);
42
 
43
  if ( isValid )
44
  {
45
+ // this->ui->plainTextEditResults->clear();
46
+ if ( this->model )
47
+ this->model->deleteLater();
48
+
49
+ this->model = new GoldbachModel(this);
50
+ this->ui->listViewResults->setModel(model);
51
+
52
  this->progressBar->reset();
53
 
54
  this->ui->pushButtonCalculate->setEnabled(false);
55
  this->ui->pushButtonStop->setEnabled(true);
56
  ui->statusBar->showMessage( tr("Calculating...") );
57
 
58
  this->time.start();
 
 
59
 
60
+ this->connect(this->model, &GoldbachModel::calculationDone, this, &MainWindow::calculationDone);
 
 
61
 
62
+ this->model->calculate(number);
 
63
  }
64
  else
65
  {
66
  ui->statusBar->showMessage( tr("Invalid number: %1").arg(text) );
67
  }
68
  }
69
 
 
 
 
 
 
70
  void MainWindow::calculationDone(long long sumCount)
71
  {
72
  double seconds = this->time.elapsed() / 1000.0;
73
  ui->statusBar->showMessage( tr("%1 sums found in %2 seconds").arg(sumCount).arg(seconds) );
74
 
75
  this->ui->pushButtonCalculate->setEnabled(true);
76
  this->ui->pushButtonStop->setEnabled(false);
77
  }
78
 
79
  void MainWindow::updateProgressBar(int percent)
80
  {
81
  this->progressBar->setValue(percent);
82
  }