File indexing completed on 2024-05-19 09:44:38
0001 // SPDX-FileCopyrightText: 2022 Felipe Kinoshita <kinofhek@gmail.com> 0002 // SPDX-License-Identifier: LGPL-2.1-or-later 0003 0004 #include "controller.h" 0005 0006 #include <QDebug> 0007 #include <QTimer> 0008 #include <qstringliteral.h> 0009 0010 Controller::Controller(QObject *parent) 0011 : QObject(parent) 0012 { 0013 m_timer = new QTimer(this); 0014 connect(m_timer, &QTimer::timeout, this, &Controller::update); 0015 generateText(); 0016 } 0017 0018 Controller::~Controller() = default; 0019 0020 QString Controller::text() const 0021 { 0022 return m_text; 0023 } 0024 0025 int Controller::pomodoros() const 0026 { 0027 return m_pomodoros; 0028 } 0029 0030 float Controller::percentage() 0031 { 0032 return m_percentage; 0033 } 0034 0035 bool Controller::running() const 0036 { 0037 return m_running; 0038 } 0039 0040 bool Controller::hasStarted() const 0041 { 0042 return m_hasStarted; 0043 } 0044 0045 bool Controller::onBreak() const 0046 { 0047 return m_onBreak; 0048 } 0049 0050 void Controller::setMinuteDuration(int duration) 0051 { 0052 m_minuteDuration = duration; 0053 } 0054 0055 void Controller::start() 0056 { 0057 m_seconds = Config::self()->intervalTime() * m_minuteDuration; 0058 m_timer->start(1000); 0059 0060 m_running = true; 0061 Q_EMIT runningChanged(); 0062 0063 m_hasStarted = true; 0064 Q_EMIT hasStartedChanged(); 0065 } 0066 0067 void Controller::toggle() 0068 { 0069 if (m_timer->isActive()) { 0070 m_timer->stop(); 0071 m_running = false; 0072 Q_EMIT runningChanged(); 0073 } else { 0074 m_timer->start(1000); 0075 m_running = true; 0076 Q_EMIT runningChanged(); 0077 } 0078 } 0079 0080 void Controller::reset() 0081 { 0082 m_seconds = Config::self()->intervalTime() * m_minuteDuration; 0083 m_changes = 0; 0084 m_pomodoros = 0; 0085 0086 m_hasStarted = false; 0087 Q_EMIT hasStartedChanged(); 0088 0089 m_running = false; 0090 Q_EMIT runningChanged(); 0091 0092 m_timer->stop(); 0093 0094 generateText(); 0095 } 0096 0097 void Controller::update() 0098 { 0099 if (m_seconds == 0) { 0100 if (m_changes == 7) { 0101 m_changes = 0; 0102 0103 m_hasStarted = false; 0104 Q_EMIT hasStartedChanged(); 0105 0106 m_running = false; 0107 Q_EMIT runningChanged(); 0108 0109 m_timer->stop(); 0110 } 0111 0112 m_changes++; 0113 0114 if (m_onBreak) { 0115 m_pomodoros++; 0116 Q_EMIT pomodorosChanged(); 0117 } 0118 0119 m_onBreak = !m_onBreak; 0120 Q_EMIT breakChanged(); 0121 0122 const int breakTime = m_changes > 5 ? Config::self()->longBreakTime() : Config::self()->breakTime(); 0123 0124 m_seconds = m_onBreak ? breakTime * m_minuteDuration : Config::self()->intervalTime() * m_minuteDuration; 0125 } 0126 0127 m_seconds--; 0128 0129 generateText(); 0130 } 0131 0132 void Controller::generateText() 0133 { 0134 int minutes = m_seconds / 60; 0135 int seconds = m_seconds % 60; 0136 const QString minutesText = minutes < 10 ? QStringLiteral("0%1").arg(m_seconds / 60) : QString::number(m_seconds / 60); 0137 const QString secondsText = seconds < 10 ? QStringLiteral("0%1").arg(m_seconds % 60) : QString::number(m_seconds % 60); 0138 0139 m_percentage = m_onBreak ? ((float(Config::breakTime()) * 60 - m_seconds)) / (float(Config::breakTime()) * 60) * 100 0140 : ((float(Config::intervalTime()) * 60 - m_seconds)) / (float(Config::intervalTime()) * 60) * 100; 0141 m_text = QStringLiteral("%1:%2").arg(minutesText, secondsText); 0142 Q_EMIT textChanged(); 0143 Q_EMIT percentageChanged(); 0144 }