1-goldbach/{v2.0 → v2.1}/MainWindow.cpp RENAMED
@@ -1,87 +1,86 @@
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->userStopped = true;
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
- this->userStopped = false;
51
  ui->statusBar->showMessage( tr("Calculating...") );
52
 
53
  this->time.start();
54
  // todo: fix the memory leak
55
- GoldbachWorker* worker = new GoldbachWorker{number};
56
 
57
- this->connect( worker, &GoldbachWorker::sumFound, this, &MainWindow::appendResult );
58
- this->connect( worker, &GoldbachWorker::calculationDone, this, &MainWindow::calculationDone );
59
- this->connect( worker, &GoldbachWorker::progressUpdated, this, &MainWindow::updateProgressBar );
60
 
61
  //long long sumCount = this->calculate(number);
62
- worker->start();
63
-
64
- this->ui->pushButtonCalculate->setEnabled(true);
65
- this->ui->pushButtonStop->setEnabled(false);
66
  }
67
  else
68
  {
69
  ui->statusBar->showMessage( tr("Invalid number: %1").arg(text) );
70
  }
71
  }
72
 
73
  void MainWindow::appendResult(const QString& result)
74
  {
75
  this->ui->plainTextEditResults->appendPlainText(result);
76
  }
77
 
78
  void MainWindow::calculationDone(long long sumCount)
79
  {
80
  double seconds = this->time.elapsed() / 1000.0;
81
  ui->statusBar->showMessage( tr("%1 sums found in %2 seconds").arg(sumCount).arg(seconds) );
 
 
 
82
  }
83
 
84
  void MainWindow::updateProgressBar(int percent)
85
  {
86
  this->progressBar->setValue(percent);
87
  }
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
  }