#include #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 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); }