165 lines
3.8 KiB
C++
Executable File
165 lines
3.8 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* Wait Dialog while trying to get Historical Operator Data from OPM on EX0
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
*
|
|
* Created by Mike Wille, SLSE, 2017-04-24
|
|
*/
|
|
|
|
#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
|
|
{
|
|
//int ConnectingDialog::s_waitForHistoricalDataWaitTime = 10000;
|
|
|
|
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 /* non-nt */)
|
|
{
|
|
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; // connection successful!
|
|
|
|
//qApp->quit();
|
|
|
|
// QApplication::quit();
|
|
// QApplication::processEvents();
|
|
// QApplication::processEvents();
|
|
//abort();
|
|
});
|
|
|
|
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
|
|
}
|
|
|
|
|
|
// these waitForHistoricalData functions are blocking, we need to make sure that drawing this dialog is already done
|
|
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); // is already shown, call directly
|
|
}
|
|
|
|
|
|
void ConnectingDialogNT::slotGo()
|
|
{
|
|
//if ((dtc.get()->*waitForHistoricalDataFunction)(s_waitForHistoricalDataWaitTime))
|
|
if (m_checkFn(ConnectingDialog::s_waitForHistoricalDataWaitTime))
|
|
{
|
|
emit signalSuccess();
|
|
deleteLater();
|
|
return; // connection successful!
|
|
}
|
|
|
|
// otherwise..
|
|
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); // auto retry in between 5 and 20 seconds
|
|
m_autoRetryTimer->start();
|
|
|
|
raise();
|
|
}
|
|
|
|
} // namespace LtsUtils
|