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 "timernotificationcontrol.h"
0010 
0011 #include <QtMath>
0012 
0013 #include "soundcontrolinterface.h"
0014 #include "timercontrol.h"
0015 
0016 TimerNotificationControl::TimerNotificationControl(QObject *parent)
0017     : QObject{parent},
0018       m_sound{nullptr},
0019       m_timer{nullptr}
0020 {
0021 }
0022 
0023 SoundControlInterface *TimerNotificationControl::sound() const
0024 {
0025     return m_sound;
0026 }
0027 
0028 TimerControl *TimerNotificationControl::timer() const
0029 {
0030     return m_timer;
0031 }
0032 
0033 void TimerNotificationControl::setSound(SoundControlInterface *sound)
0034 {
0035     if (sound == m_sound)
0036         return;
0037 
0038     m_sound = sound;
0039     emit soundChanged(sound);
0040 }
0041 
0042 void TimerNotificationControl::setTimer(TimerControl *timer)
0043 {
0044     if (timer == m_timer)
0045         return;
0046 
0047     if (m_timer) {
0048         disconnect(m_timer, &TimerControl::timerFinished, this, &TimerNotificationControl::onTimerFinished);
0049         disconnect(m_timer, &TimerControl::timerSkipped, this, &TimerNotificationControl::onTimerSkipped);
0050         disconnect(m_timer, &TimerControl::valueChanged, this, &TimerNotificationControl::onTimerValueChanged);
0051     }
0052 
0053     m_timer = timer;
0054 
0055     if (m_timer) {
0056         connect(m_timer, &TimerControl::timerFinished, this, &TimerNotificationControl::onTimerFinished);
0057         connect(m_timer, &TimerControl::timerSkipped, this, &TimerNotificationControl::onTimerSkipped);
0058         connect(m_timer, &TimerControl::valueChanged, this, &TimerNotificationControl::onTimerValueChanged);
0059     }
0060 
0061     emit timerChanged(timer);
0062 }
0063 
0064 void TimerNotificationControl::onTimerFinished()
0065 {
0066     if (!m_sound)
0067         return;
0068 
0069     m_sound->playLongBeep();
0070 }
0071 
0072 void TimerNotificationControl::onTimerSkipped()
0073 {
0074     if (!m_sound)
0075         return;
0076 
0077     m_sound->playShortBeep();
0078 }
0079 
0080 void TimerNotificationControl::onTimerValueChanged(int ms)
0081 {
0082     if (!m_sound)
0083         return;
0084 
0085     if (m_timer->model().type() != TimerModel::Countdown)
0086         return;
0087 
0088     auto seconds = ms / 1000.0;
0089     if (static_cast<int>(seconds) == qCeil(seconds)
0090      && 1.0 <= seconds && seconds <= 10.0) {
0091         m_sound->playShortBeep();
0092     }
0093 }