version 2.25.0
add qad_timers.h in PIQt, IndexedTimer
This commit is contained in:
@@ -3,7 +3,7 @@ cmake_policy(SET CMP0017 NEW) # need include() with .cmake
|
|||||||
cmake_policy(SET CMP0072 NEW) # FindOpenGL prefers GLVND by default
|
cmake_policy(SET CMP0072 NEW) # FindOpenGL prefers GLVND by default
|
||||||
project(QAD)
|
project(QAD)
|
||||||
set(QAD_MAJOR 2)
|
set(QAD_MAJOR 2)
|
||||||
set(QAD_MINOR 24)
|
set(QAD_MINOR 25)
|
||||||
set(QAD_REVISION 0)
|
set(QAD_REVISION 0)
|
||||||
set(QAD_SUFFIX )
|
set(QAD_SUFFIX )
|
||||||
set(QAD_COMPANY SHS)
|
set(QAD_COMPANY SHS)
|
||||||
|
|||||||
1
libs/piqt/qad_timers.cpp
Normal file
1
libs/piqt/qad_timers.cpp
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#include "qad_timers.h"
|
||||||
76
libs/piqt/qad_timers.h
Normal file
76
libs/piqt/qad_timers.h
Normal 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
|
||||||
Reference in New Issue
Block a user