File indexing completed on 2024-04-14 05:46:47

0001 /*
0002     SPDX-FileCopyrightText: 2005 Bram Schoenmakers <bramschoenmakers@kde.nl>
0003     SPDX-FileCopyrightText: 2005-2007, 2010 Tom Albers <toma@kde.org>
0004     SPDX-FileCopyrightText: 2010 Juan Luis Baptiste <juan.baptiste@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "rsirelaxpopup.h"
0010 
0011 #include <QIcon>
0012 #include <QLabel>
0013 #include <QProgressBar>
0014 #include <QPushButton>
0015 #include <QTimer>
0016 
0017 #include <KColorScheme>
0018 #include <KConfigGroup>
0019 #include <KFormat>
0020 #include <KLocalizedString>
0021 #include <QHBoxLayout>
0022 #include <QVBoxLayout>
0023 
0024 RSIRelaxPopup::RSIRelaxPopup(QWidget *parent)
0025     : QObject(parent)
0026     , m_wasShown(false)
0027 {
0028     m_popup = new PassivePopup(parent);
0029 
0030     QWidget *vbox = new QWidget(m_popup);
0031     QVBoxLayout *vboxVBoxLayout = new QVBoxLayout(vbox);
0032     vboxVBoxLayout->setContentsMargins(0, 0, 0, 0);
0033     vboxVBoxLayout->setSpacing(5);
0034     m_message = new QLabel(vbox);
0035     vboxVBoxLayout->addWidget(m_message);
0036 
0037     QWidget *hbox = new QWidget(vbox);
0038     QHBoxLayout *hboxHBoxLayout = new QHBoxLayout(hbox);
0039     hboxHBoxLayout->setContentsMargins(0, 0, 0, 0);
0040     vboxVBoxLayout->addWidget(hbox);
0041     hboxHBoxLayout->setSpacing(15);
0042 
0043     m_progress = new QProgressBar(hbox);
0044     hboxHBoxLayout->addWidget(m_progress);
0045     m_progress->setFormat("%v");
0046     m_progress->setRange(0, 0);
0047 
0048     m_skipbutton = new QPushButton(QIcon::fromTheme("dialog-cancel"), i18n("Skip Break"), hbox);
0049     hboxHBoxLayout->addWidget(m_skipbutton);
0050     m_skipbutton->setToolTip(i18n("Skip this break"));
0051     connect(m_skipbutton, &QPushButton::clicked, this, &RSIRelaxPopup::skip);
0052 
0053     m_postponebutton = new QPushButton(QIcon::fromTheme("go-next"), i18n("Postpone Break"), hbox);
0054     hboxHBoxLayout->addWidget(m_postponebutton);
0055     m_postponebutton->setToolTip(i18n("Postpone this break"));
0056     connect(m_postponebutton, &QPushButton::clicked, this, &RSIRelaxPopup::postpone);
0057 
0058     m_lockbutton = new QPushButton(QIcon::fromTheme("system-lock-screen"), i18n("Lock Screen"), hbox);
0059     hboxHBoxLayout->addWidget(m_lockbutton);
0060     m_lockbutton->setToolTip(i18n("Lock the session"));
0061     connect(m_lockbutton, &QPushButton::clicked, this, &RSIRelaxPopup::lock);
0062 
0063     m_popup->setTimeout(0); // no auto close
0064     m_popup->setView(vbox);
0065     readSettings();
0066 }
0067 
0068 RSIRelaxPopup::~RSIRelaxPopup()
0069 {
0070 }
0071 
0072 void RSIRelaxPopup::relax(int n, bool bigBreakNext)
0073 {
0074     /*
0075       Counts how many times a request for relax resets
0076       due to detected activity.
0077     */
0078     static int resetcount = 0;
0079 
0080     /*
0081         If n increases compared to the last call,
0082         we want a new request for a relax moment.
0083     */
0084     if (n >= m_progress->value()) {
0085         m_progress->setRange(0, n);
0086         resetcount += 1;
0087         if (n > m_progress->value())
0088             flash();
0089         else if (resetcount % 4 == 0) // flash regularly when the user keeps working
0090             flash();
0091     }
0092 
0093     if (n > 0) {
0094         QString text = i18n("Please relax for %1", KFormat().formatSpelloutDuration(n * 1000));
0095 
0096         if (bigBreakNext)
0097             text.append('\n' + i18n("Note: next break is a big break"));
0098 
0099         m_message->setText(text);
0100         m_progress->setValue(n);
0101 
0102         if (resetcount == 1)
0103             m_popup->show();
0104     } else {
0105         m_popup->setVisible(false);
0106         resetcount = 0;
0107         m_wasShown = false;
0108     }
0109 }
0110 
0111 void RSIRelaxPopup::flash()
0112 {
0113     if (!m_useFlash)
0114         return;
0115 
0116     QTimer::singleShot(500, this, &RSIRelaxPopup::unflash);
0117     QPalette normal;
0118     normal.setColor(QPalette::Inactive, QPalette::WindowText, KColorScheme(QPalette::Active, KColorScheme::Selection).background().color());
0119     normal.setColor(QPalette::Inactive, QPalette::Window, KColorScheme(QPalette::Active, KColorScheme::Selection).foreground().color());
0120     m_popup->setPalette(normal);
0121 }
0122 
0123 void RSIRelaxPopup::unflash()
0124 {
0125     QPalette normal;
0126     m_popup->setPalette(normal);
0127 }
0128 
0129 void RSIRelaxPopup::slotReadConfig()
0130 {
0131     readSettings();
0132 }
0133 
0134 void RSIRelaxPopup::readSettings()
0135 {
0136     KConfigGroup config = KSharedConfig::openConfig()->group("Popup Settings");
0137     m_useFlash = config.readEntry("UseFlash", true);
0138 }
0139 
0140 void RSIRelaxPopup::setSkipButtonHidden(bool b)
0141 {
0142     m_skipbutton->setHidden(b);
0143 }
0144 
0145 void RSIRelaxPopup::setLockButtonHidden(bool b)
0146 {
0147     m_lockbutton->setHidden(b);
0148 }
0149 
0150 void RSIRelaxPopup::setPostponeButtonHidden(bool b)
0151 {
0152     m_postponebutton->setHidden(b);
0153 }
0154 
0155 void RSIRelaxPopup::setSuspended(bool suspended)
0156 {
0157     if (suspended) {
0158         m_wasShown = m_popup->isVisible();
0159         m_popup->hide();
0160     } else if (m_wasShown) {
0161         m_popup->show();
0162     }
0163 }