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
78
79
80
81
82
83
84
85
86
87
#include <QProgressBar>

#include "GoldbachWorker.h"
#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent)
	: QMainWindow(parent)
	, ui(new Ui::MainWindow)
{
	ui->setupUi(this);

    this->progressBar = new QProgressBar();
    this->ui->statusBar->addPermanentWidget(progressBar);
    this->ui->statusBar->showMessage(tr("Ready"));
}

MainWindow::~MainWindow()
{
	delete ui;
}

#include <iostream>
void MainWindow::on_lineEditNumber_textEdited(const QString &arg1)
{
//    std::cout << qPrintable(arg1) << std::endl;
    bool enable = arg1.trimmed().length() > 0;
    this->ui->pushButtonCalculate->setEnabled(enable);
}

void MainWindow::on_pushButtonStop_clicked()
{
    this->userStopped = true;
}

void MainWindow::on_pushButtonCalculate_clicked()
{
    bool isValid = true;
    const QString& text = this->ui->lineEditNumber->text();

    long long int number = text.toLongLong(&isValid);

    if ( isValid )
    {
        this->ui->plainTextEditResults->clear();
        this->progressBar->reset();

        this->ui->pushButtonCalculate->setEnabled(false);
        this->ui->pushButtonStop->setEnabled(true);
        this->userStopped = false;
        ui->statusBar->showMessage( tr("Calculating...") );

        this->time.start();
        // todo: fix the memory leak
        GoldbachWorker* worker = new GoldbachWorker{number};

        this->connect( worker, &GoldbachWorker::sumFound, this, &MainWindow::appendResult );
        this->connect( worker, &GoldbachWorker::calculationDone, this, &MainWindow::calculationDone );
        this->connect( worker, &GoldbachWorker::progressUpdated, this, &MainWindow::updateProgressBar );

        //long long sumCount = this->calculate(number);
        worker->start();

        this->ui->pushButtonCalculate->setEnabled(true);
        this->ui->pushButtonStop->setEnabled(false);
    }
    else
    {
        ui->statusBar->showMessage( tr("Invalid number: %1").arg(text) );
    }
}

void MainWindow::appendResult(const QString& result)
{
    this->ui->plainTextEditResults->appendPlainText(result);
}

void MainWindow::calculationDone(long long sumCount)
{
    double seconds = this->time.elapsed() / 1000.0;
    ui->statusBar->showMessage( tr("%1 sums found in %2 seconds").arg(sumCount).arg(seconds) );
}

void MainWindow::updateProgressBar(int percent)
{
    this->progressBar->setValue(percent);
}