1
0
forked from BRT/arc
arc/views/widget/loader/LoaderWindow.cpp
2026-02-22 20:48:16 +07:00

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, [&](const int result) {
m_checkResult = result;
if (m_checkResult == -1) {
if (timer) {
QMetaObject::invokeMethod(timer, "stop", Qt::QueuedConnection);
}
QMetaObject::invokeMethod(this, [=]() {
emit loadingFinished(m_checkResult);
}, Qt::QueuedConnection);
} else if (progressBar->value() >= 100) {
QMetaObject::invokeMethod(this, [=]() {
emit loadingFinished(m_checkResult);
}, Qt::QueuedConnection);
}
});
thread->start();
}
void LoaderWindow::updateProgress() {
if (const int value = progressBar->value(); value < 100) {
progressBar->setValue(value + 1);
} else {
if (timer) {
timer->stop();
}
emit loadingFinished(m_checkResult);
}
}