62 lines
2.0 KiB
C++
62 lines
2.0 KiB
C++
#include "LoaderWindow.h"
|
|
#include <QVBoxLayout>
|
|
#include <QLabel>
|
|
#include <QThread>
|
|
|
|
#include "net/NetworkService.h"
|
|
|
|
LoaderWindow::LoaderWindow(QWidget *parent) : QWidget(parent) {
|
|
setWindowTitle("Проверка приложения");
|
|
setFixedSize(300, 100);
|
|
|
|
QVBoxLayout *layout = new QVBoxLayout(this);
|
|
|
|
QLabel *label = new QLabel("Пожалуйста, подождите...");
|
|
layout->addWidget(label);
|
|
|
|
progressBar = new QProgressBar();
|
|
progressBar->setRange(0, 100);
|
|
layout->addWidget(progressBar);
|
|
|
|
timer = new QTimer(this);
|
|
connect(timer, &QTimer::timeout, this, &LoaderWindow::updateProgress);
|
|
timer->start(25);
|
|
|
|
auto *thread = new QThread;
|
|
auto *networkService = new NetworkService;
|
|
networkService->moveToThread(thread);
|
|
|
|
connect(thread, &QThread::started, networkService, &NetworkService::loginAndSetup);
|
|
connect(networkService, &NetworkService::finished, thread, &QThread::quit);
|
|
connect(networkService, &NetworkService::finished, networkService, &QObject::deleteLater);
|
|
connect(thread, &QThread::finished, thread, &QThread::deleteLater);
|
|
|
|
connect(networkService, &NetworkService::finishedWithResult, this, [this](const int result) {
|
|
m_checkResult = result;
|
|
if (m_checkResult == -1) {
|
|
if (timer) {
|
|
timer->stop();
|
|
}
|
|
emit loadingFinished(m_checkResult);
|
|
} else if (progressBar->value() >= 100) {
|
|
emit loadingFinished(m_checkResult);
|
|
}
|
|
}, Qt::QueuedConnection);
|
|
thread->start();
|
|
}
|
|
|
|
void LoaderWindow::updateProgress() {
|
|
const int value = progressBar->value();
|
|
if (value < 99) {
|
|
progressBar->setValue(value + 1);
|
|
} else if (m_checkResult != 1000) {
|
|
// Результат получен — завершаем
|
|
progressBar->setValue(100);
|
|
if (timer) {
|
|
timer->stop();
|
|
}
|
|
emit loadingFinished(m_checkResult);
|
|
}
|
|
// Иначе ждём на 99 пока сеть не ответит
|
|
}
|