mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-23 23:45:35 +08:00
[skip ci] refactor: Use composition for update timer
The timer is not required by all classes and the deep inheritance makes it hard to see where it is actually used. Use composition instead
This commit is contained in:
52
src/misc/threadedtimer.cpp
Normal file
52
src/misc/threadedtimer.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2025 swift Project Community / Contributors
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
|
||||
|
||||
#include "misc/threadedtimer.h"
|
||||
|
||||
#include <QPointer>
|
||||
|
||||
#include "threadutils.h"
|
||||
|
||||
namespace swift::misc
|
||||
{
|
||||
CThreadedTimer::CThreadedTimer(QObject *owner, const QString &name, bool singleShot) : QObject(owner)
|
||||
{
|
||||
m_updateTimer.setObjectName(name + ":timer");
|
||||
m_updateTimer.setSingleShot(singleShot);
|
||||
connect(&m_updateTimer, &QTimer::timeout, this, &CThreadedTimer::timeout);
|
||||
}
|
||||
|
||||
void CThreadedTimer::startTimer(std::chrono::milliseconds ms)
|
||||
{
|
||||
if (!CThreadUtils::isInThisThread(this))
|
||||
{
|
||||
// shift in correct thread
|
||||
QPointer<CThreadedTimer> myself(this);
|
||||
QTimer::singleShot(0, this, [=] {
|
||||
if (!myself) { return; }
|
||||
this->startTimer(ms);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
connect(thread(), &QThread::finished, &m_updateTimer, &QTimer::stop, Qt::UniqueConnection);
|
||||
m_updateTimer.start(ms);
|
||||
}
|
||||
|
||||
void CThreadedTimer::stopTimer()
|
||||
{
|
||||
if (!CThreadUtils::isInThisThread(this))
|
||||
{
|
||||
// shift in correct thread
|
||||
QPointer<CThreadedTimer> myself(this);
|
||||
QTimer::singleShot(0, this, [=] {
|
||||
if (!myself) { return; }
|
||||
this->stopTimer();
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!m_updateTimer.isActive()) { return; }
|
||||
m_updateTimer.stop();
|
||||
}
|
||||
} // namespace swift::misc
|
||||
Reference in New Issue
Block a user