File indexing completed on 2024-11-10 11:03:06

0001 /***************************************************************************
0002  *  Copyright (C) 2021 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "utils/countdownobject.h"
0021 
0022 CountDownObject::CountDownObject(int tryCount,int countDown, int timeBeforeDecrease, QObject* parent)
0023     : QObject(parent), m_timer(new QTimer), m_countDown(countDown), m_tryCount(tryCount)
0024 {
0025     m_timer->setInterval(timeBeforeDecrease);
0026     init();
0027     connect(m_timer.get(), &QTimer::timeout, this, [this, countDown]() {
0028         --m_allDown;
0029 
0030         auto rest = (m_allDown % countDown);
0031         emit countDownChanged(rest);
0032         if(rest == 0)
0033         {
0034             emit triggered(m_allDown / countDown);
0035         }
0036         if(m_allDown<=0)
0037         {
0038             stop();
0039         }
0040     });
0041 }
0042 
0043 bool CountDownObject::isRunning() const
0044 {
0045     return m_running;
0046 }
0047 
0048 void CountDownObject::setRunning(bool b)
0049 {
0050     if(b == m_running)
0051         return;
0052     m_running = b;
0053     emit runningChanged();
0054 }
0055 
0056 void CountDownObject::start()
0057 {
0058     setRunning(true);
0059     m_timer->start();
0060     emit triggered(m_allDown / m_countDown);
0061 }
0062 void CountDownObject::pause()
0063 {
0064     setRunning(false);
0065     m_timer->stop();
0066 }
0067 void CountDownObject::resume()
0068 {
0069     start();
0070 }
0071 
0072 void CountDownObject::stop()
0073 {
0074     setRunning(false);
0075     m_timer->stop();
0076     init();
0077 }
0078 
0079 void CountDownObject::init()
0080 {
0081     m_allDown = m_countDown * m_tryCount;
0082 }