#include "stimer.h" #include #include #include #include namespace { //const int TIME = 300000; // 5 minute const int TIME = 30000; } STimer::STimer(): m_pTimer(new QTimer), m_nPreviousHour(-1) { m_pTimer->setInterval(TIME); connect(&(*m_pTimer), &QTimer::timeout, this, &STimer::checkTimeout); init(); } void STimer::init() { QSettings settings(QString("effect.ini"), QSettings::IniFormat); settings.setIniCodec("UTF-8"); settings.beginGroup("timer"); QString strTimes = settings.value("time", "3").toString(); if (strTimes.trimmed().isEmpty()) strTimes = "3"; qDebug() << strTimes; settings.endGroup(); QStringList slTimes = strTimes.split(QChar('.'), QString::SkipEmptyParts); qDebug() << "hahaha"; qDebug() << slTimes.size(); bool ok; int nTime; foreach (auto& strTime, slTimes) { qDebug() << strTime; nTime = strTime.toInt(&ok); if (ok) set(nTime); } } STimer::~STimer() { disconnect(&(*m_pTimer), &QTimer::timeout, this, &STimer::checkTimeout); } STimer::STimer(int time): STimer() { if (0 <= time && time < 24) m_setTime.insert(time); } STimer::STimer(QVector times): STimer() { foreach (int time, times) { if (0 <= time && time < 24) m_setTime.insert(time); } } void STimer::start() { if (!m_pTimer->isActive()) m_pTimer->start(); } void STimer::stop() { if (m_pTimer->isActive()) m_pTimer->stop(); } bool STimer::isActive() const { return m_pTimer->isActive(); } void STimer::set(int time) { if (0 <= time && time < 24) m_setTime.insert(time); } void STimer::set(QVector times) { foreach (int time, times) { set(time); } } void STimer::unset(int time) { m_setTime.remove(time); } void STimer::unset(QVector times) { foreach (int time, times) { unset(time); } } int STimer::timerId() const { return m_pTimer->timerId(); } void STimer::checkTimeout() { QTime now = QTime::currentTime(); bool bChangedHour = (m_nPreviousHour == now.hour()) ? false : true; m_nPreviousHour = now.hour(); /* qDebug() << "timer"; qDebug() << "bChangedHour : " << bChangedHour; for (auto iter = m_setTime.begin(); iter != m_setTime.end(); ++iter) qDebug() << *iter; */ if (bChangedHour && m_setTime.contains(now.hour())) { //qDebug() << "TimeOut condition satisfied"; emit timeout(); } }