NorMIT-nav  18.04
An IGT application
cxtestQueuedSignalListener.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
13 
14 #include <iostream>
15 #include <QTimer>
16 #include <QEventLoop>
17 #include <boost/lexical_cast.hpp>
18 #include "cxTypeConversions.h"
19 
20 namespace cxtest
21 {
22 
23 bool waitForQueuedSignal(QObject* object, const char* signal, int maxWaitMilliSeconds, bool silentAtArrive)
24 {
25  QueuedSignalListener listener(object, signal, maxWaitMilliSeconds);
26  listener.exec();
27  bool signalArrived = !listener.timedOut();
28  std::string feedback = signalArrived ? " arrived." : " did NOT arrive. Timed out. ";
29  if(!silentAtArrive)
30  std::cout << "[QueuedSignalListener] " << signal << feedback << std::endl;
31  return signalArrived;
32 }
33 
34 QueuedSignalListener::QueuedSignalListener(QObject* object, const char* signal, int maxWaitMilliSeconds) :
35  mTimedOut(false)
36 {
37  createTimer(maxWaitMilliSeconds);
38  createEventLoop(object, signal);
39 }
40 
42 {
43  delete mTimer;
44  delete mLoop;
45 }
46 
48 {
49  mTimer->start();
50  int retval = mLoop->exec();
51  return retval;
52 }
53 
55 {
56  return mTimedOut;
57 }
58 
59 void QueuedSignalListener::quit()
60 {
61  mTimedOut = (this->sender() == mTimer);
62  mTimer->stop();
63  mLoop->quit();
64 }
65 
66 void QueuedSignalListener::createTimer(int maxWaitMilliSeconds)
67 {
68  mTimer = new QTimer;
69  mTimer->setInterval(maxWaitMilliSeconds);
70  QObject::connect(mTimer, SIGNAL(timeout()), this, SLOT(quit()));
71 }
72 
73 void QueuedSignalListener::createEventLoop(QObject* object, const char* signal)
74 {
75  mLoop = new QEventLoop;
76  QObject::connect(object, signal, this, SLOT(quit()));
77 }
78 
79 
80 
81 
82 } /* namespace cxtest */
QueuedSignalListener(QObject *object, const char *signal, int maxWaitMilliSeconds=100)
int exec()
runs the eventloop that makes sure signals are sent
bool waitForQueuedSignal(QObject *object, const char *signal, int maxWaitMilliSeconds, bool silentAtArrive)
Object that waits for a signal to arrive from a given QObject. If this takes longer than a given time...