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

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 "circuitcontrol.h"
0010 
0011 #include "timercontrol.h"
0012 
0013 CircuitControl::CircuitControl(QObject *parent)
0014     : QObject{parent},
0015       m_currentIndex{0},
0016       m_timerControl{nullptr}
0017 {
0018 }
0019 
0020 CircuitModel CircuitControl::model() const
0021 {
0022     return m_model;
0023 }
0024 
0025 void CircuitControl::setModel(const CircuitModel &model)
0026 {
0027     if (model == m_model)
0028         return;
0029 
0030     auto m = model;
0031     setModel(std::move(m));
0032 }
0033 
0034 void CircuitControl::setModel(CircuitModel &&model)
0035 {
0036     if (model == m_model)
0037         return;
0038 
0039     const auto oldName = name();
0040     const auto oldSize = size();
0041     const auto oldRemaining = remaining();
0042 
0043     m_model = std::move(model);
0044 
0045     emit modelChanged(m_model);
0046 
0047     if (oldName != name())
0048         emit nameChanged(name());
0049 
0050     if (oldSize != size())
0051         emit sizeChanged(size());
0052 
0053     if (oldRemaining != remaining())
0054         emit remainingChanged(size());
0055 
0056 
0057     m_currentIndex = 0;
0058     applyCurrentTimer();
0059     emit currentTimerChanged(currentTimer());
0060 }
0061 
0062 TimerControl *CircuitControl::timerControl() const
0063 {
0064     return m_timerControl;
0065 }
0066 
0067 void CircuitControl::setTimerControl(TimerControl *timerControl)
0068 {
0069     if (timerControl == m_timerControl)
0070         return;
0071 
0072     if (m_timerControl) {
0073         connect(m_timerControl, &TimerControl::timerFinished, this, &CircuitControl::nextTimer);
0074     }
0075 
0076     m_timerControl = timerControl;
0077 
0078     if (m_timerControl) {
0079         connect(m_timerControl, &TimerControl::timerFinished, this, &CircuitControl::nextTimer);
0080     }
0081 
0082     emit timerControlChanged(timerControl);
0083 
0084     applyCurrentTimer();
0085 }
0086 
0087 QString CircuitControl::name() const
0088 {
0089     return m_model.name();
0090 }
0091 
0092 int CircuitControl::size() const
0093 {
0094     return m_model.size();
0095 }
0096 
0097 int CircuitControl::remaining() const
0098 {
0099     if (currentTimer().isValid())
0100         return m_model.size() - m_currentIndex;
0101     else
0102         return 0;
0103 }
0104 
0105 TimerModel CircuitControl::currentTimer() const
0106 {
0107     if (m_currentIndex < m_model.size())
0108         return m_model.at(m_currentIndex);
0109     else
0110         return {};
0111 }
0112 
0113 void CircuitControl::nextTimer()
0114 {
0115     const auto oldRemaining = remaining();
0116 
0117     m_currentIndex++;
0118     applyCurrentTimer();
0119     emit currentTimerChanged(currentTimer());
0120 
0121     if (oldRemaining != remaining()) {
0122         emit remainingChanged(remaining());
0123 
0124         if (remaining() == 0)
0125             emit circuitFinished();
0126     }
0127 }
0128 
0129 void CircuitControl::applyCurrentTimer()
0130 {
0131     if (m_timerControl && m_timerControl->model() != currentTimer())
0132         m_timerControl->setModel(currentTimer());
0133 }