version 2.25.0

add qad_timers.h in PIQt, IndexedTimer
This commit is contained in:
2024-03-05 19:39:49 +03:00
parent 03f8452512
commit 50a231a8e7
3 changed files with 78 additions and 1 deletions

1
libs/piqt/qad_timers.cpp Normal file
View File

@@ -0,0 +1 @@
#include "qad_timers.h"

76
libs/piqt/qad_timers.h Normal file
View File

@@ -0,0 +1,76 @@
/*
QAD - Qt ADvanced
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef qad_timers_H
#define qad_timers_H
#include "qad_utils_export.h"
#include <QDebug>
#include <QMap>
#include <QTimer>
#include <pisystemtime.h>
template<typename Index = int>
class QAD_UTILS_EXPORT IndexedTimer {
public:
~IndexedTimer() {
for (auto & i: timers)
i.destroy();
timers.clear();
}
void bindIndexedTimer(Index index, std::function<void()> func) { timers[index].func = func; }
void stopIndexedTimer(Index index) {
auto & t(timers[index]);
if (!t.isValid()) return;
t.destroy();
}
void startIndexedTimer(Index index, PISystemTime interval, std::function<void()> func = nullptr) {
stopIndexedTimer(index);
__TimerSlot & t(timers[index]);
if (func) t.func = func;
if (!t.func) {
qDebug() << "[IndexedTimer] startIndexedTimer error: null function!";
return;
}
t.timer = new QTimer();
QObject::connect(t.timer, &QTimer::timeout, t.func);
t.timer->start(interval.toMilliseconds());
}
bool isIndexedTimerStarted(Index index) const { return timers.value(index).isValid(); }
private:
struct __TimerSlot {
bool isValid() const { return timer; }
void destroy() {
if (!timer) return;
delete timer;
timer = nullptr;
}
std::function<void()> func = nullptr;
QTimer * timer = nullptr;
};
QMap<Index, __TimerSlot> timers;
};
#endif