/** * @RDE_HEADER@ * Helper Class, which manages a progress Dialog with two states. * $Id$ * $HeadURL$ * $Author$ * * Created by Mike Wille, SLSE, 2016-06-20 */ #include "lts_progress_dialog_helper.h" #include "lts_frameless_dialog.h" #include #include #include #include #include #include namespace LtsWidgets { LtsProgressDialogHelper::LtsProgressDialogHelper(QWidget *parent) : QObject(parent) , m_waitDlg(nullptr) // save mem , m_parentWidget(parent) , m_textLabel(nullptr) {} LtsProgressDialogHelper::~LtsProgressDialogHelper() { delete m_waitDlg; } void LtsProgressDialogHelper::setCustomText(const QString &customText) { if (m_waitDlg == nullptr || m_textLabel == nullptr) return; if (customText.isEmpty()) m_textLabel->setText(tr("Waiting for server reply...")); else m_textLabel->setText(customText); } void LtsProgressDialogHelper::setParentWidget(QWidget *newParent) { m_parentWidget = newParent; if (m_waitDlg) { m_waitDlg->setParent(newParent); QTimer::singleShot(0, m_waitDlg.data(), &LtsFramelessDialog::onUpdateGeometry); } } void LtsProgressDialogHelper::slotShowWaitForReplyWindow() { if (m_waitDlg) m_waitDlg->deleteLater(); //delete m_waitDlg; // when there is already an instance m_waitDlg = new LtsFramelessDialog(m_parentWidget, Behaviour::Modal); QHBoxLayout *l = new QHBoxLayout(m_waitDlg); QProgressBar *pb = new QProgressBar(m_waitDlg); pb->setMaximum(0); pb->setValue(-1); l->addWidget(pb); m_textLabel = new QLabel(m_waitDlg); setCustomText(); l->addWidget(m_textLabel); m_waitDlg->setLayout(l); m_waitDlg->show(); m_waitDlg->raise(); // TODO: needed when StyOnTop is active ? //QCoreApplication::processEvents(); // do not look glitched if dds hangs } void LtsProgressDialogHelper::slotStopWaitForReplyWindow(const ReplyResult &replyResult, const QString &replyString) { if (m_waitDlg == nullptr) { // TODO: debug/warning that there is no wait dlg.. return; } //qDeleteAll(m_waitDlg->children()); // delete the layout and it's children m_waitDlg->deleteLater(); // the easiest way m_waitDlg = nullptr; // .. a new layout, telling the reply result QString resultText = tr("Unknown result (%1)!").arg((int)replyResult); switch (replyResult) { case ReplyResult::Close: { return; // say nothing, just close } case ReplyResult::Success: { resultText = tr("Operation was successful"); break; } case ReplyResult::Failure: { resultText = tr("Operation has failed!\nReason: '%1'").arg(replyString); break; } case ReplyResult::TimeOut: { resultText = tr("Operation has timed out and could not be processed! Please try again later"); break; } } m_waitDlg = new LtsFramelessDialog(m_parentWidget, Behaviour::Modal); QVBoxLayout *newLayout = new QVBoxLayout(m_waitDlg); newLayout->addWidget(new QLabel(resultText, m_waitDlg)); QPushButton *btn = new QPushButton(tr("OK"),m_waitDlg); connect(btn, &QPushButton::clicked, this, &LtsProgressDialogHelper::accept); // TODO: or close? newLayout->addWidget(btn); m_waitDlg->setLayout(newLayout); m_waitDlg->show(); //QCoreApplication::processEvents(); // do not look glitched if dds hangs } void LtsProgressDialogHelper::accept() { m_waitDlg->accept(); m_waitDlg->deleteLater(); m_waitDlg = nullptr; emit accepted(); } } // namespace LtsWidgets