File indexing completed on 2024-04-28 09:49:40

0001 /*
0002     SPDX-FileCopyrightText: 2005-2007, 2009-2010 Tom Albers <toma@kde.org>
0003     SPDX-FileCopyrightText: 2006 Bram Schoenmakers <bramschoenmakers@kde.nl>
0004     SPDX-FileCopyrightText: 2010 Juan Luis Baptiste <juan.baptiste@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 // Local includes.
0010 #include "setuptiming.h"
0011 
0012 // QT includes.
0013 #include <QGroupBox>
0014 #include <QHBoxLayout>
0015 #include <QLabel>
0016 #include <QVBoxLayout>
0017 
0018 // KDE includes.
0019 #include <KConfigGroup>
0020 #include <KLocalizedString>
0021 #include <KPluralHandlingSpinBox>
0022 #include <KSharedConfig>
0023 
0024 class SetupTimingPriv
0025 {
0026 public:
0027     QGroupBox *tinyBox;
0028     KPluralHandlingSpinBox *tinyInterval;
0029     KPluralHandlingSpinBox *tinyDuration;
0030     KPluralHandlingSpinBox *tinyThreshold;
0031     KPluralHandlingSpinBox *bigInterval;
0032     KPluralHandlingSpinBox *bigDuration;
0033     KPluralHandlingSpinBox *bigThreshold;
0034     KPluralHandlingSpinBox *postponeDuration;
0035     int debug;
0036 };
0037 
0038 SetupTiming::SetupTiming(QWidget *parent)
0039     : QWidget(parent)
0040 {
0041     d = new SetupTimingPriv;
0042 
0043     QVBoxLayout *l = new QVBoxLayout(this);
0044 
0045     // ------------------------ Tinybox
0046 
0047     d->tinyBox = new QGroupBox(this);
0048     d->tinyBox->setTitle(i18n("Tiny Breaks"));
0049     d->tinyBox->setCheckable(true);
0050     connect(d->tinyBox, &QGroupBox::toggled, this, &SetupTiming::slotTinyValueEnabled);
0051 
0052     QWidget *m = new QWidget(this);
0053     QHBoxLayout *mHBoxLayout = new QHBoxLayout(m);
0054     mHBoxLayout->setContentsMargins(0, 0, 0, 0);
0055     QLabel *l1 = new QLabel(i18n("Short break every:") + ' ', m);
0056     mHBoxLayout->addWidget(l1);
0057     l1->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
0058     l1->setWhatsThis(i18n("Here you can set how often you want a short break. "));
0059     d->tinyInterval = new KPluralHandlingSpinBox(m);
0060     mHBoxLayout->addWidget(d->tinyInterval);
0061     d->tinyInterval->setRange(1, 1000);
0062     l1->setBuddy(d->tinyInterval);
0063     connect(d->tinyInterval,
0064             static_cast<void (KPluralHandlingSpinBox::*)(int)>(&KPluralHandlingSpinBox::valueChanged),
0065             this,
0066             &SetupTiming::slotTinyValueChanged);
0067 
0068     QWidget *m2 = new QWidget(this);
0069     QHBoxLayout *m2HBoxLayout = new QHBoxLayout(m2);
0070     m2HBoxLayout->setContentsMargins(0, 0, 0, 0);
0071     QLabel *l2 = new QLabel(i18n("For a duration of:") + ' ', m2);
0072     m2HBoxLayout->addWidget(l2);
0073     l2->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
0074     l2->setWhatsThis(i18n("Here you can set the duration of the short break."));
0075     d->tinyDuration = new KPluralHandlingSpinBox(m2);
0076     m2HBoxLayout->addWidget(d->tinyDuration);
0077     d->tinyDuration->setRange(1, 1000);
0078     l2->setBuddy(d->tinyDuration);
0079     connect(d->tinyDuration,
0080             static_cast<void (KPluralHandlingSpinBox::*)(int)>(&KPluralHandlingSpinBox::valueChanged),
0081             this,
0082             &SetupTiming::slotTinyDurationValueChanged);
0083 
0084     QWidget *mTinyThreshold = new QWidget(this);
0085     QHBoxLayout *hTinyThreshold = new QHBoxLayout(mTinyThreshold);
0086     hTinyThreshold->setContentsMargins(0, 0, 0, 0);
0087     QLabel *lTinyThreshold = new QLabel(i18n("Skip if no activity for:") + ' ', mTinyThreshold);
0088     hTinyThreshold->addWidget(lTinyThreshold);
0089     lTinyThreshold->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
0090     lTinyThreshold->setWhatsThis(i18n("Set the interval you are idle for that is enough to skip the next short break."));
0091     d->tinyThreshold = new KPluralHandlingSpinBox(mTinyThreshold);
0092     hTinyThreshold->addWidget(d->tinyThreshold);
0093     d->tinyThreshold->setRange(1, 1000);
0094     lTinyThreshold->setBuddy(d->tinyThreshold);
0095 
0096     QVBoxLayout *vbox0 = new QVBoxLayout(d->tinyBox);
0097     vbox0->addWidget(m);
0098     vbox0->addWidget(m2);
0099     vbox0->addWidget(mTinyThreshold);
0100     vbox0->addStretch(1);
0101     d->tinyBox->setLayout(vbox0);
0102 
0103     // ------------------------ Bigbox
0104 
0105     QGroupBox *bigBox = new QGroupBox(this);
0106     bigBox->setTitle(i18n("Big Breaks"));
0107 
0108     QWidget *m3 = new QWidget(this);
0109     QHBoxLayout *m3HBoxLayout = new QHBoxLayout(m3);
0110     m3HBoxLayout->setContentsMargins(0, 0, 0, 0);
0111     QLabel *l3 = new QLabel(i18n("Long break every:") + ' ', m3);
0112     m3HBoxLayout->addWidget(l3);
0113     l3->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
0114     l3->setWhatsThis(i18n("Here you can set how often you want a long break. "));
0115     d->bigInterval = new KPluralHandlingSpinBox(m3);
0116     m3HBoxLayout->addWidget(d->bigInterval);
0117     d->bigInterval->setRange(1, 1000);
0118     l3->setBuddy(d->bigInterval);
0119 
0120     QWidget *m4 = new QWidget(this);
0121     QHBoxLayout *m4HBoxLayout = new QHBoxLayout(m4);
0122     m4HBoxLayout->setContentsMargins(0, 0, 0, 0);
0123     QLabel *l4 = new QLabel(i18n("For a duration of:") + ' ', m4);
0124     m4HBoxLayout->addWidget(l4);
0125     l4->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
0126     l4->setWhatsThis(i18n("Here you can set the duration of the long break."));
0127     d->bigDuration = new KPluralHandlingSpinBox(m4);
0128     m4HBoxLayout->addWidget(d->bigDuration);
0129     d->bigDuration->setRange(1, 1000);
0130     l4->setBuddy(d->bigDuration);
0131     connect(d->bigDuration,
0132             static_cast<void (KPluralHandlingSpinBox::*)(int)>(&KPluralHandlingSpinBox::valueChanged),
0133             this,
0134             &SetupTiming::slotBigDurationValueChanged);
0135 
0136     QWidget *mBigThreshold = new QWidget(this);
0137     QHBoxLayout *hBigThreshold = new QHBoxLayout(mBigThreshold);
0138     hBigThreshold->setContentsMargins(0, 0, 0, 0);
0139     QLabel *lBigThreshold = new QLabel(i18n("Skip if no activity for:") + ' ', mBigThreshold);
0140     hBigThreshold->addWidget(lBigThreshold);
0141     lBigThreshold->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
0142     lBigThreshold->setWhatsThis(i18n("Set the interval you are idle for that is enough to skip the next long break."));
0143     d->bigThreshold = new KPluralHandlingSpinBox(mBigThreshold);
0144     hBigThreshold->addWidget(d->bigThreshold);
0145     d->bigThreshold->setRange(1, 1000);
0146     lBigThreshold->setBuddy(d->bigThreshold);
0147 
0148     QVBoxLayout *vbox1 = new QVBoxLayout(bigBox);
0149     vbox1->addWidget(m3);
0150     vbox1->addWidget(m4);
0151     vbox1->addWidget(mBigThreshold);
0152     vbox1->addStretch(1);
0153     bigBox->setLayout(vbox1);
0154 
0155     // ------------------------ Postpone break
0156 
0157     QGroupBox *postponeBox = new QGroupBox(this);
0158     postponeBox->setTitle(i18n("Postpone Breaks"));
0159 
0160     QWidget *m5 = new QWidget(this);
0161     QHBoxLayout *m5HBoxLayout = new QHBoxLayout(m5);
0162     m5HBoxLayout->setContentsMargins(0, 0, 0, 0);
0163     QLabel *l5 = new QLabel(i18n("For a duration of:") + ' ', m5);
0164     m5HBoxLayout->addWidget(l5);
0165     l5->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
0166     l5->setWhatsThis(i18n("Here you can set for how much time you want to postpone a break."));
0167     d->postponeDuration = new KPluralHandlingSpinBox(m5);
0168     m5HBoxLayout->addWidget(d->postponeDuration);
0169     d->postponeDuration->setRange(1, 1000);
0170     l5->setBuddy(d->postponeDuration);
0171 
0172     QVBoxLayout *vbox2 = new QVBoxLayout(postponeBox);
0173     vbox2->addWidget(m5);
0174     vbox2->addStretch(1);
0175     postponeBox->setLayout(vbox2);
0176 
0177     l->addWidget(d->tinyBox);
0178     l->addWidget(bigBox);
0179     l->addWidget(postponeBox);
0180     setLayout(l);
0181     readSettings();
0182 
0183     KLocalizedString sfx = ki18np(" second", " seconds");
0184     d->tinyDuration->setSuffix(sfx);
0185     d->tinyThreshold->setSuffix(sfx);
0186     if (!d->debug) {
0187         sfx = ki18np(" minute", " minutes");
0188     }
0189     d->tinyInterval->setSuffix(sfx);
0190     d->bigInterval->setSuffix(sfx);
0191     d->bigDuration->setSuffix(sfx);
0192     d->bigThreshold->setSuffix(sfx);
0193     d->postponeDuration->setSuffix(sfx);
0194 
0195     slotTinyValueChanged(d->tinyInterval->value());
0196 
0197     // Resize to minimum possible.
0198     d->tinyInterval->setFixedSize(d->tinyInterval->sizeHint());
0199     d->bigInterval->setFixedSize(d->tinyInterval->sizeHint());
0200     d->tinyDuration->setFixedSize(d->tinyInterval->sizeHint());
0201     d->bigDuration->setFixedSize(d->tinyInterval->sizeHint());
0202     d->tinyThreshold->setFixedSize(d->tinyThreshold->sizeHint());
0203     d->bigThreshold->setFixedSize(d->bigThreshold->sizeHint());
0204     d->postponeDuration->setFixedSize(d->tinyInterval->sizeHint());
0205 }
0206 
0207 SetupTiming::~SetupTiming()
0208 {
0209     delete d;
0210 }
0211 
0212 void SetupTiming::applySettings()
0213 {
0214     KConfigGroup config = KSharedConfig::openConfig()->group("General Settings");
0215     config.writeEntry("TinyEnabled", d->tinyBox->isChecked());
0216     config.writeEntry("TinyInterval", d->tinyInterval->value());
0217     config.writeEntry("TinyDuration", d->tinyDuration->value());
0218     config.writeEntry("TinyThreshold", d->tinyThreshold->value());
0219     config.writeEntry("BigInterval", d->bigInterval->value());
0220     config.writeEntry("BigDuration", d->bigDuration->value());
0221     config.writeEntry("BigThreshold", d->bigThreshold->value());
0222     config.writeEntry("PostponeBreakDuration", d->postponeDuration->value());
0223     config.sync();
0224 }
0225 
0226 void SetupTiming::readSettings()
0227 {
0228     KConfigGroup config = KSharedConfig::openConfig()->group("General Settings");
0229     d->debug = config.readEntry("DEBUG", 0);
0230 
0231     d->tinyBox->setChecked(config.readEntry("TinyEnabled", true));
0232     d->tinyInterval->setValue(config.readEntry("TinyInterval", 10));
0233     d->tinyDuration->setValue(config.readEntry("TinyDuration", 20));
0234     d->tinyThreshold->setValue(config.readEntry("TinyThreshold", 40));
0235     d->bigInterval->setValue(config.readEntry("BigInterval", 60));
0236     d->bigDuration->setValue(config.readEntry("BigDuration", 1));
0237     d->bigThreshold->setValue(config.readEntry("BigThreshold", 5));
0238     d->postponeDuration->setValue(config.readEntry("PostponeBreakDuration", 5));
0239 }
0240 
0241 void SetupTiming::slotTinyValueEnabled(bool enabled)
0242 {
0243     d->bigInterval->setMinimum(enabled ? d->tinyInterval->value() : 1);
0244 }
0245 
0246 void SetupTiming::slotTinyValueChanged(const int tinyIntervalValue)
0247 {
0248     if (d->tinyBox->isChecked()) {
0249         d->bigInterval->setMinimum(tinyIntervalValue);
0250     }
0251 }
0252 
0253 void SetupTiming::slotBigDurationValueChanged(const int bigDurationValue)
0254 {
0255     d->bigThreshold->setMinimum(bigDurationValue);
0256 }
0257 
0258 void SetupTiming::slotTinyDurationValueChanged(const int tinyDurationValue)
0259 {
0260     d->tinyThreshold->setMinimum(tinyDurationValue);
0261 }
0262 
0263 void SetupTiming::slotSetUseIdleTimer(const bool useIdleTimer)
0264 {
0265     d->bigThreshold->setEnabled(useIdleTimer);
0266     d->tinyThreshold->setEnabled(useIdleTimer);
0267 }