DynExp
Highly flexible laboratory automation for dynamically changing experiments.
BusyDialog.cpp
Go to the documentation of this file.
1 // This file is part of DynExp.
2 
3 #include "stdafx.h"
4 #include "moc_BusyDialog.cpp"
5 #include "BusyDialog.h"
6 
7 BusyDialog::BusyDialog(QWidget* parent)
8  : QDialog(parent, Qt::Dialog | Qt::WindowTitleHint),
9  CheckFinishedTimer(new QTimer(this)), CheckFinishedFunction(nullptr)
10 {
11  ui.setupUi(this);
12 
13  setWindowTitle(QString("Please wait for ") + DynExp::DynExpName + "...");
14  setFixedSize(size());
15 
16  // Start timer which regularly checks whether the specified action has finished.
17  // If this is the case, the dialog is closed.
18  connect(CheckFinishedTimer, &QTimer::timeout, this, &BusyDialog::OnCheckFinished);
19  CheckFinishedTimer->setInterval(std::chrono::milliseconds(1));
20 }
21 
23 {
24  if (Text.isEmpty())
25  ui.DescriptionLabel->setText("Please wait...");
26  else
27  ui.DescriptionLabel->setText(Text);
28 }
29 
31 {
32  this->CheckFinishedFunction = CheckFinishedFunction;
33 }
34 
35 void BusyDialog::showEvent(QShowEvent* event)
36 {
37  Exception = nullptr;
38 
40  CheckFinishedTimer->start();
41 
42  ui.cancelButton->setEnabled(CheckFinishedFunction != nullptr);
43 
44  event->accept();
45 }
46 
47 void BusyDialog::closeEvent(QCloseEvent* event)
48 {
49  CheckFinishedTimer->stop();
50 
51  event->accept();
52 }
53 
54 // If CheckFinishedFunction is nullptr, only the parent decides when this dialog is closed
56 {
58  QDialog::reject();
59 }
60 
62 {
63  try
64  {
67  accept();
68  }
69  catch ([[maybe_unused]] const Util::TimeoutException& e)
70  {
71  // Swallow TimeoutException, because this is most likely a temporary error.
72  }
73  catch (...)
74  {
75  Exception = std::current_exception();
76 
77  // Treat any case of an exception as aborting the action.
78  reject();
79  }
80 }
Implements a dialog with a progress bar, which shows the user that DynExp is busy.
CheckFinishedFunctionType CheckFinishedFunction
Definition: BusyDialog.h:36
virtual void closeEvent(QCloseEvent *event) override
Definition: BusyDialog.cpp:47
void SetDescriptionText(QString Text)
Definition: BusyDialog.cpp:22
std::exception_ptr Exception
Definition: BusyDialog.h:37
std::function< bool(void)> CheckFinishedFunctionType
Definition: BusyDialog.h:18
void SetCheckFinishedFunction(const CheckFinishedFunctionType CheckFinishedFunction)
Definition: BusyDialog.cpp:30
void OnCheckFinished()
Definition: BusyDialog.cpp:61
virtual void showEvent(QShowEvent *event) override
Definition: BusyDialog.cpp:35
QTimer * CheckFinishedTimer
Definition: BusyDialog.h:34
Ui::BusyDialog ui
Definition: BusyDialog.h:33
BusyDialog(QWidget *parent)
Definition: BusyDialog.cpp:7
virtual void reject() override
Definition: BusyDialog.cpp:55
Thrown when an operation timed out before it could be completed, especially used for locking shared d...
Definition: Exception.h:261
constexpr auto DynExpName
DynExp's name string
Accumulates include statements to provide a precompiled header.