File indexing completed on 2024-04-21 16:32:06

0001 /* This file is part of Kairo Timer
0002 
0003    SPDX-FileCopyrightText: 2016 (c) Kevin Ottens <ervin@kde.org>
0004 
0005    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 
0007 */
0008 
0009 #include "timercontrol.h"
0010 
0011 #include <QTimer>
0012 #include <QTime>
0013 #include <QtCore/qmath.h>
0014 
0015 TimerControl::TimerControl(QObject *parent)
0016     : QObject{parent},
0017       m_timer{new QTimer{this}}
0018 {
0019     m_timer->setTimerType(Qt::PreciseTimer);
0020     m_timer->setInterval(10);
0021     connect(m_timer, &QTimer::timeout, this, &TimerControl::onTimerTimeout);
0022 }
0023 
0024 TimerModel TimerControl::createModel() const
0025 {
0026     return {};
0027 }
0028 
0029 TimerModel TimerControl::createModel(const QString &text, int duration) const
0030 {
0031     return {text, duration};
0032 }
0033 
0034 TimerModel TimerControl::model() const
0035 {
0036     return m_model;
0037 }
0038 
0039 void TimerControl::setModel(const TimerModel &model)
0040 {
0041     if (model == m_model)
0042         return;
0043 
0044     auto m = model;
0045     setModel(std::move(m));
0046 }
0047 
0048 void TimerControl::setModel(TimerModel &&model)
0049 {
0050     if (model == m_model)
0051         return;
0052 
0053     const auto oldText = text();
0054     const auto oldValue = value();
0055     const auto oldDuration = duration();
0056     const auto skipped = m_running && m_model.isValid() && oldValue != 0;
0057 
0058     m_model = std::move(model);
0059 
0060     m_value = m_model.type() == TimerModel::Countdown ? m_model.duration() : 0;
0061 
0062     if (m_running)
0063          m_timer->start();
0064 
0065     emit modelChanged(m_model);
0066 
0067     if (skipped)
0068         emit timerSkipped();
0069 
0070     if (oldText != text())
0071         emit textChanged(text());
0072 
0073     if (oldValue != value()) {
0074         emit valueChanged(value());
0075         emit formattedValueChanged(formattedValue());
0076     }
0077 
0078     if (oldDuration != duration())
0079         emit durationChanged(duration());
0080 }
0081 
0082 QString TimerControl::text() const
0083 {
0084     return m_model.text();
0085 }
0086 
0087 int TimerControl::value() const
0088 {
0089     return m_value;
0090 }
0091 
0092 QString TimerControl::formattedValue() const
0093 {
0094     qreal time = qRound((qreal)m_value / (qreal)100)/(qreal)10;
0095     int minutes = qFloor(time / 60);
0096     //seconds rounded to first decimal position
0097     int seconds = qFloor(time - minutes * 60);
0098 
0099     return QStringLiteral("%1:%2.%3")
0100         .arg(minutes, 2, 10, QLatin1Char('0'))
0101         .arg(seconds, 2, 10, QLatin1Char('0'))
0102         .arg(((time - qFloor(time)) * 10));
0103 }
0104 
0105 int TimerControl::duration() const
0106 {
0107     return m_model.duration();
0108 }
0109 
0110 bool TimerControl::isRunning() const
0111 {
0112     return m_running;
0113 }
0114 
0115 void TimerControl::setRunning(bool running)
0116 {
0117     if (running == m_running)
0118         return;
0119 
0120     m_running = running;
0121     emit runningChanged(m_running);
0122 
0123     if (!m_model.isValid()) {
0124         if (m_running)
0125             emit timerFinished();
0126         m_timer->stop();
0127     } else if (!m_running) {
0128         m_timer->stop();
0129     } else {
0130         m_timer->start();
0131     }
0132 }
0133 
0134 void TimerControl::onTimerTimeout()
0135 {
0136     m_value += m_model.type() == TimerModel::Countdown ? -10 : 10;
0137     emit valueChanged(m_value);
0138     emit formattedValueChanged(formattedValue());
0139 
0140     if (m_value == 0) {
0141         m_timer->stop();
0142         emit timerFinished();
0143     }
0144 }