#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->worker->requestInterruption();
}
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);
ui->statusBar->showMessage( tr("Calculating...") );
this->time.start();
// todo: fix the memory leak
this->worker = new GoldbachWorker{number};
this->connect( this->worker, &GoldbachWorker::sumFound, this, &MainWindow::appendResult );
this->connect( this->worker, &GoldbachWorker::calculationDone, this, &MainWindow::calculationDone );
this->connect( this->worker, &GoldbachWorker::progressUpdated, this, &MainWindow::updateProgressBar );
//long long sumCount = this->calculate(number);
this->worker->start();
}
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) );
this->ui->pushButtonCalculate->setEnabled(true);
this->ui->pushButtonStop->setEnabled(false);
}
void MainWindow::updateProgressBar(int percent)
{
this->progressBar->setValue(percent);
}