File indexing completed on 2024-04-28 04:37:34

0001 /*
0002     SPDX-FileCopyrightText: 2008 Manuel Breugelmans <mbr.nxi@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kdevsignalspy.h"
0008 
0009 #include <QEventLoop>
0010 #include <QTimer>
0011 
0012 namespace KDevelop {
0013 KDevSignalSpy::KDevSignalSpy(QObject* obj, const char* signal,
0014                              Qt::ConnectionType ct)
0015     : QObject(nullptr)
0016     , m_obj(obj)
0017     , m_emitted(false)
0018 {
0019     m_timer = new QTimer(this);
0020     m_loop = new QEventLoop(this);
0021     connect(obj, signal, this, SLOT(signalEmitted()), ct);
0022 }
0023 
0024 bool KDevSignalSpy::wait(int timeout)
0025 {
0026     Q_ASSERT(!m_loop->isRunning()); Q_ASSERT(!m_timer->isActive());
0027 
0028     m_emitted = false;
0029     if (timeout > 0) {
0030         connect(m_timer, &QTimer::timeout, m_loop, &QEventLoop::quit);
0031         m_timer->setSingleShot(true);
0032         m_timer->start(timeout);
0033     }
0034     m_loop->exec();
0035 
0036     return m_emitted;
0037 }
0038 
0039 void KDevSignalSpy::signalEmitted()
0040 {
0041     m_emitted = true;
0042     disconnect(m_obj, nullptr, this, nullptr);
0043     m_timer->stop();
0044     m_loop->quit();
0045 }
0046 } // KDevelop
0047 
0048 #include "moc_kdevsignalspy.cpp"