launcher/loading.cpp

93 lines
2.3 KiB
C++

#include "loading.h"
#include "loading-worker.h"
#include "ui_loading.h"
#include <QThread>
#include <qdebug.h>
Loading::Loading(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Loading)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint);
this->ui->progressBar->hide();
this->startLoading();
}
Loading::~Loading()
{
delete ui;
}
void Loading::changeStep(QString text) {
this->ui->condition->setText(text);
}
void Loading::setupWelcomeLocale(QString locale) {
this->welcome.setupWelcomeLocale(locale);
}
void Loading::finishWorker() {
this->hide();
this->welcome.show();
}
void Loading::setupProgressBar(unsigned int current, unsigned int max) {
if (this->ui->progressBar->isHidden() && (max != 0))
this->ui->progressBar->show();
else if (!this->ui->progressBar->isHidden() && (max == 0))
this->ui->progressBar->hide();
if (max != 0) {
// TODO: Fix displayed progressBar
if (this->ui->progressBar->maximum() != max)
this->ui->progressBar->setRange(0, max);
if (this->ui->progressBar->value() != current)
this->ui->progressBar->setValue(current);
}
}
void Loading::startI2P() {
qDebug() << "Starting I2P...";
this->i2p.start();
}
void Loading::startLoading()
{
QThread* thread = new QThread;
LoadingWorker* worker = new LoadingWorker;
worker->moveToThread(thread);
connect(thread, &QThread::started,
worker, &LoadingWorker::process);
connect(worker, &LoadingWorker::finished,
thread, &QThread::quit);
connect(worker, &LoadingWorker::finished,
worker, &QObject::deleteLater);
connect(thread, &QThread::finished,
thread, &QObject::deleteLater);
// Connection between loading and worker
connect(worker, &LoadingWorker::progressBar,
this, &Loading::setupProgressBar);
connect(worker, &LoadingWorker::changeStep,
this, &Loading::changeStep);
connect(worker, &LoadingWorker::setupWelcomeLocale,
this, &Loading::setupWelcomeLocale);
connect(worker, &LoadingWorker::finished,
this, &Loading::finishWorker);
// I2P
connect(worker, &LoadingWorker::startI2P,
this, &Loading::startI2P);
thread->start();
}