##// END OF EJS Templates
Wait for the end of an acquisition to validate an operation (1)...
Alexandre Leroux -
r1213:2785fa3e9772
parent child
Show More
@@ -0,0 +1,38
1 #ifndef SCIQLOP_SIGNALWAITER_H
2 #define SCIQLOP_SIGNALWAITER_H
3
4 #include "CoreGlobal.h"
5
6 #include <QEventLoop>
7
8 /**
9 * Class for synchronously waiting for the reception of a signal. The signal to wait is passed to
10 * the construction of the object. When starting the wait, a timeout can be set to exit if the
11 * signal has not been sent
12 */
13 class SCIQLOP_CORE_EXPORT SignalWaiter : public QObject {
14 Q_OBJECT
15 public:
16 /**
17 * Ctor
18 * @param object the sender of the signal
19 * @param signal the signal to listen
20 */
21 explicit SignalWaiter(QObject &sender, const char *signal);
22
23 /**
24 * Starts the signal and leaves after the signal has been received, or after the timeout
25 * @param timeout the timeout set (if 0, uses a default timeout)
26 * @return true if the signal was sent, false if the timeout occured
27 */
28 bool wait(int timeout);
29
30 private:
31 bool m_Timeout;
32 QEventLoop m_EventLoop;
33
34 private slots:
35 void timeout();
36 };
37
38 #endif // SCIQLOP_SIGNALWAITER_H
@@ -0,0 +1,36
1 #include "Common/SignalWaiter.h"
2
3 #include <QTimer>
4
5 namespace {
6
7 const auto DEFAULT_TIMEOUT = 30000;
8
9 } // namespace
10
11 SignalWaiter::SignalWaiter(QObject &sender, const char *signal) : m_Timeout{false}
12 {
13 connect(&sender, signal, &m_EventLoop, SLOT(quit()));
14 }
15
16 bool SignalWaiter::wait(int timeout)
17 {
18 if (timeout == 0) {
19 timeout = DEFAULT_TIMEOUT;
20 }
21
22 QTimer timer{};
23 timer.setInterval(timeout);
24 timer.start();
25 connect(&timer, &QTimer::timeout, this, &SignalWaiter::timeout);
26
27 m_EventLoop.exec();
28
29 return !m_Timeout;
30 }
31
32 void SignalWaiter::timeout()
33 {
34 m_Timeout = true;
35 m_EventLoop.quit();
36 }
@@ -5,6 +5,7 catalogueapi_dep = dependency('CatalogueAPI', required : true, fallback:['Catalo
5 5
6 6 core_moc_headers = [
7 7 'include/Catalogue/CatalogueController.h',
8 'include/Common/SignalWaiter.h',
8 9 'include/Data/IDataProvider.h',
9 10 'include/DataSource/DataSourceController.h',
10 11 'include/DataSource/DataSourceItemAction.h',
@@ -24,6 +25,7 core_moc_files = qt5.preprocess(moc_headers : core_moc_headers)
24 25
25 26 core_sources = [
26 27 'src/Common/DateUtils.cpp',
28 'src/Common/SignalWaiter.cpp',
27 29 'src/Common/StringUtils.cpp',
28 30 'src/Common/MimeTypesDef.cpp',
29 31 'src/Catalogue/CatalogueController.cpp',
@@ -5,6 +5,7
5 5
6 6 #include "AmdaProvider.h"
7 7
8 #include <Common/SignalWaiter.h>
8 9 #include <Network/NetworkController.h>
9 10 #include <Settings/SqpSettingsDefs.h>
10 11 #include <SqpApplication.h>
General Comments 0
You need to be logged in to leave comments. Login now