158 lines
3.1 KiB
C++
Executable File
158 lines
3.1 KiB
C++
Executable File
|
|
|
|
|
|
#include "connect_dialog.h"
|
|
#include "connect_dialog_nt.h"
|
|
#include "ui_connect_dialog.h"
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
|
|
#include <QRandomGenerator>
|
|
#endif
|
|
|
|
|
|
namespace LtsUtils
|
|
{
|
|
|
|
|
|
ConnectingDialogNT::ConnectingDialogNT(const std::function<bool(int)> &checkFn,
|
|
const QString &serviceName,
|
|
const QString &exerciseName,
|
|
QWidget *parent)
|
|
: LtsWidgets::LtsFramelessDialog(parent, LtsWidgets::Behaviour::Modal)
|
|
, m_serviceName(serviceName)
|
|
, m_exerciseName(exerciseName)
|
|
, m_checkFn(checkFn)
|
|
, m_go(false)
|
|
, m_autoRetryTimer(new QTimer(this))
|
|
, m_ui(new Ui::ConnectingDialog )
|
|
{
|
|
m_ui->setupUi(this);
|
|
|
|
m_autoRetryTimer->setInterval(1000);
|
|
connect(m_autoRetryTimer, &QTimer::timeout,
|
|
this, [this]()
|
|
{
|
|
if (m_autoRetryTick < 1)
|
|
{
|
|
slotRetry();
|
|
return;
|
|
}
|
|
|
|
m_ui->pbRetry->setText(QStringLiteral("%1 [%2]").arg(tr("Retry")).arg(m_autoRetryTick));
|
|
m_autoRetryTick--;
|
|
});
|
|
|
|
reset();
|
|
setMinimumSize(sizeHint());
|
|
connect(m_ui->pbRetry, &QPushButton::clicked,
|
|
this, &ConnectingDialogNT::slotRetry);
|
|
|
|
connect(m_ui->pbIgnore, &QPushButton::clicked,
|
|
this, [this]()
|
|
{
|
|
m_ui->pbIgnore->setEnabled(false);
|
|
m_ui->pbRetry->setEnabled(false);
|
|
m_ui->pbIgnore->setText(tr("continuing.."));
|
|
|
|
emit signalFailed();
|
|
deleteLater();
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
show();
|
|
|
|
connect(this, &ConnectingDialogNT::signalGo,
|
|
this, &ConnectingDialogNT::slotGo, Qt::QueuedConnection);
|
|
|
|
m_go = true;
|
|
m_ui->pbRetry->setEnabled(false);
|
|
}
|
|
|
|
|
|
int ConnectingDialogNT::random(int min, int max)
|
|
{
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
|
|
return QRandomGenerator::global()->bounded(min, max);
|
|
#else
|
|
return (qrand() % (((max + 1) - min) + min));
|
|
#endif
|
|
}
|
|
|
|
|
|
|
|
bool ConnectingDialogNT::event(QEvent *event)
|
|
{
|
|
bool result = LtsWidgets::LtsFramelessDialog::event(event);
|
|
|
|
if (event->type() == QEvent::Paint && m_go)
|
|
{
|
|
m_go = false;
|
|
QTimer::singleShot(0, this, &ConnectingDialogNT::signalGo);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
void ConnectingDialogNT::slotRetry()
|
|
{
|
|
m_autoRetryTimer->stop();
|
|
|
|
m_ui->pbRetry->setText(tr("Retry"));
|
|
m_ui->pbRetry->setEnabled(false);
|
|
m_ui->pbIgnore->setEnabled(false);
|
|
|
|
reset();
|
|
QTimer::singleShot(1, this, &ConnectingDialogNT::slotGo);
|
|
}
|
|
|
|
|
|
void ConnectingDialogNT::slotGo()
|
|
{
|
|
|
|
if (m_checkFn(ConnectingDialog::s_waitForHistoricalDataWaitTime))
|
|
{
|
|
emit signalSuccess();
|
|
deleteLater();
|
|
return;
|
|
}
|
|
|
|
|
|
failed();
|
|
}
|
|
|
|
|
|
void ConnectingDialogNT::reset()
|
|
{
|
|
m_ui->label->setText(QStringLiteral("<font color='orange'>%1</font>").arg(tr("Connecting to %1 on %2 ...").arg(m_serviceName, m_exerciseName)));
|
|
}
|
|
|
|
|
|
ConnectingDialogNT::~ConnectingDialogNT()
|
|
{
|
|
delete m_ui;
|
|
}
|
|
|
|
|
|
void ConnectingDialogNT::failed()
|
|
{
|
|
m_ui->label->setText(QStringLiteral("<font color='red'>%1</font>").arg(tr("Could not connect to %1 on %2 !").arg(m_serviceName, m_exerciseName)));
|
|
|
|
m_ui->pbRetry->setEnabled(true);
|
|
m_ui->pbIgnore->setEnabled(true);
|
|
|
|
m_autoRetryTick = random(5, 20);
|
|
m_autoRetryTimer->start();
|
|
|
|
raise();
|
|
}
|
|
|
|
}
|