mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-21 04:45:31 +08:00
refs #859, allow to trigger digest signal manually
* Useful if the input signal was just a bogus signal used to trigger * Login component shows a use case
This commit is contained in:
committed by
Mathew Sutcliffe
parent
4e7144db08
commit
265b6d33f0
@@ -7,13 +7,20 @@
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "blackmisc/digestsignal.h"
|
||||
#include "digestsignal.h"
|
||||
#include "threadutils.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
void CDigestSignal::ps_inputSignal()
|
||||
void CDigestSignal::inputSignal()
|
||||
{
|
||||
if (!CThreadUtils::isCurrentThreadObjectThread(this))
|
||||
{
|
||||
// call in correct thread
|
||||
QTimer::singleShot(0, this, &CDigestSignal::inputSignal);
|
||||
return;
|
||||
}
|
||||
|
||||
m_timer.start(); // start or restart
|
||||
m_inputsCount++;
|
||||
if (m_inputsCount >= m_maxInputsPerDigest)
|
||||
@@ -29,4 +36,11 @@ namespace BlackMisc
|
||||
emit digestSignal();
|
||||
}
|
||||
|
||||
void CDigestSignal::init(int maxDelayMs)
|
||||
{
|
||||
QObject::connect(&m_timer, &QTimer::timeout, this, &CDigestSignal::ps_timeout);
|
||||
m_timer.setSingleShot(true);
|
||||
m_timer.setInterval(maxDelayMs);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -30,29 +30,39 @@ namespace BlackMisc
|
||||
CDigestSignal(T *sender, F1 inputSignal, F2 digestSignal, int maxDelayMs = 500, int maxInputsPerDigest = 3)
|
||||
: m_maxInputsPerDigest(maxInputsPerDigest)
|
||||
{
|
||||
QObject::connect(sender, inputSignal, this, &CDigestSignal::ps_inputSignal);
|
||||
QObject::connect(sender, inputSignal, this, &CDigestSignal::inputSignal);
|
||||
QObject::connect(this, &CDigestSignal::digestSignal, sender, digestSignal);
|
||||
|
||||
QObject::connect(&m_timer, &QTimer::timeout, this, &CDigestSignal::ps_timeout);
|
||||
m_timer.setSingleShot(true);
|
||||
m_timer.setInterval(maxDelayMs);
|
||||
init(maxDelayMs);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
//! Constructor without input signal, can be manually triggered
|
||||
template <class T, class F2>
|
||||
CDigestSignal(T *sender, F2 digestSignal, int maxDelayMs = 500, int maxInputsPerDigest = 3)
|
||||
: m_maxInputsPerDigest(maxInputsPerDigest)
|
||||
{
|
||||
QObject::connect(this, &CDigestSignal::digestSignal, sender, digestSignal);
|
||||
init(maxDelayMs);
|
||||
}
|
||||
|
||||
//! Destructor
|
||||
virtual ~CDigestSignal() {}
|
||||
|
||||
signals:
|
||||
//! Send digest signal
|
||||
void digestSignal();
|
||||
|
||||
private slots:
|
||||
//! Received input signal
|
||||
void ps_inputSignal();
|
||||
public slots:
|
||||
//! Received input signal, or manually trigger
|
||||
void inputSignal();
|
||||
|
||||
private slots:
|
||||
//! Timer timed out
|
||||
void ps_timeout();
|
||||
|
||||
private:
|
||||
//! Init in ctor
|
||||
void init(int maxDelayMs);
|
||||
|
||||
QTimer m_timer;
|
||||
const int m_maxInputsPerDigest = 3;
|
||||
int m_inputsCount = 0;
|
||||
|
||||
@@ -19,15 +19,14 @@ namespace BlackMisc
|
||||
bool CThreadUtils::isCurrentThreadObjectThread(QObject *toBeTested)
|
||||
{
|
||||
Q_ASSERT_X(toBeTested, Q_FUNC_INFO, "missing QObject");
|
||||
if (!toBeTested) { return false; }
|
||||
if (!toBeTested->thread()) { return false; }
|
||||
Q_ASSERT_X(toBeTested->thread(), Q_FUNC_INFO, "missing thread");
|
||||
return (QThread::currentThread() == toBeTested->thread());
|
||||
}
|
||||
|
||||
bool CThreadUtils::isApplicationThreadObjectThread(QObject *toBeTested)
|
||||
{
|
||||
Q_ASSERT_X(toBeTested, Q_FUNC_INFO, "missing QObject");
|
||||
if (!toBeTested || !toBeTested->thread()) { return false; }
|
||||
Q_ASSERT_X(toBeTested->thread(), Q_FUNC_INFO, "missing thread");
|
||||
if (!QCoreApplication::instance() || !QCoreApplication::instance()->thread()) { return false; }
|
||||
return (QCoreApplication::instance()->thread() == toBeTested->thread());
|
||||
}
|
||||
@@ -38,5 +37,4 @@ namespace BlackMisc
|
||||
if (!QCoreApplication::instance()->thread()) { return false; }
|
||||
return (QCoreApplication::instance()->thread() == QThread::currentThread());
|
||||
}
|
||||
|
||||
} // ns
|
||||
|
||||
Reference in New Issue
Block a user